Imported Upstream version 2.5.0
[scm/test.git] / t / t-status.sh
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 "$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 "$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 "$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 "$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 "$file_3_new" > file3.dat
42
43   expected="On branch master
44
45 Git LFS objects to be committed:
46
47         file2.dat (LFS: $file_2_oid_short)
48         file3.dat (LFS: $file_3_oid_short)
49
50 Git LFS 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 master
139
140 Git LFS objects to be committed:
141
142
143 Git LFS 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 "$contents" > file1.dat
183   git add file1.dat
184
185   expected="
186 Git LFS objects to be committed:
187
188         file1.dat (LFS: $contents_oid_short)
189
190 Git LFS 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 "$contents" > a.dat
209   printf "$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 "$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 "$contents_2" > a.dat
245
246   expected="On branch master
247
248 Git LFS objects to be committed:
249
250         a.dat (LFS: $contents_1_oid_short)
251
252 Git LFS 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 "$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 "$contents_new" > a.dat
286   git add a.dat
287
288   expected="On branch master
289
290 Git LFS objects to be committed:
291
292         a.dat (LFS: $contents_oid_short -> LFS: $contents_new_oid_short)
293
294 Git LFS 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 "$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 "$contents_new" > a.dat
326   git add a.dat
327
328   expected="On branch master
329
330 Git LFS objects to be committed:
331
332         a.dat (Git: $contents_oid_short -> LFS: $contents_new_oid_short)
333
334 Git LFS 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 "$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 master
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 master
375 Git LFS objects to be pushed to origin/master:
376
377
378 Git LFS objects to be committed:
379
380         a.dat (Git: $contents_oid_short -> LFS: $contents_oid_short)
381
382 Git LFS 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 origin master
430
431   contents="a"
432   oid="$(calc_oid "$contents")"
433   printf "$contents" > a.dat
434
435   git add a.dat
436   git commit -m "add a large file"
437
438   expected="On branch master
439 Git LFS objects to be pushed to origin/master:
440
441         a.dat ($oid)
442
443 Git LFS objects to be committed:
444
445
446 Git LFS objects not staged for commit:"
447
448   [ "$expected" = "$(git lfs status)" ]
449 )
450 end_test