]> git.proxmox.com Git - libgit2.git/blob - tests/diff/stats.c
1503556863cfec864c0fe0023cc718362c7dbea2
[libgit2.git] / tests / diff / stats.c
1 #include "clar.h"
2 #include "clar_libgit2.h"
3
4 #include "buffer.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "diff_generate.h"
8
9 static git_repository *_repo;
10 static git_diff_stats *_stats;
11
12 void test_diff_stats__initialize(void)
13 {
14 _repo = cl_git_sandbox_init("diff_format_email");
15 }
16
17 void test_diff_stats__cleanup(void)
18 {
19 git_diff_stats_free(_stats); _stats = NULL;
20 cl_git_sandbox_cleanup();
21 }
22
23 static void diff_stats_from_commit_oid(
24 git_diff_stats **stats, const char *oidstr, bool rename)
25 {
26 git_oid oid;
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 }
40
41 void test_diff_stats__stat(void)
42 {
43 git_buf buf = GIT_BUF_INIT;
44 const char *stat =
45 " file1.txt | 8 +++++---\n" \
46 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
47
48 diff_stats_from_commit_oid(
49 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
50
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));
54
55 cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
56 cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
57 git_buf_dispose(&buf);
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);
61 git_buf_dispose(&buf);
62 }
63
64 void test_diff_stats__multiple_hunks(void)
65 {
66 git_buf buf = GIT_BUF_INIT;
67 const char *stat =
68 " file2.txt | 5 +++--\n" \
69 " file3.txt | 6 ++++--\n" \
70 " 2 files changed, 7 insertions(+), 4 deletions(-)\n";
71
72 diff_stats_from_commit_oid(
73 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
74
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));
78
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));
81 git_buf_dispose(&buf);
82 }
83
84 void test_diff_stats__numstat(void)
85 {
86 git_buf buf = GIT_BUF_INIT;
87 const char *stat =
88 "3 2 file2.txt\n"
89 "4 2 file3.txt\n";
90
91 diff_stats_from_commit_oid(
92 &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
93
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));
96 git_buf_dispose(&buf);
97 }
98
99 void test_diff_stats__shortstat(void)
100 {
101 git_buf buf = GIT_BUF_INIT;
102 const char *stat =
103 " 1 file changed, 5 insertions(+), 3 deletions(-)\n";
104
105 diff_stats_from_commit_oid(
106 &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
107
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));
111
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));
114 git_buf_dispose(&buf);
115 }
116
117 void 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));
132 git_buf_dispose(&buf);
133 }
134
135 void 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));
150 git_buf_dispose(&buf);
151 }
152
153 void test_diff_stats__rename(void)
154 {
155 git_buf buf = GIT_BUF_INIT;
156 const char *stat =
157 " file2.txt => file2.txt.renamed | 1 +\n"
158 " file3.txt => file3.txt.renamed | 4 +++-\n"
159 " 2 files changed, 4 insertions(+), 1 deletion(-)\n";
160
161 diff_stats_from_commit_oid(
162 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
163
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));
167
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));
170 git_buf_dispose(&buf);
171 }
172
173 void test_diff_stats__rename_nochanges(void)
174 {
175 git_buf buf = GIT_BUF_INIT;
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
181 diff_stats_from_commit_oid(
182 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
183
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));
187
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));
190 git_buf_dispose(&buf);
191 }
192
193 void test_diff_stats__rename_and_modifiy(void)
194 {
195 git_buf buf = GIT_BUF_INIT;
196 const char *stat =
197 " file2.txt.renamed2 | 2 +-\n"
198 " file3.txt.renamed2 => file3.txt.renamed | 0\n"
199 " 2 files changed, 1 insertion(+), 1 deletion(-)\n";
200
201 diff_stats_from_commit_oid(
202 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
203
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));
207
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));
210 git_buf_dispose(&buf);
211 }
212
213 void 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);
230 }
231
232 void test_diff_stats__rename_no_find(void)
233 {
234 git_buf buf = GIT_BUF_INIT;
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
242 diff_stats_from_commit_oid(
243 &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
244
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));
248
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));
251 git_buf_dispose(&buf);
252 }
253
254 void test_diff_stats__rename_nochanges_no_find(void)
255 {
256 git_buf buf = GIT_BUF_INIT;
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
264 diff_stats_from_commit_oid(
265 &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
266
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));
270
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));
273 git_buf_dispose(&buf);
274 }
275
276 void test_diff_stats__rename_and_modify_no_find(void)
277 {
278 git_buf buf = GIT_BUF_INIT;
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
285 diff_stats_from_commit_oid(
286 &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
287
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));
291
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));
294 git_buf_dispose(&buf);
295 }
296
297 void test_diff_stats__binary(void)
298 {
299 git_buf buf = GIT_BUF_INIT;
300 const char *stat =
301 " binary.bin | Bin 3 -> 0 bytes\n"
302 " 1 file changed, 0 insertions(+), 0 deletions(-)\n";
303 /* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
304
305 diff_stats_from_commit_oid(
306 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
307
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));
311
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));
314 git_buf_dispose(&buf);
315 }
316
317 void test_diff_stats__binary_numstat(void)
318 {
319 git_buf buf = GIT_BUF_INIT;
320 const char *stat =
321 "- - binary.bin\n";
322
323 diff_stats_from_commit_oid(
324 &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
325
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));
328 git_buf_dispose(&buf);
329 }
330
331 void test_diff_stats__mode_change(void)
332 {
333 git_buf buf = GIT_BUF_INIT;
334 const char *stat =
335 " file1.txt.renamed | 0\n" \
336 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
337 " mode change 100644 => 100755 file1.txt.renamed\n";
338
339 diff_stats_from_commit_oid(
340 &_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
341
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));
344 git_buf_dispose(&buf);
345 }