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

[PATCH 11/17] merge-ort: implement apply_dir_rename() and check_dir_renamed()

From
Elijah Newren <newren@gmail.com>
Date
Jan 4, 2021, 23:50 UTC
Message-ID
<20210104235006.2867404-12-newren@gmail.com>
In-Reply-To
<20210104235006.2867404-1-newren@gmail.com>

Both of these are copied from merge-recursive.c, with just minor tweaks due to using strmap API and not having a non_unique_new_dir field.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
diff --git a/merge-ort.c b/merge-ort.c
index 9c9be645ea..f7f2470c96 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -1154,7 +1154,29 @@ struct collision_info {
 static char *apply_dir_rename(struct strmap_entry *rename_info,
 			      const char *old_path)
 {
-	die("Not yet implemented!");
+	struct strbuf new_path = STRBUF_INIT;
+	const char *old_dir = rename_info->key;
+	const char *new_dir = rename_info->value;
+	int oldlen, newlen, new_dir_len;
+
+	oldlen = strlen(old_dir);
+	if (*new_dir == '\0')
+		/*
+		 * If someone renamed/merged a subdirectory into the root
+		 * directory (e.g. 'some/subdir' -> ''), then we want to
+		 * avoid returning
+		 *     '' + '/filename'
+		 * as the rename; we need to make old_path + oldlen advance
+		 * past the '/' character.
+		 */
+		oldlen++;
+	new_dir_len = strlen(new_dir);
+	newlen = new_dir_len + (strlen(old_path) - oldlen) + 1;
+	strbuf_grow(&new_path, newlen);
+	strbuf_add(&new_path, new_dir, new_dir_len);
+	strbuf_addstr(&new_path, &old_path[oldlen]);
+
+	return strbuf_detach(&new_path, NULL);
 }
 
 static void get_renamed_dir_portion(const char *old_path, const char *new_path,
@@ -1399,7 +1421,18 @@ static void handle_directory_level_conflicts(struct merge_options *opt)
 static struct strmap_entry *check_dir_renamed(const char *path,
 					      struct strmap *dir_renames)
 {
-	die("Not yet implemented!");
+	char *temp = xstrdup(path);
+	char *end;
+	struct strmap_entry *e = NULL;
+
+	while ((end = strrchr(temp, '/'))) {
+		*end = '\0';
+		e = strmap_get_entry(dir_renames, temp);
+		if (e)
+			break;
+	}
+	free(temp);
+	return e;
 }
 
 static void compute_collisions(struct strmap *collisions,
-- 
2.29.1.106.g3ff750dc32.dirty
Previous: Elijah NewrenNext: Elijah Newren
Message 16 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.