]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/diff/stats.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / diff / stats.c
1 #include "clar.h"
2 #include "clar_libgit2.h"
3
4 #include "commit.h"
5 #include "diff.h"
6 #include "diff_generate.h"
7
8 static git_repository *_repo;
9 static git_diff_stats *_stats;
10
11 void test_diff_stats__initialize(void)
12 {
13 _repo = cl_git_sandbox_init("diff_format_email");
14 }
15
16 void test_diff_stats__cleanup(void)
17 {
18 git_diff_stats_free(_stats); _stats = NULL;
19 cl_git_sandbox_cleanup();
20 }
21
22 static void diff_stats_from_commit_oid(
23 git_diff_stats **stats, const char *oidstr, bool rename)
24 {
25 git_oid oid;
26 git_commit *commit;
27 git_diff *diff;
28
29 git_oid_fromstr(&oid, oidstr);
30 cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
31 cl_git_pass(git_diff__commit(&diff, _repo, commit, NULL));
32 if (rename)
33 cl_git_pass(git_diff_find_similar(diff, NULL));
34 cl_git_pass(git_diff_get_stats(stats, diff));
35
36 git_diff_free(diff);
37 git_commit_free(commit);
38 }
39
40 void test_diff_stats__stat(void)
41 {
42 git_buf buf = GIT_BUF_INIT;
43 const char *stat =
44 " file1.txt | 8 +++++---\n" \
45 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
46
47 diff_stats_from_commit_oid(
48 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
49
50 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
51 cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
52 cl_assert_equal_sz(3, git_diff_stats_deletions(_stats));
53
54 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
55 cl_assert(strcmp(buf.ptr, stat) == 0);
56 git_buf_dispose(&buf);
57
58 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 80));
59 cl_assert(strcmp(buf.ptr, stat) == 0);
60 git_buf_dispose(&buf);
61 }
62
63 void test_diff_stats__multiple_hunks(void)
64 {
65 git_buf buf = GIT_BUF_INIT;
66 const char *stat =
67 " file2.txt | 5 +++--\n" \
68 " file3.txt | 6 ++++--\n" \
69 " 2 files changed, 7 insertions(+), 4 deletions(-)\n";
70
71 diff_stats_from_commit_oid(
72 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
73
74 cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
75 cl_assert_equal_sz(7, git_diff_stats_insertions(_stats));
76 cl_assert_equal_sz(4, git_diff_stats_deletions(_stats));
77
78 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
79 cl_assert_equal_s(stat, buf.ptr);
80 git_buf_dispose(&buf);
81 }
82
83 void test_diff_stats__numstat(void)
84 {
85 git_buf buf = GIT_BUF_INIT;
86 const char *stat =
87 "3 2 file2.txt\n"
88 "4 2 file3.txt\n";
89
90 diff_stats_from_commit_oid(
91 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
92
93 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
94 cl_assert_equal_s(stat, buf.ptr);
95 git_buf_dispose(&buf);
96 }
97
98 void test_diff_stats__shortstat(void)
99 {
100 git_buf buf = GIT_BUF_INIT;
101 const char *stat =
102 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
103
104 diff_stats_from_commit_oid(
105 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
106
107 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
108 cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
109 cl_assert_equal_sz(3, git_diff_stats_deletions(_stats));
110
111 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
112 cl_assert_equal_s(stat, buf.ptr);
113 git_buf_dispose(&buf);
114 }
115
116 void test_diff_stats__shortstat_noinsertions(void)
117 {
118 git_buf buf = GIT_BUF_INIT;
119 const char *stat =
120 " 1 file changed, 2 deletions(-)\n";
121
122 diff_stats_from_commit_oid(
123 &_stats, "06b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6", false);
124
125 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
126 cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
127 cl_assert_equal_sz(2, git_diff_stats_deletions(_stats));
128
129 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
130 cl_assert_equal_s(stat, buf.ptr);
131 git_buf_dispose(&buf);
132 }
133
134 void test_diff_stats__shortstat_nodeletions(void)
135 {
136 git_buf buf = GIT_BUF_INIT;
137 const char *stat =
138 " 1 file changed, 3 insertions(+)\n";
139
140 diff_stats_from_commit_oid(
141 &_stats, "5219b9784f9a92d7bd7cb567a6d6a21bfb86697e", false);
142
143 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
144 cl_assert_equal_sz(3, git_diff_stats_insertions(_stats));
145 cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
146
147 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
148 cl_assert_equal_s(stat, buf.ptr);
149 git_buf_dispose(&buf);
150 }
151
152 void test_diff_stats__rename(void)
153 {
154 git_buf buf = GIT_BUF_INIT;
155 const char *stat =
156 " file2.txt => file2.txt.renamed | 1 +\n"
157 " file3.txt => file3.txt.renamed | 4 +++-\n"
158 " 2 files changed, 4 insertions(+), 1 deletion(-)\n";
159
160 diff_stats_from_commit_oid(
161 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
162
163 cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
164 cl_assert_equal_sz(4, git_diff_stats_insertions(_stats));
165 cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
166
167 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
168 cl_assert_equal_s(stat, buf.ptr);
169 git_buf_dispose(&buf);
170 }
171
172 void test_diff_stats__rename_nochanges(void)
173 {
174 git_buf buf = GIT_BUF_INIT;
175 const char *stat =
176 " file2.txt.renamed => file2.txt.renamed2 | 0\n"
177 " file3.txt.renamed => file3.txt.renamed2 | 0\n"
178 " 2 files changed, 0 insertions(+), 0 deletions(-)\n";
179
180 diff_stats_from_commit_oid(
181 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
182
183 cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
184 cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
185 cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
186
187 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
188 cl_assert_equal_s(stat, buf.ptr);
189 git_buf_dispose(&buf);
190 }
191
192 void test_diff_stats__rename_and_modifiy(void)
193 {
194 git_buf buf = GIT_BUF_INIT;
195 const char *stat =
196 " file2.txt.renamed2 | 2 +-\n"
197 " file3.txt.renamed2 => file3.txt.renamed | 0\n"
198 " 2 files changed, 1 insertion(+), 1 deletion(-)\n";
199
200 diff_stats_from_commit_oid(
201 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
202
203 cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
204 cl_assert_equal_sz(1, git_diff_stats_insertions(_stats));
205 cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
206
207 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
208 cl_assert_equal_s(stat, buf.ptr);
209 git_buf_dispose(&buf);
210 }
211
212 void test_diff_stats__rename_in_subdirectory(void)
213 {
214 git_buf buf = GIT_BUF_INIT;
215 const char *stat =
216 " dir/{orig.txt => renamed.txt} | 0\n"
217 " 1 file changed, 0 insertions(+), 0 deletions(-)\n";
218
219 diff_stats_from_commit_oid(
220 &_stats, "0db2a262bc8c5c3cba55254730045a8258da7a37", true);
221
222 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
223 cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
224 cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
225
226 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
227 cl_assert_equal_s(stat, buf.ptr);
228 git_buf_dispose(&buf);
229 }
230
231 void test_diff_stats__rename_no_find(void)
232 {
233 git_buf buf = GIT_BUF_INIT;
234 const char *stat =
235 " file2.txt | 5 -----\n"
236 " file2.txt.renamed | 6 ++++++\n"
237 " file3.txt | 5 -----\n"
238 " file3.txt.renamed | 7 +++++++\n"
239 " 4 files changed, 13 insertions(+), 10 deletions(-)\n";
240
241 diff_stats_from_commit_oid(
242 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
243
244 cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
245 cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
246 cl_assert_equal_sz(10, git_diff_stats_deletions(_stats));
247
248 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
249 cl_assert_equal_s(stat, buf.ptr);
250 git_buf_dispose(&buf);
251 }
252
253 void test_diff_stats__rename_nochanges_no_find(void)
254 {
255 git_buf buf = GIT_BUF_INIT;
256 const char *stat =
257 " file2.txt.renamed | 6 ------\n"
258 " file2.txt.renamed2 | 6 ++++++\n"
259 " file3.txt.renamed | 7 -------\n"
260 " file3.txt.renamed2 | 7 +++++++\n"
261 " 4 files changed, 13 insertions(+), 13 deletions(-)\n";
262
263 diff_stats_from_commit_oid(
264 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
265
266 cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
267 cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
268 cl_assert_equal_sz(13, git_diff_stats_deletions(_stats));
269
270 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
271 cl_assert_equal_s(stat, buf.ptr);
272 git_buf_dispose(&buf);
273 }
274
275 void test_diff_stats__rename_and_modify_no_find(void)
276 {
277 git_buf buf = GIT_BUF_INIT;
278 const char *stat =
279 " file2.txt.renamed2 | 2 +-\n"
280 " file3.txt.renamed | 7 +++++++\n"
281 " file3.txt.renamed2 | 7 -------\n"
282 " 3 files changed, 8 insertions(+), 8 deletions(-)\n";
283
284 diff_stats_from_commit_oid(
285 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
286
287 cl_assert_equal_sz(3, git_diff_stats_files_changed(_stats));
288 cl_assert_equal_sz(8, git_diff_stats_insertions(_stats));
289 cl_assert_equal_sz(8, git_diff_stats_deletions(_stats));
290
291 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
292 cl_assert_equal_s(stat, buf.ptr);
293 git_buf_dispose(&buf);
294 }
295
296 void test_diff_stats__binary(void)
297 {
298 git_buf buf = GIT_BUF_INIT;
299 const char *stat =
300 " binary.bin | Bin 3 -> 5 bytes\n"
301 " 1 file changed, 0 insertions(+), 0 deletions(-)\n";
302
303 diff_stats_from_commit_oid(
304 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
305
306 cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
307 cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
308 cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
309
310 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
311 cl_assert_equal_s(stat, buf.ptr);
312 git_buf_dispose(&buf);
313 }
314
315 void test_diff_stats__binary_numstat(void)
316 {
317 git_buf buf = GIT_BUF_INIT;
318 const char *stat =
319 "- - binary.bin\n";
320
321 diff_stats_from_commit_oid(
322 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
323
324 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
325 cl_assert_equal_s(stat, buf.ptr);
326 git_buf_dispose(&buf);
327 }
328
329 void test_diff_stats__mode_change(void)
330 {
331 git_buf buf = GIT_BUF_INIT;
332 const char *stat =
333 " file1.txt.renamed | 0\n" \
334 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
335 " mode change 100644 => 100755 file1.txt.renamed\n";
336
337 diff_stats_from_commit_oid(
338 &_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
339
340 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
341 cl_assert_equal_s(stat, buf.ptr);
342 git_buf_dispose(&buf);
343 }
344
345 void test_diff_stats__new_file(void)
346 {
347 git_diff *diff;
348 git_buf buf = GIT_BUF_INIT;
349
350 const char *input =
351 "---\n"
352 " Gurjeet Singh | 1 +\n"
353 " 1 file changed, 1 insertion(+)\n"
354 " create mode 100644 Gurjeet Singh\n"
355 "\n"
356 "diff --git a/Gurjeet Singh b/Gurjeet Singh\n"
357 "new file mode 100644\n"
358 "index 0000000..6d0ecfd\n"
359 "--- /dev/null\n"
360 "+++ b/Gurjeet Singh \n"
361 "@@ -0,0 +1 @@\n"
362 "+I'm about to try git send-email\n"
363 "-- \n"
364 "2.21.0\n";
365
366 const char *stat =
367 " Gurjeet Singh | 1 +\n"
368 " 1 file changed, 1 insertion(+)\n"
369 " create mode 100644 Gurjeet Singh\n";
370
371 cl_git_pass(git_diff_from_buffer(&diff, input, strlen(input)));
372 cl_git_pass(git_diff_get_stats(&_stats, diff));
373 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
374 cl_assert_equal_s(stat, buf.ptr);
375
376 git_buf_dispose(&buf);
377 git_diff_free(diff);
378 }