void test_repo_pathspec__tree0(void) { git_object *tree; git_strarray s; git_pathspec *ps; git_pathspec_match_list *m; /* { "*_file", "new_file", "garbage" } */ s.strings = str0; s.count = ARRAY_SIZE(str0); cl_git_pass(git_pathspec_new(&ps, &s)); cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}")); cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(4, git_pathspec_match_list_entrycount(m)); cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3)); cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 4)); cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); git_pathspec_match_list_free(m); git_object_free(tree); cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(7, git_pathspec_match_list_entrycount(m)); cl_assert_equal_s("current_file", git_pathspec_match_list_entry(m, 0)); cl_assert_equal_s("modified_file", git_pathspec_match_list_entry(m, 1)); cl_assert_equal_s("staged_changes_modified_file", git_pathspec_match_list_entry(m, 2)); cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 3)); cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 4)); cl_assert_equal_s("subdir/deleted_file", git_pathspec_match_list_entry(m, 5)); cl_assert_equal_s("subdir/modified_file", git_pathspec_match_list_entry(m, 6)); cl_assert_equal_s(NULL, git_pathspec_match_list_entry(m, 7)); cl_assert_equal_sz(2, git_pathspec_match_list_failed_entrycount(m)); cl_assert_equal_s("new_file", git_pathspec_match_list_failed_entry(m, 0)); cl_assert_equal_s("garbage", git_pathspec_match_list_failed_entry(m, 1)); cl_assert_equal_s(NULL, git_pathspec_match_list_failed_entry(m, 2)); git_pathspec_match_list_free(m); git_object_free(tree); git_pathspec_free(ps); }
void test_repo_pathspec__tree5(void) { git_object *tree; git_strarray s; git_pathspec *ps; git_pathspec_match_list *m; /* { "S*" } */ s.strings = str5; s.count = ARRAY_SIZE(str5); cl_git_pass(git_pathspec_new(&ps, &s)); cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD~2^{tree}")); cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, GIT_PATHSPEC_USE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(0, git_pathspec_match_list_entrycount(m)); cl_assert_equal_sz(1, git_pathspec_match_list_failed_entrycount(m)); git_pathspec_match_list_free(m); cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(5, git_pathspec_match_list_entrycount(m)); cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0)); cl_assert_equal_s("staged_delete_modified_file", git_pathspec_match_list_entry(m, 4)); cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); git_pathspec_match_list_free(m); git_object_free(tree); cl_git_pass(git_revparse_single(&tree, g_repo, "HEAD^{tree}")); cl_git_pass(git_pathspec_match_tree(&m, (git_tree *)tree, GIT_PATHSPEC_IGNORE_CASE | GIT_PATHSPEC_FIND_FAILURES, ps)); cl_assert_equal_sz(9, git_pathspec_match_list_entrycount(m)); cl_assert_equal_s("staged_changes", git_pathspec_match_list_entry(m, 0)); cl_assert_equal_s("subdir.txt", git_pathspec_match_list_entry(m, 5)); cl_assert_equal_s("subdir/current_file", git_pathspec_match_list_entry(m, 6)); cl_assert_equal_sz(0, git_pathspec_match_list_failed_entrycount(m)); git_pathspec_match_list_free(m); git_object_free(tree); git_pathspec_free(ps); }
/* Helper to get the latest commit of the specified file */ int git_show_last_commit(char *filename) { git_oid oid; git_commit *commit = NULL; /* Set up pathspec. */ git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; diffopts.pathspec.strings = &filename; diffopts.pathspec.count = 1; /* Use the revwalker to traverse the history. */ check_lg2(git_revwalk_push_ref(s.walker, s.ref), "Could not find repository reference", NULL); for (; !git_revwalk_next(&oid, s.walker); git_commit_free(commit)) { check_lg2(git_commit_lookup(&commit, s.repo, &oid), "Failed to look up commit", NULL); int parents = (int)git_commit_parentcount(commit); int unmatched = parents; if (parents == 0) { git_tree *tree; git_pathspec *ps; check_lg2(git_commit_tree(&tree, commit), "Get tree", NULL); check_lg2(git_pathspec_new(&ps, &diffopts.pathspec), "Building pathspec", NULL); if (git_pathspec_match_tree( NULL, tree, GIT_PATHSPEC_NO_MATCH_ERROR, ps) != 0) unmatched = 1; git_pathspec_free(ps); git_tree_free(tree); } else { int i; for (i = 0; i < parents; ++i) { if (match_with_parent(commit, i, &diffopts)) unmatched--; } } if (unmatched > 0) continue; print_commit(commit); git_commit_free(commit); break; } git_revwalk_reset(s.walker); return 0; }