Debian Codegraph @

t/t-status.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/testlib.sh"
4
5 begin_test "status"
6 (
7 set -e
8
9 mkdir repo-1
10 cd repo-1
11 git init
12 git lfs track "*.dat"
13
14 file_1="some data"
15 file_1_oid="$(calc_oid "$file_1")"
16 file_1_oid_short="$(echo "$file_1_oid" | head -c 7)"
17 printf "%s" "$file_1" > file1.dat
18 git add file1.dat
19 git commit -m "file1.dat"
20
21 file_1_new="other data"
22 file_1_new_oid="$(calc_oid "$file_1_new")"
23 file_1_new_oid_short="$(echo "$file_1_new_oid" | head -c 7)"
24 printf "%s" "$file_1_new" > file1.dat
25
26 file_2="file2 data"
27 file_2_oid="$(calc_oid "$file_2")"
28 file_2_oid_short="$(echo "$file_2_oid" | head -c 7)"
29 printf "%s" "$file_2" > file2.dat
30 git add file2.dat
31
32 file_3="file3 data"
33 file_3_oid="$(calc_oid "$file_3")"
34 file_3_oid_short="$(echo "$file_3_oid" | head -c 7)"
35 printf "%s" "$file_3" > file3.dat
36 git add file3.dat
37
38 file_3_new="file3 other data"
39 file_3_new_oid="$(calc_oid "$file_3_new")"
40 file_3_new_oid_short="$(echo "$file_3_new_oid" | head -c 7)"
41 printf "%s" "$file_3_new" > file3.dat
42
43 expected="On branch main
44
45 Objects to be committed:
46
47 file2.dat (LFS: $file_2_oid_short)
48 file3.dat (LFS: $file_3_oid_short)
49
50 Objects not staged for commit:
51
52 file1.dat (LFS: $file_1_oid_short -> File: $file_1_new_oid_short)
53 file3.dat (File: $file_3_new_oid_short)"
54
55 [ "$expected" = "$(git lfs status)" ]
56 )
57 end_test
58
59 begin_test "status --porcelain"
60 (
61 set -e
62
63 mkdir repo-2
64 cd repo-2
65 git init
66 git lfs track "*.dat"
67 echo "some data" > file1.dat
68 git add file1.dat
69 git commit -m "file1.dat"
70
71 echo "other data" > file1.dat
72 echo "file2 data" > file2.dat
73 git add file2.dat
74
75 echo "file3 data" > file3.dat
76 git add file3.dat
77
78 echo "file3 other data" > file3.dat
79
80 expected=" M file1.dat
81 A file3.dat
82 A file2.dat"
83
84 [ "$expected" = "$(git lfs status --porcelain)" ]
85 )
86 end_test
87
88 begin_test "status --json"
89 (
90 set -e
91
92 mkdir repo-3
93 cd repo-3
94 git init
95 git lfs track "*.dat"
96 echo "some data" > file1.dat
97 git add file1.dat
98 git commit -m "file1.dat"
99
100 echo "other data" > file1.dat
101
102 expected='{"files":{"file1.dat":{"status":"M"}}}'
103 [ "$expected" = "$(git lfs status --json)" ]
104
105 git add file1.dat
106 git commit -m "file1.dat changed"
107 git mv file1.dat file2.dat
108
109 expected='{"files":{"file2.dat":{"status":"R","from":"file1.dat"}}}'
110 [ "$expected" = "$(git lfs status --json)" ]
111
112 git commit -m "file1.dat -> file2.dat"
113
114 # Ensure status --json does not include non-lfs files
115 echo hi > test1.txt
116 git add test1.txt
117 expected='{"files":{}}'
118 [ "$expected" = "$(git lfs status --json)" ]
119 )
120 end_test
121
122 begin_test "status in a sub-directory"
123 (
124 set -e
125
126 reponame="status-sub-directory"
127 git init "$reponame"
128 cd "$reponame"
129
130 git lfs track "*.dat"
131 printf "asdf" > file.dat
132 mkdir -p dir
133 git add .gitattributes file.dat
134 git commit -m "initial commit"
135
136 printf "ASDF" > file.dat
137
138 expected="On branch main
139
140 Objects to be committed:
141
142
143 Objects not staged for commit:
144
145 ../file.dat (LFS: f0e4c2f -> File: 99b3bcf)"
146
147 [ "$expected" = "$(cd dir && git lfs status)" ]
148 )
149 end_test
150
151 begin_test "status: outside git repository"
152 (
153 set +e
154 git lfs status 2>&1 > status.log
155 res=$?
156
157 set -e
158 if [ "$res" = "0" ]; then
159 echo "Passes because $GIT_LFS_TEST_DIR is unset."
160 exit 0
161 fi
162 [ "$res" = "128" ]
163 grep "Not in a Git repository" status.log
164 )
165 end_test
166
167 begin_test "status - before initial commit"
168 (
169 set -e
170
171 git init repo-initial
172 cd repo-initial
173 git lfs track "*.dat"
174
175 # should not fail when nothing to display (ignore output, will be blank)
176 git lfs status
177
178 contents="some data"
179 contents_oid="$(calc_oid "$contents")"
180 contents_oid_short="$(echo "$contents_oid" | head -c 7)"
181
182 printf "%s" "$contents" > file1.dat
183 git add file1.dat
184
185 expected="
186 Objects to be committed:
187
188 file1.dat (LFS: $contents_oid_short)
189
190 Objects not staged for commit:"
191
192 [ "$expected" = "$(git lfs status)" ]
193 )
194 end_test
195
196 begin_test "status shows multiple files with identical contents"
197 (
198 set -e
199
200 reponame="uniq-status"
201 mkdir "$reponame"
202 cd "$reponame"
203
204 git init
205 git lfs track "*.dat"
206
207 contents="contents"
208 printf "%s" "$contents" > a.dat
209 printf "%s" "$contents" > b.dat
210
211 git add --all .
212
213 git lfs status | tee status.log
214
215 [ "1" -eq "$(grep -c "a.dat" status.log)" ]
216 [ "1" -eq "$(grep -c "b.dat" status.log)" ]
217 )
218 end_test
219
220 begin_test "status shows multiple copies of partially staged files"
221 (
222 set -e
223
224 reponame="status-partially-staged"
225 git init "$reponame"
226 cd "$reponame"
227
228 git lfs track "*.dat"
229 git add .gitattributes
230 git commit -m "initial commit"
231
232 contents_1="part 1"
233 contents_1_oid="$(calc_oid "$contents_1")"
234 contents_1_oid_short="$(echo "$contents_1_oid" | head -c 7)"
235 printf "%s" "$contents_1" > a.dat
236
237 # "$contents_1" changes are staged
238 git add a.dat
239
240 # "$contents_2" changes are unstaged
241 contents_2="part 2"
242 contents_2_oid="$(calc_oid "$contents_2")"
243 contents_2_oid_short="$(echo "$contents_2_oid" | head -c 7)"
244 printf "%s" "$contents_2" > a.dat
245
246 expected="On branch main
247
248 Objects to be committed:
249
250 a.dat (LFS: $contents_1_oid_short)
251
252 Objects not staged for commit:
253
254 a.dat (File: $contents_2_oid_short)"
255 actual="$(git lfs status)"
256
257 diff -u <(echo "$expected") <(echo "$actual")
258 )
259 end_test
260
261 begin_test "status: LFS to LFS change"
262 (
263 set -e
264
265 reponame="status-lfs-to-lfs-change"
266 setup_remote_repo "$reponame"
267 clone_repo "$reponame" "$reponame"
268
269 contents="contents"
270 contents_oid="$(calc_oid "$contents")"
271 contents_oid_short="$(echo "$contents_oid" | head -c 7)"
272
273 git lfs track "*.dat"
274 git add .gitattributes
275 git commit -m "track *.dat files"
276
277 printf "%s" "$contents" > a.dat
278 git add a.dat
279 git commit -m "add a.dat"
280
281 contents_new="$contents +extra"
282 contents_new_oid="$(calc_oid "$contents_new")"
283 contents_new_oid_short="$(echo $contents_new_oid | head -c 7)"
284
285 printf "%s" "$contents_new" > a.dat
286 git add a.dat
287
288 expected="On branch main
289
290 Objects to be committed:
291
292 a.dat (LFS: $contents_oid_short -> LFS: $contents_new_oid_short)
293
294 Objects not staged for commit:"
295 actual="$(git lfs status)"
296
297 [ "$expected" = "$actual" ]
298 )
299 end_test
300
301 begin_test "status: Git to LFS change"
302 (
303 set -e
304
305 reponame="status-git-to-lfs-change"
306 setup_remote_repo "$reponame"
307 clone_repo "$reponame" "$reponame"
308
309 contents="contents"
310 contents_oid="$(calc_oid "$contents")"
311 contents_oid_short="$(echo "$contents_oid" | head -c 7)"
312
313 printf "%s" "$contents" > a.dat
314 git add a.dat
315 git commit -m "add a.dat"
316
317 git lfs track "*.dat"
318 git add .gitattributes
319 git commit -m "track *.dat files"
320
321 contents_new="$contents +extra"
322 contents_new_oid="$(calc_oid "$contents_new")"
323 contents_new_oid_short="$(echo $contents_new_oid | head -c 7)"
324
325 printf "%s" "$contents_new" > a.dat
326 git add a.dat
327
328 expected="On branch main
329
330 Objects to be committed:
331
332 a.dat (Git: $contents_oid_short -> LFS: $contents_new_oid_short)
333
334 Objects not staged for commit:"
335 actual="$(git lfs status)"
336
337 [ "$expected" = "$actual" ]
338 )
339 end_test
340
341 begin_test "status: Git to LFS conversion"
342 (
343 set -e
344
345 reponame="status-git-to-lfs-conversion"
346 setup_remote_repo "$reponame"
347 clone_repo "$reponame" "$reponame"
348
349 contents="contents"
350 contents_oid="$(calc_oid "$contents")"
351 contents_oid_short="$(echo "$contents_oid" | head -c 7)"
352
353 printf "%s" "$contents" > a.dat
354 git add a.dat
355 git commit -m "add a.dat"
356
357 git lfs track "*.dat"
358 git add .gitattributes
359 git commit -m "track *.dat"
360
361 git push origin main
362
363 pushd "$TRASHDIR" > /dev/null
364 clone_repo "$reponame" "$reponame-2"
365
366 git add a.dat
367
368 git lfs status 2>&1 | tee status.log
369 if [ "0" -ne "${PIPESTATUS[0]}" ]; then
370 echo >&2 "git lfs status should have succeeded, didn't ..."
371 exit 1
372 fi
373
374 expected="On branch main
375 Objects to be pushed to origin/main:
376
377
378 Objects to be committed:
379
380 a.dat (Git: $contents_oid_short -> LFS: $contents_oid_short)
381
382 Objects not staged for commit:"
383 actual="$(cat status.log)"
384
385 [ "$expected" = "$actual" ]
386 popd > /dev/null
387 )
388 end_test
389
390 begin_test "status (missing objects)"
391 (
392 set -e
393
394 reponame="status-missing-objects"
395 git init "$reponame"
396 cd "$reponame"
397
398 git lfs track "*.dat"
399 printf "a" > a.dat
400
401 git add .gitattributes a.dat
402 git commit -m "initial commit"
403
404 # Remove the original object "a.dat" (ensure '--no-filters' is not given).
405 oid="$(git hash-object -t blob -- a.dat)"
406 rm -rf ".git/objects/${oid:0:2}/${oid:2}"
407
408 # Create an unstaged change against a source file that doesn't exist.
409 printf "b" > a.dat
410 git add a.dat
411
412 git lfs status \
413 | grep "a.dat (?: <missing> -> LFS: $(calc_oid b | head -c 7))"
414 )
415 end_test
416
417 begin_test "status (unpushed objects)"
418 (
419 set -e
420
421 reponame="status-unpushed-objects"
422 setup_remote_repo "$reponame"
423 clone_repo "$reponame" "$reponame"
424
425 git lfs track "*.dat"
426 git add .gitattributes
427 git commit -m "initial commit"
428
429 git push -u origin main
430
431 contents="a"
432 oid="$(calc_oid "$contents")"
433 printf "%s" "$contents" > a.dat
434
435 git add a.dat
436 git commit -m "add a large file"
437
438 expected="On branch main
439 Objects to be pushed to origin/main:
440
441 a.dat ($oid)
442
443 Objects to be committed:
444
445
446 Objects not staged for commit:"
447
448 [ "$expected" = "$(git lfs status)" ]
449 )
450 end_test
451
452 begin_test "status (without a working copy)"
453 (
454 reponame="status-no-working-copy.git"
455
456 git init --bare "$reponame"
457 cd "$reponame"
458
459 git lfs status 2>&1 | tee status.log
460 if [ "0" -eq "${PIPESTATUS[0]}" ]; then
461 echo >&2 "git lfs status should have failed, didn't ..."
462 exit 1
463 fi
464 [ "This operation must be run in a work tree." = "$(cat status.log)" ]
465 )
466 end_test
467
468 begin_test "status (deleted files)"
469 (
470 set -e
471
472 reponame="status-deleted-files"
473 setup_remote_repo "$reponame"
474 clone_repo "$reponame" "$reponame"
475
476 git lfs track "*.dat"
477 git add .gitattributes
478 git commit -m "initial commit"
479
480 git push -u origin main
481
482 contents="a"
483 oid="$(calc_oid "$contents")"
484 oid_short="$(calc_oid "$contents" | head -c 7)"
485 printf "%s" "$contents" > a.dat
486
487 git add a.dat
488 git commit -m "add a large file"
489
490 git rm a.dat
491
492 expected="On branch main
493 Objects to be pushed to origin/main:
494
495 a.dat ($oid)
496
497 Objects to be committed:
498
499 a.dat (LFS: $oid_short -> File: deleted)
500
501 Objects not staged for commit:"
502
503 [ "$expected" = "$(git lfs status)" ]
504 )
505 end_test
506
507 begin_test "status (file to dir)"
508 (
509 set -e
510
511 reponame="status-file-to-dir"
512 setup_remote_repo "$reponame"
513 clone_repo "$reponame" "$reponame"
514
515 git lfs track "*.dat"
516 git add .gitattributes
517 git commit -m "initial commit"
518
519 printf 'file' > test
520 git add test
521 git commit -m "add test"
522
523 obj=$(calc_oid "file" | head -c 7)
524
525 git rm test
526 mkdir test
527
528 contents="a"
529 oid="$(calc_oid "$contents")"
530 oid_short="$(calc_oid "$contents" | head -c 7)"
531 printf "%s" "$contents" > test/a.dat
532
533 git add test
534 git commit -m "add files"
535
536 git reset HEAD~
537 git add test
538
539 expected="On branch main
540
541 Objects to be committed:
542
543 test (Git: $obj -> File: deleted)
544 test/a.dat (LFS: $oid_short)
545
546 Objects not staged for commit:"
547
548 [ "$expected" = "$(git lfs status)" ]
549 )
550 end_test
551
552
553 begin_test "status: permission change"
554 (
555 set -e
556
557 # We're using chmod below.
558 if [ "$IS_WINDOWS" -eq 1 ]; then
559 exit 0
560 fi
561
562 reponame="status-permission-change"
563 setup_remote_repo "$reponame"
564 clone_repo "$reponame" "$reponame"
565
566 contents="contents"
567
568 git lfs track "*.dat"
569 git add .gitattributes
570 git commit -m "track *.dat"
571
572 printf "%s" "$contents" > a.dat
573 git add a.dat
574 git commit -m "add a.dat"
575
576 chmod 400 a.dat
577
578 # A permission change should not result in any output.
579 git lfs status 2>&1 | tee status.log
580 if [ "0" -ne "${PIPESTATUS[0]}" ]; then
581 echo >&2 "git lfs status should have succeeded, didn't ..."
582 exit 1
583 fi
584
585 expected="On branch main
586
587 Objects to be committed:
588
589
590 Objects not staged for commit:"
591 actual="$(cat status.log)"
592
593 [ "$expected" = "$actual" ]
594 )
595 end_test