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>, Taylor Blau <me@ttaylorr.com>,
	Elijah Newren <newren@gmail.com>,
	Elijah Newren <newren@gmail.com>,
	Elijah Newren <newren@gmail.com>
Subject: [PATCH v3 06/17] merge-ort: copy get_renamed_dir_portion() from merge-recursive.c
Date: Tue, 19 Jan 2021 19:53:42 +0000	[thread overview]
Message-ID: <54c1850570659b4c819a06ff5c14651c8676cfa4.1611086033.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.835.v3.git.1611086033.gitgitgadget@gmail.com>

From: Elijah Newren <newren@gmail.com>

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

diff --git a/merge-ort.c b/merge-ort.c
index 2a1c62c533b..eb609ab0063 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -721,6 +721,110 @@ static int handle_content_merge(struct merge_options *opt,
 
 /*** Function Grouping: functions related to directory rename detection ***/
 
+MAYBE_UNUSED
+static void get_renamed_dir_portion(const char *old_path, const char *new_path,
+				    char **old_dir, char **new_dir)
+{
+	char *end_of_old, *end_of_new;
+
+	/* Default return values: NULL, meaning no rename */
+	*old_dir = NULL;
+	*new_dir = NULL;
+
+	/*
+	 * For
+	 *    "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
+	 * the "e/foo.c" part is the same, we just want to know that
+	 *    "a/b/c/d" was renamed to "a/b/some/thing/else"
+	 * so, for this example, this function returns "a/b/c/d" in
+	 * *old_dir and "a/b/some/thing/else" in *new_dir.
+	 */
+
+	/*
+	 * If the basename of the file changed, we don't care.  We want
+	 * to know which portion of the directory, if any, changed.
+	 */
+	end_of_old = strrchr(old_path, '/');
+	end_of_new = strrchr(new_path, '/');
+
+	/*
+	 * If end_of_old is NULL, old_path wasn't in a directory, so there
+	 * could not be a directory rename (our rule elsewhere that a
+	 * directory which still exists is not considered to have been
+	 * renamed means the root directory can never be renamed -- because
+	 * the root directory always exists).
+	 */
+	if (end_of_old == NULL)
+		return; /* Note: *old_dir and *new_dir are still NULL */
+
+	/*
+	 * If new_path contains no directory (end_of_new is NULL), then we
+	 * have a rename of old_path's directory to the root directory.
+	 */
+	if (end_of_new == NULL) {
+		*old_dir = xstrndup(old_path, end_of_old - old_path);
+		*new_dir = xstrdup("");
+		return;
+	}
+
+	/* Find the first non-matching character traversing backwards */
+	while (*--end_of_new == *--end_of_old &&
+	       end_of_old != old_path &&
+	       end_of_new != new_path)
+		; /* Do nothing; all in the while loop */
+
+	/*
+	 * If both got back to the beginning of their strings, then the
+	 * directory didn't change at all, only the basename did.
+	 */
+	if (end_of_old == old_path && end_of_new == new_path &&
+	    *end_of_old == *end_of_new)
+		return; /* Note: *old_dir and *new_dir are still NULL */
+
+	/*
+	 * If end_of_new got back to the beginning of its string, and
+	 * end_of_old got back to the beginning of some subdirectory, then
+	 * we have a rename/merge of a subdirectory into the root, which
+	 * needs slightly special handling.
+	 *
+	 * Note: There is no need to consider the opposite case, with a
+	 * rename/merge of the root directory into some subdirectory
+	 * because as noted above the root directory always exists so it
+	 * cannot be considered to be renamed.
+	 */
+	if (end_of_new == new_path &&
+	    end_of_old != old_path && end_of_old[-1] == '/') {
+		*old_dir = xstrndup(old_path, --end_of_old - old_path);
+		*new_dir = xstrdup("");
+		return;
+	}
+
+	/*
+	 * We've found the first non-matching character in the directory
+	 * paths.  That means the current characters we were looking at
+	 * were part of the first non-matching subdir name going back from
+	 * the end of the strings.  Get the whole name by advancing both
+	 * end_of_old and end_of_new to the NEXT '/' character.  That will
+	 * represent the entire directory rename.
+	 *
+	 * The reason for the increment is cases like
+	 *    a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
+	 * After dropping the basename and going back to the first
+	 * non-matching character, we're now comparing:
+	 *    a/b/s          and         a/b/
+	 * and we want to be comparing:
+	 *    a/b/star/      and         a/b/tar/
+	 * but without the pre-increment, the one on the right would stay
+	 * a/b/.
+	 */
+	end_of_old = strchr(++end_of_old, '/');
+	end_of_new = strchr(++end_of_new, '/');
+
+	/* Copy the old and new directories into *old_dir and *new_dir. */
+	*old_dir = xstrndup(old_path, end_of_old - old_path);
+	*new_dir = xstrndup(new_path, end_of_new - new_path);
+}
+
 static void compute_rename_counts(struct diff_queue_struct *pairs,
 				  struct strmap *dir_rename_count,
 				  struct strset *dirs_removed)
-- 
gitgitgadget


  parent reply	other threads:[~2021-01-19 19:57 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   ` [PATCH v2 15/17] merge-ort: implement apply_directory_rename_modifications() Elijah Newren via GitGitGadget
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     ` Elijah Newren via GitGitGadget [this message]
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=54c1850570659b4c819a06ff5c14651c8676cfa4.1611086033.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.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).