]> git.proxmox.com Git - libgit2.git/blame - tests/diff/stats.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / diff / stats.c
CommitLineData
360314c9
JG
1#include "clar.h"
2#include "clar_libgit2.h"
3
360314c9
JG
4#include "commit.h"
5#include "diff.h"
9be638ec 6#include "diff_generate.h"
360314c9 7
8d09efa2
RB
8static git_repository *_repo;
9static git_diff_stats *_stats;
360314c9
JG
10
11void test_diff_stats__initialize(void)
12{
8d09efa2 13 _repo = cl_git_sandbox_init("diff_format_email");
360314c9
JG
14}
15
16void test_diff_stats__cleanup(void)
17{
8d09efa2 18 git_diff_stats_free(_stats); _stats = NULL;
360314c9
JG
19 cl_git_sandbox_cleanup();
20}
21
8d09efa2
RB
22static void diff_stats_from_commit_oid(
23 git_diff_stats **stats, const char *oidstr, bool rename)
360314c9
JG
24{
25 git_oid oid;
8d09efa2
RB
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}
360314c9 39
8d09efa2
RB
40void test_diff_stats__stat(void)
41{
42 git_buf buf = GIT_BUF_INIT;
360314c9
JG
43 const char *stat =
44 " file1.txt | 8 +++++---\n" \
45 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
46
8d09efa2
RB
47 diff_stats_from_commit_oid(
48 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
360314c9 49
8d09efa2
RB
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));
360314c9 53
8d09efa2 54 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 55 cl_assert(strcmp(buf.ptr, stat) == 0);
ac3d33df 56 git_buf_dispose(&buf);
ce3b71d9
RB
57
58 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 80));
e579e0f7 59 cl_assert(strcmp(buf.ptr, stat) == 0);
ac3d33df 60 git_buf_dispose(&buf);
360314c9
JG
61}
62
63void test_diff_stats__multiple_hunks(void)
64{
360314c9 65 git_buf buf = GIT_BUF_INIT;
360314c9
JG
66 const char *stat =
67 " file2.txt | 5 +++--\n" \
68 " file3.txt | 6 ++++--\n" \
69 " 2 files changed, 7 insertions(+), 4 deletions(-)\n";
70
8d09efa2
RB
71 diff_stats_from_commit_oid(
72 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
360314c9 73
8d09efa2
RB
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));
360314c9 77
8d09efa2 78 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 79 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 80 git_buf_dispose(&buf);
360314c9
JG
81}
82
83void test_diff_stats__numstat(void)
84{
360314c9 85 git_buf buf = GIT_BUF_INIT;
360314c9
JG
86 const char *stat =
87 "3 2 file2.txt\n"
88 "4 2 file3.txt\n";
89
8d09efa2
RB
90 diff_stats_from_commit_oid(
91 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
360314c9 92
8d09efa2 93 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
e579e0f7 94 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 95 git_buf_dispose(&buf);
360314c9
JG
96}
97
98void test_diff_stats__shortstat(void)
99{
360314c9 100 git_buf buf = GIT_BUF_INIT;
360314c9
JG
101 const char *stat =
102 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
103
8d09efa2
RB
104 diff_stats_from_commit_oid(
105 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
360314c9 106
8d09efa2
RB
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));
360314c9 110
8d09efa2 111 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
e579e0f7 112 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 113 git_buf_dispose(&buf);
360314c9
JG
114}
115
dc5cfdba
SD
116void 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));
e579e0f7 130 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 131 git_buf_dispose(&buf);
dc5cfdba
SD
132}
133
134void 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));
e579e0f7 148 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 149 git_buf_dispose(&buf);
dc5cfdba
SD
150}
151
360314c9
JG
152void test_diff_stats__rename(void)
153{
360314c9 154 git_buf buf = GIT_BUF_INIT;
360314c9
JG
155 const char *stat =
156 " file2.txt => file2.txt.renamed | 1 +\n"
157 " file3.txt => file3.txt.renamed | 4 +++-\n"
8d09efa2 158 " 2 files changed, 4 insertions(+), 1 deletion(-)\n";
360314c9 159
8d09efa2
RB
160 diff_stats_from_commit_oid(
161 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
360314c9 162
8d09efa2
RB
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));
360314c9 166
8d09efa2 167 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 168 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 169 git_buf_dispose(&buf);
360314c9
JG
170}
171
172void test_diff_stats__rename_nochanges(void)
173{
360314c9 174 git_buf buf = GIT_BUF_INIT;
360314c9
JG
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
8d09efa2
RB
180 diff_stats_from_commit_oid(
181 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
360314c9 182
8d09efa2
RB
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));
360314c9 186
8d09efa2 187 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 188 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 189 git_buf_dispose(&buf);
360314c9
JG
190}
191
192void test_diff_stats__rename_and_modifiy(void)
193{
360314c9 194 git_buf buf = GIT_BUF_INIT;
360314c9
JG
195 const char *stat =
196 " file2.txt.renamed2 | 2 +-\n"
197 " file3.txt.renamed2 => file3.txt.renamed | 0\n"
8d09efa2 198 " 2 files changed, 1 insertion(+), 1 deletion(-)\n";
360314c9 199
8d09efa2
RB
200 diff_stats_from_commit_oid(
201 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
360314c9 202
8d09efa2
RB
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));
360314c9 206
8d09efa2 207 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 208 cl_assert_equal_s(stat, buf.ptr);
ac3d33df
JK
209 git_buf_dispose(&buf);
210}
211
212void 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));
e579e0f7 227 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 228 git_buf_dispose(&buf);
360314c9
JG
229}
230
231void test_diff_stats__rename_no_find(void)
232{
360314c9 233 git_buf buf = GIT_BUF_INIT;
360314c9
JG
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
8d09efa2
RB
241 diff_stats_from_commit_oid(
242 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
360314c9 243
8d09efa2
RB
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));
360314c9 247
8d09efa2 248 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 249 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 250 git_buf_dispose(&buf);
360314c9
JG
251}
252
253void test_diff_stats__rename_nochanges_no_find(void)
254{
360314c9 255 git_buf buf = GIT_BUF_INIT;
360314c9
JG
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
8d09efa2
RB
263 diff_stats_from_commit_oid(
264 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
360314c9 265
8d09efa2
RB
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));
360314c9 269
8d09efa2 270 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 271 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 272 git_buf_dispose(&buf);
360314c9
JG
273}
274
4b3ec53c 275void test_diff_stats__rename_and_modify_no_find(void)
360314c9 276{
360314c9 277 git_buf buf = GIT_BUF_INIT;
360314c9
JG
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
8d09efa2
RB
284 diff_stats_from_commit_oid(
285 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
360314c9 286
8d09efa2
RB
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));
360314c9 290
8d09efa2 291 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 292 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 293 git_buf_dispose(&buf);
360314c9
JG
294}
295
296void test_diff_stats__binary(void)
297{
360314c9 298 git_buf buf = GIT_BUF_INIT;
360314c9 299 const char *stat =
22a2d3d5 300 " binary.bin | Bin 3 -> 5 bytes\n"
360314c9
JG
301 " 1 file changed, 0 insertions(+), 0 deletions(-)\n";
302
8d09efa2
RB
303 diff_stats_from_commit_oid(
304 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
360314c9 305
8d09efa2
RB
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));
360314c9 309
8d09efa2 310 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
e579e0f7 311 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 312 git_buf_dispose(&buf);
360314c9
JG
313}
314
315void test_diff_stats__binary_numstat(void)
316{
360314c9 317 git_buf buf = GIT_BUF_INIT;
360314c9
JG
318 const char *stat =
319 "- - binary.bin\n";
320
8d09efa2
RB
321 diff_stats_from_commit_oid(
322 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
360314c9 323
8d09efa2 324 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
e579e0f7 325 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 326 git_buf_dispose(&buf);
360314c9
JG
327}
328
329void test_diff_stats__mode_change(void)
330{
360314c9 331 git_buf buf = GIT_BUF_INIT;
360314c9
JG
332 const char *stat =
333 " file1.txt.renamed | 0\n" \
334 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
8d09efa2 335 " mode change 100644 => 100755 file1.txt.renamed\n";
360314c9 336
8d09efa2
RB
337 diff_stats_from_commit_oid(
338 &_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
360314c9 339
8d09efa2 340 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
e579e0f7 341 cl_assert_equal_s(stat, buf.ptr);
ac3d33df 342 git_buf_dispose(&buf);
360314c9 343}
22a2d3d5
UG
344
345void 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));
e579e0f7 374 cl_assert_equal_s(stat, buf.ptr);
22a2d3d5
UG
375
376 git_buf_dispose(&buf);
377 git_diff_free(diff);
378}