stats.c 11.2 KB
Newer Older
1 2 3 4 5 6
#include "clar.h"
#include "clar_libgit2.h"

#include "buffer.h"
#include "commit.h"
#include "diff.h"
7
#include "diff_generate.h"
8

9 10
static git_repository *_repo;
static git_diff_stats *_stats;
11 12 13

void test_diff_stats__initialize(void)
{
14
	_repo = cl_git_sandbox_init("diff_format_email");
15 16 17 18
}

void test_diff_stats__cleanup(void)
{
19
	git_diff_stats_free(_stats); _stats = NULL;
20 21 22
	cl_git_sandbox_cleanup();
}

23 24
static void diff_stats_from_commit_oid(
	git_diff_stats **stats, const char *oidstr, bool rename)
25 26
{
	git_oid oid;
27 28 29 30 31 32 33 34 35 36 37 38 39
	git_commit *commit;
	git_diff *diff;

	git_oid_fromstr(&oid, oidstr);
	cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
	cl_git_pass(git_diff__commit(&diff, _repo, commit, NULL));
	if (rename)
		cl_git_pass(git_diff_find_similar(diff, NULL));
	cl_git_pass(git_diff_get_stats(stats, diff));

	git_diff_free(diff);
	git_commit_free(commit);
}
40

41 42 43
void test_diff_stats__stat(void)
{
	git_buf buf = GIT_BUF_INIT;
44 45 46 47
	const char *stat =
	" file1.txt | 8 +++++---\n" \
	" 1 file changed, 5 insertions(+), 3 deletions(-)\n";

48 49
	diff_stats_from_commit_oid(
		&_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
50

51 52 53
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
	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 60

	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 80));
	cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
61
	git_buf_dispose(&buf);
62 63 64 65 66 67 68 69 70 71
}

void test_diff_stats__multiple_hunks(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt | 5 +++--\n" \
	" file3.txt | 6 ++++--\n" \
	" 2 files changed, 7 insertions(+), 4 deletions(-)\n";

72 73
	diff_stats_from_commit_oid(
		&_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
74

75 76 77
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(7, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(4, git_diff_stats_deletions(_stats));
78

79 80
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
81
	git_buf_dispose(&buf);
82 83 84 85 86 87 88 89 90
}

void test_diff_stats__numstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	"3       2       file2.txt\n"
	"4       2       file3.txt\n";

91 92
	diff_stats_from_commit_oid(
		&_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
93

94 95
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
96
	git_buf_dispose(&buf);
97 98 99 100 101 102 103 104
}

void test_diff_stats__shortstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" 1 file changed, 5 insertions(+), 3 deletions(-)\n";

105 106
	diff_stats_from_commit_oid(
		&_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
107

108 109 110
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(3, git_diff_stats_deletions(_stats));
111

112 113
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
114
	git_buf_dispose(&buf);
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
void test_diff_stats__shortstat_noinsertions(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" 1 file changed, 2 deletions(-)\n";

	diff_stats_from_commit_oid(
		&_stats, "06b7b69a62cbd1e53c6c4e0c3f16473dcfdb4af6", false);

	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(2, git_diff_stats_deletions(_stats));

	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
132
	git_buf_dispose(&buf);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
}

void test_diff_stats__shortstat_nodeletions(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" 1 file changed, 3 insertions(+)\n";

	diff_stats_from_commit_oid(
		&_stats, "5219b9784f9a92d7bd7cb567a6d6a21bfb86697e", false);

	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(3, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));

	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
150
	git_buf_dispose(&buf);
151 152
}

153 154 155 156 157 158
void test_diff_stats__rename(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt => file2.txt.renamed | 1 +\n"
	" file3.txt => file3.txt.renamed | 4 +++-\n"
159
	" 2 files changed, 4 insertions(+), 1 deletion(-)\n";
160

161 162
	diff_stats_from_commit_oid(
		&_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
163

164 165 166
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(4, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
167

168 169
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
170
	git_buf_dispose(&buf);
171 172 173 174 175 176 177 178 179 180
}

void test_diff_stats__rename_nochanges(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed => file2.txt.renamed2 | 0\n"
	" file3.txt.renamed => file3.txt.renamed2 | 0\n"
	" 2 files changed, 0 insertions(+), 0 deletions(-)\n";

181 182
	diff_stats_from_commit_oid(
		&_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
183

184 185 186
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
187

188 189
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
190
	git_buf_dispose(&buf);
191 192 193 194 195 196 197 198
}

void test_diff_stats__rename_and_modifiy(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed2                      | 2 +-\n"
	" file3.txt.renamed2 => file3.txt.renamed | 0\n"
199
	" 2 files changed, 1 insertion(+), 1 deletion(-)\n";
200

201 202
	diff_stats_from_commit_oid(
		&_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
203

204 205 206
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(1, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
207

208 209
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
210
	git_buf_dispose(&buf);
211 212
}

213 214 215 216
void test_diff_stats__rename_in_subdirectory(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
217
	" dir/{orig.txt => renamed.txt} | 0\n"
218 219 220 221 222 223 224 225 226 227 228 229 230 231
	" 1 file changed, 0 insertions(+), 0 deletions(-)\n";

	diff_stats_from_commit_oid(
		&_stats, "0db2a262bc8c5c3cba55254730045a8258da7a37", true);

	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));

	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
	git_buf_dispose(&buf);
}

232 233 234 235 236 237 238 239 240 241
void test_diff_stats__rename_no_find(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt         | 5 -----\n"
	" file2.txt.renamed | 6 ++++++\n"
	" file3.txt         | 5 -----\n"
	" file3.txt.renamed | 7 +++++++\n"
	" 4 files changed, 13 insertions(+), 10 deletions(-)\n";

242 243
	diff_stats_from_commit_oid(
		&_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
244

245 246 247
	cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(10, git_diff_stats_deletions(_stats));
248

249 250
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
251
	git_buf_dispose(&buf);
252 253 254 255 256 257 258 259 260 261 262 263
}

void test_diff_stats__rename_nochanges_no_find(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed  | 6 ------\n"
	" file2.txt.renamed2 | 6 ++++++\n"
	" file3.txt.renamed  | 7 -------\n"
	" file3.txt.renamed2 | 7 +++++++\n"
	" 4 files changed, 13 insertions(+), 13 deletions(-)\n";

264 265
	diff_stats_from_commit_oid(
		&_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
266

267 268 269
	cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(13, git_diff_stats_deletions(_stats));
270

271 272
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	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 279 280 281 282 283 284
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed2 | 2 +-\n"
	" file3.txt.renamed  | 7 +++++++\n"
	" file3.txt.renamed2 | 7 -------\n"
	" 3 files changed, 8 insertions(+), 8 deletions(-)\n";

285 286
	diff_stats_from_commit_oid(
		&_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
287

288 289 290
	cl_assert_equal_sz(3, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(8, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(8, git_diff_stats_deletions(_stats));
291

292 293
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
294
	git_buf_dispose(&buf);
295 296 297 298 299 300
}

void test_diff_stats__binary(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
301
	" binary.bin | Bin 3 -> 5 bytes\n"
302 303
	" 1 file changed, 0 insertions(+), 0 deletions(-)\n";

304 305
	diff_stats_from_commit_oid(
		&_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
306

307 308 309
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
310

311 312
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
313
	git_buf_dispose(&buf);
314 315 316 317 318 319 320 321
}

void test_diff_stats__binary_numstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	"-       -       binary.bin\n";

322 323
	diff_stats_from_commit_oid(
		&_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
324

325 326
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
327
	git_buf_dispose(&buf);
328 329 330 331 332 333 334 335
}

void test_diff_stats__mode_change(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file1.txt.renamed | 0\n" \
	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
336
		" mode change 100644 => 100755 file1.txt.renamed\n";
337

338 339
	diff_stats_from_commit_oid(
		&_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
340

341 342
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
343
	git_buf_dispose(&buf);
344
}
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

void test_diff_stats__new_file(void)
{
	git_diff *diff;
	git_buf buf = GIT_BUF_INIT;

	const char *input =
	"---\n"
	" Gurjeet Singh | 1 +\n"
	" 1 file changed, 1 insertion(+)\n"
	" create mode 100644 Gurjeet Singh\n"
	"\n"
	"diff --git a/Gurjeet Singh b/Gurjeet Singh\n"
	"new file mode 100644\n"
	"index 0000000..6d0ecfd\n"
	"--- /dev/null\n"
	"+++ b/Gurjeet Singh	\n"
	"@@ -0,0 +1 @@\n"
	"+I'm about to try git send-email\n"
	"-- \n"
	"2.21.0\n";

	const char *stat =
	" Gurjeet Singh | 1 +\n"
	" 1 file changed, 1 insertion(+)\n"
	" create mode 100644 Gurjeet Singh\n";

	cl_git_pass(git_diff_from_buffer(&diff, input, strlen(input)));
	cl_git_pass(git_diff_get_stats(&_stats, diff));
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));

	git_buf_dispose(&buf);
	git_diff_free(diff);
}