Raw
1 #!/bin/sh
2
3 test_description='detect some push errors early (before contacting remote)'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success 'setup commits' '
11 test_commit one
12 '
13
14 test_expect_success 'setup remote' '
15 git init --bare remote.git &&
16 git remote add origin remote.git
17 '
18
19 test_expect_success 'setup fake receive-pack' '
20 FAKE_RP_ROOT=$(pwd) &&
21 export FAKE_RP_ROOT &&
22 write_script fake-rp <<-\EOF &&
23 echo yes >"$FAKE_RP_ROOT"/rp-ran
24 exit 1
25 EOF
26 git config remote.origin.receivepack "\"\$FAKE_RP_ROOT/fake-rp\""
27 '
28
29 test_expect_success 'detect missing branches early' '
30 echo no >rp-ran &&
31 echo no >expect &&
32 test_must_fail git push origin missing &&
33 test_cmp expect rp-ran
34 '
35
36 test_expect_success 'detect missing sha1 expressions early' '
37 echo no >rp-ran &&
38 echo no >expect &&
39 test_must_fail git push origin main~2:main &&
40 test_cmp expect rp-ran
41 '
42
43 # We use an existing local_ref, since it follows a different flow in
44 # 'builtin/push.c:set_refspecs()' and we want to test that regression.
45 test_expect_success 'detect empty remote with existing local ref' '
46 test_must_fail git push "" main 2> stderr &&
47 test_grep "fatal: bad repository ${SQ}${SQ}" stderr
48 '
49
50 # While similar to the previous test, here we want to ensure that
51 # even targeted refspecs are handled.
52 test_expect_success 'detect empty remote with targeted refspec' '
53 test_must_fail git push "" HEAD:refs/heads/main 2> stderr &&
54 test_grep "fatal: bad repository ${SQ}${SQ}" stderr
55 '
56
57 test_expect_success 'suggest <remote> <branch> for a <remote>/<branch> slip' '
58 test_must_fail git push origin/main 2>stderr &&
59 test_grep "${SQ}origin/main${SQ} is not a valid push target" stderr &&
60 test_grep "hint: Did you mean to use: git push origin main?" stderr &&
61 test_must_fail git -c advice.pushRepoLooksLikeRef=false push origin/main 2>stderr &&
62 test_grep ! "Did you mean" stderr
63 '
64
65 test_expect_success 'suggest <remote> <branch> when the branch has slashes' '
66 test_must_fail git push origin/feature/x 2>stderr &&
67 test_grep "hint: Did you mean to use: git push origin feature/x?" stderr
68 '
69
70 test_expect_success 'no suggestion when prefix is not a configured remote' '
71 test_must_fail git push not-a-remote/main 2>stderr &&
72 test_grep ! "Did you mean" stderr
73 '
74
75 test_expect_success 'no suggestion for a trailing slash with no branch' '
76 test_must_fail git push origin/ 2>stderr &&
77 test_grep ! "Did you mean" stderr
78 '
79
80 test_expect_success 'no suggestion when the argument is an existing path' '
81 test_when_finished "rm -rf origin" &&
82 git init --bare origin/main &&
83 git push origin/main HEAD:refs/heads/pushed 2>stderr &&
84 test_grep ! "Did you mean" stderr &&
85 git -C origin/main rev-parse --verify refs/heads/pushed
86 '
87
88 test_expect_success 'detect ambiguous refs early' '
89 git branch foo &&
90 git tag foo &&
91 echo no >rp-ran &&
92 echo no >expect &&
93 test_must_fail git push origin foo &&
94 test_cmp expect rp-ran
95 '
96
97 test_done