Skip to content

Commit c3b9325

Browse files
committed
Merge branch 'nd/fix-sparse-checkout'
* nd/fix-sparse-checkout: unpack-trees: mark new entries skip-worktree appropriately unpack-trees: do not check for conflict entries too early unpack-trees: let read-tree -u remove index entries outside sparse area unpack-trees: only clear CE_UPDATE|CE_REMOVE when skip-worktree is always set t1011 (sparse checkout): style nitpicks
2 parents 2d98446 + 74da98f commit c3b9325

File tree

3 files changed

+108
-74
lines changed

3 files changed

+108
-74
lines changed

cache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ struct cache_entry {
179179
#define CE_UNHASHED (0x200000)
180180
#define CE_CONFLICTED (0x800000)
181181

182-
/* Only remove in work directory, not index */
183-
#define CE_WT_REMOVE (0x400000)
182+
#define CE_WT_REMOVE (0x400000) /* remove in work directory */
184183

185184
#define CE_UNPACKED (0x1000000)
186185

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

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
#!/bin/sh
22

3-
test_description='sparse checkout tests'
3+
test_description='sparse checkout tests
4+
5+
* (tag: removed, master) removed
6+
| D sub/added
7+
* (HEAD, tag: top) modified and added
8+
| M init.t
9+
| A sub/added
10+
* (tag: init) init
11+
A init.t
12+
'
413

514
. ./test-lib.sh
615

7-
cat >expected <<EOF
8-
100644 77f0ba1734ed79d12881f81b36ee134de6a3327b 0 init.t
9-
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 sub/added
10-
EOF
1116
test_expect_success 'setup' '
17+
cat >expected <<-\EOF &&
18+
100644 77f0ba1734ed79d12881f81b36ee134de6a3327b 0 init.t
19+
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 sub/added
20+
EOF
21+
cat >expected.swt <<-\EOF &&
22+
H init.t
23+
H sub/added
24+
EOF
25+
1226
test_commit init &&
13-
echo modified >> init.t &&
27+
echo modified >>init.t &&
1428
mkdir sub &&
1529
touch sub/added &&
1630
git add init.t sub/added &&
@@ -20,131 +34,146 @@ test_expect_success 'setup' '
2034
git commit -m removed &&
2135
git tag removed &&
2236
git checkout top &&
23-
git ls-files --stage > result &&
37+
git ls-files --stage >result &&
2438
test_cmp expected result
2539
'
2640

27-
cat >expected.swt <<EOF
28-
H init.t
29-
H sub/added
30-
EOF
3141
test_expect_success 'read-tree without .git/info/sparse-checkout' '
3242
git read-tree -m -u HEAD &&
33-
git ls-files --stage > result &&
43+
git ls-files --stage >result &&
3444
test_cmp expected result &&
35-
git ls-files -t > result &&
45+
git ls-files -t >result &&
3646
test_cmp expected.swt result
3747
'
3848

3949
test_expect_success 'read-tree with .git/info/sparse-checkout but disabled' '
40-
echo > .git/info/sparse-checkout
50+
echo >.git/info/sparse-checkout
4151
git read-tree -m -u HEAD &&
42-
git ls-files -t > result &&
52+
git ls-files -t >result &&
4353
test_cmp expected.swt result &&
4454
test -f init.t &&
4555
test -f sub/added
4656
'
4757

4858
test_expect_success 'read-tree --no-sparse-checkout with empty .git/info/sparse-checkout and enabled' '
4959
git config core.sparsecheckout true &&
50-
echo > .git/info/sparse-checkout &&
60+
echo >.git/info/sparse-checkout &&
5161
git read-tree --no-sparse-checkout -m -u HEAD &&
52-
git ls-files -t > result &&
62+
git ls-files -t >result &&
5363
test_cmp expected.swt result &&
5464
test -f init.t &&
5565
test -f sub/added
5666
'
5767

5868
test_expect_success 'read-tree with empty .git/info/sparse-checkout' '
5969
git config core.sparsecheckout true &&
60-
echo > .git/info/sparse-checkout &&
70+
echo >.git/info/sparse-checkout &&
6171
test_must_fail git read-tree -m -u HEAD &&
62-
git ls-files --stage > result &&
72+
git ls-files --stage >result &&
6373
test_cmp expected result &&
64-
git ls-files -t > result &&
74+
git ls-files -t >result &&
6575
test_cmp expected.swt result &&
6676
test -f init.t &&
6777
test -f sub/added
6878
'
6979

70-
cat >expected.swt <<EOF
71-
S init.t
72-
H sub/added
73-
EOF
7480
test_expect_success 'match directories with trailing slash' '
81+
cat >expected.swt-noinit <<-\EOF &&
82+
S init.t
83+
H sub/added
84+
EOF
85+
7586
echo sub/ > .git/info/sparse-checkout &&
7687
git read-tree -m -u HEAD &&
7788
git ls-files -t > result &&
78-
test_cmp expected.swt result &&
89+
test_cmp expected.swt-noinit result &&
7990
test ! -f init.t &&
8091
test -f sub/added
8192
'
8293

83-
cat >expected.swt <<EOF
84-
H init.t
85-
H sub/added
86-
EOF
8794
test_expect_failure 'match directories without trailing slash' '
88-
echo init.t > .git/info/sparse-checkout &&
89-
echo sub >> .git/info/sparse-checkout &&
95+
echo init.t >.git/info/sparse-checkout &&
96+
echo sub >>.git/info/sparse-checkout &&
9097
git read-tree -m -u HEAD &&
91-
git ls-files -t > result &&
98+
git ls-files -t >result &&
9299
test_cmp expected.swt result &&
93100
test ! -f init.t &&
94101
test -f sub/added
95102
'
96103

97-
cat >expected.swt <<EOF
98-
H init.t
99-
S sub/added
100-
EOF
101104
test_expect_success 'checkout area changes' '
102-
echo init.t > .git/info/sparse-checkout &&
105+
cat >expected.swt-nosub <<-\EOF &&
106+
H init.t
107+
S sub/added
108+
EOF
109+
110+
echo init.t >.git/info/sparse-checkout &&
103111
git read-tree -m -u HEAD &&
104-
git ls-files -t > result &&
105-
test_cmp expected.swt result &&
112+
git ls-files -t >result &&
113+
test_cmp expected.swt-nosub result &&
106114
test -f init.t &&
107115
test ! -f sub/added
108116
'
109117

110118
test_expect_success 'read-tree updates worktree, absent case' '
111-
echo sub/added > .git/info/sparse-checkout &&
119+
echo sub/added >.git/info/sparse-checkout &&
112120
git checkout -f top &&
113121
git read-tree -m -u HEAD^ &&
114122
test ! -f init.t
115123
'
116124

117125
test_expect_success 'read-tree updates worktree, dirty case' '
118-
echo sub/added > .git/info/sparse-checkout &&
126+
echo sub/added >.git/info/sparse-checkout &&
119127
git checkout -f top &&
120-
echo dirty > init.t &&
128+
echo dirty >init.t &&
121129
git read-tree -m -u HEAD^ &&
122130
grep -q dirty init.t &&
123131
rm init.t
124132
'
125133

126134
test_expect_success 'read-tree removes worktree, dirty case' '
127-
echo init.t > .git/info/sparse-checkout &&
135+
echo init.t >.git/info/sparse-checkout &&
128136
git checkout -f top &&
129-
echo dirty > added &&
137+
echo dirty >added &&
130138
git read-tree -m -u HEAD^ &&
131139
grep -q dirty added
132140
'
133141

134142
test_expect_success 'read-tree adds to worktree, absent case' '
135-
echo init.t > .git/info/sparse-checkout &&
143+
echo init.t >.git/info/sparse-checkout &&
136144
git checkout -f removed &&
137145
git read-tree -u -m HEAD^ &&
138146
test ! -f sub/added
139147
'
140148

141149
test_expect_success 'read-tree adds to worktree, dirty case' '
142-
echo init.t > .git/info/sparse-checkout &&
150+
echo init.t >.git/info/sparse-checkout &&
143151
git checkout -f removed &&
144152
mkdir sub &&
145-
echo dirty > sub/added &&
153+
echo dirty >sub/added &&
146154
git read-tree -u -m HEAD^ &&
147155
grep -q dirty sub/added
148156
'
149157

158+
test_expect_success 'index removal and worktree narrowing at the same time' '
159+
>empty &&
160+
echo init.t >.git/info/sparse-checkout &&
161+
echo sub/added >>.git/info/sparse-checkout &&
162+
git checkout -f top &&
163+
echo init.t >.git/info/sparse-checkout &&
164+
git checkout removed &&
165+
git ls-files sub/added >result &&
166+
test ! -f sub/added &&
167+
test_cmp empty result
168+
'
169+
170+
test_expect_success 'read-tree --reset removes outside worktree' '
171+
>empty &&
172+
echo init.t >.git/info/sparse-checkout &&
173+
git checkout -f top &&
174+
git reset --hard removed &&
175+
git ls-files sub/added >result &&
176+
test_cmp empty result
177+
'
178+
150179
test_done

unpack-trees.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
5858

5959
clear |= CE_HASHED | CE_UNHASHED;
6060

61+
if (set & CE_REMOVE)
62+
set |= CE_WT_REMOVE;
63+
6164
memcpy(new, ce, size);
6265
new->next = NULL;
6366
new->ce_flags = (new->ce_flags & ~clear) | set;
@@ -156,7 +159,7 @@ static int check_updates(struct unpack_trees_options *o)
156159
if (o->update && o->verbose_update) {
157160
for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
158161
struct cache_entry *ce = index->cache[cnt];
159-
if (ce->ce_flags & (CE_UPDATE | CE_REMOVE | CE_WT_REMOVE))
162+
if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
160163
total++;
161164
}
162165

@@ -176,12 +179,6 @@ static int check_updates(struct unpack_trees_options *o)
176179
unlink_entry(ce);
177180
continue;
178181
}
179-
180-
if (ce->ce_flags & CE_REMOVE) {
181-
display_progress(progress, ++cnt);
182-
if (o->update)
183-
unlink_entry(ce);
184-
}
185182
}
186183
remove_marked_cache_entries(&o->result);
187184
remove_scheduled_dirs();
@@ -210,9 +207,6 @@ static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_t
210207
{
211208
const char *basename;
212209

213-
if (ce_stage(ce))
214-
return 0;
215-
216210
basename = strrchr(ce->name, '/');
217211
basename = basename ? basename+1 : ce->name;
218212
return excluded_from_list(ce->name, ce_namelen(ce), basename, NULL, o->el) <= 0;
@@ -222,19 +216,36 @@ static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_opt
222216
{
223217
int was_skip_worktree = ce_skip_worktree(ce);
224218

225-
if (will_have_skip_worktree(ce, o))
219+
if (!ce_stage(ce) && will_have_skip_worktree(ce, o))
226220
ce->ce_flags |= CE_SKIP_WORKTREE;
227221
else
228222
ce->ce_flags &= ~CE_SKIP_WORKTREE;
229223

230224
/*
231-
* We only care about files getting into the checkout area
232-
* If merge strategies want to remove some, go ahead, this
233-
* flag will be removed eventually in unpack_trees() if it's
234-
* outside checkout area.
225+
* if (!was_skip_worktree && !ce_skip_worktree()) {
226+
* This is perfectly normal. Move on;
227+
* }
235228
*/
236-
if (ce->ce_flags & CE_REMOVE)
237-
return 0;
229+
230+
/*
231+
* Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
232+
* area as a result of ce_skip_worktree() shortcuts in
233+
* verify_absent() and verify_uptodate().
234+
* Make sure they don't modify worktree if they are already
235+
* outside checkout area
236+
*/
237+
if (was_skip_worktree && ce_skip_worktree(ce)) {
238+
ce->ce_flags &= ~CE_UPDATE;
239+
240+
/*
241+
* By default, when CE_REMOVE is on, CE_WT_REMOVE is also
242+
* on to get that file removed from both index and worktree.
243+
* If that file is already outside worktree area, don't
244+
* bother remove it.
245+
*/
246+
if (ce->ce_flags & CE_REMOVE)
247+
ce->ce_flags &= ~CE_WT_REMOVE;
248+
}
238249

239250
if (!was_skip_worktree && ce_skip_worktree(ce)) {
240251
/*
@@ -876,14 +887,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
876887
ret = -1;
877888
goto done;
878889
}
879-
/*
880-
* Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
881-
* area as a result of ce_skip_worktree() shortcuts in
882-
* verify_absent() and verify_uptodate(). Clear them.
883-
*/
884-
if (ce_skip_worktree(ce))
885-
ce->ce_flags &= ~(CE_UPDATE | CE_REMOVE);
886-
else
890+
if (!ce_skip_worktree(ce))
887891
empty_worktree = 0;
888892

889893
}
@@ -1182,6 +1186,8 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
11821186
if (!old) {
11831187
if (verify_absent(merge, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
11841188
return -1;
1189+
if (!o->skip_sparse_checkout && will_have_skip_worktree(merge, o))
1190+
update |= CE_SKIP_WORKTREE;
11851191
invalidate_ce_path(merge, o);
11861192
} else if (!(old->ce_flags & CE_CONFLICTED)) {
11871193
/*

0 commit comments

Comments
 (0)