merge-recursive: introduce new functions to handle rename logic

The amount of logic in merge_trees() relative to renames was just a few lines, but split it out into new handle_renames() and cleanup_renames() functions to prepare for additional logic to be added to each. No code or logic changes, just a new place to put stuff for when the rename detection gains additional checks. Note that process_renames() records pointers to various information (such as diff_filepairs) into rename_conflict_info structs. Even though the rename string_lists are not directly used once handle_renames() completes, we should not immediately free the lists at the end of that function because they store the information referenced in the rename_conflict_info, which is used later in process_entry(). Thus the reason for a separate cleanup_renames(). Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Apr 19, 2018 at 10:58 UTC f172589e599fca939ab6bb5f2dae4d58f6d201fc
1 file changed +33 -10
merge-recursive.c
+33 -10
@@ -1646,6 +1646,32 @@ cleanup_and_return:
1646 return clean_merge;
1647 }
1648
1649 +struct rename_info {
1650 + struct string_list *head_renames;
1651 + struct string_list *merge_renames;
1652 +};
1653 +
1654 +static int handle_renames(struct merge_options *o,
1655 + struct tree *common,
1656 + struct tree *head,
1657 + struct tree *merge,
1658 + struct string_list *entries,
1659 + struct rename_info *ri)
1660 +{
1661 + ri->head_renames = get_renames(o, head, common, head, merge, entries);
1662 + ri->merge_renames = get_renames(o, merge, common, head, merge, entries);
1663 + return process_renames(o, ri->head_renames, ri->merge_renames);
1664 +}
1665 +
1666 +static void cleanup_renames(struct rename_info *re_info)
1667 +{
1668 + string_list_clear(re_info->head_renames, 0);
1669 + string_list_clear(re_info->merge_renames, 0);
1670 +
1671 + free(re_info->head_renames);
1672 + free(re_info->merge_renames);
1673 +}
1674 +
1675 static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
1676 {
1677 return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
@@ -2005,7 +2031,8 @@ int merge_trees(struct merge_options *o,
2031 }
2032
2033 if (unmerged_cache()) {
2008 - struct string_list *entries, *re_head, *re_merge;
2034 + struct string_list *entries;
2035 + struct rename_info re_info;
2036 int i;
2037 /*
2038 * Only need the hashmap while processing entries, so
@@ -2019,9 +2046,8 @@ int merge_trees(struct merge_options *o,
2046 get_files_dirs(o, merge);
2047
2048 entries = get_unmerged();
2022 - re_head = get_renames(o, head, common, head, merge, entries);
2023 - re_merge = get_renames(o, merge, common, head, merge, entries);
2024 - clean = process_renames(o, re_head, re_merge);
2049 + clean = handle_renames(o, common, head, merge, entries,
2050 + &re_info);
2051 record_df_conflict_files(o, entries);
2052 if (clean < 0)
2053 goto cleanup;
@@ -2046,16 +2072,13 @@ int merge_trees(struct merge_options *o,
2072 }
2073
2074 cleanup:
2049 - string_list_clear(re_merge, 0);
2050 - string_list_clear(re_head, 0);
2075 + cleanup_renames(&re_info);
2076 +
2077 string_list_clear(entries, 1);
2078 + free(entries);
2079
2080 hashmap_free(&o->current_file_dir_set, 1);
2081
2055 - free(re_merge);
2056 - free(re_head);
2057 - free(entries);
2058 -
2082 if (clean < 0)
2083 return clean;
2084 }