Merge branch 'ja/doc-synopsis-markup'
[git/gitster.git] / t / t5553-set-upstream.sh
blob33e919a17e1670a40d6d55cd57e0d5f0469dd779
1 #!/bin/sh
3 test_description='"git fetch/pull --set-upstream" basic tests.'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 check_config () {
11 printf "%s\n" "$2" "$3" >"expect.$1" &&
13 git config "branch.$1.remote" && git config "branch.$1.merge"
14 } >"actual.$1" &&
15 test_cmp "expect.$1" "actual.$1"
18 check_config_missing () {
19 test_expect_code 1 git config "branch.$1.remote" &&
20 test_expect_code 1 git config "branch.$1.merge"
23 clear_config () {
24 for branch in "$@"; do
25 test_might_fail git config --unset-all "branch.$branch.remote"
26 test_might_fail git config --unset-all "branch.$branch.merge"
27 done
30 ensure_fresh_upstream () {
31 rm -rf parent && git init --bare parent
34 test_expect_success 'setup bare parent fetch' '
35 ensure_fresh_upstream &&
36 git remote add upstream parent
39 test_expect_success 'setup commit on main and other fetch' '
40 test_commit one &&
41 git push upstream main &&
42 git checkout -b other &&
43 test_commit two &&
44 git push upstream other
47 # tests for fetch --set-upstream
49 test_expect_success 'fetch --set-upstream does not set upstream w/o branch' '
50 clear_config main other &&
51 git checkout main &&
52 git fetch --set-upstream upstream &&
53 check_config_missing main &&
54 check_config_missing other
57 test_expect_success 'fetch --set-upstream upstream main sets branch main but not other' '
58 clear_config main other &&
59 git fetch --set-upstream upstream main &&
60 check_config main upstream refs/heads/main &&
61 check_config_missing other
64 test_expect_success 'fetch --set-upstream upstream other sets branch other' '
65 clear_config main other &&
66 git fetch --set-upstream upstream other &&
67 check_config main upstream refs/heads/other &&
68 check_config_missing other
71 test_expect_success 'fetch --set-upstream main:other does not set the branch other2' '
72 clear_config other2 &&
73 git fetch --set-upstream upstream main:other2 &&
74 check_config_missing other2
77 test_expect_success 'fetch --set-upstream ./does-not-exist fails with invalid url' '
78 # main explicitly not cleared, we check that it is not touched from previous value
79 clear_config other other2 &&
80 test_must_fail git fetch --set-upstream ./does-not-exist &&
81 check_config main upstream refs/heads/other &&
82 check_config_missing other &&
83 check_config_missing other2
86 test_expect_success 'fetch --set-upstream with valid URL sets upstream to URL' '
87 clear_config other other2 &&
88 url="file://$PWD" &&
89 git fetch --set-upstream "$url" &&
90 check_config main "$url" HEAD &&
91 check_config_missing other &&
92 check_config_missing other2
95 test_expect_success 'fetch --set-upstream with a detached HEAD' '
96 git checkout HEAD^0 &&
97 test_when_finished "git checkout -" &&
98 cat >expect <<-\EOF &&
99 warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
101 git fetch --set-upstream upstream main 2>actual.raw &&
102 grep ^warning: actual.raw >actual &&
103 test_cmp expect actual
106 # tests for pull --set-upstream
108 test_expect_success 'setup bare parent pull' '
109 git remote rm upstream &&
110 ensure_fresh_upstream &&
111 git remote add upstream parent
114 test_expect_success 'setup commit on main and other pull' '
115 test_commit three &&
116 git push --tags upstream main &&
117 test_commit four &&
118 git push upstream other
121 test_expect_success 'pull --set-upstream upstream main sets branch main but not other' '
122 clear_config main other &&
123 git pull --no-rebase --set-upstream upstream main &&
124 check_config main upstream refs/heads/main &&
125 check_config_missing other
128 test_expect_success 'pull --set-upstream main:other2 does not set the branch other2' '
129 clear_config other2 &&
130 git pull --no-rebase --set-upstream upstream main:other2 &&
131 check_config_missing other2
134 test_expect_success 'pull --set-upstream upstream other sets branch main' '
135 clear_config main other &&
136 git pull --no-rebase --set-upstream upstream other &&
137 check_config main upstream refs/heads/other &&
138 check_config_missing other
141 test_expect_success 'pull --set-upstream upstream tag does not set the tag' '
142 clear_config three &&
143 git pull --no-rebase --tags --set-upstream upstream three &&
144 check_config_missing three
147 test_expect_success 'pull --set-upstream ./does-not-exist fails with invalid url' '
148 # main explicitly not cleared, we check that it is not touched from previous value
149 clear_config other other2 three &&
150 test_must_fail git pull --set-upstream ./does-not-exist &&
151 check_config main upstream refs/heads/other &&
152 check_config_missing other &&
153 check_config_missing other2 &&
154 check_config_missing three
157 test_expect_success 'pull --set-upstream upstream HEAD sets branch HEAD' '
158 clear_config main other &&
159 git pull --no-rebase --set-upstream upstream HEAD &&
160 check_config main upstream HEAD &&
161 git checkout other &&
162 git pull --no-rebase --set-upstream upstream HEAD &&
163 check_config other upstream HEAD
166 test_expect_success 'pull --set-upstream upstream with more than one branch does nothing' '
167 clear_config main three &&
168 git pull --no-rebase --set-upstream upstream main three &&
169 check_config_missing main &&
170 check_config_missing three
173 test_expect_success 'pull --set-upstream with valid URL sets upstream to URL' '
174 clear_config main other other2 &&
175 git checkout main &&
176 url="file://$PWD" &&
177 git pull --set-upstream "$url" &&
178 check_config main "$url" HEAD &&
179 check_config_missing other &&
180 check_config_missing other2
183 test_expect_success 'pull --set-upstream with valid URL and branch sets branch' '
184 clear_config main other other2 &&
185 git checkout main &&
186 url="file://$PWD" &&
187 git pull --set-upstream "$url" main &&
188 check_config main "$url" refs/heads/main &&
189 check_config_missing other &&
190 check_config_missing other2
193 test_expect_success 'pull --set-upstream with a detached HEAD' '
194 git checkout HEAD^0 &&
195 test_when_finished "git checkout -" &&
196 cat >expect <<-\EOF &&
197 warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
199 git pull --no-rebase --set-upstream upstream main 2>actual.raw &&
200 grep ^warning: actual.raw >actual &&
201 test_cmp expect actual
204 test_done