diff: refactor common tail part of dirstat computation

This will become useful when we plug leaks in these two functions. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 5, 2023 at 14:19 UTC 34a94897e02fda653cf0c71edc2d5df207855160
1 file changed +15 -14
diff.c
+15 -14
@@ -2971,6 +2971,19 @@ static int dirstat_compare(const void *_a, const void *_b)
2971 return strcmp(a->name, b->name);
2972 }
2973
2974 +static void conclude_dirstat(struct diff_options *options,
2975 + struct dirstat_dir *dir,
2976 + unsigned long changed)
2977 +{
2978 + /* This can happen even with many files, if everything was renames */
2979 + if (!changed)
2980 + return;
2981 +
2982 + /* Show all directories with more than x% of the changes */
2983 + QSORT(dir->files, dir->nr, dirstat_compare);
2984 + gather_dirstat(options, dir, changed, "", 0);
2985 +}
2986 +
2987 static void show_dirstat(struct diff_options *options)
2988 {
2989 int i;
@@ -3060,13 +3073,7 @@ found_damage:
3073 dir.nr++;
3074 }
3075
3063 - /* This can happen even with many files, if everything was renames */
3064 - if (!changed)
3065 - return;
3066 -
3067 - /* Show all directories with more than x% of the changes */
3068 - QSORT(dir.files, dir.nr, dirstat_compare);
3069 - gather_dirstat(options, &dir, changed, "", 0);
3076 + conclude_dirstat(options, &dir, changed);
3077 }
3078
3079 static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *options)
@@ -3104,13 +3111,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
3111 dir.nr++;
3112 }
3113
3107 - /* This can happen even with many files, if everything was renames */
3108 - if (!changed)
3109 - return;
3110 -
3111 - /* Show all directories with more than x% of the changes */
3112 - QSORT(dir.files, dir.nr, dirstat_compare);
3113 - gather_dirstat(options, &dir, changed, "", 0);
3114 + conclude_dirstat(options, &dir, changed);
3115 }
3116
3117 static void free_diffstat_file(struct diffstat_file *f)