Use git_diff_get_stats in example/diff + refactor This takes the `--stat` and related example options in the example diff.c program and converts them to use the `git_diff_get_stats` API which nicely formats stats for you. I went to add bar-graph scaling to the stats formatter and noticed that the `git_diff_stats` structure was holding on to all of the `git_patch` objects. Unfortunately, each of these objects keeps the full text of the diff in memory, so this is very expensive. I ended up modifying `git_diff_stats` to keep just the data that it needs to keep and allowed it to release the patches. Then, I added width scaling to the output on top of that. In making the diff example program match 'git diff' output, I ended up removing an newline from the sumamry output which I then had to compensate for in the email formatting to match the expectations. Lastly, I went through and refactored the tests to use a couple of helper functions and reduce the overall amount of code there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
diff --git a/examples/diff.c b/examples/diff.c
index 1dbf85f..76ac2f3 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -34,10 +34,11 @@ static const char *colors[] = {
};
enum {
- OUTPUT_DIFF = 0,
- OUTPUT_STAT = 1,
- OUTPUT_SHORTSTAT = 2,
- OUTPUT_NUMSTAT = 3
+ OUTPUT_DIFF = (1 << 0),
+ OUTPUT_STAT = (1 << 1),
+ OUTPUT_SHORTSTAT = (1 << 2),
+ OUTPUT_NUMSTAT = (1 << 3),
+ OUTPUT_SUMMARY = (1 << 4)
};
enum {
@@ -137,14 +138,13 @@ int main(int argc, char *argv[])
/** Generate simple output using libgit2 display helper. */
- switch (o.output) {
- case OUTPUT_STAT:
- case OUTPUT_NUMSTAT:
- case OUTPUT_SHORTSTAT:
+ if (!o.output)
+ o.output = OUTPUT_DIFF;
+
+ if (o.output != OUTPUT_DIFF)
diff_print_stats(diff, &o);
- break;
- case OUTPUT_DIFF:
+ if ((o.output & OUTPUT_DIFF) != 0) {
if (o.color >= 0)
fputs(colors[0], stdout);
@@ -154,10 +154,6 @@ int main(int argc, char *argv[])
if (o.color >= 0)
fputs(colors[0], stdout);
- break;
-
- default:
- usage("Unknown output format", "programmer error");
}
/** Cleanup before exiting. */
@@ -233,8 +229,10 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
usage("Only one or two tree identifiers can be provided", NULL);
}
else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
- !strcmp(a, "--patch"))
+ !strcmp(a, "--patch")) {
+ o->output |= OUTPUT_DIFF;
o->format = GIT_DIFF_FORMAT_PATCH;
+ }
else if (!strcmp(a, "--cached"))
o->cache = CACHE_ONLY;
else if (!strcmp(a, "--nocache"))
@@ -273,11 +271,13 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
else if (!strcmp(a, "--minimal"))
o->diffopts.flags |= GIT_DIFF_MINIMAL;
else if (!strcmp(a, "--stat"))
- o->output = OUTPUT_STAT;
+ o->output |= OUTPUT_STAT;
else if (!strcmp(a, "--numstat"))
- o->output = OUTPUT_NUMSTAT;
+ o->output |= OUTPUT_NUMSTAT;
else if (!strcmp(a, "--shortstat"))
- o->output = OUTPUT_SHORTSTAT;
+ o->output |= OUTPUT_SHORTSTAT;
+ else if (!strcmp(a, "--summary"))
+ o->output |= OUTPUT_SUMMARY;
else if (match_uint16_arg(
&o->findopts.rename_threshold, &args, "-M") ||
match_uint16_arg(
@@ -308,59 +308,30 @@ static void parse_opts(struct opts *o, int argc, char *argv[])
}
}
-/** Display diff output with "--numstat" or "--shortstat" */
+/** Display diff output with "--stat", "--numstat", or "--shortstat" */
static void diff_print_stats(git_diff *diff, struct opts *o)
{
- git_patch *patch;
- size_t d, ndeltas = git_diff_num_deltas(diff);
- size_t nadditions, ndeletions;
- long nadditions_sum, ndeletions_sum;
-
- nadditions_sum = 0;
- ndeletions_sum = 0;
+ git_diff_stats *stats;
+ git_buf b = GIT_BUF_INIT_CONST(NULL, 0);
+ git_diff_stats_format_t format = 0;
- for (d = 0; d < ndeltas; d++){
- check_lg2(
- git_patch_from_diff(&patch, diff, d),
- "generating patch from diff", NULL);
+ check_lg2(
+ git_diff_get_stats(&stats, diff), "generating stats for diff", NULL);
- check_lg2(
- git_patch_line_stats(NULL, &nadditions, &ndeletions, patch),
- "generating the number of additions and deletions", NULL);
+ if (o->output & OUTPUT_STAT)
+ format |= GIT_DIFF_STATS_FULL;
+ if (o->output & OUTPUT_SHORTSTAT)
+ format |= GIT_DIFF_STATS_SHORT;
+ if (o->output & OUTPUT_NUMSTAT)
+ format |= GIT_DIFF_STATS_NUMBER;
+ if (o->output & OUTPUT_SUMMARY)
+ format |= GIT_DIFF_STATS_INCLUDE_SUMMARY;
- if (o->output == OUTPUT_NUMSTAT) {
- const git_diff_delta *delta = git_patch_get_delta(patch);
- printf("%ld\t%ld\t%s\n",
- (long)nadditions, (long)ndeletions, delta->new_file.path);
- }
- else if (o->output == OUTPUT_STAT) {
- const git_diff_delta *delta = git_patch_get_delta(patch);
- printf(" %s\t| %ld\t(%ld+ %ld-)\n",
- delta->new_file.path, (long)nadditions + (long)ndeletions,
- (long)nadditions, (long)ndeletions);
- }
+ check_lg2(
+ git_diff_stats_to_buf(&b, stats, format, 80), "formatting stats", NULL);
- nadditions_sum += nadditions;
- ndeletions_sum += ndeletions;
+ fputs(b.ptr, stdout);
- git_patch_free(patch);
- }
-
- if (o->output != OUTPUT_NUMSTAT && ndeltas > 0) {
- printf(" %ld %s", (long)ndeltas,
- 1 == ndeltas ? "file changed" : "files changed");
-
- if (nadditions_sum) {
- printf(", %ld ",nadditions_sum);
- printf("%s", 1 == nadditions_sum ? "insertion(+)" : "insertions(+)");
- }
-
- if (ndeletions_sum) {
- printf(", %ld ",ndeletions_sum);
- printf("%s", 1 == ndeletions_sum ? "deletion(-)" : "deletions(-)");
- }
-
- printf("\n");
- }
+ git_buf_free(&b);
+ git_diff_stats_free(stats);
}
-
diff --git a/include/git2/diff.h b/include/git2/diff.h
index c8e1ad1..e5e641a 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -1136,12 +1136,14 @@ GIT_EXTERN(size_t) git_diff_stats_deletions(
* @param out buffer to store the formatted diff statistics in.
* @param stats A `git_diff_stats` generated by one of the above functions.
* @param format Formatting option.
+ * @param width Target width for output (only affects GIT_DIFF_STATS_FULL)
* @return 0 on success; non-zero on error
*/
GIT_EXTERN(int) git_diff_stats_to_buf(
git_buf *out,
const git_diff_stats *stats,
- git_diff_stats_format_t format);
+ git_diff_stats_format_t format,
+ size_t width);
/**
* Deallocate a `git_diff_stats`.
diff --git a/src/diff.c b/src/diff.c
index 0d1aed4..fd881c6 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1590,7 +1590,8 @@ int git_diff_format_email(
if ((error = git_buf_puts(out, "---\n")) < 0 ||
(error = git_diff_get_stats(&stats, diff)) < 0 ||
- (error = git_diff_stats_to_buf(out, stats, format_flags)) < 0 ||
+ (error = git_diff_stats_to_buf(out, stats, format_flags, 0)) < 0 ||
+ (error = git_buf_putc(out, '\n')) < 0 ||
(error = git_diff_format_email__append_patches_tobuf(out, diff)) < 0)
goto on_error;
diff --git a/src/diff_stats.c b/src/diff_stats.c
index bb436bf..8a71be8 100644
--- a/src/diff_stats.c
+++ b/src/diff_stats.c
@@ -8,151 +8,123 @@
#include "vector.h"
#include "diff.h"
#include "diff_patch.h"
+#include <math.h>
#define DIFF_RENAME_FILE_SEPARATOR " => "
+#define STATS_FULL_MIN_SCALE 7
+
+typedef struct {
+ size_t insertions;
+ size_t deletions;
+} diff_file_stats;
struct git_diff_stats {
- git_vector patches;
+ git_diff *diff;
+ diff_file_stats *filestats;
size_t files_changed;
size_t insertions;
size_t deletions;
-};
-
-static size_t diff_get_filename_padding(
- int has_renames,
- const git_diff_stats *stats)
-{
- const git_patch *patch = NULL;
- size_t i, max_padding = 0;
-
- if (has_renames) {
- git_vector_foreach(&stats->patches, i, patch) {
- const git_diff_delta *delta = NULL;
- size_t len;
-
- delta = git_patch_get_delta(patch);
- if (strcmp(delta->old_file.path, delta->new_file.path) == 0)
- continue;
-
- if ((len = strlen(delta->old_file.path) + strlen(delta->new_file.path)) > max_padding)
- max_padding = len;
- }
- }
-
- git_vector_foreach(&stats->patches, i, patch) {
- const git_diff_delta *delta = NULL;
- size_t len;
-
- delta = git_patch_get_delta(patch);
- len = strlen(delta->new_file.path);
-
- if (strcmp(delta->old_file.path, delta->new_file.path) != 0)
- continue;
-
- if (len > max_padding)
- max_padding = len;
- }
+ size_t renames;
- return max_padding;
-}
+ size_t max_name;
+ size_t max_filestat;
+ int max_digits;
+};
int git_diff_file_stats__full_to_buf(
git_buf *out,
- size_t max_padding,
- int has_renames,
- const git_patch *patch)
+ const git_diff_delta *delta,
+ const diff_file_stats *filestat,
+ const git_diff_stats *stats,
+ double scale_to)
{
const char *old_path = NULL, *new_path = NULL;
- const git_diff_delta *delta = NULL;
size_t padding, old_size, new_size;
- int error;
-
- delta = git_patch_get_delta(patch);
old_path = delta->old_file.path;
new_path = delta->new_file.path;
old_size = delta->old_file.size;
new_size = delta->new_file.size;
- if ((error = git_buf_printf(out, " %s", old_path)) < 0)
+ if (git_buf_printf(out, " %s", old_path) < 0)
goto on_error;
if (strcmp(old_path, new_path) != 0) {
- padding = max_padding - strlen(old_path) - strlen(new_path);
+ padding = stats->max_name - strlen(old_path) - strlen(new_path);
- if ((error = git_buf_printf(out, DIFF_RENAME_FILE_SEPARATOR "%s", new_path)) < 0)
+ if (git_buf_printf(out, DIFF_RENAME_FILE_SEPARATOR "%s", new_path) < 0)
goto on_error;
- }
- else {
- padding = max_padding - strlen(old_path);
+ } else {
+ padding = stats->max_name - strlen(old_path);
- if (has_renames)
+ if (stats->renames > 0)
padding += strlen(DIFF_RENAME_FILE_SEPARATOR);
}
- if ((error = git_buf_putcn(out, ' ', padding)) < 0 ||
- (error = git_buf_puts(out, " | ")) < 0)
- goto on_error;
+ if (git_buf_putcn(out, ' ', padding) < 0 ||
+ git_buf_puts(out, " | ") < 0)
+ goto on_error;
if (delta->flags & GIT_DIFF_FLAG_BINARY) {
- if ((error = git_buf_printf(out, "Bin %" PRIuZ " -> %" PRIuZ " bytes", old_size, new_size)) < 0)
+ if (git_buf_printf(out,
+ "Bin %" PRIuZ " -> %" PRIuZ " bytes", old_size, new_size) < 0)
goto on_error;
}
else {
- size_t insertions, deletions;
-
- if ((error = git_patch_line_stats(NULL, &insertions, &deletions, patch)) < 0)
+ if (git_buf_printf(out,
+ "%*" PRIuZ, stats->max_digits,
+ filestat->insertions + filestat->deletions) < 0)
goto on_error;
- if ((error = git_buf_printf(out, "%" PRIuZ, insertions + deletions)) < 0)
- goto on_error;
+ if (filestat->insertions || filestat->deletions) {
+ if (git_buf_putc(out, ' ') < 0)
+ goto on_error;
- if (insertions || deletions) {
- if ((error = git_buf_putc(out, ' ')) < 0 ||
- (error = git_buf_putcn(out, '+', insertions)) < 0 ||
- (error = git_buf_putcn(out, '-', deletions)) < 0)
+ if (scale_to <= 0) {
+ if (git_buf_putcn(out, '+', filestat->insertions) < 0 ||
+ git_buf_putcn(out, '-', filestat->deletions) < 0)
goto on_error;
+ } else {
+ size_t total = filestat->insertions + filestat->deletions;
+ double full = round(total * scale_to / stats->max_filestat);
+ size_t plus = full * filestat->insertions / total;
+ size_t minus = (size_t)full - plus;
+
+ if (git_buf_putcn(out, '+', max(plus, 1)) < 0 ||
+ git_buf_putcn(out, '-', max(minus, 1)) < 0)
+ goto on_error;
+ }
}
}
- error = git_buf_putc(out, '\n');
+ git_buf_putc(out, '\n');
on_error:
- return error;
+ return (git_buf_oom(out) ? -1 : 0);
}
int git_diff_file_stats__number_to_buf(
git_buf *out,
- const git_patch *patch)
+ const git_diff_delta *delta,
+ const diff_file_stats *filestats)
{
- const git_diff_delta *delta = NULL;
- const char *path = NULL;
- size_t insertions, deletions;
int error;
-
- delta = git_patch_get_delta(patch);
- path = delta->new_file.path;
-
- if ((error = git_patch_line_stats(NULL, &insertions, &deletions, patch)) < 0)
- return error;
+ const char *path = delta->new_file.path;
if (delta->flags & GIT_DIFF_FLAG_BINARY)
error = git_buf_printf(out, "%-8c" "%-8c" "%s\n", '-', '-', path);
else
- error = git_buf_printf(out, "%-8" PRIuZ "%-8" PRIuZ "%s\n", insertions, deletions, path);
+ error = git_buf_printf(out, "%-8" PRIuZ "%-8" PRIuZ "%s\n",
+ filestats->insertions, filestats->deletions, path);
return error;
}
int git_diff_file_stats__summary_to_buf(
git_buf *out,
- const git_patch *patch)
+ const git_diff_delta *delta)
{
- const git_diff_delta *delta = NULL;
-
- delta = git_patch_get_delta(patch);
-
if (delta->old_file.mode != delta->new_file.mode) {
if (delta->old_file.mode == 0) {
git_buf_printf(out, " create mode %06o %s\n",
@@ -171,39 +143,6 @@ int git_diff_file_stats__summary_to_buf(
return 0;
}
-int git_diff_stats__has_renames(
- const git_diff_stats *stats)
-{
- git_patch *patch = NULL;
- size_t i;
-
- git_vector_foreach(&stats->patches, i, patch) {
- const git_diff_delta *delta = git_patch_get_delta(patch);
-
- if (strcmp(delta->old_file.path, delta->new_file.path) != 0) {
- return 1;
- }
- }
-
- return 0;
-}
-
-int git_diff_stats__add_file_stats(
- git_diff_stats *stats,
- git_patch *patch)
-{
- const git_diff_delta *delta = NULL;
- int error = 0;
-
- if ((delta = git_patch_get_delta(patch)) == NULL)
- return -1;
-
- if ((error = git_vector_insert(&stats->patches, patch)) < 0)
- return error;
-
- return error;
-}
-
int git_diff_get_stats(
git_diff_stats **out,
git_diff *diff)
@@ -220,35 +159,60 @@ int git_diff_get_stats(
deltas = git_diff_num_deltas(diff);
- for (i = 0; i < deltas; ++i) {
+ stats->filestats = git__calloc(deltas, sizeof(diff_file_stats));
+ if (!stats->filestats) {
+ git__free(stats);
+ return -1;
+ }
+
+ stats->diff = diff;
+ GIT_REFCOUNT_INC(diff);
+
+ for (i = 0; i < deltas && !error; ++i) {
git_patch *patch = NULL;
- size_t add, remove;
+ size_t add = 0, remove = 0, namelen;
+ const git_diff_delta *delta;
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
- goto on_error;
+ break;
- if ((error = git_patch_line_stats(NULL, &add, &remove, patch)) < 0 ||
- (error = git_diff_stats__add_file_stats(stats, patch)) < 0) {
- git_patch_free(patch);
- goto on_error;
+ /* keep a count of renames because it will affect formatting */
+ delta = git_patch_get_delta(patch);
+
+ namelen = strlen(delta->new_file.path);
+ if (strcmp(delta->old_file.path, delta->new_file.path) != 0) {
+ namelen += strlen(delta->old_file.path);
+ stats->renames++;
}
+ /* and, of course, count the line stats */
+ error = git_patch_line_stats(NULL, &add, &remove, patch);
+
+ git_patch_free(patch);
+
+ stats->filestats[i].insertions = add;
+ stats->filestats[i].deletions = remove;
+
total_insertions += add;
total_deletions += remove;
+
+ if (stats->max_name < namelen)
+ stats->max_name = namelen;
+ if (stats->max_filestat < add + remove)
+ stats->max_filestat = add + remove;
}
stats->files_changed = deltas;
stats->insertions = total_insertions;
stats->deletions = total_deletions;
+ stats->max_digits = (int)ceil(log10(stats->max_filestat + 1));
- *out = stats;
-
- goto done;
-
-on_error:
- git_diff_stats_free(stats);
+ if (error < 0) {
+ git_diff_stats_free(stats);
+ stats = NULL;
+ }
-done:
+ *out = stats;
return error;
}
@@ -279,48 +243,68 @@ size_t git_diff_stats_deletions(
int git_diff_stats_to_buf(
git_buf *out,
const git_diff_stats *stats,
- git_diff_stats_format_t format)
+ git_diff_stats_format_t format,
+ size_t width)
{
- git_patch *patch = NULL;
+ int error = 0;
size_t i;
- int has_renames = 0, error = 0;
+ const git_diff_delta *delta;
assert(out && stats);
- /* check if we have renames, it affects the padding */
- has_renames = git_diff_stats__has_renames(stats);
-
- git_vector_foreach(&stats->patches, i, patch) {
- if (format & GIT_DIFF_STATS_FULL) {
- size_t max_padding = diff_get_filename_padding(has_renames, stats);
+ if (format & GIT_DIFF_STATS_NUMBER) {
+ for (i = 0; i < stats->files_changed; ++i) {
+ if ((delta = git_diff_get_delta(stats->diff, i)) == NULL)
+ continue;
- error = git_diff_file_stats__full_to_buf(out, max_padding, has_renames, patch);
+ error = git_diff_file_stats__number_to_buf(
+ out, delta, &stats->filestats[i]);
+ if (error < 0)
+ return error;
}
- else if (format & GIT_DIFF_STATS_NUMBER) {
- error = git_diff_file_stats__number_to_buf(out, patch);
+ }
+
+ if (format & GIT_DIFF_STATS_FULL) {
+ double scale_to = -1;
+ if (width > 0) {
+ if (width > stats->max_name + stats->max_digits + 5)
+ scale_to = width - (stats->max_name + stats->max_digits + 5);
+ if (scale_to < STATS_FULL_MIN_SCALE)
+ scale_to = STATS_FULL_MIN_SCALE;
}
- if (error < 0)
- return error;
+ for (i = 0; i < stats->files_changed; ++i) {
+ if ((delta = git_diff_get_delta(stats->diff, i)) == NULL)
+ continue;
+
+ error = git_diff_file_stats__full_to_buf(
+ out, delta, &stats->filestats[i], stats, scale_to);
+ if (error < 0)
+ return error;
+ }
}
if (format & GIT_DIFF_STATS_FULL || format & GIT_DIFF_STATS_SHORT) {
- error = git_buf_printf(out, " %" PRIuZ " file%s changed, %" PRIuZ " insertions(+), %" PRIuZ " deletions(-)\n",
- stats->files_changed, stats->files_changed > 1 ? "s" : "",
- stats->insertions, stats->deletions);
+ error = git_buf_printf(
+ out, " %" PRIuZ " file%s changed, %" PRIuZ
+ " insertion%s(+), %" PRIuZ " deletion%s(-)\n",
+ stats->files_changed, stats->files_changed != 1 ? "s" : "",
+ stats->insertions, stats->insertions != 1 ? "s" : "",
+ stats->deletions, stats->deletions != 1 ? "s" : "");
if (error < 0)
return error;
}
if (format & GIT_DIFF_STATS_INCLUDE_SUMMARY) {
- git_vector_foreach(&stats->patches, i, patch) {
- if ((error = git_diff_file_stats__summary_to_buf(out, patch)) < 0)
+ for (i = 0; i < stats->files_changed; ++i) {
+ if ((delta = git_diff_get_delta(stats->diff, i)) == NULL)
+ continue;
+
+ error = git_diff_file_stats__summary_to_buf(out, delta);
+ if (error < 0)
return error;
}
-
- if (git_vector_length(&stats->patches) > 0)
- git_buf_putc(out, '\n');
}
return error;
@@ -328,16 +312,11 @@ int git_diff_stats_to_buf(
void git_diff_stats_free(git_diff_stats *stats)
{
- size_t i;
- git_patch *patch;
-
if (stats == NULL)
return;
- git_vector_foreach(&stats->patches, i, patch)
- git_patch_free(patch);
-
- git_vector_free(&stats->patches);
+ git_diff_free(stats->diff); /* bumped refcount in constructor */
+ git__free(stats->filestats);
git__free(stats);
}
diff --git a/tests/diff/format_email.c b/tests/diff/format_email.c
index 3260fde..18ad99b 100644
--- a/tests/diff/format_email.c
+++ b/tests/diff/format_email.c
@@ -17,15 +17,44 @@ void test_diff_format_email__cleanup(void)
cl_git_sandbox_cleanup();
}
-void test_diff_format_email__simple(void)
+static void assert_email_match(
+ const char *expected,
+ const char *oidstr,
+ git_diff_format_email_options *opts)
{
git_oid oid;
git_commit *commit = NULL;
git_diff *diff = NULL;
- git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
git_buf buf = GIT_BUF_INIT;
- const char *email =
+ git_oid_fromstr(&oid, oidstr);
+
+ cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+
+ opts->id = git_commit_id(commit);
+ opts->author = git_commit_author(commit);
+ if (!opts->summary)
+ opts->summary = git_commit_summary(commit);
+
+ cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ cl_git_pass(git_diff_format_email(&buf, diff, opts));
+
+ cl_assert_equal_s(expected, git_buf_cstr(&buf));
+ git_buf_clear(&buf);
+
+ cl_git_pass(git_diff_commit_as_email(
+ &buf, repo, commit, 1, 1, opts->flags, NULL));
+ cl_assert_equal_s(expected, git_buf_cstr(&buf));
+
+ git_diff_free(diff);
+ git_commit_free(commit);
+ git_buf_free(&buf);
+}
+
+void test_diff_format_email__simple(void)
+{
+ git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
+ const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
@@ -64,25 +93,8 @@ void test_diff_format_email__simple(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
- git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
- opts.summary = git_commit_summary(commit);
-
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_buf_clear(&buf);
- cl_git_pass(git_diff_commit_as_email(&buf, repo, commit, 1, 1, 0, NULL));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
}
void test_diff_format_email__multiple(void)
@@ -90,10 +102,10 @@ void test_diff_format_email__multiple(void)
git_oid oid;
git_commit *commit = NULL;
git_diff *diff = NULL;
- git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
+ git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
git_buf buf = GIT_BUF_INIT;
- const char *email =
+ const char *email =
"From 10808fe9c9be5a190c0ba68d1a002233fb363508 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
"Date: Thu, 10 Apr 2014 19:37:05 +0200\n" \
@@ -167,6 +179,7 @@ void test_diff_format_email__multiple(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
+
git_oid_fromstr(&oid, "10808fe9c9be5a190c0ba68d1a002233fb363508");
cl_git_pass(git_commit_lookup(&commit, repo, &oid));
@@ -196,7 +209,7 @@ void test_diff_format_email__multiple(void)
cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
+ cl_assert_equal_s(email, git_buf_cstr(&buf));
git_diff_free(diff);
git_commit_free(commit);
@@ -205,13 +218,8 @@ void test_diff_format_email__multiple(void)
void test_diff_format_email__exclude_marker(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
- git_buf buf = GIT_BUF_INIT;
-
- const char *email =
+ const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
@@ -250,27 +258,10 @@ void test_diff_format_email__exclude_marker(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
- git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
- opts.summary = git_commit_summary(commit);
-
opts.flags |= GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER;
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_buf_clear(&buf);
- cl_git_pass(git_diff_commit_as_email(&buf, repo, commit, 1, 1,
- GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER, NULL));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
}
void test_diff_format_email__invalid_no(void)
@@ -303,13 +294,8 @@ void test_diff_format_email__invalid_no(void)
void test_diff_format_email__mode_change(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
- git_buf buf = GIT_BUF_INIT;
-
- const char *email =
+ const char *email =
"From 7ade76dd34bba4733cf9878079f9fd4a456a9189 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
"Date: Thu, 10 Apr 2014 10:05:03 +0200\n" \
@@ -330,36 +316,14 @@ void test_diff_format_email__mode_change(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
- git_oid_fromstr(&oid, "7ade76dd34bba4733cf9878079f9fd4a456a9189");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
- opts.summary = git_commit_summary(commit);
-
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_buf_clear(&buf);
- cl_git_pass(git_diff_commit_as_email(&buf, repo, commit, 1, 1, 0, NULL));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "7ade76dd34bba4733cf9878079f9fd4a456a9189", &opts);
}
void test_diff_format_email__rename_add_remove(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
- git_buf buf = GIT_BUF_INIT;
-
- const char *email =
+ const char *email =
"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
@@ -422,35 +386,13 @@ void test_diff_format_email__rename_add_remove(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
- git_oid_fromstr(&oid, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
- opts.summary = git_commit_summary(commit);
-
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_buf_clear(&buf);
- cl_git_pass(git_diff_commit_as_email(&buf, repo, commit, 1, 1, 0, NULL));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
}
void test_diff_format_email__multiline_summary(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
- git_buf buf = GIT_BUF_INIT;
-
const char *email =
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
@@ -490,36 +432,15 @@ void test_diff_format_email__multiline_summary(void)
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
- git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
opts.summary = "Modify some content\nSome extra stuff here";
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_buf_clear(&buf);
- cl_git_pass(git_diff_commit_as_email(&buf, repo, commit, 1, 1, 0, NULL));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
}
void test_diff_format_email__binary(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
- git_buf buf = GIT_BUF_INIT;
-
- /* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
const char *email =
"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
"From: Jacques Germishuys <jacquesg@striata.com>\n" \
@@ -536,21 +457,11 @@ void test_diff_format_email__binary(void)
"--\n" \
"libgit2 " LIBGIT2_VERSION "\n" \
"\n";
+ /* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
- git_oid_fromstr(&oid, "8d7523f6fcb2404257889abe0d96f093d9f524f9");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
-
- opts.id = git_commit_id(commit);
- opts.author = git_commit_author(commit);
opts.summary = "Modified binary file";
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_format_email(&buf, diff, &opts));
- cl_assert(strcmp(git_buf_cstr(&buf), email) == 0);
-
- git_diff_free(diff);
- git_commit_free(commit);
- git_buf_free(&buf);
+ assert_email_match(
+ email, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
}
diff --git a/tests/diff/stats.c b/tests/diff/stats.c
index 131b768..055019f 100644
--- a/tests/diff/stats.c
+++ b/tests/diff/stats.c
@@ -5,246 +5,173 @@
#include "commit.h"
#include "diff.h"
-static git_repository *repo;
+static git_repository *_repo;
+static git_diff_stats *_stats;
void test_diff_stats__initialize(void)
{
- repo = cl_git_sandbox_init("diff_format_email");
+ _repo = cl_git_sandbox_init("diff_format_email");
}
void test_diff_stats__cleanup(void)
{
+ git_diff_stats_free(_stats); _stats = NULL;
cl_git_sandbox_cleanup();
}
-void test_diff_stats__stat(void)
+static void diff_stats_from_commit_oid(
+ git_diff_stats **stats, const char *oidstr, bool rename)
{
git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
- git_buf buf = GIT_BUF_INIT;
+ 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);
+}
+void test_diff_stats__stat(void)
+{
+ git_buf buf = GIT_BUF_INIT;
const char *stat =
" file1.txt | 8 +++++---\n" \
" 1 file changed, 5 insertions(+), 3 deletions(-)\n";
- git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
+ diff_stats_from_commit_oid(
+ &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ 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));
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 1);
- cl_assert(git_diff_stats_insertions(stats) == 5);
- cl_assert(git_diff_stats_deletions(stats) == 3);
-
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
+ cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
git_buf_free(&buf);
}
void test_diff_stats__multiple_hunks(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
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";
- git_oid_fromstr(&oid, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ diff_stats_from_commit_oid(
+ &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 2);
- cl_assert(git_diff_stats_insertions(stats) == 7);
- cl_assert(git_diff_stats_deletions(stats) == 4);
+ 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));
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__numstat(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
"3 2 file2.txt\n"
"4 2 file3.txt\n";
- git_oid_fromstr(&oid, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
-
- cl_git_pass(git_diff_get_stats(&stats, diff));
-
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_NUMBER));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
+ diff_stats_from_commit_oid(
+ &_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
+ cl_assert_equal_s(stat, git_buf_cstr(&buf));
git_buf_free(&buf);
}
void test_diff_stats__shortstat(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" 1 file changed, 5 insertions(+), 3 deletions(-)\n";
- git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ diff_stats_from_commit_oid(
+ &_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 1);
- cl_assert(git_diff_stats_insertions(stats) == 5);
- cl_assert(git_diff_stats_deletions(stats) == 3);
+ 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));
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_SHORT));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
+ cl_assert_equal_s(stat, git_buf_cstr(&buf));
git_buf_free(&buf);
}
void test_diff_stats__rename(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" file2.txt => file2.txt.renamed | 1 +\n"
" file3.txt => file3.txt.renamed | 4 +++-\n"
- " 2 files changed, 4 insertions(+), 1 deletions(-)\n";
-
- git_oid_fromstr(&oid, "8947a46e2097638ca6040ad4877246f4186ec3bd");
+ " 2 files changed, 4 insertions(+), 1 deletion(-)\n";
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_find_similar(diff, NULL));
+ diff_stats_from_commit_oid(
+ &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 2);
- cl_assert(git_diff_stats_insertions(stats) == 4);
- cl_assert(git_diff_stats_deletions(stats) == 1);
+ 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));
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__rename_nochanges(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
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";
- git_oid_fromstr(&oid, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_find_similar(diff, NULL));
+ diff_stats_from_commit_oid(
+ &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 2);
- cl_assert(git_diff_stats_insertions(stats) == 0);
- cl_assert(git_diff_stats_deletions(stats) == 0);
+ 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));
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__rename_and_modifiy(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" file2.txt.renamed2 | 2 +-\n"
" file3.txt.renamed2 => file3.txt.renamed | 0\n"
- " 2 files changed, 1 insertions(+), 1 deletions(-)\n";
+ " 2 files changed, 1 insertion(+), 1 deletion(-)\n";
- git_oid_fromstr(&oid, "4ca10087e696d2ba78d07b146a118e9a7096ed4f");
+ diff_stats_from_commit_oid(
+ &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
- cl_git_pass(git_diff_find_similar(diff, NULL));
+ 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));
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 2);
- cl_assert(git_diff_stats_insertions(stats) == 1);
- cl_assert(git_diff_stats_deletions(stats) == 1);
-
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__rename_no_find(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" file2.txt | 5 -----\n"
" file2.txt.renamed | 6 ++++++\n"
@@ -252,33 +179,21 @@ void test_diff_stats__rename_no_find(void)
" file3.txt.renamed | 7 +++++++\n"
" 4 files changed, 13 insertions(+), 10 deletions(-)\n";
- git_oid_fromstr(&oid, "8947a46e2097638ca6040ad4877246f4186ec3bd");
+ diff_stats_from_commit_oid(
+ &_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ 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));
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 4);
- cl_assert(git_diff_stats_insertions(stats) == 13);
- cl_assert(git_diff_stats_deletions(stats) == 10);
-
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__rename_nochanges_no_find(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" file2.txt.renamed | 6 ------\n"
" file2.txt.renamed2 | 6 ++++++\n"
@@ -286,143 +201,85 @@ void test_diff_stats__rename_nochanges_no_find(void)
" file3.txt.renamed2 | 7 +++++++\n"
" 4 files changed, 13 insertions(+), 13 deletions(-)\n";
- git_oid_fromstr(&oid, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+ diff_stats_from_commit_oid(
+ &_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 4);
- cl_assert(git_diff_stats_insertions(stats) == 13);
- cl_assert(git_diff_stats_deletions(stats) == 13);
-
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
+ 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));
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__rename_and_modifiy_no_find(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
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";
- git_oid_fromstr(&oid, "4ca10087e696d2ba78d07b146a118e9a7096ed4f");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
-
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 3);
- cl_assert(git_diff_stats_insertions(stats) == 8);
- cl_assert(git_diff_stats_deletions(stats) == 8);
+ diff_stats_from_commit_oid(
+ &_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
+ 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));
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__binary(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
- /* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
const char *stat =
" binary.bin | Bin 3 -> 0 bytes\n"
" 1 file changed, 0 insertions(+), 0 deletions(-)\n";
+ /* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
- git_oid_fromstr(&oid, "8d7523f6fcb2404257889abe0d96f093d9f524f9");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
-
- cl_git_pass(git_diff_get_stats(&stats, diff));
- cl_assert(git_diff_stats_files_changed(stats) == 1);
- cl_assert(git_diff_stats_insertions(stats) == 0);
- cl_assert(git_diff_stats_deletions(stats) == 0);
+ diff_stats_from_commit_oid(
+ &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_FULL));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
+ 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));
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}
void test_diff_stats__binary_numstat(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
"- - binary.bin\n";
- git_oid_fromstr(&oid, "8d7523f6fcb2404257889abe0d96f093d9f524f9");
-
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
-
- cl_git_pass(git_diff_get_stats(&stats, diff));
+ diff_stats_from_commit_oid(
+ &_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
- cl_git_pass(git_diff_stats_to_buf(&buf, stats, GIT_DIFF_STATS_NUMBER));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
+ cl_assert_equal_s(stat, git_buf_cstr(&buf));
git_buf_free(&buf);
}
void test_diff_stats__mode_change(void)
{
- git_oid oid;
- git_commit *commit = NULL;
- git_diff *diff = NULL;
- git_diff_stats *stats = NULL;
git_buf buf = GIT_BUF_INIT;
-
const char *stat =
" file1.txt.renamed | 0\n" \
" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
- " mode change 100644 => 100755 file1.txt.renamed\n" \
- "\n";
+ " mode change 100644 => 100755 file1.txt.renamed\n";
- git_oid_fromstr(&oid, "7ade76dd34bba4733cf9878079f9fd4a456a9189");
+ diff_stats_from_commit_oid(
+ &_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
- cl_git_pass(git_commit_lookup(&commit, repo, &oid));
- cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
-
- 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));
- cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
-
- git_diff_stats_free(stats);
- git_diff_free(diff);
- git_commit_free(commit);
+ 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_free(&buf);
}