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