merge-ort: implement apply_dir_rename() and check_dir_renamed()

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jan 19, 2021 at 19:53 UTC fbcfc0cc17019da292c4aeef0fe43a8980207beb
1 file changed +35 -2
merge-ort.c
+35 -2
@@ -736,7 +736,29 @@ struct collision_info {
736 static char *apply_dir_rename(struct strmap_entry *rename_info,
737 const char *old_path)
738 {
739 - die("Not yet implemented!");
739 + struct strbuf new_path = STRBUF_INIT;
740 + const char *old_dir = rename_info->key;
741 + const char *new_dir = rename_info->value;
742 + int oldlen, newlen, new_dir_len;
743 +
744 + oldlen = strlen(old_dir);
745 + if (*new_dir == '\0')
746 + /*
747 + * If someone renamed/merged a subdirectory into the root
748 + * directory (e.g. 'some/subdir' -> ''), then we want to
749 + * avoid returning
750 + * '' + '/filename'
751 + * as the rename; we need to make old_path + oldlen advance
752 + * past the '/' character.
753 + */
754 + oldlen++;
755 + new_dir_len = strlen(new_dir);
756 + newlen = new_dir_len + (strlen(old_path) - oldlen) + 1;
757 + strbuf_grow(&new_path, newlen);
758 + strbuf_add(&new_path, new_dir, new_dir_len);
759 + strbuf_addstr(&new_path, &old_path[oldlen]);
760 +
761 + return strbuf_detach(&new_path, NULL);
762 }
763
764 static void get_renamed_dir_portion(const char *old_path, const char *new_path,
@@ -980,7 +1002,18 @@ static void handle_directory_level_conflicts(struct merge_options *opt)
1002 static struct strmap_entry *check_dir_renamed(const char *path,
1003 struct strmap *dir_renames)
1004 {
983 - die("Not yet implemented!");
1005 + char *temp = xstrdup(path);
1006 + char *end;
1007 + struct strmap_entry *e = NULL;
1008 +
1009 + while ((end = strrchr(temp, '/'))) {
1010 + *end = '\0';
1011 + e = strmap_get_entry(dir_renames, temp);
1012 + if (e)
1013 + break;
1014 + }
1015 + free(temp);
1016 + return e;
1017 }
1018
1019 static void compute_collisions(struct strmap *collisions,