Skip to content

Commit f85062a

Browse files
author
lu0
committed
Include remote branches in 'worktreable' definition (fixes #4).
1 parent 0952d89 commit f85062a

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

README.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
---
33

44
This wrapper script features an API for `git-worktree` commands
5-
in order to easily create, switch and delete worktrees and branches of bare
6-
repositories using widely known commands: `git checkout` and `git branch`.
5+
in order to easily create, switch and delete worktrees of bare repositories
6+
using commands you already know: `git checkout` and `git branch`.
77

88
Quiet built-in checkouts are made after each overridden checkout to trigger
99
post-checkout hooks.
@@ -19,7 +19,7 @@ Table of Contents
1919
- [Usage](#usage)
2020
- [Clone and setup a bare repository](#clone-and-setup-a-bare-repository)
2121
- [Setup fetch rules from the remote](#setup-fetch-rules-from-the-remote)
22-
- [Open an existing branch or worktree](#open-an-existing-branch-or-worktree)
22+
- [Open an existing branch, tag or worktree](#open-an-existing-branch-tag-or-worktree)
2323
- [Comparison with vanilla `git-worktree`](#comparison-with-vanilla-git-worktree)
2424
- [Create a new branch or worktree](#create-a-new-branch-or-worktree)
2525
- [Comparison with vanilla `git-worktree`](#comparison-with-vanilla-git-worktree-1)
@@ -116,21 +116,28 @@ git config --local remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
116116
git fetch
117117
```
118118

119-
## Open an existing branch or worktree
119+
## Open an existing branch, tag or worktree
120120

121-
Switch to branch `master` and *cd* into its worktree, **even if you are *cd'd* into
122-
another worktree/branch**:
121+
Switch to **branch** `master`:
123122

124123
```sh
125124
git checkout master
126125
```
127126

127+
You can also switch to an existing **tag**:
128+
```sh
129+
git checkout v1.0.0
130+
```
131+
132+
The script will automatically create the worktree, if it does not exist, and *cd*
133+
into it, **even if you are *cd'd* into another worktree/branch/tag**:
134+
128135
### Comparison with vanilla `git-worktree`
129136

130137
Next commands should be issued to achieve the same functionality described above when
131138
`git-worktree-wrapper` is not installed:
132139

133-
If the branch exists but the worktree doesn't.
140+
If the branch/tag exists but the worktree doesn't.
134141
```language
135142
cd /path/to/the/root/of/the/bare/repository
136143
git worktree add master
@@ -145,14 +152,16 @@ cd master
145152

146153
## Create a new branch or worktree
147154

148-
Create a branch `new_branch` and *cd* into its new worktree, **even if you are *cd'd* into
149-
another worktree/branch**:
155+
To create a new branch, just issue the command you already know:
150156

151157
```sh
152158
git checkout -b new_branch <from_branch (optional)>
153159
# or use -B to force reset
154160
```
155161

162+
The script will automatically create a new worktree and *cd* into it,
163+
**even if you are *cd'd* into another worktree/branch/tag**:
164+
156165
### Comparison with vanilla `git-worktree`
157166

158167
Next commands should be issued to achieve the same functionality described above when
@@ -176,11 +185,18 @@ git checkout -B new_branch <from_branch (optional)>
176185

177186
## Delete a branch and its worktree
178187

179-
Delete a branch and its worktree, **even if you are *cd'd* into the worktree/branch you want to delete**:
188+
To delete a branch, just issue the command you already use for
189+
"normal" repositories:
190+
180191
```sh
181192
git branch -d new_branch # or -D to force removal
182193
```
183194

195+
The script will **delete both** the branch and its worktree.
196+
197+
If you are *cd'd* into the worktree/branch you are deleting, the script will
198+
*cd* you into the root directory of the bare repository.
199+
184200
### Comparison with vanilla `git-worktree`
185201

186202
Next commands should be issued to achieve the same functionality described above when

libs/_utils.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ utils::git() {
2222
# this enables detached checkouts to commits within a worktree.
2323
utils::is_worktreable() {
2424
local ref="${1}"
25-
local is_branch is_tag
26-
27-
is_branch=$(utils::git show-ref --verify -q refs/heads/"${ref}" && echo 1)
28-
is_tag=$(utils::git show-ref --verify -q refs/tags/"${ref}" && echo 1)
29-
30-
if [[ "${is_tag}" == 1 ]] || [[ "${is_branch}" == 1 ]]; then
25+
local is_local_branch is_remote_branch is_tag
26+
local cmd="utils::git show-ref --verify -q"
27+
28+
is_local_branch=$(${cmd} refs/heads/"${ref}" && echo 1)
29+
is_remote_branch=$(${cmd} refs/remotes/origin/"${ref}" && echo 1)
30+
is_tag=$(${cmd} refs/tags/"${ref}" && echo 1)
31+
32+
if [[ "${is_tag}${is_local_branch}${is_remote_branch}" ]]; then
3133
echo 1
3234
fi
3335
}

0 commit comments

Comments
 (0)