1 #include "clar_libgit2.h"
2 #include "apply_helpers.h"
4 static git_repository
*repo
;
6 #define TEST_REPO_PATH "merge-recursive"
8 void test_apply_check__initialize(void)
13 repo
= cl_git_sandbox_init(TEST_REPO_PATH
);
15 git_oid_fromstr(&oid
, "539bd011c4822c560c1d17cab095006b7a10f707");
16 cl_git_pass(git_commit_lookup(&commit
, repo
, &oid
));
17 cl_git_pass(git_reset(repo
, (git_object
*)commit
, GIT_RESET_HARD
, NULL
));
18 git_commit_free(commit
);
21 void test_apply_check__cleanup(void)
23 cl_git_sandbox_cleanup();
26 void test_apply_check__generate_diff(void)
29 git_commit
*a_commit
, *b_commit
;
30 git_tree
*a_tree
, *b_tree
;
32 git_diff_options diff_opts
= GIT_DIFF_OPTIONS_INIT
;
33 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
35 cl_git_pass(git_oid_fromstr(&a_oid
, "539bd011c4822c560c1d17cab095006b7a10f707"));
36 cl_git_pass(git_oid_fromstr(&b_oid
, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f"));
37 cl_git_pass(git_commit_lookup(&a_commit
, repo
, &a_oid
));
38 cl_git_pass(git_commit_lookup(&b_commit
, repo
, &b_oid
));
40 cl_git_pass(git_commit_tree(&a_tree
, a_commit
));
41 cl_git_pass(git_commit_tree(&b_tree
, b_commit
));
43 opts
.flags
|= GIT_APPLY_CHECK
;
44 cl_git_pass(git_diff_tree_to_tree(&diff
, repo
, a_tree
, b_tree
, &diff_opts
));
45 cl_git_pass(git_apply(repo
, diff
, GIT_APPLY_LOCATION_BOTH
, &opts
));
47 validate_index_unchanged(repo
);
48 validate_workdir_unchanged(repo
);
51 git_tree_free(a_tree
);
52 git_tree_free(b_tree
);
53 git_commit_free(a_commit
);
54 git_commit_free(b_commit
);
57 void test_apply_check__parsed_diff(void)
60 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
62 opts
.flags
|= GIT_APPLY_CHECK
;
63 cl_git_pass(git_diff_from_buffer(&diff
,
64 DIFF_MODIFY_TWO_FILES
, strlen(DIFF_MODIFY_TWO_FILES
)));
65 cl_git_pass(git_apply(repo
, diff
, GIT_APPLY_LOCATION_INDEX
, &opts
));
67 validate_index_unchanged(repo
);
68 validate_workdir_unchanged(repo
);
73 void test_apply_check__binary(void)
76 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
78 opts
.flags
|= GIT_APPLY_CHECK
;
79 cl_git_pass(git_diff_from_buffer(&diff
,
80 DIFF_MODIFY_TWO_FILES_BINARY
,
81 strlen(DIFF_MODIFY_TWO_FILES_BINARY
)));
82 cl_git_pass(git_apply(repo
, diff
, GIT_APPLY_LOCATION_INDEX
, &opts
));
84 validate_index_unchanged(repo
);
85 validate_workdir_unchanged(repo
);
90 void test_apply_check__does_not_apply(void)
94 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
96 const char *diff_file
= DIFF_MODIFY_TWO_FILES
;
98 struct merge_index_entry index_expected
[] = {
99 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
100 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
101 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
102 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
103 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
105 size_t index_expected_cnt
= sizeof(index_expected
) /
106 sizeof(struct merge_index_entry
);
108 /* mutate the index */
109 cl_git_pass(git_repository_index(&index
, repo
));
110 cl_git_pass(git_index_remove(index
, "veal.txt", 0));
111 cl_git_pass(git_index_write(index
));
112 git_index_free(index
);
114 opts
.flags
|= GIT_APPLY_CHECK
;
115 cl_git_pass(git_diff_from_buffer(&diff
, diff_file
, strlen(diff_file
)));
116 cl_git_fail_with(GIT_EAPPLYFAIL
, git_apply(repo
, diff
, GIT_APPLY_LOCATION_INDEX
, &opts
));
118 validate_apply_index(repo
, index_expected
, index_expected_cnt
);