branch: force-copy a branch to itself via @{-1} is a no-op
[alt-git.git] / t / t3204-branch-name-interpretation.sh
blob3399344f25dc859ad743b3914a3f5c37e92bffbb
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-lib.sh
14 expect_branch() {
15 git log -1 --format=%s "$1" >actual &&
16 echo "$2" >expect &&
17 test_cmp expect actual
20 expect_deleted() {
21 test_must_fail git rev-parse --verify "$1"
24 test_expect_success 'set up repo' '
25 test_commit one &&
26 test_commit two &&
27 git remote add origin foo.git
30 test_expect_success 'update branch via @{-1}' '
31 git branch previous one &&
33 git checkout previous &&
34 git checkout main &&
36 git branch -f @{-1} two &&
37 expect_branch previous two
40 test_expect_success 'update branch via local @{upstream}' '
41 git branch local one &&
42 git branch --set-upstream-to=local &&
44 git branch -f @{upstream} two &&
45 expect_branch local two
48 test_expect_success 'disallow updating branch via remote @{upstream}' '
49 git update-ref refs/remotes/origin/remote one &&
50 git branch --set-upstream-to=origin/remote &&
52 test_must_fail git branch -f @{upstream} two
55 test_expect_success 'create branch with pseudo-qualified name' '
56 git branch refs/heads/qualified two &&
57 expect_branch refs/heads/refs/heads/qualified two
60 test_expect_success 'force-copy a branch to itself via @{-1} is no-op' '
61 git branch -t copiable main &&
62 git checkout copiable &&
63 git checkout - &&
64 git branch -C @{-1} copiable &&
65 git config --get-all branch.copiable.merge >actual &&
66 echo refs/heads/main >expect &&
67 test_cmp expect actual
70 test_expect_success 'delete branch via @{-1}' '
71 git branch previous-del &&
73 git checkout previous-del &&
74 git checkout main &&
76 git branch -D @{-1} &&
77 expect_deleted previous-del
80 test_expect_success 'delete branch via local @{upstream}' '
81 git branch local-del &&
82 git branch --set-upstream-to=local-del &&
84 git branch -D @{upstream} &&
85 expect_deleted local-del
88 test_expect_success 'delete branch via remote @{upstream}' '
89 git update-ref refs/remotes/origin/remote-del two &&
90 git branch --set-upstream-to=origin/remote-del &&
92 git branch -r -D @{upstream} &&
93 expect_deleted origin/remote-del
96 # Note that we create two oddly named local branches here. We want to make
97 # sure that we do not accidentally delete either of them, even if
98 # shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
99 test_expect_success 'delete @{upstream} expansion matches -r option' '
100 git update-ref refs/remotes/origin/remote-del two &&
101 git branch --set-upstream-to=origin/remote-del &&
102 git update-ref refs/heads/origin/remote-del two &&
103 git update-ref refs/heads/remotes/origin/remote-del two &&
105 test_must_fail git branch -D @{upstream} &&
106 expect_branch refs/heads/origin/remote-del two &&
107 expect_branch refs/heads/remotes/origin/remote-del two
110 test_expect_success 'disallow deleting remote branch via @{-1}' '
111 git update-ref refs/remotes/origin/previous one &&
113 git checkout -b origin/previous two &&
114 git checkout main &&
116 test_must_fail git branch -r -D @{-1} &&
117 expect_branch refs/remotes/origin/previous one &&
118 expect_branch refs/heads/origin/previous two
121 # The thing we are testing here is that "@" is the real branch refs/heads/@,
122 # and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a
123 # sane thing, but it _is_ technically allowed for now. If we disallow it, these
124 # can be switched to test_must_fail.
125 test_expect_success 'create branch named "@"' '
126 git branch -f @ one &&
127 expect_branch refs/heads/@ one
130 test_expect_success 'delete branch named "@"' '
131 git update-ref refs/heads/@ two &&
132 git branch -D @ &&
133 expect_deleted refs/heads/@
136 test_expect_success 'checkout does not treat remote @{upstream} as a branch' '
137 git update-ref refs/remotes/origin/checkout one &&
138 git branch --set-upstream-to=origin/checkout &&
139 git update-ref refs/heads/origin/checkout two &&
140 git update-ref refs/heads/remotes/origin/checkout two &&
142 git checkout @{upstream} &&
143 expect_branch HEAD one
146 test_expect_success 'edit-description via @{-1}' '
147 git checkout -b desc-branch &&
148 git checkout -b non-desc-branch &&
149 write_script editor <<-\EOF &&
150 echo "Branch description" >"$1"
152 EDITOR=./editor git branch --edit-description @{-1} &&
153 test_must_fail git config branch.non-desc-branch.description &&
154 git config branch.desc-branch.description >actual &&
155 printf "Branch description\n\n" >expect &&
156 test_cmp expect actual
159 test_expect_success 'modify branch upstream via "@{-1}" and "@{-1}@{upstream}"' '
160 git checkout -b upstream-branch &&
161 git checkout -b upstream-other -t upstream-branch &&
162 git branch --set-upstream-to upstream-other @{-1} &&
163 git config branch.upstream-branch.merge >actual &&
164 echo "refs/heads/upstream-other" >expect &&
165 test_cmp expect actual &&
166 git branch --unset-upstream @{-1}@{upstream} &&
167 test_must_fail git config branch.upstream-other.merge
170 test_done