Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 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 | ' |
Johannes Schindelin | d6c6b10 | 2020-11-18 23:44:23 +0000 | [diff] [blame] | 9 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 10 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 11 | |
Rubén Justo | 80d32e8 | 2023-06-17 08:41:54 +0200 | [diff] [blame] | 12 | TEST_PASSES_SANITIZE_LEAK=true |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 13 | . ./test-lib.sh |
| 14 | |
| 15 | expect_branch() { |
| 16 | git log -1 --format=%s "$1" >actual && |
| 17 | echo "$2" >expect && |
| 18 | test_cmp expect actual |
| 19 | } |
| 20 | |
| 21 | expect_deleted() { |
| 22 | test_must_fail git rev-parse --verify "$1" |
| 23 | } |
| 24 | |
| 25 | test_expect_success 'set up repo' ' |
| 26 | test_commit one && |
| 27 | test_commit two && |
| 28 | git remote add origin foo.git |
| 29 | ' |
| 30 | |
| 31 | test_expect_success 'update branch via @{-1}' ' |
| 32 | git branch previous one && |
| 33 | |
| 34 | git checkout previous && |
Johannes Schindelin | d6c6b10 | 2020-11-18 23:44:23 +0000 | [diff] [blame] | 35 | git checkout main && |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 36 | |
| 37 | git branch -f @{-1} two && |
| 38 | expect_branch previous two |
| 39 | ' |
| 40 | |
| 41 | test_expect_success 'update branch via local @{upstream}' ' |
| 42 | git branch local one && |
| 43 | git branch --set-upstream-to=local && |
| 44 | |
| 45 | git branch -f @{upstream} two && |
| 46 | expect_branch local two |
| 47 | ' |
| 48 | |
Jeff King | 7d5c960 | 2017-03-02 03:23:14 -0500 | [diff] [blame] | 49 | test_expect_success 'disallow updating branch via remote @{upstream}' ' |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 50 | git update-ref refs/remotes/origin/remote one && |
| 51 | git branch --set-upstream-to=origin/remote && |
| 52 | |
| 53 | test_must_fail git branch -f @{upstream} two |
| 54 | ' |
| 55 | |
| 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 |
| 59 | ' |
| 60 | |
Rubén Justo | cfbd173 | 2022-11-17 02:36:52 +0100 | [diff] [blame] | 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 |
| 69 | ' |
| 70 | |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 71 | test_expect_success 'delete branch via @{-1}' ' |
| 72 | git branch previous-del && |
| 73 | |
| 74 | git checkout previous-del && |
Johannes Schindelin | d6c6b10 | 2020-11-18 23:44:23 +0000 | [diff] [blame] | 75 | git checkout main && |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 76 | |
| 77 | git branch -D @{-1} && |
| 78 | expect_deleted previous-del |
| 79 | ' |
| 80 | |
| 81 | test_expect_success 'delete branch via local @{upstream}' ' |
| 82 | git branch local-del && |
| 83 | git branch --set-upstream-to=local-del && |
| 84 | |
| 85 | git branch -D @{upstream} && |
| 86 | expect_deleted local-del |
| 87 | ' |
| 88 | |
| 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 && |
| 92 | |
| 93 | git branch -r -D @{upstream} && |
| 94 | expect_deleted origin/remote-del |
| 95 | ' |
| 96 | |
| 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. |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 100 | test_expect_success 'delete @{upstream} expansion matches -r option' ' |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 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 && |
| 105 | |
| 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 |
| 109 | ' |
| 110 | |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 111 | test_expect_success 'disallow deleting remote branch via @{-1}' ' |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 112 | git update-ref refs/remotes/origin/previous one && |
| 113 | |
| 114 | git checkout -b origin/previous two && |
Johannes Schindelin | d6c6b10 | 2020-11-18 23:44:23 +0000 | [diff] [blame] | 115 | git checkout main && |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 116 | |
| 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 |
| 120 | ' |
| 121 | |
| 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. |
Jeff King | 7d5c960 | 2017-03-02 03:23:14 -0500 | [diff] [blame] | 126 | test_expect_success 'create branch named "@"' ' |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 127 | git branch -f @ one && |
| 128 | expect_branch refs/heads/@ one |
| 129 | ' |
| 130 | |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 131 | test_expect_success 'delete branch named "@"' ' |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 132 | git update-ref refs/heads/@ two && |
| 133 | git branch -D @ && |
| 134 | expect_deleted refs/heads/@ |
| 135 | ' |
| 136 | |
Jeff King | fd4692f | 2017-03-02 03:23:18 -0500 | [diff] [blame] | 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 && |
| 142 | |
| 143 | git checkout @{upstream} && |
| 144 | expect_branch HEAD one |
| 145 | ' |
| 146 | |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 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" |
| 152 | EOF |
| 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 |
| 158 | ' |
| 159 | |
| 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 |
| 169 | ' |
| 170 | |
Jeff King | a356e8e | 2017-03-02 03:23:06 -0500 | [diff] [blame] | 171 | test_done |