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

[PATCH 08/10] diffcore-rename: add a new idx_possible_rename function

From
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 14, 2021, 07:59 UTC
Message-ID
<cbd055ab33998962cb7712906cdad5dff3390660.1613289544.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.844.git.1613289544.gitgitgadget@gmail.com>
From: Elijah Newren <newren@gmail.com>

find_basename_matches() is great when both the remaining set of possible rename sources and the remaining set of possible rename destinations have exactly one file each with a given basename. It allows us to match up files that have been moved to different directories without changing filenames.

When basenames are not unique, though, we want to be able to guess which directories the source files have been moved to. Since this is the job of directory rename detection, we employ it. However, since it is a directory rename detection idea, we also limit it to cases where we know there could have been a directory rename, i.e. where the source directory has been removed. This has to be signalled by dirs_removed being non-NULL and containing an entry for the relevant directory. Since merge-ort.c is the only caller that currently does so, this optimization is only effective for merge-ort right now. In the future, this condition could be reconsidered or we could modify other callers to pass the necessary strset.

Anyway, that's a lot of background so that we can actually describe the new function. Add an idx_possible_rename() function which combines the recently added dir_rename_guess and idx_map fields to provide the index within rename_dst of a potential match for a given file.

Future commits will add checks after calling this function to compare the resulting 'likely rename' candidates to see if the two files meet the elevated min_basename_score threshold for marking them as actual renames.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 diffcore-rename.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
diff --git a/diffcore-rename.c b/diffcore-rename.c
index d24f104aa81c..1e4a56adde2c 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -374,6 +374,12 @@ struct dir_rename_info {
 	unsigned setup;
 };
 
+static char *get_dirname(const char *filename)
+{
+	char *slash = strrchr(filename, '/');
+	return slash ? xstrndup(filename, slash-filename) : xstrdup("");
+}
+
 static void dirname_munge(char *filename)
 {
 	char *slash = strrchr(filename, '/');
@@ -651,6 +657,81 @@ static const char *get_basename(const char *filename)
 	return base ? base + 1 : filename;
 }
 
+MAYBE_UNUSED
+static int idx_possible_rename(char *filename, struct dir_rename_info *info)
+{
+	/*
+	 * Our comparison of files with the same basename (see
+	 * find_basename_matches() below), is only helpful when after exact
+	 * rename detection we have exactly one file with a given basename
+	 * among the rename sources and also only exactly one file with
+	 * that basename among the rename destinations.  When we have
+	 * multiple files with the same basename in either set, we do not
+	 * know which to compare against.  However, there are some
+	 * filenames that occur in large numbers (particularly
+	 * build-related filenames such as 'Makefile', '.gitignore', or
+	 * 'build.gradle' that potentially exist within every single
+	 * subdirectory), and for performance we want to be able to quickly
+	 * find renames for these files too.
+	 *
+	 * The reason basename comparisons are a useful heuristic was that it
+	 * is common for people to move files across directories while keeping
+	 * their filename the same.  If we had a way of determining or even
+	 * making a good educated guess about which directory these non-unique
+	 * basename files had moved the file to, we could check it.
+	 * Luckily...
+	 *
+	 * When an entire directory is in fact renamed, we have two factors
+	 * helping us out:
+	 *   (a) the original directory disappeared giving us a hint
+	 *       about when we can apply an extra heuristic.
+	 *   (a) we often have several files within that directory and
+	 *       subdirectories that are renamed without changes
+	 * So, rules for a heuristic:
+	 *   (0) If there basename matches are non-unique (the condition under
+	 *       which this function is called) AND
+	 *   (1) the directory in which the file was found has disappeared
+	 *       (i.e. dirs_removed is non-NULL and has a relevant entry) THEN
+	 *   (2) use exact renames of files within the directory to determine
+	 *       where the directory is likely to have been renamed to.  IF
+	 *       there is at least one exact rename from within that
+	 *       directory, we can proceed.
+	 *   (3) If there are multiple places the directory could have been
+	 *       renamed to based on exact renames, ignore all but one of them.
+	 *       Just use the destination with the most renames going to it.
+	 *   (4) Check if applying that directory rename to the original file
+	 *       would result in a destination filename that is in the
+	 *       potential rename set.  If so, return the index of the
+	 *       destination file (the index within rename_dst).
+	 *   (5) Compare the original file and returned destination for
+	 *       similarity, and if they are sufficiently similar, record the
+	 *       rename.
+	 *
+	 * This function, idx_possible_rename(), is only responsible for (4).
+	 * The conditions/steps in (1)-(3) are handled via setting up
+	 * dir_rename_count and dir_rename_guess in
+	 * initialize_dir_rename_info().  Steps (0) and (5) are handled by
+	 * the caller of this function.
+	 */
+	char *old_dir, *new_dir, *new_path;
+	int idx;
+
+	if (!info->setup)
+		return -1;
+
+	old_dir = get_dirname(filename);
+	new_dir = strmap_get(&info->dir_rename_guess, old_dir);
+	free(old_dir);
+	if (!new_dir)
+		return -1;
+
+	new_path = xstrfmt("%s/%s", new_dir, get_basename(filename));
+
+	idx = strintmap_get(&info->idx_map, new_path);
+	free(new_path);
+	return idx;
+}
+
 static int find_basename_matches(struct diff_options *options,
 				 int minimum_score,
 				 struct dir_rename_info *info,
-- 
gitgitgadget
Previous: Elijah Newren via GitGitGadgetNext: Elijah Newren via GitGitGadget
Message 8 of 61 in “Optimization batch 8: use file basenames even more”
  1. 00/10 Optimization batch 8: use file basenames even moreElijah Newren via GitGitGadget, Feb 14, 2021
  2. 02/10 diffcore-rename: add functions for clearing dir_rename_countElijah Newren via GitGitGadget, Feb 14, 2021
  3. 01/10 Move computation of dir_rename_count from merge-ort to diffcore-renameElijah Newren via GitGitGadget, Feb 14, 2021
  4. 03/10 diffcore-rename: move dir_rename_counts into a dir_rename_info structElijah Newren via GitGitGadget, Feb 14, 2021
  5. 04/10 diffcore-rename: extend cleanup_dir_rename_info()Elijah Newren via GitGitGadget, Feb 14, 2021
  6. 05/10 diffcore-rename: compute dir_rename_counts in stagesElijah Newren via GitGitGadget, Feb 14, 2021
  7. 06/10 diffcore-rename: add a mapping of destination names to their indicesElijah Newren via GitGitGadget, Feb 14, 2021
  8. 08/10 diffcore-rename: add a new idx_possible_rename functionElijah Newren via GitGitGadget, Feb 14, 2021
  9. 07/10 diffcore-rename: add a dir_rename_guess field to dir_rename_infoElijah Newren via GitGitGadget, Feb 14, 2021
  10. 10/10 diffcore-rename: use directory rename guided basename comparisonsElijah Newren via GitGitGadget, Feb 14, 2021
  11. 09/10 diffcore-rename: limit dir_rename_counts computation to relevant dirsElijah Newren via GitGitGadget, Feb 14, 2021
  12. 00/10 Optimization batch 8: use file basenames even moreElijah Newren via GitGitGadget, Feb 23, 2021
  13. 03/10 diffcore-rename: move dir_rename_counts into a dir_rename_info structElijah Newren via GitGitGadget, Feb 23, 2021
  14. 02/10 diffcore-rename: add functions for clearing dir_rename_countElijah Newren via GitGitGadget, Feb 23, 2021
  15. 01/10 Move computation of dir_rename_count from merge-ort to diffcore-renameElijah Newren via GitGitGadget, Feb 23, 2021
  16. Derrick StoleeFeb 24, 2021
  17. Elijah NewrenFeb 24, 2021
  18. 04/10 diffcore-rename: extend cleanup_dir_rename_info()Elijah Newren via GitGitGadget, Feb 23, 2021
  19. Derrick StoleeFeb 24, 2021
  20. Ævar Arnfjörð BjarmasonFeb 25, 2021
  21. Ævar Arnfjörð BjarmasonFeb 25, 2021
  22. Junio C HamanoFeb 25, 2021
  23. 08/10 diffcore-rename: add a new idx_possible_rename functionElijah Newren via GitGitGadget, Feb 23, 2021
  24. Derrick StoleeFeb 24, 2021
  25. Elijah NewrenFeb 25, 2021
  26. 06/10 diffcore-rename: add a mapping of destination names to their indicesElijah Newren via GitGitGadget, Feb 23, 2021
  27. 10/10 diffcore-rename: use directory rename guided basename comparisonsElijah Newren via GitGitGadget, Feb 23, 2021
  28. Derrick StoleeFeb 24, 2021
  29. 09/10 diffcore-rename: limit dir_rename_counts computation to relevant dirsElijah Newren via GitGitGadget, Feb 23, 2021
  30. 07/10 diffcore-rename: add a dir_rename_guess field to dir_rename_infoElijah Newren via GitGitGadget, Feb 23, 2021
  31. 05/10 diffcore-rename: compute dir_rename_counts in stagesElijah Newren via GitGitGadget, Feb 23, 2021
  32. Derrick StoleeFeb 24, 2021
  33. Derrick StoleeFeb 24, 2021
  34. Elijah NewrenFeb 25, 2021
  35. 00/10 Optimization batch 8: use file basenames even moreElijah Newren via GitGitGadget, Feb 26, 2021
  36. 01/10 diffcore-rename: use directory rename guided basename comparisonsElijah Newren via GitGitGadget, Feb 26, 2021
  37. 02/10 diffcore-rename: add a new idx_possible_rename functionElijah Newren via GitGitGadget, Feb 26, 2021
  38. Derrick StoleeFeb 26, 2021
  39. 03/10 diffcore-rename: add a mapping of destination names to their indicesElijah Newren via GitGitGadget, Feb 26, 2021
  40. 04/10 Move computation of dir_rename_count from merge-ort to diffcore-renameElijah Newren via GitGitGadget, Feb 26, 2021
  41. Derrick StoleeFeb 26, 2021
  42. 05/10 diffcore-rename: add function for clearing dir_rename_countElijah Newren via GitGitGadget, Feb 26, 2021
  43. 06/10 diffcore-rename: move dir_rename_counts into dir_rename_info structElijah Newren via GitGitGadget, Feb 26, 2021
  44. 07/10 diffcore-rename: extend cleanup_dir_rename_info()Elijah Newren via GitGitGadget, Feb 26, 2021
  45. 09/10 diffcore-rename: limit dir_rename_counts computation to relevant dirsElijah Newren via GitGitGadget, Feb 26, 2021
  46. 10/10 diffcore-rename: compute dir_rename_guess from dir_rename_countsElijah Newren via GitGitGadget, Feb 26, 2021
  47. 08/10 diffcore-rename: compute dir_rename_counts in stagesElijah Newren via GitGitGadget, Feb 26, 2021
  48. Derrick StoleeFeb 26, 2021
  49. Elijah NewrenFeb 26, 2021
  50. 00/10 Optimization batch 8: use file basenames even moreElijah Newren via GitGitGadget, Feb 27, 2021
  51. 03/10 diffcore-rename: add a mapping of destination names to their indicesElijah Newren via GitGitGadget, Feb 27, 2021
  52. 02/10 diffcore-rename: provide basic implementation of idx_possible_rename()Elijah Newren via GitGitGadget, Feb 27, 2021
  53. 01/10 diffcore-rename: use directory rename guided basename comparisonsElijah Newren via GitGitGadget, Feb 27, 2021
  54. 04/10 Move computation of dir_rename_count from merge-ort to diffcore-renameElijah Newren via GitGitGadget, Feb 27, 2021
  55. 05/10 diffcore-rename: add function for clearing dir_rename_countElijah Newren via GitGitGadget, Feb 27, 2021
  56. 06/10 diffcore-rename: move dir_rename_counts into dir_rename_info structElijah Newren via GitGitGadget, Feb 27, 2021
  57. 07/10 diffcore-rename: extend cleanup_dir_rename_info()Elijah Newren via GitGitGadget, Feb 27, 2021
  58. 09/10 diffcore-rename: limit dir_rename_counts computation to relevant dirsElijah Newren via GitGitGadget, Feb 27, 2021
  59. 08/10 diffcore-rename: compute dir_rename_counts in stagesElijah Newren via GitGitGadget, Feb 27, 2021
  60. 10/10 diffcore-rename: compute dir_rename_guess from dir_rename_countsElijah Newren via GitGitGadget, Feb 27, 2021
  61. Derrick StoleeMar 9, 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.