Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git add in sparse checked out working trees' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | SPARSE_ENTRY_BLOB="" |
| 8 | |
| 9 | # Optionally take a printf format string to write to the sparse_entry file |
| 10 | setup_sparse_entry () { |
| 11 | # 'sparse_entry' might already be in the index with the skip-worktree |
| 12 | # bit set. Remove it so that the subsequent git add can update it. |
| 13 | git update-index --force-remove sparse_entry && |
| 14 | if test $# -eq 1 |
| 15 | then |
| 16 | printf "$1" >sparse_entry |
| 17 | else |
| 18 | >sparse_entry |
| 19 | fi && |
| 20 | git add sparse_entry && |
| 21 | git update-index --skip-worktree sparse_entry && |
Elijah Newren | af6a518 | 2022-01-14 15:59:41 +0000 | [diff] [blame] | 22 | git config core.sparseCheckout false && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 23 | git commit --allow-empty -m "ensure sparse_entry exists at HEAD" && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 24 | SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry) |
| 25 | } |
| 26 | |
| 27 | test_sparse_entry_unchanged () { |
| 28 | echo "100644 $SPARSE_ENTRY_BLOB 0 sparse_entry" >expected && |
| 29 | git ls-files --stage sparse_entry >actual && |
| 30 | test_cmp expected actual |
| 31 | } |
| 32 | |
| 33 | setup_gitignore () { |
| 34 | test_when_finished rm -f .gitignore && |
| 35 | cat >.gitignore <<-EOF |
| 36 | * |
| 37 | !/sparse_entry |
| 38 | EOF |
| 39 | } |
| 40 | |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 41 | test_sparse_entry_unstaged () { |
| 42 | git diff --staged -- sparse_entry >diff && |
| 43 | test_must_be_empty diff |
| 44 | } |
| 45 | |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 46 | test_expect_success 'setup' " |
| 47 | cat >sparse_error_header <<-EOF && |
Derrick Stolee | 6579e78 | 2021-09-24 15:39:14 +0000 | [diff] [blame] | 48 | The following paths and/or pathspecs matched paths that exist |
| 49 | outside of your sparse-checkout definition, so will not be |
| 50 | updated in the index: |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 51 | EOF |
| 52 | |
| 53 | cat >sparse_hint <<-EOF && |
Derrick Stolee | 6579e78 | 2021-09-24 15:39:14 +0000 | [diff] [blame] | 54 | hint: If you intend to update such entries, try one of the following: |
| 55 | hint: * Use the --sparse option. |
| 56 | hint: * Disable or modify the sparsity rules. |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 57 | hint: Disable this message with \"git config advice.updateSparsePath false\" |
| 58 | EOF |
| 59 | |
| 60 | echo sparse_entry | cat sparse_error_header - >sparse_entry_error && |
| 61 | cat sparse_entry_error sparse_hint >error_and_hint |
| 62 | " |
| 63 | |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 64 | test_expect_success 'git add does not remove sparse entries' ' |
| 65 | setup_sparse_entry && |
| 66 | rm sparse_entry && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 67 | test_must_fail git add sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 68 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 69 | test_cmp error_and_hint stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 70 | test_sparse_entry_unchanged |
| 71 | ' |
| 72 | |
| 73 | test_expect_success 'git add -A does not remove sparse entries' ' |
| 74 | setup_sparse_entry && |
| 75 | rm sparse_entry && |
| 76 | setup_gitignore && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 77 | git add -A 2>stderr && |
| 78 | test_must_be_empty stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 79 | test_sparse_entry_unchanged |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'git add . does not remove sparse entries' ' |
| 83 | setup_sparse_entry && |
| 84 | rm sparse_entry && |
| 85 | setup_gitignore && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 86 | test_must_fail git add . 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 87 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 88 | |
| 89 | cat sparse_error_header >expect && |
| 90 | echo . >>expect && |
| 91 | cat sparse_hint >>expect && |
| 92 | |
| 93 | test_cmp expect stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 94 | test_sparse_entry_unchanged |
| 95 | ' |
| 96 | |
| 97 | for opt in "" -f -u --ignore-removal --dry-run |
| 98 | do |
| 99 | test_expect_success "git add${opt:+ $opt} does not update sparse entries" ' |
| 100 | setup_sparse_entry && |
| 101 | echo modified >sparse_entry && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 102 | test_must_fail git add $opt sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 103 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 104 | test_cmp error_and_hint stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 105 | test_sparse_entry_unchanged |
| 106 | ' |
| 107 | done |
| 108 | |
| 109 | test_expect_success 'git add --refresh does not update sparse entries' ' |
| 110 | setup_sparse_entry && |
| 111 | git ls-files --debug sparse_entry | grep mtime >before && |
| 112 | test-tool chmtime -60 sparse_entry && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 113 | test_must_fail git add --refresh sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 114 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 115 | test_cmp error_and_hint stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 116 | git ls-files --debug sparse_entry | grep mtime >after && |
| 117 | test_cmp before after |
| 118 | ' |
| 119 | |
Matheus Tavares | d73dbaf | 2021-04-08 17:41:24 -0300 | [diff] [blame] | 120 | test_expect_success 'git add --chmod does not update sparse entries' ' |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 121 | setup_sparse_entry && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 122 | test_must_fail git add --chmod=+x sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 123 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 124 | test_cmp error_and_hint stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 125 | test_sparse_entry_unchanged && |
| 126 | ! test -x sparse_entry |
| 127 | ' |
| 128 | |
Matheus Tavares | d73dbaf | 2021-04-08 17:41:24 -0300 | [diff] [blame] | 129 | test_expect_success 'git add --renormalize does not update sparse entries' ' |
Elijah Newren | af6a518 | 2022-01-14 15:59:41 +0000 | [diff] [blame] | 130 | test_when_finished rm .gitattributes && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 131 | test_config core.autocrlf false && |
| 132 | setup_sparse_entry "LINEONE\r\nLINETWO\r\n" && |
| 133 | echo "sparse_entry text=auto" >.gitattributes && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 134 | test_must_fail git add --renormalize sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 135 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 136 | test_cmp error_and_hint stderr && |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 137 | test_sparse_entry_unchanged |
| 138 | ' |
| 139 | |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 140 | test_expect_success 'git add --dry-run --ignore-missing warn on sparse path' ' |
| 141 | setup_sparse_entry && |
| 142 | rm sparse_entry && |
| 143 | test_must_fail git add --dry-run --ignore-missing sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 144 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 145 | test_cmp error_and_hint stderr && |
| 146 | test_sparse_entry_unchanged |
| 147 | ' |
| 148 | |
| 149 | test_expect_success 'do not advice about sparse entries when they do not match the pathspec' ' |
| 150 | setup_sparse_entry && |
| 151 | test_must_fail git add nonexistent 2>stderr && |
| 152 | grep "fatal: pathspec .nonexistent. did not match any files" stderr && |
| 153 | ! grep -F -f sparse_error_header stderr |
| 154 | ' |
| 155 | |
| 156 | test_expect_success 'do not warn when pathspec matches dense entries' ' |
| 157 | setup_sparse_entry && |
| 158 | echo modified >sparse_entry && |
| 159 | >dense_entry && |
| 160 | git add "*_entry" 2>stderr && |
| 161 | test_must_be_empty stderr && |
| 162 | test_sparse_entry_unchanged && |
| 163 | git ls-files --error-unmatch dense_entry |
| 164 | ' |
| 165 | |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 166 | test_expect_success 'git add fails outside of sparse-checkout definition' ' |
| 167 | test_when_finished git sparse-checkout disable && |
| 168 | test_commit a && |
Elijah Newren | dde1358 | 2022-04-22 02:32:18 +0000 | [diff] [blame] | 169 | git sparse-checkout init --no-cone && |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 170 | git sparse-checkout set a && |
| 171 | echo >>sparse_entry && |
| 172 | |
| 173 | git update-index --no-skip-worktree sparse_entry && |
| 174 | test_must_fail git add sparse_entry && |
Derrick Stolee | 0299a69 | 2021-09-24 15:39:08 +0000 | [diff] [blame] | 175 | test_sparse_entry_unstaged && |
| 176 | |
Derrick Stolee | 63b60b3 | 2021-09-24 15:39:09 +0000 | [diff] [blame] | 177 | test_must_fail git add --chmod=+x sparse_entry && |
| 178 | test_sparse_entry_unstaged && |
| 179 | |
Derrick Stolee | 61d450f | 2021-09-24 15:39:10 +0000 | [diff] [blame] | 180 | test_must_fail git add --renormalize sparse_entry && |
| 181 | test_sparse_entry_unstaged && |
| 182 | |
Derrick Stolee | 0299a69 | 2021-09-24 15:39:08 +0000 | [diff] [blame] | 183 | # Avoid munging CRLFs to avoid an error message |
| 184 | git -c core.autocrlf=input add --sparse sparse_entry 2>stderr && |
| 185 | test_must_be_empty stderr && |
Derrick Stolee | c2a2940 | 2021-12-22 14:20:55 +0000 | [diff] [blame] | 186 | git ls-files --stage >actual && |
| 187 | grep "^100644 .*sparse_entry\$" actual && |
Derrick Stolee | 63b60b3 | 2021-09-24 15:39:09 +0000 | [diff] [blame] | 188 | |
| 189 | git add --sparse --chmod=+x sparse_entry 2>stderr && |
| 190 | test_must_be_empty stderr && |
Derrick Stolee | c2a2940 | 2021-12-22 14:20:55 +0000 | [diff] [blame] | 191 | git ls-files --stage >actual && |
| 192 | grep "^100755 .*sparse_entry\$" actual && |
Derrick Stolee | 61d450f | 2021-09-24 15:39:10 +0000 | [diff] [blame] | 193 | |
| 194 | git reset && |
| 195 | |
| 196 | # This will print a message over stderr on Windows. |
| 197 | git add --sparse --renormalize sparse_entry && |
| 198 | git status --porcelain >actual && |
| 199 | grep "^M sparse_entry\$" actual |
Derrick Stolee | 49fdd51 | 2021-09-24 15:39:07 +0000 | [diff] [blame] | 200 | ' |
| 201 | |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 202 | test_expect_success 'add obeys advice.updateSparsePath' ' |
| 203 | setup_sparse_entry && |
| 204 | test_must_fail git -c advice.updateSparsePath=false add sparse_entry 2>stderr && |
Derrick Stolee | ca267ae | 2021-09-24 15:39:02 +0000 | [diff] [blame] | 205 | test_sparse_entry_unstaged && |
Matheus Tavares | a20f704 | 2021-04-08 17:41:27 -0300 | [diff] [blame] | 206 | test_cmp sparse_entry_error stderr |
| 207 | |
| 208 | ' |
| 209 | |
Derrick Stolee | 0299a69 | 2021-09-24 15:39:08 +0000 | [diff] [blame] | 210 | test_expect_success 'add allows sparse entries with --sparse' ' |
Elijah Newren | dde1358 | 2022-04-22 02:32:18 +0000 | [diff] [blame] | 211 | git sparse-checkout set --no-cone a && |
Derrick Stolee | 0299a69 | 2021-09-24 15:39:08 +0000 | [diff] [blame] | 212 | echo modified >sparse_entry && |
| 213 | test_must_fail git add sparse_entry && |
| 214 | test_sparse_entry_unchanged && |
| 215 | git add --sparse sparse_entry 2>stderr && |
| 216 | test_must_be_empty stderr |
| 217 | ' |
| 218 | |
Matheus Tavares | 20141e3 | 2021-10-28 11:21:11 -0300 | [diff] [blame] | 219 | test_expect_success 'can add files from non-sparse dir' ' |
| 220 | git sparse-checkout set w !/x y/ && |
| 221 | mkdir -p w x/y && |
| 222 | touch w/f x/y/f && |
| 223 | git add w/f x/y/f 2>stderr && |
| 224 | test_must_be_empty stderr |
| 225 | ' |
| 226 | |
| 227 | test_expect_success 'refuse to add non-skip-worktree file from sparse dir' ' |
| 228 | git sparse-checkout set !/x y/ !x/y/z && |
| 229 | mkdir -p x/y/z && |
| 230 | touch x/y/z/f && |
| 231 | test_must_fail git add x/y/z/f 2>stderr && |
| 232 | echo x/y/z/f | cat sparse_error_header - sparse_hint >expect && |
| 233 | test_cmp expect stderr |
| 234 | ' |
| 235 | |
Matheus Tavares | 6594afc | 2021-04-08 17:41:23 -0300 | [diff] [blame] | 236 | test_done |