From 31f5c3099900e3964827a8b55b69c48f6dc8215d Mon Sep 17 00:00:00 2001 From: Talya Connor Date: Sat, 29 Mar 2025 18:16:06 +1100 Subject: [PATCH 1/3] test: failing diff stat test. --- tests/libgit2/diff/stats.c | 20 ++++++++++++++++++ .../resources/diff_format_email/.gitted/index | Bin 621 -> 621 bytes .../7f/4c6d9d6ba363e3352f7c21807ca3a7835072b2 | 1 + .../7f/9b9a5984d19db4a566151374762e2628ec26b3 | Bin 0 -> 30 bytes .../8f/6fa527fd62979afab1d2346828c7e801daac32 | Bin 0 -> 58 bytes .../eb/d0b64ca4dd3e5dbcd9d1a12a1f2b84f893a22d | Bin 0 -> 180 bytes .../ff/03799ab60143654e260d91049a99551299b2a5 | Bin 0 -> 151 bytes .../.gitted/refs/heads/master | 2 +- .../dir/{renamed.txt => rerenamed.txt} | 0 tests/resources/diff_format_email/file3.txt | 2 +- 10 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/resources/diff_format_email/.gitted/objects/7f/4c6d9d6ba363e3352f7c21807ca3a7835072b2 create mode 100644 tests/resources/diff_format_email/.gitted/objects/7f/9b9a5984d19db4a566151374762e2628ec26b3 create mode 100644 tests/resources/diff_format_email/.gitted/objects/8f/6fa527fd62979afab1d2346828c7e801daac32 create mode 100644 tests/resources/diff_format_email/.gitted/objects/eb/d0b64ca4dd3e5dbcd9d1a12a1f2b84f893a22d create mode 100644 tests/resources/diff_format_email/.gitted/objects/ff/03799ab60143654e260d91049a99551299b2a5 rename tests/resources/diff_format_email/dir/{renamed.txt => rerenamed.txt} (100%) diff --git a/tests/libgit2/diff/stats.c b/tests/libgit2/diff/stats.c index f80813622..5a51aac78 100644 --- a/tests/libgit2/diff/stats.c +++ b/tests/libgit2/diff/stats.c @@ -229,6 +229,26 @@ void test_diff_stats__rename_in_subdirectory(void) git_buf_dispose(&buf); } +void test_diff_stats__rename_in_subdirectory_aligns(void) +{ + git_buf buf = GIT_BUF_INIT; + const char *stat = + " dir/{renamed.txt => rerenamed.txt} | 0\n" + " file3.txt | 2 +-\n" + " 2 files changed, 1 insertion(+), 1 deletion(-)\n"; + + diff_stats_from_commit_oid( + &_stats, "7f4c6d9d6ba363e3352f7c21807ca3a7835072b2", true); + + 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_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0)); + cl_assert_equal_s(stat, buf.ptr); + git_buf_dispose(&buf); +} + void test_diff_stats__rename_no_find(void) { git_buf buf = GIT_BUF_INIT; diff --git a/tests/resources/diff_format_email/.gitted/index b/tests/resources/diff_format_email/.gitted/index index 4514a6b9c762f486cbb51587e1c2c4f1d6c60e1e..7053c2159d682f0f4e8e8c173d82fc5abe018a56 100644 GIT binary patch delta 294 zcmaFM@|MNG#WTp6fq{Vuh*{E~&y6;_rspkf1bO0rb;)W2X_yuJjiZkvuTvO1u1bn0i%ifp+!cgxZ=QQ?v@JvEItYMUn; zFiINyXRe&JjnO&PPmOmX%dDB9LNhll1v;C-P{Dw!KYywE-=yiYer>#DlA&?@1>>zX gMw2TUABb2PKAqY0KIyHhf12`E?v_RWY|icg0GyL)*#H0l delta 294 zcmaFM@|MNG#WTp6fq{Vuh*_ey-YnG0bEpE+F^vAHw;34zF)|pi{bgWiT*AP>`~rk0 z8c3S+r(_oC7p3MU=BB3Tl~j}f^+t64Fk=^??0+WZ)P<6Xe zthk_J6ScX!RQ^YLD2#@vW3ok72eelExD@N<1l1F4o>?VVa%&^=HW^JlGTDGp(ja)! zdz-2AOT0U^_w5$z-o1T84D)B8vl$E(47hkB?N3QuTrGHS+Y6?c`A@`M^ItSiu4H^5 aVliR6Tc!Jdo^+4WH2-^Ff%bxC`v6#%}dNpP0=f-bYm88ZFJ)KA7v1BkY00M=S%p!*V{H5xDlcvx5wegZkhQ{$1jJMVp85$Uv zo0=-5W#*(B>XlTK=oO{rCFZ84Fsv*LUvME@Y(`OczJ6KhmavmOk%>UXW+o Date: Sat, 29 Mar 2025 18:29:16 +1100 Subject: [PATCH 2/3] diff: account for common prefix in max namelen. --- src/libgit2/diff_stats.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libgit2/diff_stats.c b/src/libgit2/diff_stats.c index 259939844..b67aa48dd 100644 --- a/src/libgit2/diff_stats.c +++ b/src/libgit2/diff_stats.c @@ -28,7 +28,6 @@ struct git_diff_stats { size_t files_changed; size_t insertions; size_t deletions; - size_t renames; size_t max_name; size_t max_filestat; @@ -68,17 +67,19 @@ static int diff_file_stats_full_to_buf( size_t common_dirlen; int error; - padding = stats->max_name - strlen(old_path) - strlen(new_path); - if ((common_dirlen = git_fs_path_common_dirlen(old_path, new_path)) && common_dirlen <= INT_MAX) { error = git_str_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}", (int) common_dirlen, old_path, old_path + common_dirlen, new_path + common_dirlen); + padding = stats->max_name + common_dirlen - strlen(old_path) + - strlen(new_path) - 2 - strlen(DIFF_RENAME_FILE_SEPARATOR); } else { error = git_str_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s", old_path, new_path); + padding = stats->max_name - strlen(old_path) + - strlen(new_path) - strlen(DIFF_RENAME_FILE_SEPARATOR); } if (error < 0) @@ -89,9 +90,6 @@ static int diff_file_stats_full_to_buf( goto on_error; padding = stats->max_name - strlen(adddel_path); - - if (stats->renames > 0) - padding += strlen(DIFF_RENAME_FILE_SEPARATOR); } if (git_str_putcn(out, ' ', padding) < 0 || @@ -210,14 +208,23 @@ int git_diff_get_stats( if ((error = git_patch_from_diff(&patch, diff, i)) < 0) break; - /* keep a count of renames because it will affect formatting */ + /* Length calculation for renames mirrors the actual presentation format + * generated in diff_file_stats_full_to_buf; namelen is the full length of + * what will be printed, taking into account renames and common prefixes. + */ delta = patch->delta; - - /* TODO ugh */ namelen = strlen(delta->new_file.path); - if (delta->old_file.path && strcmp(delta->old_file.path, delta->new_file.path) != 0) { - namelen += strlen(delta->old_file.path); - stats->renames++; + if (delta->old_file.path && + strcmp(delta->old_file.path, delta->new_file.path) != 0) { + size_t common_dirlen; + if ((common_dirlen = git_fs_path_common_dirlen(delta->old_file.path, delta->new_file.path)) && + common_dirlen <= INT_MAX) { + namelen += strlen(delta->old_file.path) + 2 + + strlen(DIFF_RENAME_FILE_SEPARATOR) - common_dirlen; + } else { + namelen += strlen(delta->old_file.path) + + strlen(DIFF_RENAME_FILE_SEPARATOR); + } } /* and, of course, count the line stats */ From d88347a89cc197a57c5eaeb767aa99d9ac836175 Mon Sep 17 00:00:00 2001 From: Talya Connor Date: Sat, 29 Mar 2025 18:29:16 +1100 Subject: [PATCH 3/3] AUTHORS: add self. --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index f4e852357..ee55e6ad5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -70,6 +70,7 @@ Shawn O. Pearce Shuhei Tanuma Steve Frécinaux Sven Strickroth +Talya "kivikakk" Connor Tim Branyen Tim Clem Tim Harder