QGitSignature QGitCommit::author() const { return QGitSignature(git_commit_author(data())); }
void QGit::commitDiff(QString commitId) { const git_diff_delta *delta = nullptr; git_tree *commit_tree = nullptr; git_tree *parent_tree = nullptr; git_repository *repo = nullptr; git_commit *parent = nullptr; git_commit *commit = nullptr; git_object *obj = nullptr; git_diff *diff = nullptr; QList<QGitCommitDiffParent> parents; QGitSignature commitAuthor, commitCommiter; QDateTime commitTime; QString commitMessage; QGitCommit commitDiff; unsigned int parentCount; int res = 0; QGitError error; try { res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } if (commitId.isEmpty()) { commitId = QStringLiteral("HEAD"); } res = git_revparse_single(&obj, repo, commitId.toLatin1()); if (res) { throw QGitError("git_revparse_single", res); } res = git_commit_lookup(&commit, repo, git_object_id(obj)); if (res) { throw QGitError("git_commit_lookup", res); } res = git_commit_tree(&commit_tree, commit); if (res) { throw QGitError("git_commit_tree", res); } commitAuthor = QGitSignature(QString::fromUtf8(git_commit_author(commit)->name), QString::fromUtf8(git_commit_author(commit)->email), QDateTime::fromMSecsSinceEpoch(git_commit_author(commit)->when.time * 1000)); commitCommiter = QGitSignature(QString::fromUtf8(git_commit_committer(commit)->name), QString::fromUtf8(git_commit_committer(commit)->email), QDateTime::fromMSecsSinceEpoch(git_commit_committer(commit)->when.time * 1000)); auto time = git_commit_time(commit); auto timeOffset = git_commit_time_offset(commit); commitTime = QDateTime::fromMSecsSinceEpoch(time * 1000); commitTime.setOffsetFromUtc(timeOffset * 60); commitMessage = QString::fromUtf8(git_commit_message(commit)); parentCount = git_commit_parentcount(commit); if (parentCount == 0) { QGitCommitDiffParent item; res = git_diff_tree_to_tree(&diff, repo, nullptr, commit_tree, nullptr); if (res) { throw QGitError("git_diff_tree_to_tree", res); } size_t _count = git_diff_num_deltas(diff); for(size_t c = 0; c < _count; c++) { delta = git_diff_get_delta(diff, c); item.addFile(delta); } git_diff_free(diff); diff = nullptr; parents.append(item); } else { for(unsigned int c = 0; c < parentCount; c++) { res = git_commit_parent(&parent, commit, c); if (res) { throw QGitError("git_commit_parent", res); } char commit_id[41] = {0, }; const git_oid *oid = git_commit_id(parent); git_oid_tostr(commit_id, 41, oid); QGitCommitDiffParent item(commit_id); res = git_commit_tree(&parent_tree, parent); if (res) { throw QGitError("git_commit_tree", res); } res = git_diff_tree_to_tree(&diff, repo, parent_tree, commit_tree, nullptr); if (res) { throw QGitError("git_diff_tree_to_tree", res); } size_t _count = git_diff_num_deltas(diff); for(size_t c = 0; c < _count; c++) { delta = git_diff_get_delta(diff, c); item.addFile(delta); } git_diff_free(diff); diff = nullptr; git_tree_free(parent_tree); parent_tree = nullptr; git_commit_free(parent); parent = nullptr; parents.append(item); } } } catch(const QGitError &ex) { error = ex; } commitDiff = QGitCommit(commitId, parents, commitTime, commitAuthor, commitCommiter, commitMessage); emit commitDiffReply(commitId, commitDiff, error); if (diff) { git_diff_free(diff); diff = nullptr; } if (parent_tree) { git_tree_free(parent_tree); parent_tree = nullptr; } if (parent) { git_commit_free(parent); parent = nullptr; } if (commit_tree) { git_tree_free(commit_tree); commit_tree = nullptr; } if(commit) { git_commit_free(commit); commit = nullptr; } if(obj) { git_object_free(obj); obj = nullptr; } if (repo) { git_repository_free(repo); repo = nullptr; } }
QGitSignature QGitCommit::committer() const { return QGitSignature(git_commit_committer(data())); }
void QGit::listCommits(QString object, int length) { QList<QGitCommit> commits; git_repository *repo = nullptr; git_revwalk *walker = nullptr; git_commit *parent = nullptr; git_commit *commit = nullptr; git_diff *diff = nullptr; git_oid oid; int count = 0; int res = 0; QGitError error; try { res = git_repository_open(&repo, m_path.absolutePath().toUtf8().constData()); if (res) { throw QGitError("git_repository_open", res); } res = git_revwalk_new(&walker, repo); if (res) { throw QGitError("git_revwalk_new", res); } if (object.isEmpty()) { res = git_revwalk_push_head(walker); if (res) { length = 0; } } else { res = git_oid_fromstr(&oid, object.toLatin1().constData()); if (res) { throw QGitError("git_oid_fromstr", res); } res = git_revwalk_push(walker, &oid); if (res) { throw QGitError("git_revwalk_push", res); } memset(&oid, 0, sizeof(oid)); git_revwalk_next(&oid, walker); memset(&oid, 0, sizeof(oid)); } while ((!git_revwalk_next(&oid, walker))&&(count < length)) { QGitCommit item; unsigned int parents = 0; QString commit_id; QList<QGitCommitDiffParent> commit_parents; QDateTime commit_time; QGitSignature commit_author; QGitSignature commit_commiter; QString commit_message; res = git_commit_lookup(&commit, repo, &oid); if (res) { throw QGitError("git_commit_lookup", res); } commit_id = QString::fromUtf8(git_oid_tostr_s(&oid)); parents = git_commit_parentcount(commit); for(unsigned int index = 0; index < parents; index++) { git_commit *parent = nullptr; QByteArray parentStr; res = git_commit_parent(&parent, commit, index); if (res) { throw QGitError("git_commit_parent", res); } const git_oid *parent_iod = git_commit_id(parent); parentStr = QByteArray(git_oid_tostr_s(parent_iod)); commit_parents.append(QGitCommitDiffParent(parentStr)); git_commit_free(parent); parent = nullptr; } auto time = git_commit_time(commit); auto timeOffset = git_commit_time_offset(commit); commit_time = QDateTime::fromMSecsSinceEpoch(time * 1000); commit_time.setOffsetFromUtc(timeOffset * 60); QString author_name = QString::fromUtf8(git_commit_author(commit)->name); QString author_email = QString::fromUtf8(git_commit_author(commit)->email); QDateTime author_when = QDateTime::fromMSecsSinceEpoch(git_commit_author(commit)->when.time * 1000); author_when.setOffsetFromUtc(git_commit_author(commit)->when.offset * 60); commit_author = QGitSignature(author_name, author_email, author_when); QString commiter_name = QString::fromUtf8(git_commit_committer(commit)->name); QString commiter_email = QString::fromUtf8(git_commit_committer(commit)->email); QDateTime commiter_when = QDateTime::fromMSecsSinceEpoch(git_commit_committer(commit)->when.time * 1000); commiter_when.setOffsetFromUtc(git_commit_committer(commit)->when.offset * 60); commit_commiter = QGitSignature(commiter_name, commiter_email, commiter_when); commit_message = QString::fromUtf8(git_commit_message(commit)); item = QGitCommit(commit_id, commit_parents, commit_time, commit_author, commit_commiter, commit_message); commits.push_back(item); git_diff_free(diff); diff = nullptr; git_commit_free(parent); parent = nullptr; git_commit_free(commit); commit = nullptr; count++; } } catch(const QGitError &ex) { error = ex; } emit listCommitsReply(commits, error); if (diff) { git_diff_free(diff); diff = nullptr; } if (parent) { git_commit_free(parent); parent = nullptr; } if (walker) { git_revwalk_free(walker); walker = nullptr; } if (repo) { git_repository_free(repo); repo = nullptr; } }