Skip to content

Commit 8fb3ad7

Browse files
divanoramagitster
authored andcommitted
fast-import: prevent producing bad delta
To produce deltas for tree objects fast-import tracks two versions of tree's entries - base and current one. Base version stands both for a delta base of this tree, and for a entry inside a delta base of a parent tree. So care should be taken to keep it in sync. tree_content_set cuts away a whole subtree and replaces it with a new one (or NULL for lazy load of a tree with known sha1). It keeps a base sha1 for this subtree (needed for parent tree). And here is the problem, 'subtree' tree root doesn't have the implied base version entries. Adjusting the subtree to include them would mean a deep rewrite of subtree. Invalidating the subtree base version would mean recursive invalidation of parents' base versions. So just mark this tree as do-not-delta me. Abuse setuid bit for this purpose. tree_content_replace is the same as tree_content_set except that is is used to replace the root, so just clearing base sha1 here (instead of setting the bit) is fine. [di: log message] Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Dmitry Ivankov <divanorama@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9a0edb7 commit 8fb3ad7

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

fast-import.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ Format of STDIN stream:
170170
#define DEPTH_BITS 13
171171
#define MAX_DEPTH ((1<<DEPTH_BITS)-1)
172172

173+
/*
174+
* We abuse the setuid bit on directories to mean "do not delta".
175+
*/
176+
#define NO_DELTA S_ISUID
177+
173178
struct object_entry {
174179
struct pack_idx_entry idx;
175180
struct object_entry *next;
@@ -1414,8 +1419,9 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
14141419
struct tree_entry *e = t->entries[i];
14151420
if (!e->versions[v].mode)
14161421
continue;
1417-
strbuf_addf(b, "%o %s%c", (unsigned int)e->versions[v].mode,
1418-
e->name->str_dat, '\0');
1422+
strbuf_addf(b, "%o %s%c",
1423+
(unsigned int)(e->versions[v].mode & ~NO_DELTA),
1424+
e->name->str_dat, '\0');
14191425
strbuf_add(b, e->versions[v].sha1, 20);
14201426
}
14211427
}
@@ -1425,7 +1431,7 @@ static void store_tree(struct tree_entry *root)
14251431
struct tree_content *t = root->tree;
14261432
unsigned int i, j, del;
14271433
struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
1428-
struct object_entry *le;
1434+
struct object_entry *le = NULL;
14291435

14301436
if (!is_null_sha1(root->versions[1].sha1))
14311437
return;
@@ -1435,7 +1441,8 @@ static void store_tree(struct tree_entry *root)
14351441
store_tree(t->entries[i]);
14361442
}
14371443

1438-
le = find_object(root->versions[0].sha1);
1444+
if (!(root->versions[0].mode & NO_DELTA))
1445+
le = find_object(root->versions[0].sha1);
14391446
if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
14401447
mktree(t, 0, &old_tree);
14411448
lo.data = old_tree;
@@ -1469,6 +1476,7 @@ static void tree_content_replace(
14691476
{
14701477
if (!S_ISDIR(mode))
14711478
die("Root cannot be a non-directory");
1479+
hashclr(root->versions[0].sha1);
14721480
hashcpy(root->versions[1].sha1, sha1);
14731481
if (root->tree)
14741482
release_tree_content_recursive(root->tree);
@@ -1513,6 +1521,23 @@ static int tree_content_set(
15131521
if (e->tree)
15141522
release_tree_content_recursive(e->tree);
15151523
e->tree = subtree;
1524+
1525+
/*
1526+
* We need to leave e->versions[0].sha1 alone
1527+
* to avoid modifying the preimage tree used
1528+
* when writing out the parent directory.
1529+
* But after replacing the subdir with a
1530+
* completely different one, it's not a good
1531+
* delta base any more, and besides, we've
1532+
* thrown away the tree entries needed to
1533+
* make a delta against it.
1534+
*
1535+
* So let's just explicitly disable deltas
1536+
* for the subtree.
1537+
*/
1538+
if (S_ISDIR(e->versions[0].mode))
1539+
e->versions[0].mode |= NO_DELTA;
1540+
15161541
hashclr(root->versions[1].sha1);
15171542
return 1;
15181543
}
@@ -2927,7 +2952,7 @@ static void print_ls(int mode, const unsigned char *sha1, const char *path)
29272952
/* mode SP type SP object_name TAB path LF */
29282953
strbuf_reset(&line);
29292954
strbuf_addf(&line, "%06o %s %s\t",
2930-
mode, type, sha1_to_hex(sha1));
2955+
mode & ~NO_DELTA, type, sha1_to_hex(sha1));
29312956
quote_c_style(path, &line, NULL, 0);
29322957
strbuf_addch(&line, '\n');
29332958
}

t/t9300-fast-import.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ g/b/f
765765
g/b/h
766766
EOF
767767

768-
test_expect_failure \
768+
test_expect_success \
769769
'L: nested tree copy does not corrupt deltas' \
770770
'git fast-import <input &&
771771
git ls-tree L2 g/b/ >tmp &&

0 commit comments

Comments
 (0)