Skip to content

Commit 0cd976c

Browse files
authored
Merge pull request libgit2#4830 from pks-t/pks/diff-stats-rename-common
diff_stats: use git's formatting of renames with common directories
2 parents 0c97335 + e5090ee commit 0cd976c

14 files changed

+45
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,25 @@ void test_diff_stats__rename_and_modifiy(void)
210210
git_buf_dispose(&buf);
211211
}
212212

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+
213232
void test_diff_stats__rename_no_find(void)
214233
{
215234
git_buf buf = GIT_BUF_INIT;
332 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/resources/diff_format_email/.gitted/objects/0d/b2a262bc8c5c3cba55254730045a8258da7a37

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��A
2+
�0@Q�9���f�6� z��tBCm-i�����?���4���VU�S��)E����hET��|}B�d6��6����=&�9�8K�ƀ<Ɓ�d��6�*ܸՒfx4-��ulp���6鱗\��z�"�"���k��4w]yQ��PV��2�j>�TI�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)