void test_revwalk_basic__push_head(void) { int i = 0; git_oid oid; cl_git_pass(git_revwalk_push_head(_walk)); while (git_revwalk_next(&oid, _walk) == 0) { i++; } /* git log HEAD --oneline | wc -l => 7 */ cl_assert(i == 7); }
int cmd_log(git_repository *repo, int argc, char **argv) { int err = 0; int rc; git_revwalk *walk; git_oid oid; git_reference *revision_ref = NULL; git_object *revision_obj = NULL; rc = EXIT_FAILURE; git_revwalk_new(&walk,repo); if (argc > 1) { if ((err = git_reference_dwim(&revision_ref, repo, argv[1]))) goto out; if ((err = git_reference_peel(&revision_obj, revision_ref, GIT_OBJ_ANY))) goto out; if ((err = git_revwalk_push(walk, git_object_id(revision_obj)))) goto out; } else { git_revwalk_push_head(walk); } while ((git_revwalk_next(&oid, walk)) == 0) { struct git_commit *wcommit; if (git_commit_lookup(&wcommit, repo, &oid) != 0) continue; print_commit(wcommit,"commit %C\nAuthor: %a\nDate: %d\n\n%m\n"); git_commit_free(wcommit); } git_revwalk_free(walk); rc = EXIT_SUCCESS; out: if (err != GIT_OK) libgit_error(); if (revision_obj) git_object_free(revision_obj); if (revision_ref) git_reference_free(revision_ref); return rc; }
void test_revwalk_basic__push_head_hide_ref_nobase(void) { int i = 0; git_oid oid; cl_git_pass(git_revwalk_push_head(_walk)); cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed")); while (git_revwalk_next(&oid, _walk) == 0) { i++; } /* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */ cl_assert(i == 7); }
void test_revwalk_basic__push_head(void) { int i = 0; git_oid oid; revwalk_basic_setup_walk(NULL); cl_git_pass(git_revwalk_push_head(_walk)); while (git_revwalk_next(&oid, _walk) == 0) { i++; } /* git log HEAD --oneline | wc -l => 7 */ cl_assert_equal_i(i, 7); }
void test_revwalk_basic__push_head_hide_ref(void) { int i = 0; git_oid oid; revwalk_basic_setup_walk(NULL); cl_git_pass(git_revwalk_push_head(_walk)); cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test")); while (git_revwalk_next(&oid, _walk) == 0) { i++; } /* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */ cl_assert_equal_i(i, 4); }
/* * Difference between test_revwalk_basic__multiple_push_1 and * test_revwalk_basic__multiple_push_2 is in the order reference * refs/heads/packed-test and commit 5b5b02 are pushed. * revwalk should return same commits in both the tests. * $ git rev-list 5b5b02 HEAD ^refs/heads/packed-test * a65fedf39aefe402d3bb6e24df4d4f5fe4547750 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644 * c47800c7266a2be04c571c04d5a6614691ea99bd * 9fd738e8f7967c078dceed8190330fc8648ee56a * $ git log 5b5b02 HEAD --oneline --not refs/heads/packed-test | wc -l => 4 * a65fedf * be3563a Merge branch 'br2' * c47800c branch commit one * 9fd738e a fourth commit */ void test_revwalk_basic__multiple_push_2(void) { int i = 0; git_oid oid; revwalk_basic_setup_walk(NULL); cl_git_pass(git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644")); cl_git_pass(git_revwalk_push(_walk, &oid)); cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test")); cl_git_pass(git_revwalk_push_head(_walk)); while (git_revwalk_next(&oid, _walk) == 0) i++; cl_assert_equal_i(i, 4); }
int cmd_revwalk(git_repository *repo, int argc, const char **argv) { git_revwalk *walker; git_oid oid; int error; if (git_revwalk_new(&walker, repo) < 0) return -1; git_revwalk_sorting(walker, GIT_SORT_TIME); if (git_revwalk_push_head(walker) < 0) return -1; while ((error = git_revwalk_next(&oid, walker)) == 0) { char hex[41]; hex[40] = '\0'; git_oid_fmt(hex, &oid); printf("%s\n", hex); } return error == GIT_ITEROVER ? 0 : error; }
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; } }
static git_commit* FindFileRecentCommit(git_repository* repository, const CString& path) { CAutoRevwalk walk; if (git_revwalk_new(walk.GetPointer(), repository)) return nullptr; CStringA pathA = CUnicodeUtils::GetUTF8(path); if (pathA.GetLength() >= MAX_PATH) return nullptr; const char *pathC = pathA; char folder[MAX_PATH] = {0}, file[MAX_PATH] = {0}; const char *slash = strrchr(pathC, '/'); if (slash) { strncpy(folder, pathC, slash - pathC + 1); folder[slash - pathC + 1] = '\0'; strcpy(file, slash + 1); } else { folder[0] = '\0'; strcpy(file, pathC); } TreewalkStruct treewalkstruct = { folder, file }; if (git_revwalk_push_head(walk)) return nullptr; git_oid oid; CAutoCommit commit; while (!git_revwalk_next(&oid, walk)) { if (git_commit_lookup(commit.GetPointer(), repository, &oid)) return nullptr; CAutoTree tree; if (git_commit_tree(tree.GetPointer(), commit)) return nullptr; memset(&treewalkstruct.oid.id, 0, sizeof(treewalkstruct.oid.id)); int ret = git_tree_walk(tree, GIT_TREEWALK_PRE, TreewalkCB_FindFileRecentCommit, &treewalkstruct); if (ret < 0 && ret != GIT_EUSER) return nullptr; // check if file not found if (git_oid_iszero(&treewalkstruct.oid)) return nullptr; bool diff = true; // for merge point, check if it is different to all parents, if yes then there are real change in the merge point. // if no parent then of course it is different for (unsigned int i = 0; i < git_commit_parentcount(commit); ++i) { CAutoCommit commit2; if (git_commit_parent(commit2.GetPointer(), commit, i)) return nullptr; CAutoTree tree2; if (git_commit_tree(tree2.GetPointer(), commit2)) return nullptr; TreewalkStruct treewalkstruct2 = { folder, file }; memset(&treewalkstruct2.oid.id, 0, sizeof(treewalkstruct2.oid.id)); int ret = git_tree_walk(tree2, GIT_TREEWALK_PRE, TreewalkCB_FindFileRecentCommit, &treewalkstruct2); if (ret < 0 && ret != GIT_EUSER) return nullptr; if (!git_oid_cmp(&treewalkstruct.oid, &treewalkstruct2.oid)) diff = false; else if (git_revwalk_hide(walk, git_commit_parent_id(commit, i))) return nullptr; } if (diff) break; } return commit.Detach(); }