fast-import: filemodify after M 040000 <tree> "" crashes
Until M 040000 <tree> "" syntax was introduced in commit 2794ad5 (fast-import: Allow filemodify to set the root, 2010-10-10), it was impossible for the root entry to refer to an unloaded tree. Update various functions to take that possibility into account. Otherwise M 040000 <tree> "" M 100644 :1 "foo" and similar commands (using D, C, or R after resetting the root tree) segfault. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Oct 17, 2010 at 20:03 UTC
5edde510181357b0d0376d5542ddfa51a7e7ba12
2 files changed
+53
-4
fast-import.c
+16
-4
@@ -1444,7 +1444,7 @@ static int tree_content_set(
1444
const uint16_t mode,
1445
struct tree_content *subtree)
1446
{
1447
- struct tree_content *t = root->tree;
1447
+ struct tree_content *t;
1448
const char *slash1;
1449
unsigned int i, n;
1450
struct tree_entry *e;
@@ -1468,6 +1468,9 @@ static int tree_content_set(
1468
if (!slash1 && !S_ISDIR(mode) && subtree)
1469
die("Non-directories cannot have subtrees");
1470
1471
+ if (!root->tree)
1472
+ load_tree(root);
1473
+ t = root->tree;
1474
for (i = 0; i < t->entry_count; i++) {
1475
e = t->entries[i];
1476
if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
@@ -1523,7 +1526,7 @@ static int tree_content_remove(
1526
const char *p,
1527
struct tree_entry *backup_leaf)
1528
{
1526
- struct tree_content *t = root->tree;
1529
+ struct tree_content *t;
1530
const char *slash1;
1531
unsigned int i, n;
1532
struct tree_entry *e;
@@ -1534,6 +1537,9 @@ static int tree_content_remove(
1537
else
1538
n = strlen(p);
1539
1540
+ if (!root->tree)
1541
+ load_tree(root);
1542
+ t = root->tree;
1543
for (i = 0; i < t->entry_count; i++) {
1544
e = t->entries[i];
1545
if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
@@ -1581,7 +1587,7 @@ static int tree_content_get(
1587
const char *p,
1588
struct tree_entry *leaf)
1589
{
1584
- struct tree_content *t = root->tree;
1590
+ struct tree_content *t;
1591
const char *slash1;
1592
unsigned int i, n;
1593
struct tree_entry *e;
@@ -1592,6 +1598,9 @@ static int tree_content_get(
1598
else
1599
n = strlen(p);
1600
1601
+ if (!root->tree)
1602
+ load_tree(root);
1603
+ t = root->tree;
1604
for (i = 0; i < t->entry_count; i++) {
1605
e = t->entries[i];
1606
if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
@@ -2056,13 +2065,16 @@ static uintmax_t do_change_note_fanout(
2065
char *fullpath, unsigned int fullpath_len,
2066
unsigned char fanout)
2067
{
2059
- struct tree_content *t = root->tree;
2068
+ struct tree_content *t;
2069
struct tree_entry *e, leaf;
2070
unsigned int i, tmp_hex_sha1_len, tmp_fullpath_len;
2071
uintmax_t num_notes = 0;
2072
unsigned char sha1[20];
2073
char realpath[60];
2074
2075
+ if (!root->tree);
2076
+ load_tree(root);
2077
+ t = root->tree;
2078
for (i = 0; t && i < t->entry_count; i++) {
2079
e = t->entries[i];
2080
tmp_hex_sha1_len = hex_sha1_len + e->name->str_len;
t/t9300-fast-import.sh
+37
@@ -928,6 +928,43 @@ test_expect_success \
928
git diff-tree -C --find-copies-harder -r N5^^ N5 >actual &&
929
compare_diff_raw expect actual'
930
931
+test_expect_success \
932
+ 'N: copy to root by id and modify' \
933
+ 'echo "hello, world" >expect.foo &&
934
+ echo hello >expect.bar &&
935
+ git fast-import <<-SETUP_END &&
936
+ commit refs/heads/N7
937
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
938
+ data <<COMMIT
939
+ hello, tree
940
+ COMMIT
941
+
942
+ deleteall
943
+ M 644 inline foo/bar
944
+ data <<EOF
945
+ hello
946
+ EOF
947
+ SETUP_END
948
+
949
+ tree=$(git rev-parse --verify N7:) &&
950
+ git fast-import <<-INPUT_END &&
951
+ commit refs/heads/N8
952
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
953
+ data <<COMMIT
954
+ copy to root by id and modify
955
+ COMMIT
956
+
957
+ M 040000 $tree ""
958
+ M 644 inline foo/foo
959
+ data <<EOF
960
+ hello, world
961
+ EOF
962
+ INPUT_END
963
+ git show N8:foo/foo >actual.foo &&
964
+ git show N8:foo/bar >actual.bar &&
965
+ test_cmp expect.foo actual.foo &&
966
+ test_cmp expect.bar actual.bar'
967
+
968
###
969
### series O
970
###