Debian Codegraph @

t/t-fsck.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/testlib.sh"
4
5 begin_test "fsck default"
6 (
7 set -e
8
9 reponame="fsck-default"
10 git init $reponame
11 cd $reponame
12
13 # Create a commit with some files tracked by git-lfs
14 git lfs track *.dat
15 echo "test data" > a.dat
16 echo "test data 2" > b.dat
17 git add .gitattributes *.dat
18 git commit -m "first commit"
19
20 [ "Git LFS fsck OK" = "$(git lfs fsck)" ]
21
22 aOid=$(git log --patch a.dat | grep "^+oid" | cut -d ":" -f 2)
23 aOid12=$(echo $aOid | cut -b 1-2)
24 aOid34=$(echo $aOid | cut -b 3-4)
25 if [ "$aOid" != "$(calc_oid_file .git/lfs/objects/$aOid12/$aOid34/$aOid)" ]; then
26 echo "oid for a.dat does not match"
27 exit 1
28 fi
29
30 bOid=$(git log --patch b.dat | grep "^+oid" | cut -d ":" -f 2)
31 bOid12=$(echo $bOid | cut -b 1-2)
32 bOid34=$(echo $bOid | cut -b 3-4)
33 if [ "$bOid" != "$(calc_oid_file .git/lfs/objects/$bOid12/$bOid34/$bOid)" ]; then
34 echo "oid for b.dat does not match"
35 exit 1
36 fi
37
38 echo "CORRUPTION" >> .git/lfs/objects/$aOid12/$aOid34/$aOid
39
40 moved=$(canonical_path "$TRASHDIR/$reponame/.git/lfs/bad")
41 expected="$(printf 'objects: corruptObject: a.dat (%s) is corrupt
42 objects: repair: moving corrupt objects to %s' "$aOid" "$moved")"
43 [ "$expected" = "$(git lfs fsck)" ]
44
45 [ -e ".git/lfs/bad/$aOid" ]
46 [ ! -e ".git/lfs/objects/$aOid12/$aOid34/$aOid" ]
47 [ "$bOid" = "$(calc_oid_file .git/lfs/objects/$bOid12/$bOid34/$bOid)" ]
48 )
49 end_test
50
51 begin_test "fsck dry run"
52 (
53 set -e
54
55 reponame="fsck-dry-run"
56 git init $reponame
57 cd $reponame
58
59 # Create a commit with some files tracked by git-lfs
60 git lfs track *.dat
61 echo "test data" > a.dat
62 echo "test data 2" > b.dat
63 git add .gitattributes *.dat
64 git commit -m "first commit"
65
66 [ "Git LFS fsck OK" = "$(git lfs fsck --dry-run)" ]
67
68 aOid=$(git log --patch a.dat | grep "^+oid" | cut -d ":" -f 2)
69 aOid12=$(echo $aOid | cut -b 1-2)
70 aOid34=$(echo $aOid | cut -b 3-4)
71 if [ "$aOid" != "$(calc_oid_file .git/lfs/objects/$aOid12/$aOid34/$aOid)" ]; then
72 echo "oid for a.dat does not match"
73 exit 1
74 fi
75
76 bOid=$(git log --patch b.dat | grep "^+oid" | cut -d ":" -f 2)
77 bOid12=$(echo $bOid | cut -b 1-2)
78 bOid34=$(echo $bOid | cut -b 3-4)
79 if [ "$bOid" != "$(calc_oid_file .git/lfs/objects/$bOid12/$bOid34/$bOid)" ]; then
80 echo "oid for b.dat does not match"
81 exit 1
82 fi
83
84 echo "CORRUPTION" >> .git/lfs/objects/$aOid12/$aOid34/$aOid
85
86 [ "objects: corruptObject: a.dat ($aOid) is corrupt" = "$(git lfs fsck --dry-run)" ]
87
88 if [ "$aOid" = "$(calc_oid_file .git/lfs/objects/$aOid12/$aOid34/$aOid)" ]; then
89 echo "oid for a.dat still matches match"
90 exit 1
91 fi
92
93 if [ "$bOid" != "$(calc_oid_file .git/lfs/objects/$bOid12/$bOid34/$bOid)" ]; then
94 echo "oid for b.dat does not match"
95 exit 1
96 fi
97 )
98 end_test
99
100 begin_test "fsck does not fail with shell characters in paths"
101 (
102 set -e
103
104 mkdir '[[path]]'
105 cd '[[path]]'
106 reponame="fsck-shell-paths"
107 git init $reponame
108 cd $reponame
109
110 # Create a commit with some files tracked by git-lfs
111 git lfs track *.dat
112 echo "test data" > a.dat
113 echo "test data 2" > b.dat
114 git add .gitattributes *.dat
115 git commit -m "first commit"
116
117 # Verify that the pack code handles glob patterns properly.
118 git gc --aggressive --prune=now
119
120 [ "Git LFS fsck OK" = "$(git lfs fsck)" ]
121 )
122 end_test
123
124 begin_test "fsck: outside git repository"
125 (
126 set +e
127 git lfs fsck 2>&1 > fsck.log
128 res=$?
129 set -e
130
131 if [ "$res" = "0" ]; then
132 echo "Passes because $GIT_LFS_TEST_DIR is unset."
133 exit 0
134 fi
135 [ "$res" = "128" ]
136 grep "Not in a Git repository" fsck.log
137 )
138 end_test
139
140 create_invalid_pointers() {
141 valid="$1"
142 ext="${2:-dat}"
143
144 git cat-file blob ":$valid" | awk '{ sub(/$/, "\r"); print }' >"crlf.$ext"
145
146 # Test with a file size larger than the size of the read buffer used when
147 # decoding pointers. See https://github.com/git-lfs/git-lfs/issues/1729,
148 # https://github.com/git-lfs/git-lfs/pull/1796,
149 # and https://github.com/git-lfs/git-lfs/pull/4525.
150 max_pointer_size="$(lfstest-getlimit --max-pointer-size)"
151 lfstest-genrandom --base64 $((max_pointer_size + 1)) >"large.$ext"
152
153 git \
154 -c "filter.lfs.process=" \
155 -c "filter.lfs.clean=cat" \
156 -c "filter.lfs.required=false" \
157 add "crlf.$ext" "large.$ext"
158 git commit -m "invalid pointers"
159 }
160
161 setup_invalid_pointers () {
162 git init $reponame
163 cd $reponame
164
165 # Create a commit with some files tracked by git-lfs
166 git lfs track *.dat
167 echo "test data" > a.dat
168 echo "test data 2" > b.dat
169 git add .gitattributes *.dat
170 git commit -m "first commit"
171
172 create_invalid_pointers "a.dat"
173 }
174
175 begin_test "fsck detects invalid pointers"
176 (
177 set -e
178
179 reponame="fsck-pointers"
180 setup_invalid_pointers
181
182 set +e
183 git lfs fsck >test.log 2>&1
184 RET=$?
185 git lfs fsck --pointers >>test.log 2>&1
186 RET2=$?
187 set -e
188
189 [ "$RET" -eq 1 ]
190 [ "$RET2" -eq 1 ]
191 [ $(grep -c 'pointer: nonCanonicalPointer: Pointer.*was not canonical' test.log) -eq 2 ]
192 [ $(grep -c 'pointer: unexpectedGitObject: "large.dat".*should have been a pointer but was not' test.log) -eq 2 ]
193 )
194 end_test
195
196 begin_test "fsck detects invalid pointers with macro patterns"
197 (
198 set -e
199
200 reponame="fsck-pointers-macros"
201 git init $reponame
202 cd $reponame
203
204 printf '[attr]lfs filter=lfs diff=lfs merge=lfs -text\n*.dat lfs\n' \
205 >.gitattributes
206 echo "test data" >a.dat
207 mkdir dir
208 printf '*.bin lfs\n' >dir/.gitattributes
209 git add .gitattributes a.dat dir
210 git commit -m "first commit"
211
212 create_invalid_pointers "a.dat"
213
214 cd dir
215 create_invalid_pointers "a.dat" "bin"
216 cd ..
217
218 # NOTE: We should also create a .dir directory with the same files as
219 # as in the dir/ directory, and confirm those .dir/*.bin files are
220 # reported by "git lfs fsck" as well. However, at the moment
221 # "git lfs fsck" will not resolve a macro attribute reference
222 # in .dir/.gitattributes because it sorts that file before
223 # .gitattributes and then processes them in that order.
224
225 set +e
226 git lfs fsck >test.log 2>&1
227 RET=$?
228 git lfs fsck --pointers >>test.log 2>&1
229 RET2=$?
230 set -e
231
232 [ "$RET" -eq 1 ]
233 [ "$RET2" -eq 1 ]
234 [ $(grep -c 'pointer: nonCanonicalPointer: Pointer.*was not canonical' test.log) -eq 4 ]
235 [ $(grep -c 'pointer: unexpectedGitObject: "large.dat".*should have been a pointer but was not' test.log) -eq 2 ]
236 [ $(grep -c 'pointer: unexpectedGitObject: "dir/large.bin".*should have been a pointer but was not' test.log) -eq 2 ]
237 )
238 end_test
239
240 begin_test "fsck detects invalid pointers with GIT_OBJECT_DIRECTORY"
241 (
242 set -e
243
244 reponame="fsck-pointers-object-directory"
245 setup_invalid_pointers
246
247 head=$(git rev-parse HEAD)
248 objdir="$(lfstest-realpath .git/objects)"
249 cd ..
250 git init "$reponame-2"
251 gitdir="$(lfstest-realpath "$reponame-2/.git")"
252 GIT_WORK_TREE="$reponame-2" GIT_DIR="$gitdir" GIT_OBJECT_DIRECTORY="$objdir" git update-ref refs/heads/main "$head"
253
254 set +e
255 GIT_WORK_TREE="$reponame-2" GIT_DIR="$gitdir" GIT_OBJECT_DIRECTORY="$objdir" git lfs fsck --pointers >test.log 2>&1
256 RET=$?
257 set -e
258
259 [ "$RET" -eq 1 ]
260 grep 'pointer: nonCanonicalPointer: Pointer.*was not canonical' test.log
261 grep 'pointer: unexpectedGitObject: "large.dat".*should have been a pointer but was not' test.log
262 )
263 end_test
264
265 begin_test "fsck does not detect invalid pointers with no LFS objects"
266 (
267 set -e
268
269 reponame="fsck-pointers-none"
270 git init "$reponame"
271 cd "$reponame"
272
273 echo "# README" > README.md
274 git add README.md
275 git commit -m "Add README"
276
277 git lfs fsck
278 git lfs fsck --pointers
279 )
280 end_test
281
282 begin_test "fsck does not detect invalid pointers with symlinks"
283 (
284 set -e
285
286 reponame="fsck-pointers-symlinks"
287 git init "$reponame"
288 cd "$reponame"
289
290 git lfs track '*.dat'
291
292 echo "# Test" > a.dat
293 ln -s a.dat b.dat
294 git add .gitattributes *.dat
295 git commit -m "Add files"
296
297 git lfs fsck
298 git lfs fsck --pointers
299 )
300 end_test
301
302 begin_test "fsck does not detect invalid pointers with negated patterns"
303 (
304 set -e
305
306 reponame="fsck-pointers-none"
307 git init "$reponame"
308 cd "$reponame"
309
310 cat > .gitattributes <<EOF
311 *.dat filter=lfs diff=lfs merge=lfs -text
312 b.dat !filter !diff !merge text
313 EOF
314
315 echo "# Test" > a.dat
316 cp a.dat b.dat
317 git add .gitattributes *.dat
318 git commit -m "Add files"
319
320 git lfs fsck
321 git lfs fsck --pointers
322 )
323 end_test
324
325 begin_test "fsck does not detect invalid pointers with negated macro patterns"
326 (
327 set -e
328
329 reponame="fsck-pointers-macros-none"
330 git init "$reponame"
331 cd "$reponame"
332
333 printf '[attr]lfs filter=lfs diff=lfs merge=lfs -text\n*.dat lfs\nb.dat !lfs\n' \
334 >.gitattributes
335 echo "test data" >a.dat
336 cp a.dat b.dat
337 mkdir dir .dir
338 printf '*.dat !lfs\n' >dir/.gitattributes
339 cp b.dat dir
340 printf '*.dat !lfs\n' >.dir/.gitattributes
341 cp b.dat .dir
342 git add .gitattributes *.dat dir .dir
343 git commit -m "first commit"
344
345 # NOTE: The "git lfs fsck" command exempts the .dir/b.dat file from the
346 # *.dat pattern from the top-level .gitattributes and so permits
347 # it as a valid non-pointer file; however, it permits it for a
348 # different reason than the dir/b.dat file, because it processes
349 # the .dir/.gitattributes file before the .gitattributes one
350 # and does not recognize the "!lfs" macro attribute reference until
351 # after it has processed .gitattributes. Ideally both the dir/
352 # and .dir/ directories should be processed identically.
353
354 git lfs fsck
355 git lfs fsck --pointers
356 )
357 end_test
358
359 setup_invalid_objects () {
360 git init $reponame
361 cd $reponame
362
363 # Create a commit with some files tracked by git-lfs
364 git lfs track *.dat
365 echo "test data" > a.dat
366 echo "test data 2" > b.dat
367 mkdir foo
368 echo "test test 3" > foo/a.dat
369 echo "test data 4" > foo/b.dat
370 git add .gitattributes *.dat foo
371 git commit -m "first commit"
372
373 oid1=$(calc_oid_file a.dat)
374 oid2=$(calc_oid_file b.dat)
375 oid3=$(calc_oid_file foo/a.dat)
376 oid4=$(calc_oid_file foo/b.dat)
377 echo "CORRUPTION" >>".git/lfs/objects/${oid1:0:2}/${oid1:2:2}/$oid1"
378 rm ".git/lfs/objects/${oid2:0:2}/${oid2:2:2}/$oid2"
379 echo "CORRUPTION" >>".git/lfs/objects/${oid3:0:2}/${oid3:2:2}/$oid3"
380 rm ".git/lfs/objects/${oid4:0:2}/${oid4:2:2}/$oid4"
381 }
382
383 begin_test "fsck detects invalid objects"
384 (
385 set -e
386
387 reponame="fsck-objects"
388 setup_invalid_objects
389
390 set +e
391 git lfs fsck >test.log 2>&1
392 RET=$?
393 set -e
394
395 [ "$RET" -eq 1 ]
396 [ $(grep -c 'objects: corruptObject: a.dat (.*) is corrupt' test.log) -eq 1 ]
397 [ $(grep -c 'objects: openError: b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
398 [ $(grep -c 'objects: corruptObject: foo/a.dat (.*) is corrupt' test.log) -eq 1 ]
399 [ $(grep -c 'objects: openError: foo/b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
400 [ $(grep -c 'objects: repair: moving corrupt objects to .*' test.log) -eq 1 ]
401
402 cd ..
403 rm -rf $reponame
404 setup_invalid_objects
405
406 set +e
407 git lfs fsck --objects >test.log 2>&1
408 RET=$?
409 set -e
410
411 [ "$RET" -eq 1 ]
412 [ $(grep -c 'objects: corruptObject: a.dat (.*) is corrupt' test.log) -eq 1 ]
413 [ $(grep -c 'objects: openError: b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
414 [ $(grep -c 'objects: corruptObject: foo/a.dat (.*) is corrupt' test.log) -eq 1 ]
415 [ $(grep -c 'objects: openError: foo/b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
416 [ $(grep -c 'objects: repair: moving corrupt objects to .*' test.log) -eq 1 ]
417 )
418 end_test
419
420 begin_test "fsck detects invalid objects except in excluded paths"
421 (
422 set -e
423
424 reponame="fsck-objects-exclude"
425 setup_invalid_objects
426
427 # We need to prevent MSYS from rewriting /foo into a Windows path.
428 MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo"
429
430 set +e
431 git lfs fsck >test.log 2>&1
432 RET=$?
433 set -e
434
435 [ "$RET" -eq 1 ]
436 [ $(grep -c 'objects: corruptObject: a.dat (.*) is corrupt' test.log) -eq 1 ]
437 [ $(grep -c 'objects: openError: b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
438 [ $(grep -c 'objects: corruptObject: foo/a.dat (.*) is corrupt' test.log) -eq 0 ]
439 [ $(grep -c 'objects: openError: foo/b.dat (.*) could not be checked: .*' test.log) -eq 0 ]
440 [ $(grep -c 'objects: repair: moving corrupt objects to .*' test.log) -eq 1 ]
441
442 cd ..
443 rm -rf $reponame
444 setup_invalid_objects
445
446 # We need to prevent MSYS from rewriting /foo into a Windows path.
447 MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo"
448
449 set +e
450 git lfs fsck --objects >test.log 2>&1
451 RET=$?
452 set -e
453
454 [ "$RET" -eq 1 ]
455 [ $(grep -c 'objects: corruptObject: a.dat (.*) is corrupt' test.log) -eq 1 ]
456 [ $(grep -c 'objects: openError: b.dat (.*) could not be checked: .*' test.log) -eq 1 ]
457 [ $(grep -c 'objects: corruptObject: foo/a.dat (.*) is corrupt' test.log) -eq 0 ]
458 [ $(grep -c 'objects: openError: foo/b.dat (.*) could not be checked: .*' test.log) -eq 0 ]
459 [ $(grep -c 'objects: repair: moving corrupt objects to .*' test.log) -eq 1 ]
460 )
461 end_test
462
463 begin_test "fsck does not detect invalid objects with no LFS objects"
464 (
465 set -e
466
467 reponame="fsck-objects-none"
468 git init "$reponame"
469 cd "$reponame"
470
471 echo "# README" > README.md
472 git add README.md
473 git commit -m "Add README"
474
475 git lfs fsck
476 git lfs fsck --objects
477 )
478 end_test
479
480 begin_test "fsck operates on specified refs"
481 (
482 set -e
483
484 reponame="fsck-refs"
485 setup_invalid_pointers
486
487 git rm -f crlf.dat large.dat
488 echo "# Test" > new.dat
489 git add new.dat
490 git commit -m 'third commit'
491
492 git commit --allow-empty -m 'fourth commit'
493
494 # Should succeed. (HEAD and index).
495
496 git lfs fsck
497 git lfs fsck HEAD
498 git lfs fsck HEAD^^ && exit 1
499 git lfs fsck HEAD^
500 git lfs fsck HEAD^..HEAD
501 git lfs fsck HEAD^^^..HEAD && exit 1
502 git lfs fsck HEAD^^^..HEAD^ && exit 1
503
504 git lfs fsck --pointers HEAD^^^..HEAD^^ >test.log 2>&1 && exit 1
505
506 grep 'pointer: nonCanonicalPointer: Pointer.*was not canonical' test.log
507 grep 'pointer: unexpectedGitObject: "large.dat".*should have been a pointer but was not' test.log
508
509 oid=$(calc_oid_file new.dat)
510 echo "CORRUPTION" >>".git/lfs/objects/${oid:0:2}/${oid:2:2}/$oid"
511
512 git lfs fsck --objects HEAD^^..HEAD^ >test.log 2>&1 && exit 1
513
514 grep 'objects: corruptObject: new.dat (.*) is corrupt' test.log
515 grep 'objects: repair: moving corrupt objects to .*' test.log
516
517 # Make the result of the subshell a success.
518 true
519 )
520 end_test
521
522 begin_test "fsck detects invalid ref"
523 (
524 set -e
525 reponame="fsck-default"
526 git init $reponame
527 cd $reponame
528
529 git lfs fsck jibberish >fsck.log 2>&1 && exit 1
530 grep "can't resolve ref" fsck.log
531 )
532 end_test