| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | # Only test with Git version 2.11.0 and above as that version introduced |
| 6 | # support for the "filter" attribute and protocol. |
| 7 | ensure_git_version_isnt $VERSION_LOWER "2.11.0" |
| 8 | |
| 9 | begin_test "filter process: checking out a branch" |
| 10 | ( |
| 11 | set -e |
| 12 | |
| 13 | reponame="filter_process_checkout" |
| 14 | setup_remote_repo "$reponame" |
| 15 | clone_repo "$reponame" repo |
| 16 | |
| 17 | git lfs track "*.dat" |
| 18 | git add .gitattributes |
| 19 | git commit -m "initial commit" |
| 20 | |
| 21 | contents_a="contents_a" |
| 22 | contents_a_oid="$(calc_oid $contents_a)" |
| 23 | printf "%s" "$contents_a" > a.dat |
| 24 | |
| 25 | git add a.dat |
| 26 | git commit -m "add a.dat" |
| 27 | |
| 28 | git checkout -b b |
| 29 | |
| 30 | contents_b="contents_b" |
| 31 | contents_b_oid="$(calc_oid $contents_b)" |
| 32 | printf "%s" "$contents_b" > b.dat |
| 33 | |
| 34 | git add b.dat |
| 35 | git commit -m "add b.dat" |
| 36 | |
| 37 | git push origin --all |
| 38 | |
| 39 | pushd .. |
| 40 | # Git will choose filter.lfs.process over `filter.lfs.clean` and |
| 41 | # `filter.lfs.smudge` |
| 42 | GIT_TRACE_PACKET=1 git \ |
| 43 | -c "filter.lfs.process=git-lfs filter-process" \ |
| 44 | -c "filter.lfs.clean=false"\ |
| 45 | -c "filter.lfs.smudge=false" \ |
| 46 | -c "filter.lfs.required=true" \ |
| 47 | clone "$GITSERVER/$reponame" "$reponame-assert" |
| 48 | |
| 49 | cd "$reponame-assert" |
| 50 | |
| 51 | # Assert that we are on the "main" branch, and have a.dat |
| 52 | [ "main" = "$(git rev-parse --abbrev-ref HEAD)" ] |
| 53 | [ "$contents_a" = "$(cat a.dat)" ] |
| 54 | assert_pointer "main" "a.dat" "$contents_a_oid" 10 |
| 55 | |
| 56 | git checkout b |
| 57 | |
| 58 | # Assert that we are on the "b" branch, and have b.dat |
| 59 | [ "b" = "$(git rev-parse --abbrev-ref HEAD)" ] |
| 60 | [ "$contents_b" = "$(cat b.dat)" ] |
| 61 | assert_pointer "b" "b.dat" "$contents_b_oid" 10 |
| 62 | popd |
| 63 | ) |
| 64 | end_test |
| 65 | |
| 66 | begin_test "filter process: include/exclude" |
| 67 | ( |
| 68 | set -e |
| 69 | |
| 70 | reponame="$(basename "$0" ".sh")-includeexclude" |
| 71 | setup_remote_repo "$reponame" |
| 72 | clone_repo "$reponame" "$reponame" |
| 73 | |
| 74 | git lfs track "*.dat" |
| 75 | mkdir -p foo/bar |
| 76 | |
| 77 | contents_a="contents_a" |
| 78 | contents_a_oid="$(calc_oid $contents_a)" |
| 79 | printf "%s" "$contents_a" > a.dat |
| 80 | cp a.dat foo |
| 81 | cp a.dat foo/bar |
| 82 | |
| 83 | git add .gitattributes a.dat foo |
| 84 | git commit -m "initial commit" |
| 85 | |
| 86 | git push origin main |
| 87 | |
| 88 | # The Git LFS objects for a.dat and foo/bar/a.dat would both download except |
| 89 | # we're going to prevent them from doing so with include/exclude. |
| 90 | # We also need to prevent MSYS from rewriting /foo into a Windows path. |
| 91 | MSYS_NO_PATHCONV=1 git config --global "lfs.fetchinclude" "/foo" |
| 92 | MSYS_NO_PATHCONV=1 git config --global "lfs.fetchexclude" "/foo/bar" |
| 93 | |
| 94 | pushd .. |
| 95 | # Git will choose filter.lfs.process over `filter.lfs.clean` and |
| 96 | # `filter.lfs.smudge` |
| 97 | GIT_TRACE_PACKET=1 git \ |
| 98 | -c "filter.lfs.process=git-lfs filter-process" \ |
| 99 | -c "filter.lfs.clean=false"\ |
| 100 | -c "filter.lfs.smudge=false" \ |
| 101 | -c "filter.lfs.required=true" \ |
| 102 | clone "$GITSERVER/$reponame" "$reponame-assert" |
| 103 | |
| 104 | cd "$reponame-assert" |
| 105 | |
| 106 | pointer="$(pointer "$contents_a_oid" 10)" |
| 107 | |
| 108 | [ "$pointer" = "$(cat a.dat)" ] |
| 109 | assert_pointer "main" "a.dat" "$contents_a_oid" 10 |
| 110 | |
| 111 | [ "$contents_a" = "$(cat foo/a.dat)" ] |
| 112 | assert_pointer "main" "foo/a.dat" "$contents_a_oid" 10 |
| 113 | |
| 114 | [ "$pointer" = "$(cat foo/bar/a.dat)" ] |
| 115 | assert_pointer "main" "foo/bar/a.dat" "$contents_a_oid" 10 |
| 116 | popd |
| 117 | ) |
| 118 | end_test |
| 119 | |
| 120 | begin_test "filter process: adding a file" |
| 121 | ( |
| 122 | set -e |
| 123 | |
| 124 | reponame="filter_process_add" |
| 125 | setup_remote_repo "$reponame" |
| 126 | clone_repo "$reponame" "$reponame" |
| 127 | |
| 128 | git lfs track "*.dat" |
| 129 | git add .gitattributes |
| 130 | git commit -m "initial commit" |
| 131 | |
| 132 | contents="contents" |
| 133 | contents_oid="$(calc_oid "$contents")" |
| 134 | printf "%s" "$contents" > a.dat |
| 135 | |
| 136 | git add a.dat |
| 137 | |
| 138 | expected="$(pointer "$contents_oid" "${#contents}")" |
| 139 | got="$(git cat-file -p :a.dat)" |
| 140 | |
| 141 | diff -u <(echo "$expected") <(echo "$got") |
| 142 | ) |
| 143 | end_test |
| 144 | |
| 145 | begin_test "filter-process: pointer extension" |
| 146 | ( |
| 147 | set -e |
| 148 | |
| 149 | reponame="filter-process-pointer-extension" |
| 150 | git init "$reponame" |
| 151 | cd "$reponame" |
| 152 | |
| 153 | # Git will choose "filter.lfs.process" over "filter.lfs.clean" and |
| 154 | # "filter.lfs.smudge". |
| 155 | git config --global --unset filter.lfs.clean |
| 156 | git config --global --unset filter.lfs.smudge |
| 157 | |
| 158 | setup_case_inverter_extension |
| 159 | |
| 160 | git lfs track "*.dat" |
| 161 | |
| 162 | contents="$(printf "%s\n%s" "abc" "def")" |
| 163 | contents_oid="$(calc_oid "$contents")" |
| 164 | inverted_contents_oid="$(calc_oid "$(invert_case "$contents")")" |
| 165 | mkdir dir1 |
| 166 | printf "%s" "$contents" >dir1/abc.dat |
| 167 | git add .gitattributes dir1 |
| 168 | git commit -m "initial commit" |
| 169 | |
| 170 | pointer="$(case_inverter_extension_pointer "$contents_oid" "$inverted_contents_oid" 7)" |
| 171 | [ "$pointer" = "$(git cat-file -p ":dir1/abc.dat")" ] |
| 172 | grep "clean: dir1/abc.dat" "$LFSTEST_EXT_LOG" |
| 173 | |
| 174 | assert_local_object "$inverted_contents_oid" 7 |
| 175 | |
| 176 | rm -rf dir1 "$LFSTEST_EXT_LOG" |
| 177 | git checkout -- . |
| 178 | [ "$contents" = "$(cat "dir1/abc.dat")" ] |
| 179 | grep "smudge: dir1/abc.dat" "$LFSTEST_EXT_LOG" |
| 180 | |
| 181 | rm -rf dir1 "$LFSTEST_EXT_LOG" |
| 182 | mkdir dir2 |
| 183 | |
| 184 | pushd dir2 |
| 185 | git checkout -- .. |
| 186 | popd |
| 187 | |
| 188 | [ "$contents" = "$(cat "dir1/abc.dat")" ] |
| 189 | grep "smudge: dir1/abc.dat" "$LFSTEST_EXT_LOG" |
| 190 | ) |
| 191 | end_test |
| 192 | |
| 193 | begin_test "filter process: non-pointer file of maximum pointer size" |
| 194 | ( |
| 195 | set -e |
| 196 | |
| 197 | mkdir repo-file-max-pointer-size |
| 198 | cd repo-file-max-pointer-size |
| 199 | git init |
| 200 | git lfs track "*.dat" |
| 201 | |
| 202 | # Test with a file size equal to the size of the read buffer used when |
| 203 | # decoding pointers. See https://github.com/git-lfs/git-lfs/issues/1697 |
| 204 | # and https://github.com/git-lfs/git-lfs/pull/1699. |
| 205 | max_pointer_size="$(lfstest-getlimit --max-pointer-size)" |
| 206 | head -c "$max_pointer_size" /dev/zero >first.dat |
| 207 | printf "any contents" > second.dat |
| 208 | |
| 209 | git add . |
| 210 | ) |
| 211 | end_test |
| 212 | |
| 213 | begin_test "filter process: hash-object --stdin --path does not hang" |
| 214 | ( |
| 215 | set -e |
| 216 | |
| 217 | mkdir repo-hash-object |
| 218 | cd repo-hash-object |
| 219 | git init |
| 220 | git lfs track "*.dat" |
| 221 | contents="test" |
| 222 | contents_oid="$(calc_oid "$contents")" |
| 223 | expected="$(pointer "$contents_oid" "${#contents}" | git hash-object --stdin)" |
| 224 | |
| 225 | # Test with a file size smaller than the size of the read buffer used when |
| 226 | # decoding pointers. See https://github.com/git-lfs/git-lfs/issues/3884 |
| 227 | # and https://github.com/git-lfs/git-lfs/pull/3902. |
| 228 | max_pointer_size="$(lfstest-getlimit --max-pointer-size)" |
| 229 | head -c $((max_pointer_size * 1000 / 1024)) /dev/zero >first.dat |
| 230 | echo "a" >second.dat |
| 231 | |
| 232 | # Works for existing file longer than the input data. |
| 233 | output="$(printf "%s" "$contents"| git hash-object --path first.dat --stdin)" |
| 234 | [ "$expected" = "$output" ] |
| 235 | # Works for existing file shorter than the input data. |
| 236 | output="$(printf "%s" "$contents" | git hash-object --path second.dat --stdin)" |
| 237 | [ "$expected" = "$output" ] |
| 238 | # Works for absent file. |
| 239 | output="$(printf "%s" "$contents" | git hash-object --path third.dat --stdin)" |
| 240 | [ "$expected" = "$output" ] |
| 241 | |
| 242 | # Test with a file size larger than the maximum length of a Git packet line |
| 243 | # (plus some arbitrary additional padding). |
| 244 | max_pktline_len="$(lfstest-getlimit --max-pktline-len)" |
| 245 | contents_size=$((max_pktline_len + max_pointer_size + 1)) |
| 246 | head -c "$contents_size" /dev/zero >large.dat |
| 247 | contents_oid="$(calc_oid_file large.dat)" |
| 248 | |
| 249 | expected="$(pointer "$contents_oid" "$contents_size" | git hash-object --stdin)" |
| 250 | output="$(git hash-object --path large.dat --stdin <large.dat)" |
| 251 | [ "$expected" = "$output" ] |
| 252 | |
| 253 | git add . |
| 254 | ) |
| 255 | end_test |
| 256 | |
| 257 | begin_test "filter process: checking out a branch with --skip-smudge and checkout-index" |
| 258 | ( |
| 259 | set -e |
| 260 | |
| 261 | reponame="filter-process-skip-smudge-checkout-index" |
| 262 | setup_remote_repo "$reponame" |
| 263 | clone_repo "$reponame" "$reponame" |
| 264 | |
| 265 | git lfs track "*.dat" |
| 266 | git add .gitattributes |
| 267 | git commit -m "initial commit" |
| 268 | |
| 269 | contents_a="contents_a" |
| 270 | contents_a_oid="$(calc_oid $contents_a)" |
| 271 | printf "%s" "$contents_a" > a.dat |
| 272 | |
| 273 | git add a.dat |
| 274 | git commit -m "add a.dat" |
| 275 | |
| 276 | git checkout -b b |
| 277 | |
| 278 | contents_b="contents_b" |
| 279 | contents_b_oid="$(calc_oid $contents_b)" |
| 280 | printf "%s" "$contents_b" > b.dat |
| 281 | |
| 282 | git add b.dat |
| 283 | git commit -m "add b.dat" |
| 284 | |
| 285 | git lfs install --local --skip-smudge |
| 286 | |
| 287 | git checkout main |
| 288 | |
| 289 | rm a.dat |
| 290 | git checkout-index -af |
| 291 | git lfs pointer --check --file a.dat |
| 292 | |
| 293 | assert_pointer "main" "a.dat" "$contents_a_oid" 10 |
| 294 | |
| 295 | git checkout b |
| 296 | |
| 297 | rm *.dat |
| 298 | git checkout-index -af |
| 299 | git lfs pointer --check --file a.dat |
| 300 | git lfs pointer --check --file b.dat |
| 301 | |
| 302 | # Assert that we are on the "b" branch, and have b.dat |
| 303 | assert_pointer "b" "b.dat" "$contents_b_oid" 10 |
| 304 | ) |
| 305 | end_test |
| 306 | |
| 307 | begin_test "filter process: git archive does not invoke SSH" |
| 308 | ( |
| 309 | set -e |
| 310 | |
| 311 | setup_pure_ssh |
| 312 | |
| 313 | reponame="filter-process-archive" |
| 314 | setup_remote_repo "$reponame" |
| 315 | clone_repo "$reponame" "$reponame" |
| 316 | |
| 317 | sshurl=$(ssh_remote "$reponame") |
| 318 | git config lfs.url "$sshurl" |
| 319 | |
| 320 | contents="test" |
| 321 | git lfs track "*.dat" |
| 322 | printf "%s" "$contents" > test.dat |
| 323 | git add .gitattributes test.dat |
| 324 | git commit -m "initial commit" |
| 325 | |
| 326 | git push origin main 2>&1 |
| 327 | cd .. |
| 328 | GIT_TRACE=1 git clone "$sshurl" "$reponame-2" 2>&1 | tee trace.log |
| 329 | grep "lfs-ssh-echo.*git-lfs-transfer .*$reponame.git download" trace.log |
| 330 | cd "$reponame-2" |
| 331 | GIT_TRACE=1 GIT_TRACE_PACKET=1 git archive -o foo.tar HEAD 2>&1 | tee archive.log |
| 332 | grep 'pure SSH' archive.log && exit 1 |
| 333 | true |
| 334 | ) |
| 335 | end_test |
| 336 | |
| 337 | begin_test "filter process: ignore file size on disk when cleaning" |
| 338 | ( |
| 339 | set -e |
| 340 | |
| 341 | reponame="filter-process-ignore-file-size" |
| 342 | setup_remote_repo "$reponame" |
| 343 | clone_repo "$reponame" "$reponame" |
| 344 | |
| 345 | git lfs track "*.dat" |
| 346 | git add .gitattributes |
| 347 | git commit -m "initial commit" |
| 348 | |
| 349 | # Test with a file size larger than the size of the read buffer used when |
| 350 | # decoding pointers. See the secondary issue reported in |
| 351 | # https://github.com/git-lfs/git-lfs/issues/4974#issuecomment-2717907543 |
| 352 | # and https://github.com/git-lfs/git-lfs/pull/6011. |
| 353 | max_pointer_size="$(lfstest-getlimit --max-pointer-size)" |
| 354 | contents_size=$((max_pointer_size * 2)) |
| 355 | head -c "$contents_size" /dev/zero >large.dat |
| 356 | contents_oid="$(calc_oid_file "large.dat")" |
| 357 | |
| 358 | # To replicate the reported issue, the "git-lfs filter-process" command's |
| 359 | # input from Git should be larger than the size of the pointer read buffer, |
| 360 | # but the file on disk should be smaller, so the lfstest-badlocalfile helper |
| 361 | # renames the initial file after Git has started piping its data, and then |
| 362 | # replaces the file with an empty file. |
| 363 | GIT_TRACE_PACKET=1 git \ |
| 364 | -c "filter.lfs.process=lfstest-badlocalfile | git-lfs filter-process" \ |
| 365 | -c "filter.lfs.clean=false"\ |
| 366 | -c "filter.lfs.smudge=false" \ |
| 367 | -c "filter.lfs.required=true" \ |
| 368 | add large.dat 2>&1 | tee filter.log |
| 369 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 370 | |
| 371 | [ 0 -eq "$(grep -c "unknown command" filter.log)" ] |
| 372 | |
| 373 | expected="$(pointer "$contents_oid" "$contents_size")" |
| 374 | got="$(git cat-file -p :large.dat)" |
| 375 | |
| 376 | diff -u <(echo "$expected") <(echo "$got") |
| 377 | ) |
| 378 | end_test |