Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f93169

Browse files
committed
diff: account for common prefix in max namelen.
1 parent 31f5c30 commit 9f93169

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/libgit2/diff_stats.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct git_diff_stats {
2828
size_t files_changed;
2929
size_t insertions;
3030
size_t deletions;
31-
size_t renames;
3231

3332
size_t max_name;
3433
size_t max_filestat;
@@ -68,17 +67,19 @@ static int diff_file_stats_full_to_buf(
6867
size_t common_dirlen;
6968
int error;
7069

71-
padding = stats->max_name - strlen(old_path) - strlen(new_path);
72-
7370
if ((common_dirlen = git_fs_path_common_dirlen(old_path, new_path)) &&
7471
common_dirlen <= INT_MAX) {
7572
error = git_str_printf(out, " %.*s{%s"DIFF_RENAME_FILE_SEPARATOR"%s}",
7673
(int) common_dirlen, old_path,
7774
old_path + common_dirlen,
7875
new_path + common_dirlen);
76+
padding = stats->max_name + common_dirlen - strlen(old_path)
77+
- strlen(new_path) - 2 - strlen(DIFF_RENAME_FILE_SEPARATOR);
7978
} else {
8079
error = git_str_printf(out, " %s" DIFF_RENAME_FILE_SEPARATOR "%s",
8180
old_path, new_path);
81+
padding = stats->max_name - strlen(old_path)
82+
- strlen(new_path) - strlen(DIFF_RENAME_FILE_SEPARATOR);
8283
}
8384

8485
if (error < 0)
@@ -89,9 +90,6 @@ static int diff_file_stats_full_to_buf(
8990
goto on_error;
9091

9192
padding = stats->max_name - strlen(adddel_path);
92-
93-
if (stats->renames > 0)
94-
padding += strlen(DIFF_RENAME_FILE_SEPARATOR);
9593
}
9694

9795
if (git_str_putcn(out, ' ', padding) < 0 ||
@@ -210,14 +208,23 @@ int git_diff_get_stats(
210208
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
211209
break;
212210

213-
/* keep a count of renames because it will affect formatting */
211+
/* Length calculation for renames mirrors the actual presentation format
212+
* generated in diff_file_stats_full_to_buf; namelen is the full length of
213+
* what will be printed, taking into account renames and common prefixes.
214+
*/
214215
delta = patch->delta;
215-
216-
/* TODO ugh */
217216
namelen = strlen(delta->new_file.path);
218-
if (delta->old_file.path && strcmp(delta->old_file.path, delta->new_file.path) != 0) {
219-
namelen += strlen(delta->old_file.path);
220-
stats->renames++;
217+
if (delta->old_file.path &&
218+
strcmp(delta->old_file.path, delta->new_file.path) != 0) {
219+
size_t common_dirlen;
220+
if ((common_dirlen = git_fs_path_common_dirlen(delta->old_file.path, delta->new_file.path)) &&
221+
common_dirlen <= INT_MAX) {
222+
namelen += strlen(delta->old_file.path) + 2 +
223+
strlen(DIFF_RENAME_FILE_SEPARATOR) - common_dirlen;
224+
} else {
225+
namelen += strlen(delta->old_file.path) +
226+
strlen(DIFF_RENAME_FILE_SEPARATOR);
227+
}
221228
}
222229

223230
/* and, of course, count the line stats */

0 commit comments

Comments
 (0)