Merge branch 'ps/maintenance-detach-fix-more' into next
[alt-git.git] / t / t3705-add-sparse-checkout.sh
blob6ae45a788d1a97e04bf504a25efbd98a6ab5e41a
1 #!/bin/sh
3 test_description='git add in sparse checked out working trees'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 SPARSE_ENTRY_BLOB=""
10 # Optionally take a printf format string to write to the sparse_entry file
11 setup_sparse_entry () {
12 # 'sparse_entry' might already be in the index with the skip-worktree
13 # bit set. Remove it so that the subsequent git add can update it.
14 git update-index --force-remove sparse_entry &&
15 if test $# -eq 1
16 then
17 printf "$1" >sparse_entry
18 else
19 >sparse_entry
20 fi &&
21 git add sparse_entry &&
22 git update-index --skip-worktree sparse_entry &&
23 git config core.sparseCheckout false &&
24 git commit --allow-empty -m "ensure sparse_entry exists at HEAD" &&
25 SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry)
28 test_sparse_entry_unchanged () {
29 echo "100644 $SPARSE_ENTRY_BLOB 0 sparse_entry" >expected &&
30 git ls-files --stage sparse_entry >actual &&
31 test_cmp expected actual
34 setup_gitignore () {
35 test_when_finished rm -f .gitignore &&
36 cat >.gitignore <<-EOF
38 !/sparse_entry
39 EOF
42 test_sparse_entry_unstaged () {
43 git diff --staged -- sparse_entry >diff &&
44 test_must_be_empty diff
47 test_expect_success 'setup' "
48 cat >sparse_error_header <<-EOF &&
49 The following paths and/or pathspecs matched paths that exist
50 outside of your sparse-checkout definition, so will not be
51 updated in the index:
52 EOF
54 cat >sparse_hint <<-EOF &&
55 hint: If you intend to update such entries, try one of the following:
56 hint: * Use the --sparse option.
57 hint: * Disable or modify the sparsity rules.
58 hint: Disable this message with \"git config advice.updateSparsePath false\"
59 EOF
61 echo sparse_entry | cat sparse_error_header - >sparse_entry_error &&
62 cat sparse_entry_error sparse_hint >error_and_hint
65 test_expect_success 'git add does not remove sparse entries' '
66 setup_sparse_entry &&
67 rm sparse_entry &&
68 test_must_fail git add sparse_entry 2>stderr &&
69 test_sparse_entry_unstaged &&
70 test_cmp error_and_hint stderr &&
71 test_sparse_entry_unchanged
74 test_expect_success 'git add -A does not remove sparse entries' '
75 setup_sparse_entry &&
76 rm sparse_entry &&
77 setup_gitignore &&
78 git add -A 2>stderr &&
79 test_must_be_empty stderr &&
80 test_sparse_entry_unchanged
83 test_expect_success 'git add . does not remove sparse entries' '
84 setup_sparse_entry &&
85 rm sparse_entry &&
86 setup_gitignore &&
87 test_must_fail git add . 2>stderr &&
88 test_sparse_entry_unstaged &&
90 cat sparse_error_header >expect &&
91 echo . >>expect &&
92 cat sparse_hint >>expect &&
94 test_cmp expect stderr &&
95 test_sparse_entry_unchanged
98 for opt in "" -f -u --ignore-removal --dry-run
100 test_expect_success "git add${opt:+ $opt} does not update sparse entries" '
101 setup_sparse_entry &&
102 echo modified >sparse_entry &&
103 test_must_fail git add $opt sparse_entry 2>stderr &&
104 test_sparse_entry_unstaged &&
105 test_cmp error_and_hint stderr &&
106 test_sparse_entry_unchanged
108 done
110 test_expect_success 'git add --refresh does not update sparse entries' '
111 setup_sparse_entry &&
112 git ls-files --debug sparse_entry | grep mtime >before &&
113 test-tool chmtime -60 sparse_entry &&
114 test_must_fail git add --refresh sparse_entry 2>stderr &&
115 test_sparse_entry_unstaged &&
116 test_cmp error_and_hint stderr &&
117 git ls-files --debug sparse_entry | grep mtime >after &&
118 test_cmp before after
121 test_expect_success 'git add --chmod does not update sparse entries' '
122 setup_sparse_entry &&
123 test_must_fail git add --chmod=+x sparse_entry 2>stderr &&
124 test_sparse_entry_unstaged &&
125 test_cmp error_and_hint stderr &&
126 test_sparse_entry_unchanged &&
127 ! test -x sparse_entry
130 test_expect_success 'git add --renormalize does not update sparse entries' '
131 test_when_finished rm .gitattributes &&
132 test_config core.autocrlf false &&
133 setup_sparse_entry "LINEONE\r\nLINETWO\r\n" &&
134 echo "sparse_entry text=auto" >.gitattributes &&
135 test_must_fail git add --renormalize sparse_entry 2>stderr &&
136 test_sparse_entry_unstaged &&
137 test_cmp error_and_hint stderr &&
138 test_sparse_entry_unchanged
141 test_expect_success 'git add --dry-run --ignore-missing warn on sparse path' '
142 setup_sparse_entry &&
143 rm sparse_entry &&
144 test_must_fail git add --dry-run --ignore-missing sparse_entry 2>stderr &&
145 test_sparse_entry_unstaged &&
146 test_cmp error_and_hint stderr &&
147 test_sparse_entry_unchanged
150 test_expect_success 'do not advice about sparse entries when they do not match the pathspec' '
151 setup_sparse_entry &&
152 test_must_fail git add nonexistent 2>stderr &&
153 grep "fatal: pathspec .nonexistent. did not match any files" stderr &&
154 ! grep -F -f sparse_error_header stderr
157 test_expect_success 'do not warn when pathspec matches dense entries' '
158 setup_sparse_entry &&
159 echo modified >sparse_entry &&
160 >dense_entry &&
161 git add "*_entry" 2>stderr &&
162 test_must_be_empty stderr &&
163 test_sparse_entry_unchanged &&
164 git ls-files --error-unmatch dense_entry
167 test_expect_success 'git add fails outside of sparse-checkout definition' '
168 test_when_finished git sparse-checkout disable &&
169 test_commit a &&
170 git sparse-checkout init --no-cone &&
171 git sparse-checkout set a &&
172 echo >>sparse_entry &&
174 git update-index --no-skip-worktree sparse_entry &&
175 test_must_fail git add sparse_entry &&
176 test_sparse_entry_unstaged &&
178 test_must_fail git add --chmod=+x sparse_entry &&
179 test_sparse_entry_unstaged &&
181 test_must_fail git add --renormalize sparse_entry &&
182 test_sparse_entry_unstaged &&
184 # Avoid munging CRLFs to avoid an error message
185 git -c core.autocrlf=input add --sparse sparse_entry 2>stderr &&
186 test_must_be_empty stderr &&
187 git ls-files --stage >actual &&
188 grep "^100644 .*sparse_entry\$" actual &&
190 git add --sparse --chmod=+x sparse_entry 2>stderr &&
191 test_must_be_empty stderr &&
192 git ls-files --stage >actual &&
193 grep "^100755 .*sparse_entry\$" actual &&
195 git reset &&
197 # This will print a message over stderr on Windows.
198 git add --sparse --renormalize sparse_entry &&
199 git status --porcelain >actual &&
200 grep "^M sparse_entry\$" actual
203 test_expect_success 'add obeys advice.updateSparsePath' '
204 setup_sparse_entry &&
205 test_must_fail git -c advice.updateSparsePath=false add sparse_entry 2>stderr &&
206 test_sparse_entry_unstaged &&
207 test_cmp sparse_entry_error stderr
211 test_expect_success 'add allows sparse entries with --sparse' '
212 git sparse-checkout set --no-cone a &&
213 echo modified >sparse_entry &&
214 test_must_fail git add sparse_entry &&
215 test_sparse_entry_unchanged &&
216 git add --sparse sparse_entry 2>stderr &&
217 test_must_be_empty stderr
220 test_expect_success 'can add files from non-sparse dir' '
221 git sparse-checkout set w !/x y/ &&
222 mkdir -p w x/y &&
223 touch w/f x/y/f &&
224 git add w/f x/y/f 2>stderr &&
225 test_must_be_empty stderr
228 test_expect_success 'refuse to add non-skip-worktree file from sparse dir' '
229 git sparse-checkout set !/x y/ !x/y/z &&
230 mkdir -p x/y/z &&
231 touch x/y/z/f &&
232 test_must_fail git add x/y/z/f 2>stderr &&
233 echo x/y/z/f | cat sparse_error_header - sparse_hint >expect &&
234 test_cmp expect stderr
237 test_done