Skip to content

Commit e5090ee

Browse files
committed
diff_stats: use git's formatting of renames with common directories
In cases where a file gets renamed such that the directories containing it previous and after the rename have a common prefix, then git will avoid printing this prefix twice and instead format the rename as "prefix/{old => new}". We currently didn't do anything like that, but simply printed "prefix/old -> prefix/new". Adjust our behaviour to instead match upstream. Adjust the test for this behaviour to expect the new format.
1 parent 3148efd commit e5090ee

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/diff_stats.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,29 @@ int git_diff_file_stats__full_to_buf(
6161
old_size = delta->old_file.size;
6262
new_size = delta->new_file.size;
6363

64-
if (git_buf_printf(out, " %s", old_path) < 0)
65-
goto on_error;
66-
6764
if (strcmp(old_path, new_path) != 0) {
65+
size_t common_dirlen;
66+
int error;
67+
6868
padding = stats->max_name - strlen(old_path) - strlen(new_path);
6969

70-
if (git_buf_printf(out, DIFF_RENAME_FILE_SEPARATOR "%s", new_path) < 0)
70+
if ((common_dirlen = git_path_common_dirlen(old_path, new_path)) &&
71+
common_dirlen <= INT_MAX) {
72+
error = git_buf_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}",
73+
(int) common_dirlen, old_path,
74+
old_path + common_dirlen,
75+
new_path + common_dirlen);
76+
} else {
77+
error = git_buf_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s",
78+
old_path, new_path);
79+
}
80+
81+
if (error < 0)
7182
goto on_error;
7283
} else {
84+
if (git_buf_printf(out, " %s", old_path) < 0)
85+
goto on_error;
86+
7387
padding = stats->max_name - strlen(old_path);
7488

7589
if (stats->renames > 0)

tests/diff/stats.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void test_diff_stats__rename_in_subdirectory(void)
214214
{
215215
git_buf buf = GIT_BUF_INIT;
216216
const char *stat =
217-
" dir/orig.txt => dir/renamed.txt | 0\n"
217+
" dir/{orig.txt => renamed.txt} | 0\n"
218218
" 1 file changed, 0 insertions(+), 0 deletions(-)\n";
219219

220220
diff_stats_from_commit_oid(

0 commit comments

Comments
 (0)