@@ -28,7 +28,6 @@ struct git_diff_stats {
28
28
size_t files_changed ;
29
29
size_t insertions ;
30
30
size_t deletions ;
31
- size_t renames ;
32
31
33
32
size_t max_name ;
34
33
size_t max_filestat ;
@@ -68,17 +67,19 @@ static int diff_file_stats_full_to_buf(
68
67
size_t common_dirlen ;
69
68
int error ;
70
69
71
- padding = stats -> max_name - strlen (old_path ) - strlen (new_path );
72
-
73
70
if ((common_dirlen = git_fs_path_common_dirlen (old_path , new_path )) &&
74
71
common_dirlen <= INT_MAX ) {
75
72
error = git_str_printf (out , " %.*s{%s" DIFF_RENAME_FILE_SEPARATOR "%s}" ,
76
73
(int ) common_dirlen , old_path ,
77
74
old_path + common_dirlen ,
78
75
new_path + common_dirlen );
76
+ padding = stats -> max_name + common_dirlen - strlen (old_path )
77
+ - strlen (new_path ) - 2 - strlen (DIFF_RENAME_FILE_SEPARATOR );
79
78
} else {
80
79
error = git_str_printf (out , " %s" DIFF_RENAME_FILE_SEPARATOR "%s" ,
81
80
old_path , new_path );
81
+ padding = stats -> max_name - strlen (old_path )
82
+ - strlen (new_path ) - strlen (DIFF_RENAME_FILE_SEPARATOR );
82
83
}
83
84
84
85
if (error < 0 )
@@ -89,9 +90,6 @@ static int diff_file_stats_full_to_buf(
89
90
goto on_error ;
90
91
91
92
padding = stats -> max_name - strlen (adddel_path );
92
-
93
- if (stats -> renames > 0 )
94
- padding += strlen (DIFF_RENAME_FILE_SEPARATOR );
95
93
}
96
94
97
95
if (git_str_putcn (out , ' ' , padding ) < 0 ||
@@ -210,14 +208,23 @@ int git_diff_get_stats(
210
208
if ((error = git_patch_from_diff (& patch , diff , i )) < 0 )
211
209
break ;
212
210
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
+ */
214
215
delta = patch -> delta ;
215
-
216
- /* TODO ugh */
217
216
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
+ }
221
228
}
222
229
223
230
/* and, of course, count the line stats */
0 commit comments