| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='interpreting exotic branch name arguments |
| 4 | |
| 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. |
| 8 | ' |
| 9 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 10 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 11 | |
| 12 | . ./test-lib.sh |
| 13 | |
| 14 | expect_branch() { |
| 15 | git log -1 --format=%s "$1" >actual && |
| 16 | echo "$2" >expect && |
| 17 | test_cmp expect actual |
| 18 | } |
| 19 | |
| 20 | expect_deleted() { |
| 21 | test_must_fail git rev-parse --verify "$1" |
| 22 | } |
| 23 | |
| 24 | test_expect_success 'set up repo' ' |
| 25 | test_commit one && |
| 26 | test_commit two && |
| 27 | git remote add origin foo.git |
| 28 | ' |
| 29 | |
| 30 | test_expect_success 'update branch via @{-1}' ' |
| 31 | git branch previous one && |
| 32 | |
| 33 | git checkout previous && |
| 34 | git checkout main && |
| 35 | |
| 36 | git branch -f @{-1} two && |
| 37 | expect_branch previous two |
| 38 | ' |
| 39 | |
| 40 | test_expect_success 'update branch via local @{upstream}' ' |
| 41 | git branch local one && |
| 42 | git branch --set-upstream-to=local && |
| 43 | |
| 44 | git branch -f @{upstream} two && |
| 45 | expect_branch local two |
| 46 | ' |
| 47 | |
| 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 && |
| 51 | |
| 52 | test_must_fail git branch -f @{upstream} two |
| 53 | ' |
| 54 | |
| 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 |
| 58 | ' |
| 59 | |
| 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 |
| 68 | ' |
| 69 | |
| 70 | test_expect_success 'delete branch via @{-1}' ' |
| 71 | git branch previous-del && |
| 72 | |
| 73 | git checkout previous-del && |
| 74 | git checkout main && |
| 75 | |
| 76 | git branch -D @{-1} && |
| 77 | expect_deleted previous-del |
| 78 | ' |
| 79 | |
| 80 | test_expect_success 'delete branch via local @{upstream}' ' |
| 81 | git branch local-del && |
| 82 | git branch --set-upstream-to=local-del && |
| 83 | |
| 84 | git branch -D @{upstream} && |
| 85 | expect_deleted local-del |
| 86 | ' |
| 87 | |
| 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 && |
| 91 | |
| 92 | git branch -r -D @{upstream} && |
| 93 | expect_deleted origin/remote-del |
| 94 | ' |
| 95 | |
| 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 && |
| 104 | |
| 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 |
| 108 | ' |
| 109 | |
| 110 | test_expect_success 'disallow deleting remote branch via @{-1}' ' |
| 111 | git update-ref refs/remotes/origin/previous one && |
| 112 | |
| 113 | git checkout -b origin/previous two && |
| 114 | git checkout main && |
| 115 | |
| 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 |
| 119 | ' |
| 120 | |
| 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 |
| 128 | ' |
| 129 | |
| 130 | test_expect_success 'delete branch named "@"' ' |
| 131 | git update-ref refs/heads/@ two && |
| 132 | git branch -D @ && |
| 133 | expect_deleted refs/heads/@ |
| 134 | ' |
| 135 | |
| 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 && |
| 141 | |
| 142 | git checkout @{upstream} && |
| 143 | expect_branch HEAD one |
| 144 | ' |
| 145 | |
| 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" |
| 151 | EOF |
| 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 |
| 157 | ' |
| 158 | |
| 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 |
| 168 | ' |
| 169 | |
| 170 | test_done |