git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Elijah Newren via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Elijah Newren <newren@gmail.com>,
	Elijah Newren <newren@gmail.com>
Subject: [PATCH v2 15/17] merge-ort: implement apply_directory_rename_modifications()
Date: Thu, 07 Jan 2021 21:36:03 +0000	[thread overview]
Message-ID: <d74417e86c54ea03f5a7802a7e4fd2bc3a22f935.1610055365.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.835.v2.git.1610055365.gitgitgadget@gmail.com>

From: Elijah Newren <newren@gmail.com>

This function roughly follows the same outline as the function of the
same name from merge-recursive.c, but the code diverges in multiple
ways due to some special considerations:
  * merge-ort's version needs to update opt->priv->paths with any new
    paths (and opt->priv->paths points to struct conflict_infos which
    track quite a bit of metadata for each path); merge-recursive's
    version would directly update the index
  * merge-ort requires that opt->priv->paths has any leading directories
    of any relevant files also be included in the set of paths.  And
    due to pointer equality requirements on merged_info.directory_name,
    we have to be careful how we compute and insert these.
  * due to the above requirements on opt->priv->paths, merge-ort's
    version starts with a long comment to explain all the special
    considerations that need to be handled
  * merge-ort can use the full data stored in opt->priv->paths to avoid
    making expensive get_tree_entry() calls to regather the necessary
    data.
  * due to messages being deferred automatically in merge-ort, this is
    the best place to handle conflict messages whereas in
    merge-recursive.c they are deferred manually so that processing of
    entries does all the printing

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 167 insertions(+), 1 deletion(-)

diff --git a/merge-ort.c b/merge-ort.c
index 19850ffc0ab..ea6a08e23e2 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -1224,7 +1224,173 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
 						 struct diff_filepair *pair,
 						 char *new_path)
 {
-	die("Not yet implemented.");
+	/*
+	 * The basic idea is to get the conflict_info from opt->priv->paths
+	 * at old path, and insert it into new_path; basically just this:
+	 *     ci = strmap_get(&opt->priv->paths, old_path);
+	 *     strmap_remove(&opt->priv->paths, old_path, 0);
+	 *     strmap_put(&opt->priv->paths, new_path, ci);
+	 * However, there are some factors complicating this:
+	 *     - opt->priv->paths may already have an entry at new_path
+	 *     - Each ci tracks its containing directory, so we need to
+	 *       update that
+	 *     - If another ci has the same containing directory, then
+	 *       the two char*'s MUST point to the same location.  See the
+	 *       comment in struct merged_info.  strcmp equality is not
+	 *       enough; we need pointer equality.
+	 *     - opt->priv->paths must hold the parent directories of any
+	 *       entries that are added.  So, if this directory rename
+	 *       causes entirely new directories, we must recursively add
+	 *       parent directories.
+	 *     - For each parent directory added to opt->priv->paths, we
+	 *       also need to get its parent directory stored in its
+	 *       conflict_info->merged.directory_name with all the same
+	 *       requirements about pointer equality.
+	 */
+	struct string_list dirs_to_insert = STRING_LIST_INIT_NODUP;
+	struct conflict_info *ci, *new_ci;
+	struct strmap_entry *entry;
+	const char *branch_with_new_path, *branch_with_dir_rename;
+	const char *old_path = pair->two->path;
+	const char *parent_name;
+	const char *cur_path;
+	int i, len;
+
+	entry = strmap_get_entry(&opt->priv->paths, old_path);
+	old_path = entry->key;
+	ci = entry->value;
+	VERIFY_CI(ci);
+
+	/* Find parent directories missing from opt->priv->paths */
+	cur_path = new_path;
+	while (1) {
+		/* Find the parent directory of cur_path */
+		char *last_slash = strrchr(cur_path, '/');
+		if (last_slash) {
+			parent_name = xstrndup(cur_path, last_slash - cur_path);
+		} else {
+			parent_name = opt->priv->toplevel_dir;
+			break;
+		}
+
+		/* Look it up in opt->priv->paths */
+		entry = strmap_get_entry(&opt->priv->paths, parent_name);
+		if (entry) {
+			free((char*)parent_name);
+			parent_name = entry->key; /* reuse known pointer */
+			break;
+		}
+
+		/* Record this is one of the directories we need to insert */
+		string_list_append(&dirs_to_insert, parent_name);
+		cur_path = parent_name;
+	}
+
+	/* Traverse dirs_to_insert and insert them into opt->priv->paths */
+	for (i = dirs_to_insert.nr-1; i >= 0; --i) {
+		struct conflict_info *dir_ci;
+		char *cur_dir = dirs_to_insert.items[i].string;
+
+		dir_ci = xcalloc(1, sizeof(*dir_ci));
+
+		dir_ci->merged.directory_name = parent_name;
+		len = strlen(parent_name);
+		/* len+1 because of trailing '/' character */
+		dir_ci->merged.basename_offset = (len > 0 ? len+1 : len);
+		dir_ci->dirmask = ci->filemask;
+		strmap_put(&opt->priv->paths, cur_dir, dir_ci);
+
+		parent_name = cur_dir;
+	}
+
+	/*
+	 * We are removing old_path from opt->priv->paths.  old_path also will
+	 * eventually need to be freed, but it may still be used by e.g.
+	 * ci->pathnames.  So, store it in another string-list for now.
+	 */
+	string_list_append(&opt->priv->paths_to_free, old_path);
+
+	assert(ci->filemask == 2 || ci->filemask == 4);
+	assert(ci->dirmask == 0);
+	strmap_remove(&opt->priv->paths, old_path, 0);
+
+	branch_with_new_path   = (ci->filemask == 2) ? opt->branch1 : opt->branch2;
+	branch_with_dir_rename = (ci->filemask == 2) ? opt->branch2 : opt->branch1;
+
+	/* Now, finally update ci and stick it into opt->priv->paths */
+	ci->merged.directory_name = parent_name;
+	len = strlen(parent_name);
+	ci->merged.basename_offset = (len > 0 ? len+1 : len);
+	new_ci = strmap_get(&opt->priv->paths, new_path);
+	if (!new_ci) {
+		/* Place ci back into opt->priv->paths, but at new_path */
+		strmap_put(&opt->priv->paths, new_path, ci);
+	} else {
+		int index;
+
+		/* A few sanity checks */
+		VERIFY_CI(new_ci);
+		assert(ci->filemask == 2 || ci->filemask == 4);
+		assert((new_ci->filemask & ci->filemask) == 0);
+		assert(!new_ci->merged.clean);
+
+		/* Copy stuff from ci into new_ci */
+		new_ci->filemask |= ci->filemask;
+		if (new_ci->dirmask)
+			new_ci->df_conflict = 1;
+		index = (ci->filemask >> 1);
+		new_ci->pathnames[index] = ci->pathnames[index];
+		new_ci->stages[index].mode = ci->stages[index].mode;
+		oidcpy(&new_ci->stages[index].oid, &ci->stages[index].oid);
+
+		free(ci);
+		ci = new_ci;
+	}
+
+	if (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) {
+		/* Notify user of updated path */
+		if (pair->status == 'A')
+			path_msg(opt, new_path, 1,
+				 _("Path updated: %s added in %s inside a "
+				   "directory that was renamed in %s; moving "
+				   "it to %s."),
+				 old_path, branch_with_new_path,
+				 branch_with_dir_rename, new_path);
+		else
+			path_msg(opt, new_path, 1,
+				 _("Path updated: %s renamed to %s in %s, "
+				   "inside a directory that was renamed in %s; "
+				   "moving it to %s."),
+				 pair->one->path, old_path, branch_with_new_path,
+				 branch_with_dir_rename, new_path);
+	} else {
+		/*
+		 * opt->detect_directory_renames has the value
+		 * MERGE_DIRECTORY_RENAMES_CONFLICT, so mark these as conflicts.
+		 */
+		ci->path_conflict = 1;
+		if (pair->status == 'A')
+			path_msg(opt, new_path, 0,
+				 _("CONFLICT (file location): %s added in %s "
+				   "inside a directory that was renamed in %s, "
+				   "suggesting it should perhaps be moved to "
+				   "%s."),
+				 old_path, branch_with_new_path,
+				 branch_with_dir_rename, new_path);
+		else
+			path_msg(opt, new_path, 0,
+				 _("CONFLICT (file location): %s renamed to %s "
+				   "in %s, inside a directory that was renamed "
+				   "in %s, suggesting it should perhaps be "
+				   "moved to %s."),
+				 pair->one->path, old_path, branch_with_new_path,
+				 branch_with_dir_rename, new_path);
+	}
+
+	/*
+	 * Finally, record the new location.
+	 */
+	pair->two->path = new_path;
 }
 
 /*** Function Grouping: functions related to regular rename detection ***/
-- 
gitgitgadget


  parent reply	other threads:[~2021-01-07 21:38 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-07 20:01 [PATCH 00/18] Add directory rename detection to merge-ort Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 01/18] merge-ort: add new data structures for directory rename detection Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 02/18] merge-ort: initialize and free new directory rename data structures Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 03/18] merge-ort: collect which directories are removed in dirs_removed Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 04/18] merge-ort: add outline for computing directory renames Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 05/18] merge-ort: add outline of get_provisional_directory_renames() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 06/18] merge-ort: copy get_renamed_dir_portion() from merge-recursive.c Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 07/18] merge-ort: implement compute_rename_counts() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 08/18] merge-ort: implement handle_directory_level_conflicts() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 09/18] merge-ort: modify collect_renames() for directory rename handling Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 10/18] merge-ort: implement compute_collisions() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 11/18] merge-ort: implement apply_dir_rename() and check_dir_renamed() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 12/18] merge-ort: implement check_for_directory_rename() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 13/18] merge-ort: implement handle_path_level_conflicts() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 14/18] merge-ort: add a new toplevel_dir field Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 15/18] merge-ort: implement apply_directory_rename_modifications() Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 16/18] merge-ort: process_renames() now needs more defensiveness Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 17/18] merge-ort: fix a directory rename detection bug Elijah Newren via GitGitGadget
2021-01-07 20:01 ` [PATCH 18/18] DO NOT SUBMIT: directory rename stuff for redo_after_renames Elijah Newren via GitGitGadget
2021-01-07 20:02   ` Elijah Newren
2021-01-07 21:35 ` [PATCH v2 00/17] Add directory rename detection to merge-ort Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 01/17] merge-ort: add new data structures for directory rename detection Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 02/17] merge-ort: initialize and free new directory rename data structures Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 03/17] merge-ort: collect which directories are removed in dirs_removed Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 04/17] merge-ort: add outline for computing directory renames Elijah Newren via GitGitGadget
2021-01-18 19:54     ` Taylor Blau
2021-01-18 20:15       ` Elijah Newren
2021-01-18 20:28         ` Taylor Blau
2021-01-07 21:35   ` [PATCH v2 05/17] merge-ort: add outline of get_provisional_directory_renames() Elijah Newren via GitGitGadget
2021-01-18 20:10     ` Taylor Blau
2021-01-18 20:34       ` Elijah Newren
2021-01-07 21:35   ` [PATCH v2 06/17] merge-ort: copy get_renamed_dir_portion() from merge-recursive.c Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 07/17] merge-ort: implement compute_rename_counts() Elijah Newren via GitGitGadget
2021-01-18 20:36     ` Taylor Blau
2021-01-18 20:41       ` Elijah Newren
2021-01-07 21:35   ` [PATCH v2 08/17] merge-ort: implement handle_directory_level_conflicts() Elijah Newren via GitGitGadget
2021-01-18 21:00     ` Taylor Blau
2021-01-18 21:36       ` Elijah Newren
2021-01-07 21:35   ` [PATCH v2 09/17] merge-ort: modify collect_renames() for directory rename handling Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 10/17] merge-ort: implement compute_collisions() Elijah Newren via GitGitGadget
2021-01-07 21:35   ` [PATCH v2 11/17] merge-ort: implement apply_dir_rename() and check_dir_renamed() Elijah Newren via GitGitGadget
2021-01-07 21:36   ` [PATCH v2 12/17] merge-ort: implement check_for_directory_rename() Elijah Newren via GitGitGadget
2021-01-07 21:36   ` [PATCH v2 13/17] merge-ort: implement handle_path_level_conflicts() Elijah Newren via GitGitGadget
2021-01-07 21:36   ` [PATCH v2 14/17] merge-ort: add a new toplevel_dir field Elijah Newren via GitGitGadget
2021-01-07 21:36   ` Elijah Newren via GitGitGadget [this message]
2021-01-07 21:36   ` [PATCH v2 16/17] merge-ort: process_renames() now needs more defensiveness Elijah Newren via GitGitGadget
2021-01-07 21:36   ` [PATCH v2 17/17] merge-ort: fix a directory rename detection bug Elijah Newren via GitGitGadget
2021-01-19 19:53   ` [PATCH v3 00/17] Add directory rename detection to merge-ort Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 01/17] merge-ort: add new data structures for directory rename detection Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 02/17] merge-ort: initialize and free new directory rename data structures Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 03/17] merge-ort: collect which directories are removed in dirs_removed Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 04/17] merge-ort: add outline for computing directory renames Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 05/17] merge-ort: add outline of get_provisional_directory_renames() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 06/17] merge-ort: copy get_renamed_dir_portion() from merge-recursive.c Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 07/17] merge-ort: implement compute_rename_counts() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 08/17] merge-ort: implement handle_directory_level_conflicts() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 09/17] merge-ort: modify collect_renames() for directory rename handling Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 10/17] merge-ort: implement compute_collisions() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 11/17] merge-ort: implement apply_dir_rename() and check_dir_renamed() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 12/17] merge-ort: implement check_for_directory_rename() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 13/17] merge-ort: implement handle_path_level_conflicts() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 14/17] merge-ort: add a new toplevel_dir field Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 15/17] merge-ort: implement apply_directory_rename_modifications() Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 16/17] merge-ort: process_renames() now needs more defensiveness Elijah Newren via GitGitGadget
2021-01-19 19:53     ` [PATCH v3 17/17] merge-ort: fix a directory rename detection bug Elijah Newren via GitGitGadget
2021-01-19 22:48     ` [PATCH v3 00/17] Add directory rename detection to merge-ort Taylor Blau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d74417e86c54ea03f5a7802a7e4fd2bc3a22f935.1610055365.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).