]> git.proxmox.com Git - libgit2.git/blob - tests/diff/drivers.c
Import git drivers and test HTML driver
[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_chmod_at_offset(git_buf *buf, size_t offset)
19 {
20 if (cl_is_chmod_supported())
21 return;
22
23 if (buf->size > offset + 6 && memcmp(&buf->ptr[offset], "100644", 6) != 0)
24 memcpy(&buf->ptr[offset], "100644", 6);
25 }
26
27 void test_diff_drivers__patterns(void)
28 {
29 git_config *cfg;
30 const char *one_sha = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13";
31 git_tree *one;
32 git_diff *diff;
33 git_patch *patch;
34 git_buf actual = GIT_BUF_INIT;
35 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";
36 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";
37 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";
38
39 g_repo = cl_git_sandbox_init("renames");
40
41 one = resolve_commit_oid_to_tree(g_repo, one_sha);
42
43 /* no diff */
44
45 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
46 cl_assert_equal_i(0, (int)git_diff_num_deltas(diff));
47 git_diff_free(diff);
48
49 /* default diff */
50
51 cl_git_append2file("renames/untimely.txt", "\r\nSome new stuff\r\n");
52
53 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
54 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
55
56 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
57 cl_git_pass(git_patch_to_buf(&actual, patch));
58 cl_assert_equal_s(expected0, actual.ptr);
59
60 git_buf_free(&actual);
61 git_patch_free(patch);
62 git_diff_free(diff);
63
64 /* attribute diff set to false */
65
66 cl_git_rewritefile("renames/.gitattributes", "untimely.txt -diff\n");
67
68 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
69 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
70
71 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
72 cl_git_pass(git_patch_to_buf(&actual, patch));
73 cl_assert_equal_s(expected1, actual.ptr);
74
75 git_buf_free(&actual);
76 git_patch_free(patch);
77 git_diff_free(diff);
78
79 /* attribute diff set to unconfigured value (should use default) */
80
81 cl_git_rewritefile("renames/.gitattributes", "untimely.txt diff=kipling0\n");
82
83 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
84 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
85
86 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
87 cl_git_pass(git_patch_to_buf(&actual, patch));
88 cl_assert_equal_s(expected0, actual.ptr);
89
90 git_buf_free(&actual);
91 git_patch_free(patch);
92 git_diff_free(diff);
93
94 /* let's define that driver */
95
96 cl_git_pass(git_repository_config(&cfg, g_repo));
97 cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 1));
98 git_config_free(cfg);
99
100 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
101 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
102
103 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
104 cl_git_pass(git_patch_to_buf(&actual, patch));
105 cl_assert_equal_s(expected1, actual.ptr);
106
107 git_buf_free(&actual);
108 git_patch_free(patch);
109 git_diff_free(diff);
110
111 /* let's use a real driver with some regular expressions */
112
113 git_diff_driver_registry_free(g_repo->diff_drivers);
114 g_repo->diff_drivers = NULL;
115
116 cl_git_pass(git_repository_config(&cfg, g_repo));
117 cl_git_pass(git_config_set_bool(cfg, "diff.kipling0.binary", 0));
118 cl_git_pass(git_config_set_string(cfg, "diff.kipling0.xfuncname", "^H"));
119 git_config_free(cfg);
120
121 cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, one, NULL));
122 cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
123
124 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
125 cl_git_pass(git_patch_to_buf(&actual, patch));
126 cl_assert_equal_s(expected2, actual.ptr);
127
128 git_buf_free(&actual);
129 git_patch_free(patch);
130 git_diff_free(diff);
131
132 git_tree_free(one);
133 }
134
135 void test_diff_drivers__long_lines(void)
136 {
137 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";
138 git_index *idx;
139 git_diff *diff;
140 git_patch *patch;
141 git_buf actual = GIT_BUF_INIT;
142 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";
143
144 g_repo = cl_git_sandbox_init("empty_standard_repo");
145
146 cl_git_mkfile("empty_standard_repo/longlines.txt", base);
147 cl_git_pass(git_repository_index(&idx, g_repo));
148 cl_git_pass(git_index_add_bypath(idx, "longlines.txt"));
149 cl_git_pass(git_index_write(idx));
150 git_index_free(idx);
151
152 cl_git_append2file("empty_standard_repo/longlines.txt", "newline\nnewline\n");
153
154 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, NULL));
155 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
156 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
157 cl_git_pass(git_patch_to_buf(&actual, patch));
158
159 /* if chmod not supported, overwrite mode bits since anything is possible */
160 overwrite_chmod_at_offset(&actual, 66);
161
162 cl_assert_equal_s(expected, actual.ptr);
163
164 git_buf_free(&actual);
165 git_patch_free(patch);
166 git_diff_free(diff);
167 }
168
169 void test_diff_drivers__builtins(void)
170 {
171 git_index *idx;
172 git_diff *diff;
173 git_patch *patch;
174 git_buf actual = GIT_BUF_INIT;
175 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
176 const char *base =
177 "<html>\n<body>\n"
178 " <h1 id=\"first section\">\n <ol>\n <li>item 1.1</li>\n <li>item 1.2</li>\n <li>item 1.3</li>\n <li>item 1.4</li>\n <li>item 1.5</li>\n <li>item 1.6</li>\n <li>item 1.7</li>\n <li>item 1.8</li>\n <li>item 1.9</li>\n </ol>\n </h1>\n"
179 " <h1 id=\"second section\">\n <ol>\n <li>item 2.1</li>\n <li>item 2.2</li>\n <li>item 2.3</li>\n <li>item 2.4</li>\n <li>item 2.5</li>\n <li>item 2.6</li>\n <li>item 2.7</li>\n <li>item 2.8</li>\n </ol>\n </h1>\n"
180 " <h1 id=\"third section\">\n <ol>\n <li>item 3.1</li>\n <li>item 3.2</li>\n <li>item 3.3</li>\n <li>item 3.4</li>\n <li>item 3.5</li>\n <li>item 3.6</li>\n <li>item 3.7</li>\n <li>item 3.8</li>\n </ol>\n </h1>\n"
181 "</body></html>\n";
182 const char *modified =
183 "<html>\n<body>\n"
184 " <h1 id=\"first section\">\n <ol>\n <li>item 1.1</li>\n <li>item 1.2 changed</li>\n <li>item 1.3 changed</li>\n <li>item 1.4</li>\n <li>item 1.5</li>\n <li>item 1.6</li>\n <li>item 1.7</li>\n <li>item 1.8</li>\n <li>item 1.9</li>\n <li>item 1.10 added</li>\n </ol>\n </h1>\n"
185 " <h1 id=\"second section\">\n <ol>\n <li>item 2.1</li>\n <li>item 2.2</li>\n <li>item 2.3</li>\n <li>item 2.4</li>\n <li>item 2.5</li>\n <li>item 2.6</li>\n <li>item 2.7 changed</li>\n <li>item 2.7.1 added</li>\n <li>item 2.8</li>\n </ol>\n </h1>\n"
186 " <h1 id=\"third section\">\n <ol>\n <li>item 3.1</li>\n <li>item 3.2</li>\n <li>item 3.3</li>\n <li>item 3.4</li>\n <li>item 3.5</li>\n <li>item 3.6</li>\n </ol>\n </h1>\n"
187 "</body></html>\n";
188 const char *expected_nodriver =
189 "diff --git a/file.html b/file.html\nindex 97b34db..c7dbed3 100644\n--- a/file.html\n+++ b/file.html\n@@ -5,4 +5,4 @@\n <li>item 1.1</li>\n- <li>item 1.2</li>\n- <li>item 1.3</li>\n+ <li>item 1.2 changed</li>\n+ <li>item 1.3 changed</li>\n <li>item 1.4</li>\n@@ -13,2 +13,3 @@\n <li>item 1.9</li>\n+ <li>item 1.10 added</li>\n </ol>\n@@ -23,3 +24,4 @@\n <li>item 2.6</li>\n- <li>item 2.7</li>\n+ <li>item 2.7 changed</li>\n+ <li>item 2.7.1 added</li>\n <li>item 2.8</li>\n@@ -35,4 +37,2 @@\n <li>item 3.6</li>\n- <li>item 3.7</li>\n- <li>item 3.8</li>\n </ol>\n";
190 const char *expected_driver =
191 "diff --git a/file.html b/file.html\nindex 97b34db..c7dbed3 100644\n--- a/file.html\n+++ b/file.html\n@@ -5,4 +5,4 @@ <h1 id=\"first section\">\n <li>item 1.1</li>\n- <li>item 1.2</li>\n- <li>item 1.3</li>\n+ <li>item 1.2 changed</li>\n+ <li>item 1.3 changed</li>\n <li>item 1.4</li>\n@@ -13,2 +13,3 @@ <h1 id=\"first section\">\n <li>item 1.9</li>\n+ <li>item 1.10 added</li>\n </ol>\n@@ -23,3 +24,4 @@ <h1 id=\"second section\">\n <li>item 2.6</li>\n- <li>item 2.7</li>\n+ <li>item 2.7 changed</li>\n+ <li>item 2.7.1 added</li>\n <li>item 2.8</li>\n@@ -35,4 +37,2 @@ <h1 id=\"third section\">\n <li>item 3.6</li>\n- <li>item 3.7</li>\n- <li>item 3.8</li>\n </ol>\n";
192
193 g_repo = cl_git_sandbox_init("empty_standard_repo");
194
195 cl_git_mkfile("empty_standard_repo/file.html", base);
196 cl_git_pass(git_repository_index(&idx, g_repo));
197 cl_git_pass(git_index_add_bypath(idx, "file.html"));
198 cl_git_pass(git_index_write(idx));
199 git_index_free(idx);
200
201 cl_git_rewritefile("empty_standard_repo/file.html", modified);
202
203 /* do diff with no special driver */
204
205 opts.interhunk_lines = 1;
206 opts.context_lines = 1;
207
208 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
209 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
210 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
211 cl_git_pass(git_patch_to_buf(&actual, patch));
212
213 overwrite_chmod_at_offset(&actual, 59);
214
215 cl_assert_equal_s(expected_nodriver, actual.ptr);
216
217 git_buf_free(&actual);
218 git_patch_free(patch);
219 git_diff_free(diff);
220
221 /* do diff with HTML driver */
222
223 cl_git_mkfile("empty_standard_repo/.gitattributes", "*.html diff=html\n");
224
225 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
226 cl_assert_equal_sz(1, git_diff_num_deltas(diff));
227 cl_git_pass(git_patch_from_diff(&patch, diff, 0));
228 cl_git_pass(git_patch_to_buf(&actual, patch));
229
230 overwrite_chmod_at_offset(&actual, 59);
231
232 cl_assert_equal_s(expected_driver, actual.ptr);
233
234 git_buf_free(&actual);
235 git_patch_free(patch);
236 git_diff_free(diff);
237 }