]>
Commit | Line | Data |
---|---|---|
7cb904ba ET |
1 | #include "clar_libgit2.h" |
2 | #include "git2/sys/repository.h" | |
3 | ||
4 | #include "apply.h" | |
5 | #include "repository.h" | |
6 | #include "buf_text.h" | |
7 | ||
8 | #include "apply_common.h" | |
9 | ||
10 | static git_repository *repo = NULL; | |
11 | ||
12 | void test_apply_fromdiff__initialize(void) | |
13 | { | |
14 | repo = cl_git_sandbox_init("renames"); | |
15 | } | |
16 | ||
17 | void test_apply_fromdiff__cleanup(void) | |
18 | { | |
19 | cl_git_sandbox_cleanup(); | |
20 | } | |
21 | ||
22 | static int apply_buf( | |
23 | const char *old, | |
24 | const char *oldname, | |
25 | const char *new, | |
26 | const char *newname, | |
27 | const char *patch_expected, | |
28 | const git_diff_options *diff_opts) | |
29 | { | |
30 | git_patch *patch; | |
31 | git_buf result = GIT_BUF_INIT; | |
32 | git_buf patchbuf = GIT_BUF_INIT; | |
33 | char *filename; | |
34 | unsigned int mode; | |
35 | int error; | |
36 | ||
37 | cl_git_pass(git_patch_from_buffers(&patch, | |
38 | old, old ? strlen(old) : 0, oldname, | |
39 | new, new ? strlen(new) : 0, newname, | |
40 | diff_opts)); | |
41 | cl_git_pass(git_patch_to_buf(&patchbuf, patch)); | |
42 | ||
43 | cl_assert_equal_s(patch_expected, patchbuf.ptr); | |
44 | ||
45 | error = git_apply__patch(&result, &filename, &mode, old, old ? strlen(old) : 0, patch); | |
46 | ||
47 | if (error == 0 && new == NULL) { | |
48 | cl_assert_equal_i(0, result.size); | |
49 | cl_assert_equal_p(NULL, filename); | |
50 | cl_assert_equal_i(0, mode); | |
51 | } else { | |
52 | cl_assert_equal_s(new, result.ptr); | |
53 | cl_assert_equal_s("file.txt", filename); | |
54 | cl_assert_equal_i(0100644, mode); | |
55 | } | |
56 | ||
57 | git__free(filename); | |
58 | git_buf_free(&result); | |
59 | git_buf_free(&patchbuf); | |
60 | git_patch_free(patch); | |
61 | ||
62 | return error; | |
63 | } | |
64 | ||
65 | void test_apply_fromdiff__change_middle(void) | |
66 | { | |
67 | cl_git_pass(apply_buf( | |
68 | FILE_ORIGINAL, "file.txt", | |
69 | FILE_CHANGE_MIDDLE, "file.txt", | |
70 | PATCH_ORIGINAL_TO_CHANGE_MIDDLE, NULL)); | |
71 | } | |
72 | ||
73 | void test_apply_fromdiff__change_middle_nocontext(void) | |
74 | { | |
75 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
76 | diff_opts.context_lines = 0; | |
77 | ||
78 | cl_git_pass(apply_buf( | |
79 | FILE_ORIGINAL, "file.txt", | |
80 | FILE_CHANGE_MIDDLE, "file.txt", | |
81 | PATCH_ORIGINAL_TO_CHANGE_MIDDLE_NOCONTEXT, &diff_opts)); | |
82 | } | |
83 | ||
84 | void test_apply_fromdiff__change_firstline(void) | |
85 | { | |
86 | cl_git_pass(apply_buf( | |
87 | FILE_ORIGINAL, "file.txt", | |
88 | FILE_CHANGE_FIRSTLINE, "file.txt", | |
89 | PATCH_ORIGINAL_TO_CHANGE_FIRSTLINE, NULL)); | |
90 | } | |
91 | ||
92 | void test_apply_fromdiff__lastline(void) | |
93 | { | |
94 | cl_git_pass(apply_buf( | |
95 | FILE_ORIGINAL, "file.txt", | |
96 | FILE_CHANGE_LASTLINE, "file.txt", | |
97 | PATCH_ORIGINAL_TO_CHANGE_LASTLINE, NULL)); | |
98 | } | |
99 | ||
100 | void test_apply_fromdiff__prepend(void) | |
101 | { | |
102 | cl_git_pass(apply_buf( | |
103 | FILE_ORIGINAL, "file.txt", | |
104 | FILE_PREPEND, "file.txt", | |
105 | PATCH_ORIGINAL_TO_PREPEND, NULL)); | |
106 | } | |
107 | ||
108 | void test_apply_fromdiff__prepend_nocontext(void) | |
109 | { | |
110 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
111 | diff_opts.context_lines = 0; | |
112 | ||
113 | cl_git_pass(apply_buf( | |
114 | FILE_ORIGINAL, "file.txt", | |
115 | FILE_PREPEND, "file.txt", | |
116 | PATCH_ORIGINAL_TO_PREPEND_NOCONTEXT, &diff_opts)); | |
117 | } | |
118 | ||
119 | void test_apply_fromdiff__append(void) | |
120 | { | |
121 | cl_git_pass(apply_buf( | |
122 | FILE_ORIGINAL, "file.txt", | |
123 | FILE_APPEND, "file.txt", | |
124 | PATCH_ORIGINAL_TO_APPEND, NULL)); | |
125 | } | |
126 | ||
127 | void test_apply_fromdiff__append_nocontext(void) | |
128 | { | |
129 | git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; | |
130 | diff_opts.context_lines = 0; | |
131 | ||
132 | cl_git_pass(apply_buf( | |
133 | FILE_ORIGINAL, "file.txt", | |
134 | FILE_APPEND, "file.txt", | |
135 | PATCH_ORIGINAL_TO_APPEND_NOCONTEXT, &diff_opts)); | |
136 | } | |
137 | ||
138 | void test_apply_fromdiff__prepend_and_append(void) | |
139 | { | |
140 | cl_git_pass(apply_buf( | |
141 | FILE_ORIGINAL, "file.txt", | |
142 | FILE_PREPEND_AND_APPEND, "file.txt", | |
143 | PATCH_ORIGINAL_TO_PREPEND_AND_APPEND, NULL)); | |
144 | } | |
145 | ||
146 | void test_apply_fromdiff__to_empty_file(void) | |
147 | { | |
148 | cl_git_pass(apply_buf( | |
149 | FILE_ORIGINAL, "file.txt", | |
150 | "", NULL, | |
151 | PATCH_ORIGINAL_TO_EMPTY_FILE, NULL)); | |
152 | } | |
153 | ||
154 | void test_apply_fromdiff__from_empty_file(void) | |
155 | { | |
156 | cl_git_pass(apply_buf( | |
157 | "", NULL, | |
158 | FILE_ORIGINAL, "file.txt", | |
159 | PATCH_EMPTY_FILE_TO_ORIGINAL, NULL)); | |
160 | } | |
161 | ||
162 | void test_apply_fromdiff__add(void) | |
163 | { | |
164 | cl_git_pass(apply_buf( | |
165 | NULL, NULL, | |
166 | FILE_ORIGINAL, "file.txt", | |
167 | PATCH_ADD_ORIGINAL, NULL)); | |
168 | } | |
169 | ||
170 | void test_apply_fromdiff__delete(void) | |
171 | { | |
172 | cl_git_pass(apply_buf( | |
173 | FILE_ORIGINAL, "file.txt", | |
174 | NULL, NULL, | |
175 | PATCH_DELETE_ORIGINAL, NULL)); | |
176 | } | |
0004386f ET |
177 | |
178 | void test_apply_fromdiff__no_change(void) | |
179 | { | |
180 | cl_git_pass(apply_buf( | |
181 | FILE_ORIGINAL, "file.txt", | |
182 | FILE_ORIGINAL, "file.txt", | |
183 | "", NULL)); | |
184 | } |