Skip to content

Commit b147301

Browse files
newrengitster
authored andcommitted
diffcore-rename: extend cleanup_dir_rename_info()
When diffcore_rename_extended() is passed a NULL dir_rename_count, we will still want to create a temporary one for use by find_basename_matches(), but have it fully deallocated before diffcore_rename_extended() returns. However, when diffcore_rename_extended() is passed a dir_rename_count, we want to fill that strmap with appropriate values and return it. However, for our interim purposes we may also add entries corresponding to directories that cannot have been renamed due to still existing on both sides. Extend cleanup_dir_rename_info() to handle these two different cases, cleaning up the relevant bits of information for each case. Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b6e3d27 commit b147301

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

diffcore-rename.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,15 @@ void partial_clear_dir_rename_count(struct strmap *dir_rename_count)
543543
strmap_partial_clear(dir_rename_count, 1);
544544
}
545545

546-
static void cleanup_dir_rename_info(struct dir_rename_info *info)
546+
static void cleanup_dir_rename_info(struct dir_rename_info *info,
547+
struct strset *dirs_removed,
548+
int keep_dir_rename_count)
547549
{
550+
struct hashmap_iter iter;
551+
struct strmap_entry *entry;
552+
struct string_list to_remove = STRING_LIST_INIT_NODUP;
553+
int i;
554+
548555
if (!info->setup)
549556
return;
550557

@@ -555,8 +562,33 @@ static void cleanup_dir_rename_info(struct dir_rename_info *info)
555562
strmap_clear(&info->dir_rename_guess, 1);
556563

557564
/* dir_rename_count */
558-
partial_clear_dir_rename_count(info->dir_rename_count);
559-
strmap_clear(info->dir_rename_count, 1);
565+
if (!keep_dir_rename_count) {
566+
partial_clear_dir_rename_count(info->dir_rename_count);
567+
strmap_clear(info->dir_rename_count, 1);
568+
FREE_AND_NULL(info->dir_rename_count);
569+
return;
570+
}
571+
572+
/*
573+
* Although dir_rename_count was passed in
574+
* diffcore_rename_extended() and we want to keep it around and
575+
* return it to that caller, we first want to remove any data
576+
* associated with directories that weren't renamed.
577+
*/
578+
strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
579+
const char *source_dir = entry->key;
580+
struct strintmap *counts = entry->value;
581+
582+
if (!strset_contains(dirs_removed, source_dir)) {
583+
string_list_append(&to_remove, source_dir);
584+
strintmap_clear(counts);
585+
continue;
586+
}
587+
}
588+
for (i = 0; i < to_remove.nr; ++i)
589+
strmap_remove(info->dir_rename_count,
590+
to_remove.items[i].string, 1);
591+
string_list_clear(&to_remove, 0);
560592
}
561593

562594
static const char *get_basename(const char *filename)
@@ -1218,7 +1250,7 @@ void diffcore_rename_extended(struct diff_options *options,
12181250
if (rename_dst[i].filespec_to_free)
12191251
free_filespec(rename_dst[i].filespec_to_free);
12201252

1221-
cleanup_dir_rename_info(&info);
1253+
cleanup_dir_rename_info(&info, dirs_removed, dir_rename_count != NULL);
12221254
FREE_AND_NULL(rename_dst);
12231255
rename_dst_nr = rename_dst_alloc = 0;
12241256
FREE_AND_NULL(rename_src);

0 commit comments

Comments
 (0)