Imported Upstream version 2.5.1
[scm/test.git] / t / t-pre-push.sh
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/testlib.sh"
4
5 begin_test "pre-push with good ref"
6 (
7   set -e
8   reponame="pre-push-master-branch-required"
9   setup_remote_repo "$reponame"
10   clone_repo "$reponame" "$reponame"
11
12   git config "lfs.$(repo_endpoint "$GITSERVER" "$reponame").locksverify" false
13   git lfs track "*.dat"
14   echo "hi" > a.dat
15   git add .gitattributes a.dat
16   git commit -m "add a.dat"
17
18   refute_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/master"
19
20   # for some reason, using 'tee' and $PIPESTATUS does not work here
21   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
22     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 > push.log
23
24   assert_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/master"
25 )
26 end_test
27
28 begin_test "pre-push with tracked ref"
29 (
30   set -e
31   reponame="pre-push-tracked-branch-required"
32   setup_remote_repo "$reponame"
33   clone_repo "$reponame" "$reponame"
34
35   git config "lfs.$(repo_endpoint "$GITSERVER" "$reponame").locksverify" false
36   git lfs track "*.dat"
37   echo "hi" > a.dat
38   git add .gitattributes a.dat
39   git commit -m "add a.dat"
40
41   refute_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/tracked"
42
43   # for some reason, using 'tee' and $PIPESTATUS does not work here
44   echo "refs/heads/master master refs/heads/tracked 0000000000000000000000000000000000000000" |
45     git lfs pre-push origin master 2>&1 > push.log
46
47   assert_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/tracked"
48 )
49 end_test
50
51 begin_test "pre-push with bad ref"
52 (
53   set -e
54   reponame="pre-push-other-branch-required"
55   setup_remote_repo "$reponame"
56   clone_repo "$reponame" "$reponame"
57
58   git config "lfs.$(repo_endpoint "$GITSERVER" "$reponame").locksverify" false
59   git lfs track "*.dat"
60   echo "hi" > a.dat
61   git add .gitattributes a.dat
62   git commit -m "add a.dat"
63
64   refute_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/other"
65
66   # for some reason, using 'tee' and $PIPESTATUS does not work here
67   set +e
68   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
69     git lfs pre-push origin "$GITSERVER/$reponame" 2> push.log
70   pushcode=$?
71   set -e
72
73   if [ "0" -eq "$pushcode" ]; then
74     echo "expected command to fail"
75     exit 1
76   fi
77
78   grep 'Expected ref "refs/heads/other", got "refs/heads/master"' push.log
79
80   refute_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4 "refs/heads/other"
81 )
82 end_test
83
84 begin_test "pre-push"
85 (
86   set -e
87
88   reponame="$(basename "$0" ".sh")"
89   setup_remote_repo "$reponame"
90
91   clone_repo "$reponame" repo
92   git lfs track "*.dat"
93   git add .gitattributes
94   git commit -m "add git attributes"
95
96   git config "lfs.$(repo_endpoint $GITSERVER $reponame).locksverify" true
97
98   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
99     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
100     tee push.log
101   # no output if nothing to do
102   [ "$(du -k push.log | cut -f 1)" == "0" ]
103
104   git lfs track "*.dat"
105   echo "hi" > hi.dat
106   git add hi.dat
107   git commit -m "add hi.dat"
108   git show
109
110   refute_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4
111
112   # push file to the git lfs server
113   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
114     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
115     tee push.log
116   grep "Uploading LFS objects: 100% (1/1), 3 B" push.log
117
118   assert_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4
119 )
120 end_test
121
122 begin_test "pre-push dry-run"
123 (
124   set -e
125
126   reponame="$(basename "$0" ".sh")-dry-run"
127   setup_remote_repo "$reponame"
128
129   clone_repo "$reponame" repo-dry-run
130   git lfs track "*.dat"
131   git add .gitattributes
132   git commit -m "add git attributes"
133
134   git config "lfs.$(repo_endpoint $GITSERVER $reponame).locksverify" true
135
136   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
137     git lfs pre-push --dry-run origin "$GITSERVER/$reponame" 2>&1 |
138     tee push.log
139
140   [ "" = "$(cat push.log)" ]
141
142   git lfs track "*.dat"
143   echo "dry" > hi.dat
144   git add hi.dat
145   git commit -m "add hi.dat"
146   git show
147
148   refute_server_object "$reponame" 2840e0eafda1d0760771fe28b91247cf81c76aa888af28a850b5648a338dc15b
149
150   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
151     git lfs pre-push --dry-run origin "$GITSERVER/$reponame" 2>&1 |
152     tee push.log
153   grep "push 2840e0eafda1d0760771fe28b91247cf81c76aa888af28a850b5648a338dc15b => hi.dat" push.log
154   cat push.log
155   [ `wc -l < push.log` = 1 ]
156
157   refute_server_object "$reponame" 2840e0eafda1d0760771fe28b91247cf81c76aa888af28a850b5648a338dc15b
158 )
159 end_test
160
161 begin_test "pre-push 307 redirects"
162 (
163   set -e
164
165   reponame="$(basename "$0" ".sh")"
166   setup_remote_repo "$reponame"
167
168   clone_repo "$reponame" repo-307
169   git lfs track "*.dat"
170   git add .gitattributes
171   git commit -m "add git attributes"
172
173   # relative redirect
174   git config remote.origin.lfsurl "$GITSERVER/redirect307/rel/$reponame.git/info/lfs"
175
176   git lfs track "*.dat"
177   echo "hi" > hi.dat
178   git add hi.dat
179   git commit -m "add hi.dat"
180   git show
181
182   # push file to the git lfs server
183   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
184     git lfs pre-push origin "$GITSERVER/redirect307/rel/$reponame.git/info/lfs" 2>&1 |
185     tee push.log
186   grep "Uploading LFS objects: 100% (1/1), 3 B" push.log
187
188   assert_server_object "$reponame" 98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4
189
190   # absolute redirect
191   git config remote.origin.lfsurl "$GITSERVER/redirect307/abs/$reponame.git/info/lfs"
192
193   echo "hi" > hi2.dat
194   git add hi2.dat
195   git commit -m "add hi2.dat"
196   git show
197
198   # push file to the git lfs server
199   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
200     git lfs pre-push origin "$GITSERVER/redirect307/abs/$reponame.git/info/lfs" 2>&1 |
201     tee push.log
202   grep "Uploading LFS objects: 100% (1/1), 3 B" push.log
203 )
204 end_test
205
206 begin_test "pre-push with existing file"
207 (
208   set -e
209
210   reponame="$(basename "$0" ".sh")-existing-file"
211   setup_remote_repo "$reponame"
212
213   clone_repo "$reponame" existing-file
214   echo "existing" > existing.dat
215   git add existing.dat
216   git commit -m "add existing dat"
217
218   git lfs track "*.dat"
219   echo "new" > new.dat
220   git add new.dat
221   git add .gitattributes
222   git commit -m "add new file through git lfs"
223
224   # push file to the git lfs server
225   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
226     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
227     tee push.log
228   grep "Uploading LFS objects: 100% (1/1), 4 B" push.log
229
230   # now the file exists
231   assert_server_object "$reponame" 7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c
232 )
233 end_test
234
235 begin_test "pre-push with existing pointer"
236 (
237   set -e
238
239   reponame="$(basename "$0" ".sh")-existing-pointer"
240   setup_remote_repo "$reponame"
241   clone_repo "$reponame" existing-pointer
242
243   echo "$(pointer "7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c" 4)" > new.dat
244   git add new.dat
245   git commit -m "add new pointer"
246   mkdir -p .git/lfs/objects/7a/a7
247   echo "new" > .git/lfs/objects/7a/a7/7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c
248
249   # push file to the git lfs server
250   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
251     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
252     tee push.log
253   grep "Uploading LFS objects: 100% (1/1), 4 B" push.log
254 )
255 end_test
256
257 begin_test "pre-push with missing pointer not on server"
258 (
259   set -e
260
261   reponame="$(basename "$0" ".sh")-missing-pointer"
262   setup_remote_repo "$reponame"
263   clone_repo "$reponame" missing-pointer
264
265   oid="7aa7a5359173d05b63cfd682e3c38487f3cb4f7f1d60659fe59fab1505977d4c"
266
267   echo "$(pointer "$oid" 4)" > new.dat
268   git add new.dat
269   git commit -m "add new pointer"
270
271   # assert that push fails
272   set +e
273   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
274     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
275     tee push.log
276   set -e
277
278   grep "  (missing) new.dat ($oid)" push.log
279 )
280 end_test
281
282 begin_test "pre-push with missing pointer which is on server"
283 (
284   # should permit push if files missing locally but are on server, shouldn't
285   # require client to have every file (prune)
286   set -e
287
288   reponame="$(basename "$0" ".sh")-missing-but-on-server"
289   setup_remote_repo "$reponame"
290   clone_repo "$reponame" missing-but-on-server
291
292   contents="common data"
293   contents_oid=$(calc_oid "$contents")
294   git lfs track "*.dat"
295   printf "$contents" > common1.dat
296   git add common1.dat
297   git add .gitattributes
298   git commit -m "add first file"
299
300   # push file to the git lfs server
301   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
302     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
303     tee push.log
304   grep "Uploading LFS objects: 100% (1/1), 11 B" push.log
305
306   # now the file exists
307   assert_server_object "$reponame" "$contents_oid"
308
309   # create another commit referencing same oid, then delete local data & push
310   printf "$contents" > common2.dat
311   git add common2.dat
312   git commit -m "add second file, same content"
313   rm -rf .git/lfs/objects
314   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
315     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
316     tee push.log
317   # make sure there were no errors reported
318   [ -z "$(grep -i 'Error' push.log)" ]
319
320 )
321 end_test
322
323 begin_test "pre-push with missing and present pointers (lfs.allowincompletepush true)"
324 (
325   set -e
326
327   reponame="pre-push-missing-and-present"
328   setup_remote_repo "$reponame"
329   clone_repo "$reponame" "$reponame"
330
331   git lfs track "*.dat"
332   git add .gitattributes
333   git commit -m "initial commit"
334
335   present="present"
336   present_oid="$(calc_oid "$present")"
337   printf "$present" > present.dat
338
339   missing="missing"
340   missing_oid="$(calc_oid "$missing")"
341   printf "$missing" > missing.dat
342
343   git add present.dat missing.dat
344   git commit -m "add present.dat and missing.dat"
345
346   git rm missing.dat
347   git commit -m "remove missing"
348
349   # :fire: the "missing" object
350   missing_oid_part_1="$(echo "$missing_oid" | cut -b 1-2)"
351   missing_oid_part_2="$(echo "$missing_oid" | cut -b 3-4)"
352   missing_oid_path=".git/lfs/objects/$missing_oid_part_1/$missing_oid_part_2/$missing_oid"
353   rm "$missing_oid_path"
354
355   git config lfs.allowincompletepush true
356
357   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
358     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
359     tee push.log
360
361   if [ "0" -ne "${PIPESTATUS[1]}" ]; then
362     echo >&2 "fatal: expected \`git lfs pre-push origin $GITSERVER/$reponame\` to succeed..."
363     exit 1
364   fi
365
366   grep "LFS upload missing objects" push.log
367   grep "  (missing) missing.dat ($missing_oid)" push.log
368
369   assert_server_object "$reponame" "$present_oid"
370   refute_server_object "$reponame" "$missing_oid"
371 )
372 end_test
373
374 begin_test "pre-push reject missing pointers (lfs.allowincompletepush default)"
375 (
376   set -e
377
378   reponame="pre-push-reject-missing-and-present"
379   setup_remote_repo "$reponame"
380   clone_repo "$reponame" "$reponame"
381
382   git lfs track "*.dat"
383   git add .gitattributes
384   git commit -m "initial commit"
385
386   present="present"
387   present_oid="$(calc_oid "$present")"
388   printf "$present" > present.dat
389
390   missing="missing"
391   missing_oid="$(calc_oid "$missing")"
392   printf "$missing" > missing.dat
393
394   git add present.dat missing.dat
395   git commit -m "add present.dat and missing.dat"
396
397   git rm missing.dat
398   git commit -m "remove missing"
399
400   # :fire: the "missing" object
401   missing_oid_part_1="$(echo "$missing_oid" | cut -b 1-2)"
402   missing_oid_part_2="$(echo "$missing_oid" | cut -b 3-4)"
403   missing_oid_path=".git/lfs/objects/$missing_oid_part_1/$missing_oid_part_2/$missing_oid"
404   rm "$missing_oid_path"
405
406   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
407     git lfs pre-push origin "$GITSERVER/$reponame" 2>&1 |
408     tee push.log
409
410   if [ "2" -ne "${PIPESTATUS[1]}" ]; then
411     echo >&2 "fatal: expected \`git lfs pre-push origin $GITSERVER/$reponame\` to fail..."
412     exit 1
413   fi
414
415   grep "no such file or directory" push.log || # unix
416     grep "cannot find the file" push.log       # windows
417
418   refute_server_object "$reponame" "$present_oid"
419   refute_server_object "$reponame" "$missing_oid"
420 )
421 end_test
422
423 begin_test "pre-push multiple branches"
424 (
425   set -e
426
427   reponame="$(basename "$0" ".sh")-multiple-branches"
428   setup_remote_repo "$reponame"
429   clone_repo "$reponame" "$reponame"
430
431
432   git lfs track "*.dat" 2>&1 | tee track.log
433   grep "Tracking \"\*.dat\"" track.log
434
435   NUMFILES=6
436   # generate content we'll use
437   for ((a=0; a < NUMFILES ; a++))
438   do
439     content[$a]="filecontent$a"
440     oid[$a]=$(calc_oid "${content[$a]}")
441   done
442
443   echo "[
444   {
445     \"CommitDate\":\"$(get_date -10d)\",
446     \"Files\":[
447       {\"Filename\":\"file1.dat\",\"Size\":${#content[0]}, \"Data\":\"${content[0]}\"},
448       {\"Filename\":\"file2.dat\",\"Size\":${#content[1]}, \"Data\":\"${content[1]}\"}]
449   },
450   {
451     \"NewBranch\":\"branch1\",
452     \"CommitDate\":\"$(get_date -5d)\",
453     \"Files\":[
454       {\"Filename\":\"file2.dat\",\"Size\":${#content[2]}, \"Data\":\"${content[2]}\"}]
455   },
456   {
457     \"ParentBranches\":[\"master\"],
458     \"NewBranch\":\"branch2\",
459     \"CommitDate\":\"$(get_date -5d)\",
460     \"Files\":[
461       {\"Filename\":\"file3.dat\",\"Size\":${#content[3]}, \"Data\":\"${content[3]}\"}]
462   },
463   {
464     \"ParentBranches\":[\"master\"],
465     \"NewBranch\":\"branch3\",
466     \"CommitDate\":\"$(get_date -2d)\",
467     \"Files\":[
468       {\"Filename\":\"file1.dat\",\"Size\":${#content[4]}, \"Data\":\"${content[4]}\"}]
469   },
470   {
471     \"ParentBranches\":[\"master\"],
472     \"NewBranch\":\"branch4\",
473     \"CommitDate\":\"$(get_date -1d)\",
474     \"Files\":[
475       {\"Filename\":\"file4.dat\",\"Size\":${#content[5]}, \"Data\":\"${content[5]}\"}]
476   }
477   ]" | lfstest-testutils addcommits
478
479   # make sure when called via git push all branches are updated
480   git push origin master branch1 branch2 branch3 branch4
481   for ((a=0; a < NUMFILES ; a++))
482   do
483     assert_server_object "$reponame" "${oid[$a]}"
484   done
485
486 )
487 end_test
488
489 begin_test "pre-push with bad remote"
490 (
491   set -e
492
493   cd repo
494
495   echo "refs/heads/master master refs/heads/master 0000000000000000000000000000000000000000" |
496     git lfs pre-push not-a-remote "$GITSERVER/$reponame" 2>&1 |
497     tee pre-push.log
498   grep "Invalid remote name" pre-push.log
499 )
500 end_test
501
502 begin_test "pre-push unfetched deleted remote branch & server GC"
503 (
504   # point of this is to simulate the case where the local cache of the remote
505   # branch state contains a branch which has actually been deleted on the remote,
506   # the client just doesn't know yet (hasn't done 'git fetch origin --prune')
507   # If the server GC'd the objects that deleted branch contained, but they were
508   # referenced by a branch being pushed (earlier commit), push might assume it
509   # doesn't have to push it, but it does. Tests that we check the real remote refs
510   # before making an assumption about the diff we need to push
511   set -e
512
513   reponame="$(basename "$0" ".sh")-server-deleted-branch-gc"
514   setup_remote_repo "$reponame"
515   clone_repo "$reponame" "$reponame"
516
517
518   git lfs track "*.dat" 2>&1 | tee track.log
519   grep "Tracking \"\*.dat\"" track.log
520
521   NUMFILES=4
522   # generate content we'll use
523   for ((a=0; a < NUMFILES ; a++))
524   do
525     content[$a]="filecontent$a"
526     oid[$a]=$(calc_oid "${content[$a]}")
527   done
528
529   echo "[
530   {
531     \"CommitDate\":\"$(get_date -10d)\",
532     \"Files\":[
533       {\"Filename\":\"file1.dat\",\"Size\":${#content[0]}, \"Data\":\"${content[0]}\"},
534       {\"Filename\":\"file2.dat\",\"Size\":${#content[1]}, \"Data\":\"${content[1]}\"}]
535   },
536   {
537     \"NewBranch\":\"branch-to-delete\",
538     \"CommitDate\":\"$(get_date -5d)\",
539     \"Files\":[
540       {\"Filename\":\"file3.dat\",\"Size\":${#content[2]}, \"Data\":\"${content[2]}\"}]
541   },
542   {
543     \"NewBranch\":\"branch-to-push-after\",
544     \"CommitDate\":\"$(get_date -2d)\",
545     \"Files\":[
546       {\"Filename\":\"file4.dat\",\"Size\":${#content[3]}, \"Data\":\"${content[3]}\"}]
547   }
548   ]" | lfstest-testutils addcommits
549
550   # push only the first 2 branches
551   git push origin master branch-to-delete
552   for ((a=0; a < 3 ; a++))
553   do
554     assert_server_object "$reponame" "${oid[$a]}"
555   done
556   # confirm we haven't pushed the last one yet
557   refute_server_object "$reponame" "${oid[3]}"
558   # copy the cached remote ref for the branch we're going to delete remotely
559   cp .git/refs/remotes/origin/branch-to-delete branch-to-delete.ref
560   # now delete the branch on the server
561   git push origin --delete branch-to-delete
562   # remove the OID in it, as if GC'd
563   delete_server_object "$reponame" "${oid[2]}"
564   refute_server_object "$reponame" "${oid[2]}"
565   # Now put the cached remote ref back, as if someone else had deleted it but
566   # we hadn't done git fetch --prune yet
567   mv branch-to-delete.ref .git/refs/remotes/origin/branch-to-delete
568   # Confirm that local cache of remote branch is back
569   git branch -r 2>&1 | tee branch-r.log
570   grep "origin/branch-to-delete" branch-r.log
571   # Now push later branch which should now need to re-push previous commits LFS too
572   git push origin branch-to-push-after
573   # all objects should now be there even though cached remote branch claimed it already had file3.dat
574   for ((a=0; a < NUMFILES ; a++))
575   do
576     assert_server_object "$reponame" "${oid[$a]}"
577   done
578
579 )
580 end_test
581
582 begin_test "pre-push delete branch"
583 (
584   set -e
585
586   reponame="$(basename "$0" ".sh")-delete-branch"
587   setup_remote_repo "$reponame"
588   clone_repo "$reponame" "$reponame"
589
590
591   git lfs track "*.dat" 2>&1 | tee track.log
592   grep "Tracking \"\*.dat\"" track.log
593
594   NUMFILES=4
595   # generate content we'll use
596   for ((a=0; a < NUMFILES ; a++))
597   do
598     content[$a]="filecontent$a"
599     oid[$a]=$(calc_oid "${content[$a]}")
600   done
601
602   echo "[
603   {
604     \"CommitDate\":\"$(get_date -2d)\",
605     \"Files\":[
606       {\"Filename\":\"file1.dat\",\"Size\":${#content[0]}, \"Data\":\"${content[0]}\"},
607       {\"Filename\":\"file2.dat\",\"Size\":${#content[1]}, \"Data\":\"${content[1]}\"}]
608   },
609   {
610     \"NewBranch\":\"branch-to-delete\",
611     \"CommitDate\":\"$(get_date -1d)\",
612     \"Files\":[
613       {\"Filename\":\"file3.dat\",\"Size\":${#content[2]}, \"Data\":\"${content[2]}\"}]
614   },
615   {
616     \"ParentBranches\":[\"master\"],
617     \"CommitDate\":\"$(get_date -0d)\",
618     \"Files\":[
619       {\"Filename\":\"file4.dat\",\"Size\":${#content[3]}, \"Data\":\"${content[3]}\"}]
620   }
621   ]" | lfstest-testutils addcommits
622
623   # push all branches
624   git push origin master branch-to-delete
625   for ((a=0; a < NUMFILES ; a++))
626   do
627     assert_server_object "$reponame" "${oid[$a]}"
628   done
629
630   # deleting a branch with git push should not fail
631   # (requires correct special casing of "(delete) 0000000000.." in hook)
632   git push origin --delete branch-to-delete
633 )
634 end_test
635
636 begin_test "pre-push with our lock"
637 (
638   set -e
639
640   reponame="pre_push_owned_locks"
641   setup_remote_repo "$reponame"
642   clone_repo "$reponame" "$reponame"
643
644   git lfs track "*.dat"
645   git add .gitattributes
646   git commit -m "initial commit"
647
648   contents="locked contents"
649   printf "$contents" > locked.dat
650   git add locked.dat
651   git commit -m "add locked.dat"
652
653   git push origin master
654
655   git lfs lock --json "locked.dat" | tee lock.log
656
657   id=$(assert_lock lock.log locked.dat)
658   assert_server_lock $id
659
660   printf "authorized changes" >> locked.dat
661   git add locked.dat
662   git commit -m "add unauthorized changes"
663
664   GIT_CURL_VERBOSE=1 git push origin master 2>&1 | tee push.log
665   grep "Consider unlocking your own locked files" push.log
666   grep "* locked.dat" push.log
667
668   assert_server_lock "$id"
669 )
670 end_test
671
672 begin_test "pre-push with their lock on lfs file"
673 (
674   set -e
675
676   reponame="pre_push_unowned_lock"
677   setup_remote_repo "$reponame"
678   clone_repo "$reponame" "$reponame"
679
680   git lfs track "*.dat"
681   git add .gitattributes
682   git commit -m "initial commit"
683
684   contents="locked contents"
685
686   # any lock path with "theirs" is returned as "their" lock by /locks/verify
687   printf "$contents" > locked_theirs.dat
688   git add locked_theirs.dat
689   git commit -m "add locked_theirs.dat"
690
691   git push origin master
692
693   git lfs lock --json "locked_theirs.dat" | tee lock.log
694   id=$(assert_lock lock.log locked_theirs.dat)
695   assert_server_lock $id
696
697   pushd "$TRASHDIR" >/dev/null
698     clone_repo "$reponame" "$reponame-assert"
699     git config lfs.locksverify true
700
701     printf "unauthorized changes" >> locked_theirs.dat
702     git add locked_theirs.dat
703     # --no-verify is used to avoid the pre-commit hook which is not under test
704     git commit --no-verify -m "add unauthorized changes"
705
706     git push origin master 2>&1 | tee push.log
707     res="${PIPESTATUS[0]}"
708     if [ "0" -eq "$res" ]; then
709       echo "push should fail"
710       exit 1
711     fi
712
713     grep "Unable to push locked files" push.log
714     grep "* locked_theirs.dat - Git LFS Tests" push.log
715
716     grep "ERROR: Cannot update locked files." push.log
717     refute_server_object "$reponame" "$(calc_oid_file locked_theirs.dat)"
718   popd >/dev/null
719 )
720 end_test
721
722 begin_test "pre-push with their lock on non-lfs lockable file"
723 (
724   set -e
725
726   reponame="pre_push_unowned_lock_not_lfs"
727   setup_remote_repo "$reponame"
728   clone_repo "$reponame" "$reponame"
729
730   echo "*.dat lockable" > .gitattributes
731   git add .gitattributes
732   git commit -m "initial commit"
733
734   # any lock path with "theirs" is returned as "their" lock by /locks/verify
735   echo "hi" > readme.txt
736   echo "tiny" > tiny_locked_theirs.dat
737   git help > large_locked_theirs.dat
738   git add readme.txt tiny_locked_theirs.dat large_locked_theirs.dat
739   git commit -m "add initial files"
740
741   git push origin master
742
743   git lfs lock --json "tiny_locked_theirs.dat" | tee lock.log
744   id=$(assert_lock lock.log tiny_locked_theirs.dat)
745   assert_server_lock $id
746
747   git lfs lock --json "large_locked_theirs.dat" | tee lock.log
748   id=$(assert_lock lock.log large_locked_theirs.dat)
749   assert_server_lock $id
750
751   pushd "$TRASHDIR" >/dev/null
752     clone_repo "$reponame" "$reponame-assert"
753     git config lfs.locksverify true
754
755     git lfs update # manually add pre-push hook, since lfs clean hook is not used
756     echo "other changes" >> readme.txt
757     echo "unauthorized changes" >> large_locked_theirs.dat
758     echo "unauthorized changes" >> tiny_locked_theirs.dat
759     # --no-verify is used to avoid the pre-commit hook which is not under test
760     git commit --no-verify -am "add unauthorized changes"
761
762     git push origin master 2>&1 | tee push.log
763     res="${PIPESTATUS[0]}"
764     if [ "0" -eq "$res" ]; then
765       echo "push should fail"
766       exit 1
767     fi
768
769     grep "Unable to push locked files" push.log
770     grep "* large_locked_theirs.dat - Git LFS Tests" push.log
771     grep "* tiny_locked_theirs.dat - Git LFS Tests" push.log
772     grep "ERROR: Cannot update locked files." push.log
773
774     refute_server_object "$reponame" "$(calc_oid_file large_locked_theirs.dat)"
775     refute_server_object "$reponame" "$(calc_oid_file tiny_locked_theirs.dat)"
776   popd >/dev/null
777 )
778 end_test
779
780 begin_test "pre-push locks verify 5xx with verification enabled"
781 (
782   set -e
783
784   reponame="lock-enabled-verify-5xx"
785   setup_remote_repo "$reponame"
786   clone_repo "$reponame" "$reponame"
787
788   endpoint="$(repo_endpoint $GITSERVER $reponame)"
789
790   contents="example"
791   contents_oid="$(calc_oid "$contents")"
792   printf "$contents" > a.dat
793   git lfs track "*.dat"
794   git add .gitattributes a.dat
795   git commit --message "initial commit"
796
797   git config "lfs.$endpoint.locksverify" true
798
799   git push origin master 2>&1 | tee push.log
800   grep "\"origin\" does not support the LFS locking API" push.log
801   grep "git config lfs.$endpoint.locksverify false" push.log
802
803   refute_server_object "$reponame" "$contents_oid"
804 )
805 end_test
806
807 begin_test "pre-push disable locks verify on exact url"
808 (
809   set -e
810
811   reponame="lock-disabled-verify-5xx"
812   setup_remote_repo "$reponame"
813   clone_repo "$reponame" "$reponame"
814
815   endpoint="$(repo_endpoint $GITSERVER $reponame)"
816
817   contents="example"
818   contents_oid="$(calc_oid "$contents")"
819   printf "$contents" > a.dat
820   git lfs track "*.dat"
821   git add .gitattributes a.dat
822   git commit --message "initial commit"
823
824   git config "lfs.$endpoint.locksverify" false
825
826   git push origin master 2>&1 | tee push.log
827   [ "0" -eq "$(grep -c "\"origin\" does not support the LFS locking API" push.log)" ]
828
829   assert_server_object "$reponame" "$contents_oid"
830 )
831 end_test
832
833 begin_test "pre-push disable locks verify on partial url"
834 (
835   set -e
836
837   reponame="lock-disabled-verify-5xx-partial"
838   setup_remote_repo "$reponame"
839   clone_repo "$reponame" "$reponame"
840
841   endpoint="$server/$repo"
842
843   contents="example"
844   contents_oid="$(calc_oid "$contents")"
845   printf "$contents" > a.dat
846   git lfs track "*.dat"
847   git add .gitattributes a.dat
848   git commit --message "initial commit"
849
850   git config "lfs.$endpoint.locksverify" false
851
852   git push origin master 2>&1 | tee push.log
853   [ "0" -eq "$(grep -c "\"origin\" does not support the LFS locking API" push.log)" ]
854
855   assert_server_object "$reponame" "$contents_oid"
856 )
857 end_test
858
859 begin_test "pre-push locks verify 403 with good ref"
860 (
861   set -e
862
863   reponame="lock-verify-master-branch-required"
864   setup_remote_repo "$reponame"
865   clone_repo "$reponame" "$reponame"
866
867   contents="example"
868   contents_oid="$(calc_oid "$contents")"
869   printf "$contents" > a.dat
870   git lfs track "*.dat"
871   git add .gitattributes a.dat
872   git commit --message "initial commit"
873
874   git config "lfs.$GITSERVER/$reponame.git.locksverify" true
875   git push origin master 2>&1 | tee push.log
876
877   assert_server_object "$reponame" "$contents_oid" "refs/heads/master"
878 )
879 end_test
880
881 begin_test "pre-push locks verify 403 with good tracked ref"
882 (
883   set -e
884
885   reponame="lock-verify-tracked-branch-required"
886   setup_remote_repo "$reponame"
887   clone_repo "$reponame" "$reponame"
888
889   contents="example"
890   contents_oid="$(calc_oid "$contents")"
891   printf "$contents" > a.dat
892   git lfs track "*.dat"
893   git add .gitattributes a.dat
894   git commit --message "initial commit"
895
896   git config push.default upstream
897   git config branch.master.merge refs/heads/tracked
898   git config "lfs.$GITSERVER/$reponame.git.locksverify" true
899   git push 2>&1 | tee push.log
900
901   assert_server_object "$reponame" "$contents_oid" "refs/heads/tracked"
902 )
903 end_test
904
905 begin_test "pre-push locks verify 403 with explicit ref"
906 (
907   set -e
908
909   reponame="lock-verify-explicit-branch-required"
910   setup_remote_repo "$reponame"
911   clone_repo "$reponame" "$reponame"
912
913   contents="example"
914   contents_oid="$(calc_oid "$contents")"
915   printf "$contents" > a.dat
916   git lfs track "*.dat"
917   git add .gitattributes a.dat
918   git commit --message "initial commit"
919
920   git config "lfs.$GITSERVER/$reponame.git.locksverify" true
921   git push origin master:explicit 2>&1 | tee push.log
922
923   assert_server_object "$reponame" "$contents_oid" "refs/heads/explicit"
924 )
925 end_test
926
927 begin_test "pre-push locks verify 403 with bad ref"
928 (
929   set -e
930
931   reponame="lock-verify-other-branch-required"
932   setup_remote_repo "$reponame"
933   clone_repo "$reponame" "$reponame"
934
935   contents="example"
936   contents_oid="$(calc_oid "$contents")"
937   printf "$contents" > a.dat
938   git lfs track "*.dat"
939   git add .gitattributes a.dat
940   git commit --message "initial commit"
941
942   git config "lfs.$GITSERVER/$reponame.git.locksverify" true
943   git push origin master 2>&1 | tee push.log
944   grep "failed to push some refs" push.log
945   refute_server_object "$reponame" "$contents_oid" "refs/heads/other"
946 )
947 end_test
948
949 begin_test "pre-push locks verify 5xx with verification unset"
950 (
951   set -e
952
953   reponame="lock-unset-verify-5xx"
954   setup_remote_repo "$reponame"
955   clone_repo "$reponame" "$reponame"
956
957   endpoint="$(repo_endpoint $GITSERVER $reponame)"
958
959   contents="example"
960   contents_oid="$(calc_oid "$contents")"
961   printf "$contents" > a.dat
962   git lfs track "*.dat"
963   git add .gitattributes a.dat
964   git commit --message "initial commit"
965
966   [ -z "$(git config "lfs.$endpoint.locksverify")" ]
967
968   git push origin master 2>&1 | tee push.log
969   grep "\"origin\" does not support the LFS locking API" push.log
970
971   assert_server_object "$reponame" "$contents_oid"
972 )
973 end_test
974
975 begin_test "pre-push locks verify 501 with verification enabled"
976 (
977   set -e
978
979   reponame="lock-enabled-verify-501"
980   setup_remote_repo "$reponame"
981   clone_repo "$reponame" "$reponame"
982
983   endpoint="$(repo_endpoint $GITSERVER $reponame)"
984
985   contents="example"
986   contents_oid="$(calc_oid "$contents")"
987   printf "$contents" > a.dat
988   git lfs track "*.dat"
989   git add .gitattributes a.dat
990   git commit --message "initial commit"
991
992   git config "lfs.$endpoint.locksverify" true
993
994   git push origin master 2>&1 | tee push.log
995
996   assert_server_object "$reponame" "$contents_oid"
997   [ "false" = "$(git config "lfs.$endpoint.locksverify")" ]
998 )
999 end_test
1000
1001
1002 begin_test "pre-push locks verify 501 with verification disabled"
1003 (
1004   set -e
1005
1006   reponame="lock-disabled-verify-501"
1007   setup_remote_repo "$reponame"
1008   clone_repo "$reponame" "$reponame"
1009
1010   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1011
1012   contents="example"
1013   contents_oid="$(calc_oid "$contents")"
1014   printf "$contents" > a.dat
1015   git lfs track "*.dat"
1016   git add .gitattributes a.dat
1017   git commit --message "initial commit"
1018
1019   git config "lfs.$endpoint.locksverify" false
1020
1021   git push origin master 2>&1 | tee push.log
1022
1023   assert_server_object "$reponame" "$contents_oid"
1024   [ "false" = "$(git config "lfs.$endpoint.locksverify")" ]
1025 )
1026 end_test
1027
1028 begin_test "pre-push locks verify 501 with verification unset"
1029 (
1030   set -e
1031
1032   reponame="lock-unset-verify-501"
1033   setup_remote_repo "$reponame"
1034   clone_repo "$reponame" "$reponame"
1035
1036   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1037
1038   contents="example"
1039   contents_oid="$(calc_oid "$contents")"
1040   printf "$contents" > a.dat
1041   git lfs track "*.dat"
1042   git add .gitattributes a.dat
1043   git commit --message "initial commit"
1044
1045   [ -z "$(git config "lfs.$endpoint.locksverify")" ]
1046
1047   git push origin master 2>&1 | tee push.log
1048
1049   assert_server_object "$reponame" "$contents_oid"
1050   [ "false" = "$(git config "lfs.$endpoint.locksverify")" ]
1051 )
1052 end_test
1053
1054 begin_test "pre-push locks verify 200"
1055 (
1056   set -e
1057
1058   reponame="lock-verify-200"
1059   setup_remote_repo "$reponame"
1060   clone_repo "$reponame" "$reponame"
1061
1062   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1063   [ -z "$(git config "lfs.$endpoint.locksverify")" ]
1064
1065   contents="example"
1066   contents_oid="$(calc_oid "$contents")"
1067   printf "$contents" > a.dat
1068   git lfs track "*.dat"
1069   git add .gitattributes a.dat
1070   git commit --message "initial commit"
1071
1072   git push origin master 2>&1 | tee push.log
1073
1074   grep "Locking support detected on remote \"origin\"." push.log
1075   grep "git config lfs.$endpoint.locksverify true" push.log
1076   assert_server_object "$reponame" "$contents_oid"
1077 )
1078 end_test
1079
1080 begin_test "pre-push locks verify 403 with verification enabled"
1081 (
1082   set -e
1083
1084   reponame="lock-enabled-verify-403"
1085   setup_remote_repo "$reponame"
1086   clone_repo "$reponame" "$reponame"
1087
1088   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1089
1090   contents="example"
1091   contents_oid="$(calc_oid "$contents")"
1092   printf "$contents" > a.dat
1093   git lfs track "*.dat"
1094   git add .gitattributes a.dat
1095   git commit --message "initial commit"
1096
1097   git config "lfs.$endpoint.locksverify" true
1098
1099   git push origin master 2>&1 | tee push.log
1100   grep "ERROR: Authentication error" push.log
1101
1102   refute_server_object "$reponame" "$contents_oid"
1103   [ "true" = "$(git config "lfs.$endpoint.locksverify")" ]
1104 )
1105 end_test
1106
1107 begin_test "pre-push locks verify 403 with verification disabled"
1108 (
1109   set -e
1110
1111   reponame="lock-disabled-verify-403"
1112   setup_remote_repo "$reponame"
1113   clone_repo "$reponame" "$reponame"
1114
1115   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1116
1117   contents="example"
1118   contents_oid="$(calc_oid "$contents")"
1119   printf "$contents" > a.dat
1120   git lfs track "*.dat"
1121   git add .gitattributes a.dat
1122   git commit --message "initial commit"
1123
1124   git config "lfs.$endpoint.locksverify" false
1125
1126   git push origin master 2>&1 | tee push.log
1127
1128   assert_server_object "$reponame" "$contents_oid"
1129   [ "false" = "$(git config "lfs.$endpoint.locksverify")" ]
1130 )
1131 end_test
1132
1133 begin_test "pre-push locks verify 403 with verification unset"
1134 (
1135   set -e
1136
1137   reponame="lock-unset-verify-403"
1138   setup_remote_repo "$reponame"
1139   clone_repo "$reponame" "$reponame"
1140
1141   endpoint="$(repo_endpoint $GITSERVER $reponame)"
1142
1143   contents="example"
1144   contents_oid="$(calc_oid "$contents")"
1145   printf "$contents" > a.dat
1146   git lfs track "*.dat"
1147   git add .gitattributes a.dat
1148   git commit --message "initial commit"
1149
1150   [ -z "$(git config "lfs.$endpoint.locksverify")" ]
1151
1152   git push origin master 2>&1 | tee push.log
1153   grep "WARNING: Authentication error" push.log
1154
1155   assert_server_object "$reponame" "$contents_oid"
1156   [ -z "$(git config "lfs.$endpoint.locksverify")" ]
1157 )
1158 end_test