Skip to content

Commit fbcfc0c

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9d015d commit fbcfc0c

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

merge-ort.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,29 @@ struct collision_info {
736736
static char *apply_dir_rename(struct strmap_entry *rename_info,
737737
const char *old_path)
738738
{
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);
740762
}
741763

742764
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)
9801002
static struct strmap_entry *check_dir_renamed(const char *path,
9811003
struct strmap *dir_renames)
9821004
{
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;
9841017
}
9851018

9861019
static void compute_collisions(struct strmap *collisions,

0 commit comments

Comments
 (0)