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_callbacks__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_callbacks__cleanup(void)
23 cl_git_sandbox_cleanup();
26 static int delta_abort_cb(const git_diff_delta
*delta
, void *payload
)
30 if (!strcmp(delta
->old_file
.path
, "veal.txt"))
36 void test_apply_callbacks__delta_aborts(void)
39 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
41 opts
.delta_cb
= delta_abort_cb
;
43 cl_git_pass(git_diff_from_buffer(&diff
,
44 DIFF_MODIFY_TWO_FILES
, strlen(DIFF_MODIFY_TWO_FILES
)));
46 git_apply(repo
, diff
, GIT_APPLY_LOCATION_INDEX
, &opts
));
48 validate_index_unchanged(repo
);
49 validate_workdir_unchanged(repo
);
54 static int delta_skip_cb(const git_diff_delta
*delta
, void *payload
)
58 if (!strcmp(delta
->old_file
.path
, "asparagus.txt"))
64 void test_apply_callbacks__delta_can_skip(void)
67 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
69 struct merge_index_entry workdir_expected
[] = {
70 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
71 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
72 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
73 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
74 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
75 { 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
77 size_t workdir_expected_cnt
= sizeof(workdir_expected
) /
78 sizeof(struct merge_index_entry
);
80 opts
.delta_cb
= delta_skip_cb
;
82 cl_git_pass(git_diff_from_buffer(&diff
,
83 DIFF_MODIFY_TWO_FILES
, strlen(DIFF_MODIFY_TWO_FILES
)));
84 cl_git_pass(git_apply(repo
, diff
, GIT_APPLY_LOCATION_WORKDIR
, &opts
));
86 validate_index_unchanged(repo
);
87 validate_apply_workdir(repo
, workdir_expected
, workdir_expected_cnt
);
92 static int hunk_skip_odds_cb(const git_diff_hunk
*hunk
, void *payload
)
94 int *count
= (int *)payload
;
97 return ((*count
)++ % 2 == 1);
100 void test_apply_callbacks__hunk_can_skip(void)
103 git_apply_options opts
= GIT_APPLY_OPTIONS_INIT
;
106 struct merge_index_entry workdir_expected
[] = {
107 { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
108 { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
109 { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
110 { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" },
111 { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
112 { 0100644, "06f751b6ba4f017ddbf4248015768300268e092a", 0, "veal.txt" },
114 size_t workdir_expected_cnt
= sizeof(workdir_expected
) /
115 sizeof(struct merge_index_entry
);
117 opts
.hunk_cb
= hunk_skip_odds_cb
;
118 opts
.payload
= &count
;
120 cl_git_pass(git_diff_from_buffer(&diff
,
121 DIFF_MANY_CHANGES_ONE
, strlen(DIFF_MANY_CHANGES_ONE
)));
122 cl_git_pass(git_apply(repo
, diff
, GIT_APPLY_LOCATION_WORKDIR
, &opts
));
124 validate_index_unchanged(repo
);
125 validate_apply_workdir(repo
, workdir_expected
, workdir_expected_cnt
);