Skip to content

Commit 3cc75c1

Browse files
committed
Merge branch 'nd/cache-tree-ita'
"git add -N dir/file && git write-tree" produced an incorrect tree when there are other paths in the same directory that sorts after "file". * nd/cache-tree-ita: cache-tree: do not generate empty trees as a result of all i-t-a subentries cache-tree.c: fix i-t-a entry skipping directory updates sometimes test-lib.sh: introduce and use $EMPTY_BLOB test-lib.sh: introduce and use $EMPTY_TREE
2 parents 0d54ad9 + 6d6a782 commit 3cc75c1

15 files changed

+87
-53
lines changed

cache-tree.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,13 @@ static int update_one(struct cache_tree *it,
319319
i = 0;
320320
while (i < entries) {
321321
const struct cache_entry *ce = cache[i];
322-
struct cache_tree_sub *sub;
322+
struct cache_tree_sub *sub = NULL;
323323
const char *path, *slash;
324324
int pathlen, entlen;
325325
const unsigned char *sha1;
326326
unsigned mode;
327327
int expected_missing = 0;
328+
int contains_ita = 0;
328329

329330
path = ce->name;
330331
pathlen = ce_namelen(ce);
@@ -341,7 +342,8 @@ static int update_one(struct cache_tree *it,
341342
i += sub->count;
342343
sha1 = sub->cache_tree->sha1;
343344
mode = S_IFDIR;
344-
if (sub->cache_tree->entry_count < 0) {
345+
contains_ita = sub->cache_tree->entry_count < 0;
346+
if (contains_ita) {
345347
to_invalidate = 1;
346348
expected_missing = 1;
347349
}
@@ -375,11 +377,17 @@ static int update_one(struct cache_tree *it,
375377
* they are not part of generated trees. Invalidate up
376378
* to root to force cache-tree users to read elsewhere.
377379
*/
378-
if (ce_intent_to_add(ce)) {
380+
if (!sub && ce_intent_to_add(ce)) {
379381
to_invalidate = 1;
380382
continue;
381383
}
382384

385+
/*
386+
* "sub" can be an empty tree if all subentries are i-t-a.
387+
*/
388+
if (contains_ita && !hashcmp(sha1, EMPTY_TREE_SHA1_BIN))
389+
continue;
390+
383391
strbuf_grow(&buffer, entlen + 100);
384392
strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
385393
strbuf_add(&buffer, sha1, 20);

t/t0000-basic.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ test_expect_success 'git write-tree should be able to write an empty tree' '
834834
'
835835

836836
test_expect_success 'validate object ID of a known tree' '
837-
test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
837+
test "$tree" = $EMPTY_TREE
838838
'
839839

840840
# Various types of objects

t/t1011-read-tree-sparse-checkout.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ test_description='sparse checkout tests
1515
. "$TEST_DIRECTORY"/lib-read-tree.sh
1616

1717
test_expect_success 'setup' '
18-
cat >expected <<-\EOF &&
18+
cat >expected <<-EOF &&
1919
100644 77f0ba1734ed79d12881f81b36ee134de6a3327b 0 init.t
20-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 sub/added
21-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 sub/addedtoo
22-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 subsub/added
20+
100644 $EMPTY_BLOB 0 sub/added
21+
100644 $EMPTY_BLOB 0 sub/addedtoo
22+
100644 $EMPTY_BLOB 0 subsub/added
2323
EOF
2424
cat >expected.swt <<-\EOF &&
2525
H init.t

t/t1100-commit-tree-options.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Also make sure that command line parser understands the normal
1515
. ./test-lib.sh
1616

1717
cat >expected <<EOF
18-
tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
18+
tree $EMPTY_TREE
1919
author Author Name <author@email> 1117148400 +0000
2020
committer Committer Name <committer@email> 1117150200 +0000
2121

t/t1700-split-index.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ test_expect_success 'add one file' '
3333
git update-index --add one &&
3434
git ls-files --stage >ls-files.actual &&
3535
cat >ls-files.expect <<EOF &&
36-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
36+
100644 $EMPTY_BLOB 0 one
3737
EOF
3838
test_cmp ls-files.expect ls-files.actual &&
3939
4040
test-dump-split-index .git/index | sed "/^own/d" >actual &&
4141
cat >expect <<EOF &&
4242
base $base
43-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
43+
100644 $EMPTY_BLOB 0 one
4444
replacements:
4545
deletions:
4646
EOF
@@ -51,7 +51,7 @@ test_expect_success 'disable split index' '
5151
git update-index --no-split-index &&
5252
git ls-files --stage >ls-files.actual &&
5353
cat >ls-files.expect <<EOF &&
54-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
54+
100644 $EMPTY_BLOB 0 one
5555
EOF
5656
test_cmp ls-files.expect ls-files.actual &&
5757
@@ -67,7 +67,7 @@ test_expect_success 'enable split index again, "one" now belongs to base index"'
6767
git update-index --split-index &&
6868
git ls-files --stage >ls-files.actual &&
6969
cat >ls-files.expect <<EOF &&
70-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
70+
100644 $EMPTY_BLOB 0 one
7171
EOF
7272
test_cmp ls-files.expect ls-files.actual &&
7373
@@ -105,15 +105,15 @@ test_expect_success 'add another file, which stays index' '
105105
git ls-files --stage >ls-files.actual &&
106106
cat >ls-files.expect <<EOF &&
107107
100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0 one
108-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 two
108+
100644 $EMPTY_BLOB 0 two
109109
EOF
110110
test_cmp ls-files.expect ls-files.actual &&
111111
112112
test-dump-split-index .git/index | sed "/^own/d" >actual &&
113113
q_to_tab >expect <<EOF &&
114114
$BASE
115115
100644 2e0996000b7e9019eabcad29391bf0f5c7702f0b 0Q
116-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 two
116+
100644 $EMPTY_BLOB 0 two
117117
replacements: 0
118118
deletions:
119119
EOF
@@ -159,14 +159,14 @@ test_expect_success 'add original file back' '
159159
git update-index --add one &&
160160
git ls-files --stage >ls-files.actual &&
161161
cat >ls-files.expect <<EOF &&
162-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
162+
100644 $EMPTY_BLOB 0 one
163163
EOF
164164
test_cmp ls-files.expect ls-files.actual &&
165165
166166
test-dump-split-index .git/index | sed "/^own/d" >actual &&
167167
cat >expect <<EOF &&
168168
$BASE
169-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
169+
100644 $EMPTY_BLOB 0 one
170170
replacements:
171171
deletions: 0
172172
EOF
@@ -178,8 +178,8 @@ test_expect_success 'add new file' '
178178
git update-index --add two &&
179179
git ls-files --stage >actual &&
180180
cat >expect <<EOF &&
181-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
182-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 two
181+
100644 $EMPTY_BLOB 0 one
182+
100644 $EMPTY_BLOB 0 two
183183
EOF
184184
test_cmp expect actual
185185
'
@@ -188,8 +188,8 @@ test_expect_success 'unify index, two files remain' '
188188
git update-index --no-split-index &&
189189
git ls-files --stage >ls-files.actual &&
190190
cat >ls-files.expect <<EOF &&
191-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 one
192-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 two
191+
100644 $EMPTY_BLOB 0 one
192+
100644 $EMPTY_BLOB 0 two
193193
EOF
194194
test_cmp ls-files.expect ls-files.actual &&
195195

t/t2203-add-intent.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,36 @@ test_expect_success 'cache-tree invalidates i-t-a paths' '
8282
test_cmp expect actual
8383
'
8484

85+
test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' '
86+
git init ita-in-dir &&
87+
(
88+
cd ita-in-dir &&
89+
mkdir 2 &&
90+
for f in 1 2/1 2/2 3
91+
do
92+
echo "$f" >"$f"
93+
done &&
94+
git add 1 2/2 3 &&
95+
git add -N 2/1 &&
96+
git commit -m committed &&
97+
git ls-tree -r HEAD >actual &&
98+
grep 2/2 actual
99+
)
100+
'
101+
102+
test_expect_success 'cache-tree does skip dir that becomes empty' '
103+
rm -fr ita-in-dir &&
104+
git init ita-in-dir &&
105+
(
106+
cd ita-in-dir &&
107+
mkdir -p 1/2/3 &&
108+
echo 4 >1/2/3/4 &&
109+
git add -N 1/2/3/4 &&
110+
git write-tree >actual &&
111+
echo $EMPTY_TREE >expected &&
112+
test_cmp expected actual
113+
)
114+
'
115+
85116
test_done
86117

t/t3102-ls-tree-wildcards.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ test_expect_success 'setup' '
1212
'
1313

1414
test_expect_success 'ls-tree a[a] matches literally' '
15-
cat >expect <<-\EOF &&
16-
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 a[a]/three
15+
cat >expect <<-EOF &&
16+
100644 blob $EMPTY_BLOB a[a]/three
1717
EOF
1818
git ls-tree -r HEAD "a[a]" >actual &&
1919
test_cmp expect actual
2020
'
2121

2222
test_expect_success 'ls-tree outside prefix' '
23-
cat >expect <<-\EOF &&
24-
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 ../a[a]/three
23+
cat >expect <<-EOF &&
24+
100644 blob $EMPTY_BLOB ../a[a]/three
2525
EOF
2626
( cd aa && git ls-tree -r HEAD "../a[a]"; ) >actual &&
2727
test_cmp expect actual

t/t4010-diff-pathspec.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ test_expect_success 'diff-tree pathspec' '
7878
test_cmp expected current
7979
'
8080

81-
EMPTY_TREE=4b825dc642cb6eb9a060e54bf8d69288fbee4904
82-
8381
test_expect_success 'diff-tree with wildcard shows dir also matches' '
8482
git diff-tree --name-only $EMPTY_TREE $tree -- "f*" >result &&
8583
echo file0 >expected &&

t/t4054-diff-bogus-tree.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
test_description='test diff with a bogus tree containing the null sha1'
44
. ./test-lib.sh
55

6-
empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
7-
86
test_expect_success 'create bogus tree' '
97
bogus_tree=$(
108
printf "100644 fooQQQQQQQQQQQQQQQQQQQQQ" |
@@ -22,13 +20,13 @@ test_expect_success 'create tree with matching file' '
2220

2321
test_expect_success 'raw diff shows null sha1 (addition)' '
2422
echo ":000000 100644 $_z40 $_z40 A foo" >expect &&
25-
git diff-tree $empty_tree $bogus_tree >actual &&
23+
git diff-tree $EMPTY_TREE $bogus_tree >actual &&
2624
test_cmp expect actual
2725
'
2826

2927
test_expect_success 'raw diff shows null sha1 (removal)' '
3028
echo ":100644 000000 $_z40 $_z40 D foo" >expect &&
31-
git diff-tree $bogus_tree $empty_tree >actual &&
29+
git diff-tree $bogus_tree $EMPTY_TREE >actual &&
3230
test_cmp expect actual
3331
'
3432

@@ -57,11 +55,11 @@ test_expect_success 'raw diff shows null sha1 (index)' '
5755
'
5856

5957
test_expect_success 'patch fails due to bogus sha1 (addition)' '
60-
test_must_fail git diff-tree -p $empty_tree $bogus_tree
58+
test_must_fail git diff-tree -p $EMPTY_TREE $bogus_tree
6159
'
6260

6361
test_expect_success 'patch fails due to bogus sha1 (removal)' '
64-
test_must_fail git diff-tree -p $bogus_tree $empty_tree
62+
test_must_fail git diff-tree -p $bogus_tree $EMPTY_TREE
6563
'
6664

6765
test_expect_success 'patch fails due to bogus sha1 (modification)' '

t/t5504-fetch-receive-strict.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ test_expect_success 'push with transfer.fsckobjects' '
115115
test_cmp exp act
116116
'
117117

118-
cat >bogus-commit <<\EOF
119-
tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
118+
cat >bogus-commit <<EOF
119+
tree $EMPTY_TREE
120120
author Bugs Bunny 1234567890 +0000
121121
committer Bugs Bunny <[email protected]> 1234567890 +0000
122122

t/t7011-skip-worktree-reading.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ S sub/1
2323
H sub/2
2424
EOF
2525

26-
NULL_SHA1=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
27-
2826
setup_absent() {
2927
test -f 1 && rm 1
3028
git update-index --remove 1 &&
31-
git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
29+
git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
3230
git update-index --skip-worktree 1
3331
}
3432

3533
test_absent() {
36-
echo "100644 $NULL_SHA1 0 1" > expected &&
34+
echo "100644 $EMPTY_BLOB 0 1" > expected &&
3735
git ls-files --stage 1 > result &&
3836
test_cmp expected result &&
3937
test ! -f 1
@@ -42,12 +40,12 @@ test_absent() {
4240
setup_dirty() {
4341
git update-index --force-remove 1 &&
4442
echo dirty > 1 &&
45-
git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
43+
git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
4644
git update-index --skip-worktree 1
4745
}
4846

4947
test_dirty() {
50-
echo "100644 $NULL_SHA1 0 1" > expected &&
48+
echo "100644 $EMPTY_BLOB 0 1" > expected &&
5149
git ls-files --stage 1 > result &&
5250
test_cmp expected result &&
5351
echo dirty > expected
@@ -120,7 +118,7 @@ test_expect_success 'grep with skip-worktree file' '
120118
test "$(git grep --no-ext-grep test)" = "1:test"
121119
'
122120

123-
echo ":000000 100644 $_z40 $NULL_SHA1 A 1" > expected
121+
echo ":000000 100644 $_z40 $EMPTY_BLOB A 1" > expected
124122
test_expect_success 'diff-index does not examine skip-worktree absent entries' '
125123
setup_absent &&
126124
git diff-index HEAD -- 1 > result &&

t/t7012-skip-worktree-writing.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,15 @@ test_expect_success 'read-tree removes worktree, dirty case' '
5353
git update-index --no-skip-worktree added
5454
'
5555

56-
NULL_SHA1=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
57-
5856
setup_absent() {
5957
test -f 1 && rm 1
6058
git update-index --remove 1 &&
61-
git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
59+
git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
6260
git update-index --skip-worktree 1
6361
}
6462

6563
test_absent() {
66-
echo "100644 $NULL_SHA1 0 1" > expected &&
64+
echo "100644 $EMPTY_BLOB 0 1" > expected &&
6765
git ls-files --stage 1 > result &&
6866
test_cmp expected result &&
6967
test ! -f 1
@@ -72,12 +70,12 @@ test_absent() {
7270
setup_dirty() {
7371
git update-index --force-remove 1 &&
7472
echo dirty > 1 &&
75-
git update-index --add --cacheinfo 100644 $NULL_SHA1 1 &&
73+
git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
7674
git update-index --skip-worktree 1
7775
}
7876

7977
test_dirty() {
80-
echo "100644 $NULL_SHA1 0 1" > expected &&
78+
echo "100644 $EMPTY_BLOB 0 1" > expected &&
8179
git ls-files --stage 1 > result &&
8280
test_cmp expected result &&
8381
echo dirty > expected

t/t7063-status-untracked-cache.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ A two
5353
EOF
5454

5555
cat >../dump.expect <<EOF &&
56-
info/exclude e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
56+
info/exclude $EMPTY_BLOB
5757
core.excludesfile 0000000000000000000000000000000000000000
5858
exclude_per_dir .gitignore
5959
flags 00000006
@@ -137,7 +137,7 @@ EOF
137137
test_expect_success 'verify untracked cache dump' '
138138
test-dump-untracked-cache >../actual &&
139139
cat >../expect <<EOF &&
140-
info/exclude e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
140+
info/exclude $EMPTY_BLOB
141141
core.excludesfile 0000000000000000000000000000000000000000
142142
exclude_per_dir .gitignore
143143
flags 00000006
@@ -184,7 +184,7 @@ EOF
184184
test_expect_success 'verify untracked cache dump' '
185185
test-dump-untracked-cache >../actual &&
186186
cat >../expect <<EOF &&
187-
info/exclude e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
187+
info/exclude $EMPTY_BLOB
188188
core.excludesfile 0000000000000000000000000000000000000000
189189
exclude_per_dir .gitignore
190190
flags 00000006

0 commit comments

Comments
 (0)