Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git worktree list' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success 'setup' ' |
| 8 | test_commit init |
| 9 | ' |
| 10 | |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 11 | test_expect_success 'rev-parse --git-common-dir on main worktree' ' |
Nguyễn Thái Ngọc Duy | 17f1365 | 2016-02-12 11:31:45 +0700 | [diff] [blame] | 12 | git rev-parse --git-common-dir >actual && |
| 13 | echo .git >expected && |
| 14 | test_cmp expected actual && |
| 15 | mkdir sub && |
| 16 | git -C sub rev-parse --git-common-dir >actual2 && |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 17 | echo ../.git >expected2 && |
Nguyễn Thái Ngọc Duy | 17f1365 | 2016-02-12 11:31:45 +0700 | [diff] [blame] | 18 | test_cmp expected2 actual2 |
| 19 | ' |
| 20 | |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 21 | test_expect_success 'rev-parse --git-path objects linked worktree' ' |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 22 | echo "$(git rev-parse --show-toplevel)/.git/objects" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 23 | test_when_finished "rm -rf linked-tree actual expect && git worktree prune" && |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 24 | git worktree add --detach linked-tree master && |
| 25 | git -C linked-tree rev-parse --git-path objects >actual && |
| 26 | test_cmp expect actual |
| 27 | ' |
| 28 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 29 | test_expect_success '"list" all worktrees from main' ' |
| 30 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 31 | test_when_finished "rm -rf here out actual expect && git worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 32 | git worktree add --detach here master && |
| 33 | echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 34 | git worktree list >out && |
| 35 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 36 | test_cmp expect actual |
| 37 | ' |
| 38 | |
| 39 | test_expect_success '"list" all worktrees from linked' ' |
| 40 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 41 | test_when_finished "rm -rf here out actual expect && git worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 42 | git worktree add --detach here master && |
| 43 | echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 44 | git -C here worktree list >out && |
| 45 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 46 | test_cmp expect actual |
| 47 | ' |
| 48 | |
| 49 | test_expect_success '"list" all worktrees --porcelain' ' |
| 50 | echo "worktree $(git rev-parse --show-toplevel)" >expect && |
| 51 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 52 | echo "branch $(git symbolic-ref HEAD)" >>expect && |
| 53 | echo >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 54 | test_when_finished "rm -rf here actual expect && git worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 55 | git worktree add --detach here master && |
| 56 | echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect && |
| 57 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 58 | echo "detached" >>expect && |
| 59 | echo >>expect && |
| 60 | git worktree list --porcelain >actual && |
| 61 | test_cmp expect actual |
| 62 | ' |
| 63 | |
Johannes Schindelin | 8d88931 | 2020-11-03 11:48:07 +0000 | [diff] [blame] | 64 | test_expect_success '"list" all worktrees with locked annotation' ' |
Rafael Silva | c57b336 | 2020-10-11 10:11:52 +0000 | [diff] [blame] | 65 | test_when_finished "rm -rf locked unlocked out && git worktree prune" && |
| 66 | git worktree add --detach locked master && |
| 67 | git worktree add --detach unlocked master && |
| 68 | git worktree lock locked && |
Rafael Silva | 47409e7 | 2021-01-27 09:03:07 +0100 | [diff] [blame] | 69 | test_when_finished "git worktree unlock locked" && |
Rafael Silva | c57b336 | 2020-10-11 10:11:52 +0000 | [diff] [blame] | 70 | git worktree list >out && |
| 71 | grep "/locked *[0-9a-f].* locked$" out && |
| 72 | ! grep "/unlocked *[0-9a-f].* locked$" out |
| 73 | ' |
| 74 | |
Rafael Silva | 862c723 | 2021-01-27 09:03:08 +0100 | [diff] [blame^] | 75 | test_expect_success '"list" all worktrees --porcelain with locked' ' |
| 76 | test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" && |
| 77 | echo "locked" >expect && |
| 78 | echo "locked with reason" >>expect && |
| 79 | git worktree add --detach locked1 && |
| 80 | git worktree add --detach locked2 && |
| 81 | # unlocked worktree should not be annotated with "locked" |
| 82 | git worktree add --detach unlocked && |
| 83 | git worktree lock locked1 && |
| 84 | test_when_finished "git worktree unlock locked1" && |
| 85 | git worktree lock locked2 --reason "with reason" && |
| 86 | test_when_finished "git worktree unlock locked2" && |
| 87 | git worktree list --porcelain >out && |
| 88 | grep "^locked" out >actual && |
| 89 | test_cmp expect actual |
| 90 | ' |
| 91 | |
| 92 | test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' ' |
| 93 | test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" && |
| 94 | printf "locked \"locked\\\\r\\\\nreason\"\n" >expect && |
| 95 | printf "locked \"locked\\\\nreason\"\n" >>expect && |
| 96 | git worktree add --detach locked_lf && |
| 97 | git worktree add --detach locked_crlf && |
| 98 | git worktree lock locked_lf --reason "$(printf "locked\nreason")" && |
| 99 | test_when_finished "git worktree unlock locked_lf" && |
| 100 | git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" && |
| 101 | test_when_finished "git worktree unlock locked_crlf" && |
| 102 | git worktree list --porcelain >out && |
| 103 | grep "^locked" out >actual && |
| 104 | test_cmp expect actual |
| 105 | ' |
| 106 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 107 | test_expect_success 'bare repo setup' ' |
| 108 | git init --bare bare1 && |
| 109 | echo "data" >file1 && |
| 110 | git add file1 && |
| 111 | git commit -m"File1: add data" && |
| 112 | git push bare1 master && |
| 113 | git reset --hard HEAD^ |
| 114 | ' |
| 115 | |
| 116 | test_expect_success '"list" all worktrees from bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 117 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 118 | git -C bare1 worktree add --detach ../there master && |
| 119 | echo "$(pwd)/bare1 (bare)" >expect && |
| 120 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 121 | git -C bare1 worktree list >out && |
| 122 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 123 | test_cmp expect actual |
| 124 | ' |
| 125 | |
| 126 | test_expect_success '"list" all worktrees --porcelain from bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 127 | test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 128 | git -C bare1 worktree add --detach ../there master && |
| 129 | echo "worktree $(pwd)/bare1" >expect && |
| 130 | echo "bare" >>expect && |
| 131 | echo >>expect && |
| 132 | echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect && |
| 133 | echo "HEAD $(git -C there rev-parse HEAD)" >>expect && |
| 134 | echo "detached" >>expect && |
| 135 | echo >>expect && |
| 136 | git -C bare1 worktree list --porcelain >actual && |
| 137 | test_cmp expect actual |
| 138 | ' |
| 139 | |
| 140 | test_expect_success '"list" all worktrees from linked with a bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 141 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 142 | git -C bare1 worktree add --detach ../there master && |
| 143 | echo "$(pwd)/bare1 (bare)" >expect && |
| 144 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 145 | git -C there worktree list >out && |
| 146 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 147 | test_cmp expect actual |
| 148 | ' |
| 149 | |
| 150 | test_expect_success 'bare repo cleanup' ' |
| 151 | rm -rf bare1 |
| 152 | ' |
| 153 | |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 154 | test_expect_success 'broken main worktree still at the top' ' |
| 155 | git init broken-main && |
| 156 | ( |
| 157 | cd broken-main && |
| 158 | test_commit new && |
| 159 | git worktree add linked && |
| 160 | cat >expected <<-EOF && |
| 161 | worktree $(pwd) |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 162 | HEAD $ZERO_OID |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 163 | |
| 164 | EOF |
| 165 | cd linked && |
| 166 | echo "worktree $(pwd)" >expected && |
| 167 | echo "ref: .broken" >../.git/HEAD && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 168 | git worktree list --porcelain >out && |
| 169 | head -n 3 out >actual && |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 170 | test_cmp ../expected actual && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 171 | git worktree list >out && |
| 172 | head -n 1 out >actual.2 && |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 173 | grep -F "(error)" actual.2 |
| 174 | ) |
| 175 | ' |
| 176 | |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 177 | test_expect_success 'linked worktrees are sorted' ' |
| 178 | mkdir sorted && |
| 179 | git init sorted/main && |
| 180 | ( |
| 181 | cd sorted/main && |
| 182 | test_tick && |
| 183 | test_commit new && |
| 184 | git worktree add ../first && |
| 185 | git worktree add ../second && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 186 | git worktree list --porcelain >out && |
| 187 | grep ^worktree out >actual |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 188 | ) && |
| 189 | cat >expected <<-EOF && |
| 190 | worktree $(pwd)/sorted/main |
| 191 | worktree $(pwd)/sorted/first |
| 192 | worktree $(pwd)/sorted/second |
| 193 | EOF |
| 194 | test_cmp expected sorted/main/actual |
| 195 | ' |
| 196 | |
Hariom Verma | 4d86489 | 2020-03-04 07:00:00 +0000 | [diff] [blame] | 197 | test_expect_success 'worktree path when called in .git directory' ' |
Andrei Rybak | 64d1022 | 2020-03-22 22:14:22 +0100 | [diff] [blame] | 198 | git worktree list >list1 && |
Hariom Verma | 4d86489 | 2020-03-04 07:00:00 +0000 | [diff] [blame] | 199 | git -C .git worktree list >list2 && |
| 200 | test_cmp list1 list2 |
| 201 | ' |
| 202 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 203 | test_done |