git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 13/17] merge-ort: implement handle_path_level_conflicts()

From
Elijah Newren <newren@gmail.com>
Date
Jan 4, 2021, 23:50 UTC
Message-ID
<20210104235006.2867404-14-newren@gmail.com>
In-Reply-To
<20210104235006.2867404-1-newren@gmail.com>
This is copied from merge-recursive.c, with minor tweaks due to:
  * using strmap API
  * merge-ort not using the non_unique_new_dir field, since it'll
    obviate its need entirely later with performance improvements
  * adding a new path_in_way() function that uses opt->priv->paths
    instead of doing an expensive tree_has_path() lookup to see if
    a tree has a given path.
Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 71 insertions(+), 1 deletion(-)
diff --git a/merge-ort.c b/merge-ort.c
index 84f4f4776b..d0fb8f4282 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -1282,6 +1282,16 @@ static void get_renamed_dir_portion(const char *old_path, const char *new_path,
 	*new_dir = xstrndup(new_path, end_of_new - new_path);
 }
 
+static int path_in_way(struct strmap *paths, const char *path, unsigned side_mask)
+{
+	struct merged_info *mi = strmap_get(paths, path);
+	struct conflict_info *ci;
+	if (!mi)
+		return 0;
+	INITIALIZE_CI(ci, mi);
+	return mi->clean || (side_mask & (ci->filemask | ci->dirmask));
+}
+
 /*
  * See if there is a directory rename for path, and if there are any file
  * level conflicts on the given side for the renamed location.  If there is
@@ -1294,7 +1304,67 @@ static char *handle_path_level_conflicts(struct merge_options *opt,
 					 struct strmap_entry *rename_info,
 					 struct strmap *collisions)
 {
-	die("Not yet implemented");
+	char *new_path = NULL;
+	struct collision_info *c_info;
+	int clean = 1;
+	struct strbuf collision_paths = STRBUF_INIT;
+
+	/*
+	 * entry has the mapping of old directory name to new directory name
+	 * that we want to apply to path.
+	 */
+	new_path = apply_dir_rename(rename_info, path);
+	if (!new_path)
+		BUG("Failed to apply directory rename!");
+
+	/*
+	 * The caller needs to have ensured that it has pre-populated
+	 * collisions with all paths that map to new_path.  Do a quick check
+	 * to ensure that's the case.
+	 */
+	c_info = strmap_get(collisions, new_path);
+	if (c_info == NULL)
+		BUG("c_info is NULL");
+
+	/*
+	 * Check for one-sided add/add/.../add conflicts, i.e.
+	 * where implicit renames from the other side doing
+	 * directory rename(s) can affect this side of history
+	 * to put multiple paths into the same location.  Warn
+	 * and bail on directory renames for such paths.
+	 */
+	if (c_info->reported_already) {
+		clean = 0;
+	} else if (path_in_way(&opt->priv->paths, new_path, 1 << side_index)) {
+		c_info->reported_already = 1;
+		strbuf_add_separated_string_list(&collision_paths, ", ",
+						 &c_info->source_files);
+		path_msg(opt, new_path, 0,
+			 _("CONFLICT (implicit dir rename): Existing file/dir "
+			   "at %s in the way of implicit directory rename(s) "
+			   "putting the following path(s) there: %s."),
+		       new_path, collision_paths.buf);
+		clean = 0;
+	} else if (c_info->source_files.nr > 1) {
+		c_info->reported_already = 1;
+		strbuf_add_separated_string_list(&collision_paths, ", ",
+						 &c_info->source_files);
+		path_msg(opt, new_path, 0,
+			 _("CONFLICT (implicit dir rename): Cannot map more "
+			   "than one path to %s; implicit directory renames "
+			   "tried to put these paths there: %s"),
+		       new_path, collision_paths.buf);
+		clean = 0;
+	}
+
+	/* Free memory we no longer need */
+	strbuf_release(&collision_paths);
+	if (!clean && new_path) {
+		free(new_path);
+		return NULL;
+	}
+
+	return new_path;
 }
 
 static void increment_count(struct strmap *dir_rename_count,
-- 
2.29.1.106.g3ff750dc32.dirty
Previous: Elijah NewrenNext: Elijah Newren
Message 17 of 18 in “Add directory rename detection to merge-ort”
  1. 00/17 Add directory rename detection to merge-ortElijah Newren, Jan 4, 2021
  2. 08/17 merge-ort: implement handle_directory_level_conflicts()Elijah Newren, Jan 4, 2021
  3. 02/17 merge-ort: initialize and free new directory rename data structuresElijah Newren, Jan 4, 2021
  4. 10/17 merge-ort: implement compute_collisions()Elijah Newren, Jan 4, 2021
  5. 01/17 merge-ort: add new data structures for directory rename detectionElijah Newren, Jan 4, 2021
  6. 12/17 merge-ort: implement check_for_directory_rename()Elijah Newren, Jan 4, 2021
  7. 03/17 merge-ort: collect which directories are removed in dirs_removedElijah Newren, Jan 4, 2021
  8. 04/17 merge-ort: add outline for computing directory renamesElijah Newren, Jan 4, 2021
  9. 06/17 merge-ort: copy get_renamed_dir_portion() from merge-recursive.cElijah Newren, Jan 4, 2021
  10. 14/17 merge-ort: add a new toplevel_dir fieldElijah Newren, Jan 4, 2021
  11. 05/17 merge-ort: add outline of get_provisional_directory_renames()Elijah Newren, Jan 4, 2021
  12. 09/17 merge-ort: modify collect_renames() for directory rename handlingElijah Newren, Jan 4, 2021
  13. 07/17 merge-ort: implement compute_rename_counts()Elijah Newren, Jan 4, 2021
  14. 15/17 merge-ort: implement apply_directory_rename_modifications()Elijah Newren, Jan 4, 2021
  15. 17/17 merge-ort: fix a directory rename detection bugElijah Newren, Jan 4, 2021
  16. 11/17 merge-ort: implement apply_dir_rename() and check_dir_renamed()Elijah Newren, Jan 4, 2021
  17. 13/17 merge-ort: implement handle_path_level_conflicts()Elijah Newren, Jan 4, 2021
  18. 16/17 merge-ort: process_renames() now needs more defensivenessElijah Newren, Jan 4, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.