diffcore-rename: add computation of number of unknown renames

The previous commit can only be effective if we have a computation of the number of paths under a given directory which are still have pending renames, and expected this number to be recorded in the dir_rename_count map under the key UNKNOWN_DIR. Add the code necessary to compute these values. Note that this change means dir_rename_count might have a directory whose only entry (for UNKNOWN_DIR) was removed by the time merge-ort goes to check it. To account for this, merge-ort needs to check for the case where the max count is 0. With this change we are now computing the necessary value for each directory in dirs_removed, but are not using that value anywhere. The next two commits will make use of the values stored in dirs_removed in order to compute whether each relevant_source (that is needed only for directory rename detection) has become unnecessary. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Mar 13, 2021 at 22:22 UTC bf238b71377e924066ca5155a787c167177f706c
2 files changed +40 -4
diffcore-rename.c
+37 -4
@@ -699,7 +699,8 @@ static void cleanup_dir_rename_info(struct dir_rename_info *info,
699 /*
700 * Although dir_rename_count was passed in
701 * diffcore_rename_extended() and we want to keep it around and
702 - * return it to that caller, we first want to remove any data
702 + * return it to that caller, we first want to remove any counts in
703 + * the maps associated with UNKNOWN_DIR entries and any data
704 * associated with directories that weren't renamed.
705 */
706 strmap_for_each_entry(info->dir_rename_count, &iter, entry) {
@@ -711,6 +712,9 @@ static void cleanup_dir_rename_info(struct dir_rename_info *info,
712 strintmap_clear(counts);
713 continue;
714 }
715 +
716 + if (strintmap_contains(counts, UNKNOWN_DIR))
717 + strintmap_remove(counts, UNKNOWN_DIR);
718 }
719 for (i = 0; i < to_remove.nr; ++i)
720 strmap_remove(info->dir_rename_count,
@@ -1125,6 +1129,7 @@ static void handle_early_known_dir_renames(struct dir_rename_info *info,
1129 * a majority.
1130 */
1131
1132 + int i;
1133 struct hashmap_iter iter;
1134 struct strmap_entry *entry;
1135
@@ -1134,10 +1139,38 @@ static void handle_early_known_dir_renames(struct dir_rename_info *info,
1139 return; /* culling incompatbile with break detection */
1140
1141 /*
1137 - * FIXME: Supplement dir_rename_count with number of potential
1138 - * renames, marking all potential rename sources as mapping to
1139 - * UNKNOWN_DIR.
1142 + * Supplement dir_rename_count with number of potential renames,
1143 + * marking all potential rename sources as mapping to UNKNOWN_DIR.
1144 */
1145 + for (i = 0; i < rename_src_nr; i++) {
1146 + char *old_dir;
1147 + struct diff_filespec *one = rename_src[i].p->one;
1148 +
1149 + /*
1150 + * sources that are part of a rename will have already been
1151 + * removed by a prior call to remove_unneeded_paths_from_src()
1152 + */
1153 + assert(!one->rename_used);
1154 +
1155 + old_dir = get_dirname(one->path);
1156 + while (*old_dir != '\0' &&
1157 + NOT_RELEVANT != strintmap_get(dirs_removed, old_dir)) {
1158 + char *freeme = old_dir;
1159 +
1160 + increment_count(info, old_dir, UNKNOWN_DIR);
1161 + old_dir = get_dirname(old_dir);
1162 +
1163 + /* Free resources we don't need anymore */
1164 + free(freeme);
1165 + }
1166 + /*
1167 + * old_dir and new_dir free'd in increment_count, but
1168 + * get_dirname() gives us a new pointer we need to free for
1169 + * old_dir. Also, if the loop runs 0 times we need old_dir
1170 + * to be freed.
1171 + */
1172 + free(old_dir);
1173 + }
1174
1175 /*
1176 * For any directory which we need a potential rename detected for
merge-ort.c
+3
@@ -1546,6 +1546,9 @@ static void get_provisional_directory_renames(struct merge_options *opt,
1546 }
1547 }
1548
1549 + if (max == 0)
1550 + continue;
1551 +
1552 if (bad_max == max) {
1553 path_msg(opt, source_dir, 0,
1554 _("CONFLICT (directory rename split): "