Skip to content

Commit 62bfa11

Browse files
johnkeepinggitster
authored andcommitted
fast-import: allow moving the root tree
Because fast-import.c::tree_content_remove does not check for the empty path, it is not possible to move the root tree to a subdirectory. Instead the error "Path not in branch" is produced (note the double space where the empty path has been inserted). Fix this by explicitly checking for the empty path and handling it. Signed-off-by: John Keeping <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0eb6b9 commit 62bfa11

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

fast-import.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,8 @@ static int tree_content_set(
15681568
static int tree_content_remove(
15691569
struct tree_entry *root,
15701570
const char *p,
1571-
struct tree_entry *backup_leaf)
1571+
struct tree_entry *backup_leaf,
1572+
int allow_root)
15721573
{
15731574
struct tree_content *t;
15741575
const char *slash1;
@@ -1583,6 +1584,12 @@ static int tree_content_remove(
15831584

15841585
if (!root->tree)
15851586
load_tree(root);
1587+
1588+
if (!*p && allow_root) {
1589+
e = root;
1590+
goto del_entry;
1591+
}
1592+
15861593
t = root->tree;
15871594
for (i = 0; i < t->entry_count; i++) {
15881595
e = t->entries[i];
@@ -1599,7 +1606,7 @@ static int tree_content_remove(
15991606
goto del_entry;
16001607
if (!e->tree)
16011608
load_tree(e);
1602-
if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1609+
if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
16031610
for (n = 0; n < e->tree->entry_count; n++) {
16041611
if (e->tree->entries[n]->versions[1].mode) {
16051612
hashclr(root->versions[1].sha1);
@@ -2188,7 +2195,7 @@ static uintmax_t do_change_note_fanout(
21882195
}
21892196

21902197
/* Rename fullpath to realpath */
2191-
if (!tree_content_remove(orig_root, fullpath, &leaf))
2198+
if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
21922199
die("Failed to remove path %s", fullpath);
21932200
tree_content_set(orig_root, realpath,
21942201
leaf.versions[1].sha1,
@@ -2323,7 +2330,7 @@ static void file_change_m(struct branch *b)
23232330

23242331
/* Git does not track empty, non-toplevel directories. */
23252332
if (S_ISDIR(mode) && !memcmp(sha1, EMPTY_TREE_SHA1_BIN, 20) && *p) {
2326-
tree_content_remove(&b->branch_tree, p, NULL);
2333+
tree_content_remove(&b->branch_tree, p, NULL, 0);
23272334
return;
23282335
}
23292336

@@ -2384,7 +2391,7 @@ static void file_change_d(struct branch *b)
23842391
die("Garbage after path in: %s", command_buf.buf);
23852392
p = uq.buf;
23862393
}
2387-
tree_content_remove(&b->branch_tree, p, NULL);
2394+
tree_content_remove(&b->branch_tree, p, NULL, 1);
23882395
}
23892396

23902397
static void file_change_cr(struct branch *b, int rename)
@@ -2422,7 +2429,7 @@ static void file_change_cr(struct branch *b, int rename)
24222429

24232430
memset(&leaf, 0, sizeof(leaf));
24242431
if (rename)
2425-
tree_content_remove(&b->branch_tree, s, &leaf);
2432+
tree_content_remove(&b->branch_tree, s, &leaf, 1);
24262433
else
24272434
tree_content_get(&b->branch_tree, s, &leaf, 1);
24282435
if (!leaf.versions[1].mode)
@@ -2530,7 +2537,7 @@ static void note_change_n(struct branch *b, unsigned char *old_fanout)
25302537
}
25312538

25322539
construct_path_with_fanout(sha1_to_hex(commit_sha1), *old_fanout, path);
2533-
if (tree_content_remove(&b->branch_tree, path, NULL))
2540+
if (tree_content_remove(&b->branch_tree, path, NULL, 0))
25342541
b->num_notes--;
25352542

25362543
if (is_null_sha1(sha1))

t/t9300-fast-import.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ cat >expect <<EOF
10501050
:100755 100755 e74b7d465e52746be2b4bae983670711e6e66657 e74b7d465e52746be2b4bae983670711e6e66657 R100 newdir/exec.sh sub/newdir/exec.sh
10511051
:100644 100644 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 R100 newdir/interesting sub/newdir/interesting
10521052
EOF
1053-
test_expect_failure \
1053+
test_expect_success \
10541054
'M: rename root to subdirectory' \
10551055
'git fast-import <input &&
10561056
git diff-tree -M -r M4^ M4 >actual &&

0 commit comments

Comments
 (0)