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

[PATCH 09/17] merge-ort: modify collect_renames() for directory rename handling

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

collect_renames() is similar to merge-recursive.c's get_renames(), but lacks the directory rename handling found in the latter. Port that code structure over to merge-ort. This introduces three new die-not-yet-implemented functions that will be defined in future commits.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 74 insertions(+), 4 deletions(-)
diff --git a/merge-ort.c b/merge-ort.c
index c4f437d4c0..a52905c4ee 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -1139,6 +1139,11 @@ static int handle_content_merge(struct merge_options *opt,
 
 /*** Function Grouping: functions related to directory rename detection ***/
 
+struct collision_info {
+	struct string_list source_files;
+	unsigned reported_already:1;
+};
+
 static void get_renamed_dir_portion(const char *old_path, const char *new_path,
 				    char **old_dir, char **new_dir)
 {
@@ -1378,6 +1383,31 @@ static void handle_directory_level_conflicts(struct merge_options *opt)
 	string_list_clear(&duplicated, 0);
 }
 
+static void compute_collisions(struct strmap *collisions,
+			       struct strmap *dir_renames,
+			       struct diff_queue_struct *pairs)
+{
+	die("Not yet implemented.");
+}
+
+static char *check_for_directory_rename(struct merge_options *opt,
+					const char *path,
+					unsigned side_index,
+					struct strmap *dir_renames,
+					struct strmap *dir_rename_exclusions,
+					struct strmap *collisions,
+					int *clean_merge)
+{
+	die("Not yet implemented.");
+}
+
+static void apply_directory_rename_modifications(struct merge_options *opt,
+						 struct diff_filepair *pair,
+						 char *new_path)
+{
+	die("Not yet implemented.");
+}
+
 /*** Function Grouping: functions related to regular rename detection ***/
 
 static int process_renames(struct merge_options *opt,
@@ -1703,22 +1733,44 @@ static void detect_regular_renames(struct merge_options *opt,
  */
 static int collect_renames(struct merge_options *opt,
 			   struct diff_queue_struct *result,
-			   unsigned side_index)
+			   unsigned side_index,
+			   struct strmap *dir_renames_for_side,
+			   struct strmap *rename_exclusions)
 {
 	int i, clean = 1;
+	struct strmap collisions;
 	struct diff_queue_struct *side_pairs;
+	struct hashmap_iter iter;
+	struct strmap_entry *entry;
 	struct rename_info *renames = &opt->priv->renames;
 
 	side_pairs = &renames->pairs[side_index];
+	compute_collisions(&collisions, dir_renames_for_side, side_pairs);
 
 	for (i = 0; i < side_pairs->nr; ++i) {
 		struct diff_filepair *p = side_pairs->queue[i];
+		char *new_path; /* non-NULL only with directory renames */
 
-		if (p->status != 'R') {
+		if (p->status != 'A' && p->status != 'R') {
 			diff_free_filepair(p);
 			continue;
 		}
 
+		new_path = check_for_directory_rename(opt, p->two->path,
+						      side_index,
+						      dir_renames_for_side,
+						      rename_exclusions,
+						      &collisions,
+						      &clean);
+
+		if (p->status != 'R' && !new_path) {
+			diff_free_filepair(p);
+			continue;
+		}
+
+		if (new_path)
+			apply_directory_rename_modifications(opt, p, new_path);
+
 		/*
 		 * p->score comes back from diffcore_rename_extended() with
 		 * the similarity of the renamed file.  The similarity is
@@ -1733,6 +1785,20 @@ static int collect_renames(struct merge_options *opt,
 		result->queue[result->nr++] = p;
 	}
 
+	/* Free each value in the collisions map */
+	strmap_for_each_entry(&collisions, &iter, entry) {
+		struct collision_info *info = entry->value;
+		string_list_clear(&info->source_files, 0);
+	}
+	/*
+	 * In compute_collisions(), we set collisions.strdup_strings to 0
+	 * so that we wouldn't have to make another copy of the new_path
+	 * allocated by apply_dir_rename().  But now that we've used them
+	 * and have no other references to these strings, it is time to
+	 * deallocate them.
+	 */
+	free_strmap_strings(&collisions);
+	strmap_clear(&collisions, 1);
 	return clean;
 }
 
@@ -1764,8 +1830,12 @@ static int detect_and_process_renames(struct merge_options *opt,
 	ALLOC_GROW(combined.queue,
 		   renames->pairs[1].nr + renames->pairs[2].nr,
 		   combined.alloc);
-	clean &= collect_renames(opt, &combined, MERGE_SIDE1);
-	clean &= collect_renames(opt, &combined, MERGE_SIDE2);
+	clean &= collect_renames(opt, &combined, MERGE_SIDE1,
+				 &renames->dir_renames[2],
+				 &renames->dir_renames[1]);
+	clean &= collect_renames(opt, &combined, MERGE_SIDE2,
+				 &renames->dir_renames[1],
+				 &renames->dir_renames[2]);
 	QSORT(combined.queue, combined.nr, compare_pairs);
 
 	clean &= process_renames(opt, &combined);
-- 
2.29.1.106.g3ff750dc32.dirty
Previous: Elijah NewrenNext: Elijah Newren
Message 12 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.