Merge branch 'fixes/2.45.1/2.41' into fixes/2.45.1/2.42
[alt-git.git] / t / t3204-branch-name-interpretation.sh
blob594e3e43e129a8848b5f2e65538087ca978743d6
1 #!/bin/sh
3 test_description='interpreting exotic branch name arguments
5 Branch name arguments are usually names which are taken to be inside of
6 refs/heads/, but we interpret some magic syntax like @{-1}, @{upstream}, etc.
7 This script aims to check the behavior of those corner cases.
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 TEST_PASSES_SANITIZE_LEAK=true
13 . ./test-lib.sh
15 expect_branch() {
16 git log -1 --format=%s "$1" >actual &&
17 echo "$2" >expect &&
18 test_cmp expect actual
21 expect_deleted() {
22 test_must_fail git rev-parse --verify "$1"
25 test_expect_success 'set up repo' '
26 test_commit one &&
27 test_commit two &&
28 git remote add origin foo.git
31 test_expect_success 'update branch via @{-1}' '
32 git branch previous one &&
34 git checkout previous &&
35 git checkout main &&
37 git branch -f @{-1} two &&
38 expect_branch previous two
41 test_expect_success 'update branch via local @{upstream}' '
42 git branch local one &&
43 git branch --set-upstream-to=local &&
45 git branch -f @{upstream} two &&
46 expect_branch local two
49 test_expect_success 'disallow updating branch via remote @{upstream}' '
50 git update-ref refs/remotes/origin/remote one &&
51 git branch --set-upstream-to=origin/remote &&
53 test_must_fail git branch -f @{upstream} two
56 test_expect_success 'create branch with pseudo-qualified name' '
57 git branch refs/heads/qualified two &&
58 expect_branch refs/heads/refs/heads/qualified two
61 test_expect_success 'force-copy a branch to itself via @{-1} is no-op' '
62 git branch -t copiable main &&
63 git checkout copiable &&
64 git checkout - &&
65 git branch -C @{-1} copiable &&
66 git config --get-all branch.copiable.merge >actual &&
67 echo refs/heads/main >expect &&
68 test_cmp expect actual
71 test_expect_success 'delete branch via @{-1}' '
72 git branch previous-del &&
74 git checkout previous-del &&
75 git checkout main &&
77 git branch -D @{-1} &&
78 expect_deleted previous-del
81 test_expect_success 'delete branch via local @{upstream}' '
82 git branch local-del &&
83 git branch --set-upstream-to=local-del &&
85 git branch -D @{upstream} &&
86 expect_deleted local-del
89 test_expect_success 'delete branch via remote @{upstream}' '
90 git update-ref refs/remotes/origin/remote-del two &&
91 git branch --set-upstream-to=origin/remote-del &&
93 git branch -r -D @{upstream} &&
94 expect_deleted origin/remote-del
97 # Note that we create two oddly named local branches here. We want to make
98 # sure that we do not accidentally delete either of them, even if
99 # shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
100 test_expect_success 'delete @{upstream} expansion matches -r option' '
101 git update-ref refs/remotes/origin/remote-del two &&
102 git branch --set-upstream-to=origin/remote-del &&
103 git update-ref refs/heads/origin/remote-del two &&
104 git update-ref refs/heads/remotes/origin/remote-del two &&
106 test_must_fail git branch -D @{upstream} &&
107 expect_branch refs/heads/origin/remote-del two &&
108 expect_branch refs/heads/remotes/origin/remote-del two
111 test_expect_success 'disallow deleting remote branch via @{-1}' '
112 git update-ref refs/remotes/origin/previous one &&
114 git checkout -b origin/previous two &&
115 git checkout main &&
117 test_must_fail git branch -r -D @{-1} &&
118 expect_branch refs/remotes/origin/previous one &&
119 expect_branch refs/heads/origin/previous two
122 # The thing we are testing here is that "@" is the real branch refs/heads/@,
123 # and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a
124 # sane thing, but it _is_ technically allowed for now. If we disallow it, these
125 # can be switched to test_must_fail.
126 test_expect_success 'create branch named "@"' '
127 git branch -f @ one &&
128 expect_branch refs/heads/@ one
131 test_expect_success 'delete branch named "@"' '
132 git update-ref refs/heads/@ two &&
133 git branch -D @ &&
134 expect_deleted refs/heads/@
137 test_expect_success 'checkout does not treat remote @{upstream} as a branch' '
138 git update-ref refs/remotes/origin/checkout one &&
139 git branch --set-upstream-to=origin/checkout &&
140 git update-ref refs/heads/origin/checkout two &&
141 git update-ref refs/heads/remotes/origin/checkout two &&
143 git checkout @{upstream} &&
144 expect_branch HEAD one
147 test_expect_success 'edit-description via @{-1}' '
148 git checkout -b desc-branch &&
149 git checkout -b non-desc-branch &&
150 write_script editor <<-\EOF &&
151 echo "Branch description" >"$1"
153 EDITOR=./editor git branch --edit-description @{-1} &&
154 test_must_fail git config branch.non-desc-branch.description &&
155 git config branch.desc-branch.description >actual &&
156 printf "Branch description\n\n" >expect &&
157 test_cmp expect actual
160 test_expect_success 'modify branch upstream via "@{-1}" and "@{-1}@{upstream}"' '
161 git checkout -b upstream-branch &&
162 git checkout -b upstream-other -t upstream-branch &&
163 git branch --set-upstream-to upstream-other @{-1} &&
164 git config branch.upstream-branch.merge >actual &&
165 echo "refs/heads/upstream-other" >expect &&
166 test_cmp expect actual &&
167 git branch --unset-upstream @{-1}@{upstream} &&
168 test_must_fail git config branch.upstream-other.merge
171 test_done