]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/apply/callbacks.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / apply / callbacks.c
1 #include "clar_libgit2.h"
2 #include "apply_helpers.h"
3
4 static git_repository *repo;
5
6 #define TEST_REPO_PATH "merge-recursive"
7
8 void test_apply_callbacks__initialize(void)
9 {
10 git_oid oid;
11 git_commit *commit;
12
13 repo = cl_git_sandbox_init(TEST_REPO_PATH);
14
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);
19 }
20
21 void test_apply_callbacks__cleanup(void)
22 {
23 cl_git_sandbox_cleanup();
24 }
25
26 static int delta_abort_cb(const git_diff_delta *delta, void *payload)
27 {
28 GIT_UNUSED(payload);
29
30 if (!strcmp(delta->old_file.path, "veal.txt"))
31 return -99;
32
33 return 0;
34 }
35
36 void test_apply_callbacks__delta_aborts(void)
37 {
38 git_diff *diff;
39 git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
40
41 opts.delta_cb = delta_abort_cb;
42
43 cl_git_pass(git_diff_from_buffer(&diff,
44 DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES)));
45 cl_git_fail_with(-99,
46 git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts));
47
48 validate_index_unchanged(repo);
49 validate_workdir_unchanged(repo);
50
51 git_diff_free(diff);
52 }
53
54 static int delta_skip_cb(const git_diff_delta *delta, void *payload)
55 {
56 GIT_UNUSED(payload);
57
58 if (!strcmp(delta->old_file.path, "asparagus.txt"))
59 return 1;
60
61 return 0;
62 }
63
64 void test_apply_callbacks__delta_can_skip(void)
65 {
66 git_diff *diff;
67 git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
68
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" },
76 };
77 size_t workdir_expected_cnt = sizeof(workdir_expected) /
78 sizeof(struct merge_index_entry);
79
80 opts.delta_cb = delta_skip_cb;
81
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));
85
86 validate_index_unchanged(repo);
87 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
88
89 git_diff_free(diff);
90 }
91
92 static int hunk_skip_odds_cb(const git_diff_hunk *hunk, void *payload)
93 {
94 int *count = (int *)payload;
95 GIT_UNUSED(hunk);
96
97 return ((*count)++ % 2 == 1);
98 }
99
100 void test_apply_callbacks__hunk_can_skip(void)
101 {
102 git_diff *diff;
103 git_apply_options opts = GIT_APPLY_OPTIONS_INIT;
104 int count = 0;
105
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" },
113 };
114 size_t workdir_expected_cnt = sizeof(workdir_expected) /
115 sizeof(struct merge_index_entry);
116
117 opts.hunk_cb = hunk_skip_odds_cb;
118 opts.payload = &count;
119
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));
123
124 validate_index_unchanged(repo);
125 validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
126
127 git_diff_free(diff);
128 }