]> git.proxmox.com Git - libgit2.git/blob - tests/diff/drivers.c
Merge pull request #3401 from phatblat/pb/doc-warning
[libgit2.git] / tests / diff / drivers.c
1 #include "clar_libgit2.h"
2 #include "diff_helpers.h"
3 #include "repository.h"
4 #include "diff_driver.h"
5
6 static git_repository *g_repo = NULL;
7
8 void test_diff_drivers__initialize(void)
9 {
10 }
11
12 void test_diff_drivers__cleanup(void)
13 {
14 cl_git_sandbox_cleanup();
15 g_repo = NULL;
16 }
17
18 static void overwrite_filemode(const char *expected, git_buf *actual)
19 {
20 size_t offset;
21 char *found;
22
23 found = strstr(expected, "100644");
24 if (!found)
25 return;
26
27 offset = ((const char *)found) - expected;
28 if (actual->size < offset + 6)
29 return;
30
31 if (memcmp(&actual->ptr[offset], "100644", 6) != 0)
32 memcpy(&actual->ptr[offset], "100644", 6);
33 }
34
35 void test_diff_drivers__patterns(void)
36 {
37 git_config *cfg;
38 const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13";
39 git_tree *one;
40 git_diff *diff;
41 git_patch *patch;
42 git_buf actual = GIT_BUF_INIT;
43 const char *expected0 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Comes through the blood of the vanguards who\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
44 const char *expected1 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\nBinary files a/untimely.txt and b/untimely.txt differ\n";
45 const char *expected2 = "diff --git a/untimely.txt b/untimely.txt\nindex 9a69d96..57fd0cf 100644\n--- a/untimely.txt\n+++ b/untimely.txt\n@@ -22,3 +22,5 @@ Heaven delivers on earth the Hour that cannot be\n dreamed--too soon--it had sounded.\r\n \r\n -- Rudyard Kipling\r\n+\r\n+Some new stuff\r\n";
46
47 g_repo = cl_git_sandbox_init("renames");
48
49 one = resolve_commit_oid_to_tree(g_repo, one_sha);
50
51 /* no diff */
52
53 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
54 cl_assert_equal_i(0, (int)git_diff_num_deltas(diff));
55 git_diff_free(diff);
56
57 /* default diff */
58
59 cl_git_append2file("renames/untimely.txt", "\r\nSome new stuff\r\n");
60
61 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
62 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
63
64 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
65 cl_git_pass(git_patch_to_buf(&actual, patch));
66 cl_assert_equal_s(expected0, actual.ptr);
67
68 git_buf_free(&actual);
69 git_patch_free(patch);
70 git_diff_free(diff);
71
72 /* attribute diff set to false */
73
74 cl_git_rewritefile("renames/.gitattributes", "untimely.txt -diff\n");
75
76 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
77 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
78
79 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
80 cl_git_pass(git_patch_to_buf(&actual, patch));
81 cl_assert_equal_s(expected1, actual.ptr);
82
83 git_buf_free(&actual);
84 git_patch_free(patch);
85 git_diff_free(diff);
86
87 /* attribute diff set to unconfigured value (should use default) */
88
89 cl_git_rewritefile("renames/.gitattributes", "untimely.txt diff=kipling0\n");
90
91 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
92 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
93
94 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
95 cl_git_pass(git_patch_to_buf(&actual, patch));
96 cl_assert_equal_s(expected0, actual.ptr);
97
98 git_buf_free(&actual);
99 git_patch_free(patch);
100 git_diff_free(diff);
101
102 /* let's define that driver */
103
104 cl_git_pass(git_repository_config(&cfg, g_repo));
105 cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 1));
106 git_config_free(cfg);
107
108 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
109 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
110
111 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
112 cl_git_pass(git_patch_to_buf(&actual, patch));
113 cl_assert_equal_s(expected1, actual.ptr);
114
115 git_buf_free(&actual);
116 git_patch_free(patch);
117 git_diff_free(diff);
118
119 /* let's use a real driver with some regular expressions */
120
121 git_diff_driver_registry_free(g_repo->diff_drivers);
122 g_repo->diff_drivers = NULL;
123
124 cl_git_pass(git_repository_config(&cfg, g_repo));
125 cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0));
126 cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H.*$"));
127 git_config_free(cfg);
128
129 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
130 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
131
132 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
133 cl_git_pass(git_patch_to_buf(&actual, patch));
134 cl_assert_equal_s(expected2, actual.ptr);
135
136 git_buf_free(&actual);
137 git_patch_free(patch);
138 git_diff_free(diff);
139
140 git_tree_free(one);
141 }
142
143 void test_diff_drivers__long_lines(void)
144 {
145 const char *base = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non nisi ligula. Ut viverra enim sed lobortis suscipit.\nPhasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissim risus. Suspendisse at nisi quis turpis fringilla rutrum id sit amet nulla.\nNam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\nMauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\nAliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n";
146 git_index *idx;
147 git_diff *diff;
148 git_patch *patch;
149 git_buf actual = GIT_BUF_INIT;
150 const char *expected = "diff --git a/longlines.txt b/longlines.txt\nindex c1ce6ef..0134431 100644\n--- a/longlines.txt\n+++ b/longlines.txt\n@@ -3,3 +3,5 @@ Phasellus eget erat odio. Praesent at est iaculis, ultricies augue vel, dignissi\n Nam eget dolor fermentum, aliquet nisl at, convallis tellus. Pellentesque rhoncus erat enim, id porttitor elit euismod quis.\n Mauris sollicitudin magna odio, non egestas libero vehicula ut. Etiam et quam velit. Fusce eget libero rhoncus, ultricies felis sit amet, egestas purus.\n Aliquam in semper tellus. Pellentesque adipiscing rutrum velit, quis malesuada lacus consequat eget.\n+newline\n+newline\n";
151
152 g_repo = cl_git_sandbox_init("empty_standard_repo");
153
154 cl_git_mkfile("empty_standard_repo/longlines.txt", base);
155 cl_git_pass(git_repository_index(&idx, g_repo));
156 cl_git_pass(git_index_add_bypath(idx, "longlines.txt"));
157 cl_git_pass(git_index_write(idx));
158 git_index_free(idx);
159
160 cl_git_append2file("empty_standard_repo/longlines.txt", "newline\nnewline\n");
161
162 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
163 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
164 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
165 cl_git_pass(git_patch_to_buf(&actual, patch));
166
167 /* if chmod not supported, overwrite mode bits since anything is possible */
168 overwrite_filemode(expected, &actual);
169
170 cl_assert_equal_s(expected, actual.ptr);
171
172 git_buf_free(&actual);
173 git_patch_free(patch);
174 git_diff_free(diff);
175 }
176
177 void test_diff_drivers__builtins(void)
178 {
179 git_diff *diff;
180 git_patch *patch;
181 git_buf file = GIT_BUF_INIT, actual = GIT_BUF_INIT, expected = GIT_BUF_INIT;
182 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
183 git_vector files = GIT_VECTOR_INIT;
184 size_t i;
185 char *path, *extension;
186
187 g_repo = cl_git_sandbox_init("userdiff");
188
189 cl_git_pass(git_path_dirload(&files, "userdiff/files", 9, 0));
190
191 opts.interhunk_lines = 1;
192 opts.context_lines = 1;
193 opts.pathspec.count = 1;
194
195 git_vector_foreach(&files, i, path) {
196 if (git__prefixcmp(path, "files/file."))
197 continue;
198 extension = path + strlen("files/file.");
199 opts.pathspec.strings = &path;
200
201 /* do diff with no special driver */
202
203 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
204 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
205 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
206 cl_git_pass(git_patch_to_buf(&actual, patch));
207
208 git_buf_sets(&expected, "userdiff/expected/nodriver/diff.");
209 git_buf_puts(&expected, extension);
210 cl_git_pass(git_futils_readbuffer(&expected, expected.ptr));
211
212 overwrite_filemode(expected.ptr, &actual);
213
214 cl_assert_equal_s(expected.ptr, actual.ptr);
215
216 git_buf_clear(&actual);
217 git_patch_free(patch);
218 git_diff_free(diff);
219
220 /* do diff with driver */
221
222 {
223 FILE *fp = fopen("userdiff/.gitattributes", "w");
224 fprintf(fp, "*.%s diff=%s\n", extension, extension);
225 fclose(fp);
226 }
227
228 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
229 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
230 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
231 cl_git_pass(git_patch_to_buf(&actual, patch));
232
233 git_buf_sets(&expected, "userdiff/expected/driver/diff.");
234 git_buf_puts(&expected, extension);
235 cl_git_pass(git_futils_readbuffer(&expected, expected.ptr));
236
237 overwrite_filemode(expected.ptr, &actual);
238
239 cl_assert_equal_s(expected.ptr, actual.ptr);
240
241 git_buf_clear(&actual);
242 git_patch_free(patch);
243 git_diff_free(diff);
244
245 git__free(path);
246 }
247
248 git_buf_free(&file);
249 git_buf_free(&actual);
250 git_buf_free(&expected);
251 git_vector_free(&files);
252 }
253
254 void test_diff_drivers__invalid_pattern(void)
255 {
256 git_config *cfg;
257 git_index *idx;
258 git_diff *diff;
259 git_patch *patch;
260 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
261
262 g_repo = cl_git_sandbox_init("userdiff");
263 cl_git_mkfile("userdiff/.gitattributes", "*.storyboard diff=storyboard\n");
264
265 cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
266 cl_git_pass(git_config_set_string(cfg, "diff.storyboard.xfuncname", "<!--(.*?)-->"));
267
268 cl_git_mkfile("userdiff/dummy.storyboard", "");
269 cl_git_pass(git_repository_index__weakptr(&idx, g_repo));
270 cl_git_pass(git_index_add_bypath(idx, "dummy.storyboard"));
271 cl_git_mkfile("userdiff/dummy.storyboard", "some content\n");
272
273 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
274 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
275
276 git_patch_free(patch);
277 git_diff_free(diff);
278 }