@w5s/git-worktree-manager 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -2
- package/bin/git-worktree-manager +848 -0
- package/package.json +33 -4
package/README.md
CHANGED
|
@@ -1,3 +1,123 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @w5s/git-worktree-manager
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bash CLI to manage git worktrees. It is a port of [git-worktree-utils](https://www.npmjs.com/package/git-worktree-utils).
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- `git` 2.30 or newer (for `git worktree repair`). `cleanup` also uses `git merge-tree --write-tree` (2.38+) to detect squash merges, and falls back to patch comparison on older versions.
|
|
8
|
+
- `bash`
|
|
9
|
+
- [`shellcheck`](https://www.shellcheck.net/) to run the lint script
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
git-worktree-manager <command> [options]
|
|
15
|
+
# or, from git:
|
|
16
|
+
git worktree-manager <command> [options]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
| Command | Description |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `add --branch=<branch> [--dir=<dir>]` | Create a worktree in `<root>/<dir>` and sync files. `<dir>` defaults to the branch name and vice versa. |
|
|
22
|
+
| `list [--plain] [--sort]` | List worktrees with markers: `[main]`, `[dirty:N]`, `[unpushed:N]`, `[no-upstream]`, `[missing directory]`. `--plain` prints raw `git worktree list`. `--sort` puts the main worktree first, then clean ones. |
|
|
23
|
+
| `find --search=<text>` | List worktrees whose line contains `<text>` (case-insensitive). |
|
|
24
|
+
| `remove --dir=<dir>` | Remove a worktree without uncommitted or unpushed changes. The branch is kept. |
|
|
25
|
+
| `move --old-dir=<dir> --new-dir=<dir>` | Move a worktree directory. The worktree must be clean and pushed. |
|
|
26
|
+
| `checkout --from-dir=<dir> --branch=<branch> [--to-dir=<dir>]` | Check out `<branch>` in an existing worktree and move it to `<to-dir>` (default: `<branch>`). The worktree must be clean and pushed. |
|
|
27
|
+
| `sync (--to=<dir> \| --all)` | Copy files matching `syncInclude` from the main worktree into one or all other worktrees. |
|
|
28
|
+
| `cleanup [--dry-run] [--yes] [--base=<ref>] [--min-age=<days>] [--include-gone] [--older-than=<days>] [--delete-branch] [--no-fetch]` | Remove worktrees that are no longer needed. See [Cleanup](#cleanup). |
|
|
29
|
+
| `init` | Create `.gitworktrees` in the main worktree, with every setting commented out at its default value, and add the worktrees root to the main worktree `.gitignore` unless it is already listed. Refuses to overwrite an existing file. |
|
|
30
|
+
| `help` | Show the usage text. |
|
|
31
|
+
|
|
32
|
+
Safety rules: the main worktree is never removed, moved or checked out. A worktree with uncommitted changes or unpushed commits is refused by `remove`, `move` and `checkout`. `add` refuses a branch that is already checked out in another worktree.
|
|
33
|
+
|
|
34
|
+
`add` runs `git fetch` first when `autoFetch` is enabled. If the branch exists locally, it is checked out. If it exists only on `origin`, a tracking branch is created. Otherwise a new branch is created from the main worktree's `HEAD`.
|
|
35
|
+
|
|
36
|
+
### Cleanup
|
|
37
|
+
|
|
38
|
+
`cleanup` removes the worktrees under `root` whose work is done. It prints a plan, asks for confirmation, then removes them with `git worktree remove` (never forced).
|
|
39
|
+
|
|
40
|
+
```text
|
|
41
|
+
Base: origin/main
|
|
42
|
+
remove .worktrees/feat-a (merged)
|
|
43
|
+
remove .worktrees/feat-b (squash-merged)
|
|
44
|
+
remove .worktrees/old-spike (missing directory)
|
|
45
|
+
keep .worktrees/wip (uncommitted changes)
|
|
46
|
+
Remove 3 worktree(s)? [y/N]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A worktree is removed when it is clean and its branch is:
|
|
50
|
+
|
|
51
|
+
| Reason | Detection | Default |
|
|
52
|
+
| --- | --- | --- |
|
|
53
|
+
| `merged` | `HEAD` is an ancestor of the base (merge commit or fast-forward). | on |
|
|
54
|
+
| `rebase-merged` | Every commit of the branch has an equivalent patch in the base (`git cherry`). | on |
|
|
55
|
+
| `squash-merged` | Merging the branch into the base would not change the base tree (`git merge-tree`, git 2.38+), or the whole branch diff has an equivalent patch in the base. | on |
|
|
56
|
+
| `missing directory` | The directory no longer exists. The worktree is pruned. | on |
|
|
57
|
+
| `upstream gone` | The upstream branch was deleted on the remote (after `git fetch --prune`). It may also mean a pull request closed without merging, so it is opt-in. | `--include-gone` |
|
|
58
|
+
| `inactive for N day(s)` | The last commit is older than N days and every commit is pushed. | `--older-than=<days>` |
|
|
59
|
+
|
|
60
|
+
The base is, in order: `--base`, the `baseBranch` setting, `origin`'s default branch, then `origin/main`, `origin/master`, `main` or `master`. It is compared as a remote ref, so a stale local `main` does not matter. `git fetch --prune` runs first unless `--no-fetch` is passed or `autoFetch` is `false`.
|
|
61
|
+
|
|
62
|
+
A worktree is never removed when it is the main worktree, the current directory, locked, on a detached `HEAD`, on a protected branch (`protectedBranches`, plus the base), dirty (untracked files included), in the middle of a rebase, merge, cherry-pick, revert or bisect, or younger than `--min-age` days (default `1`, `0` disables). The age guard matters because a branch just created from the base looks merged. Worktrees outside `root` are ignored, except that missing directories are always pruned.
|
|
63
|
+
|
|
64
|
+
Removing a worktree keeps its branch. `--delete-branch` also deletes the branch, only when it is `merged`, `rebase-merged` or `squash-merged`. Remote branches are never deleted. Files ignored by git that live in the worktree, such as a hand-edited `.env`, are lost with it.
|
|
65
|
+
|
|
66
|
+
Without a terminal, `cleanup` refuses to remove anything unless `--yes` is passed. `--dry-run` only prints the plan. The exit code is non-zero when a removal failed.
|
|
67
|
+
|
|
68
|
+
### File syncing
|
|
69
|
+
|
|
70
|
+
`add` and `sync` copy files from the main worktree. They search every directory under the main worktree (skipping `node_modules` and `.git`). In each directory, the top-level files matching `syncInclude` are copied to the same relative location in the target worktree, except those listed in `syncExclude`.
|
|
71
|
+
|
|
72
|
+
## Configuration
|
|
73
|
+
|
|
74
|
+
The configuration file is `.gitworktrees`, at the root of the main worktree (the folder that contains `.git`). It uses git config syntax and is read with `git config --file`, so it accepts the same format as `.gitconfig` and `.gitmodules`. `root` is resolved relative to that same root:
|
|
75
|
+
|
|
76
|
+
```ini
|
|
77
|
+
[worktree-manager]
|
|
78
|
+
root = .worktrees
|
|
79
|
+
syncInclude = .env .env.*
|
|
80
|
+
syncExclude = .env.template .env.example .env.sample
|
|
81
|
+
autoFetch = true
|
|
82
|
+
protectedBranches = main master develop
|
|
83
|
+
baseBranch =
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`git-worktree-manager init` generates this file with every setting disabled, so the defaults apply until you change them. Two kinds of comments are used:
|
|
87
|
+
|
|
88
|
+
- `##` lines are explanations.
|
|
89
|
+
- A single `#` disables a setting that is shown with its default value. Remove the `#` to override it.
|
|
90
|
+
|
|
91
|
+
```ini
|
|
92
|
+
[worktree-manager]
|
|
93
|
+
## Directory where worktrees are created, relative to the main worktree.
|
|
94
|
+
#root = .worktrees
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Multi-value keys can be repeated as in any git config file. Their values are joined with spaces.
|
|
98
|
+
|
|
99
|
+
| Config key | Environment variable | Default | Description |
|
|
100
|
+
| --- | --- | --- | --- |
|
|
101
|
+
| `root` | `GIT_WORKTREE_MANAGER_ROOT` | `.worktrees` | Directory where worktrees are created. Relative paths are resolved from the main worktree. Created on first use. |
|
|
102
|
+
| `syncInclude` | `GIT_WORKTREE_MANAGER_SYNC_INCLUDE` | `.env .env.*` | Filename patterns of files to sync. |
|
|
103
|
+
| `syncExclude` | `GIT_WORKTREE_MANAGER_SYNC_EXCLUDE` | `.env.template .env.example .env.sample` | Filenames never synced. |
|
|
104
|
+
| `autoFetch` | `GIT_WORKTREE_MANAGER_AUTO_FETCH` | `true` | Run `git fetch` before `add` and `cleanup`. Accepts `true` or `false`. |
|
|
105
|
+
| `protectedBranches` | `GIT_WORKTREE_MANAGER_PROTECTED_BRANCHES` | `main master develop` | Branches whose worktrees `cleanup` never removes. |
|
|
106
|
+
| `baseBranch` | `GIT_WORKTREE_MANAGER_BASE_BRANCH` | _(empty)_ | Ref that `cleanup` compares worktrees to. Empty means `origin`'s default branch. |
|
|
107
|
+
|
|
108
|
+
Environment variables take a space-separated list for the multi-value settings.
|
|
109
|
+
|
|
110
|
+
The default `.worktrees` folder lives inside the main worktree, so it must be ignored to keep it out of `git status`. `init` does this for you: it appends the worktrees root to the `.gitignore` of the main worktree (creating the file if needed) and changes nothing when an equivalent entry (`.worktrees`, `.worktrees/`, `/.worktrees` or `/.worktrees/`) is already present. A root outside the main worktree is not added.
|
|
111
|
+
|
|
112
|
+
```gitignore
|
|
113
|
+
/.worktrees/
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The entry uses the root in effect when `init` runs: `GIT_WORKTREE_MANAGER_ROOT` if set, otherwise the default. If you later set a different `root` in the configuration file, add that folder to `.gitignore` yourself.
|
|
117
|
+
|
|
118
|
+
## Development
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
pnpm run test # runs test/git-worktree-manager.test.sh against temporary repositories
|
|
122
|
+
pnpm run lint # runs shellcheck
|
|
123
|
+
```
|
|
@@ -0,0 +1,848 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# git-worktree-manager: create, list, move and remove git worktrees.
|
|
4
|
+
# Port of git-worktree-utils (https://www.npmjs.com/package/git-worktree-utils).
|
|
5
|
+
#
|
|
6
|
+
# Configuration is read from environment variables and from a git-config
|
|
7
|
+
# formatted file named .gitworktrees at the root of the main worktree.
|
|
8
|
+
#
|
|
9
|
+
# bin/git-worktree-manager is generated from src/ by scripts/build.sh: edit src/.
|
|
10
|
+
set -eu
|
|
11
|
+
set -f # never let shell globbing expand configured patterns
|
|
12
|
+
|
|
13
|
+
PROGRAM=git-worktree-manager
|
|
14
|
+
CONFIG_FILE_NAME=.gitworktrees
|
|
15
|
+
|
|
16
|
+
# Default settings. Used by load_settings and written (commented out) by init.
|
|
17
|
+
DEFAULT_ROOT=.worktrees
|
|
18
|
+
DEFAULT_SYNC_INCLUDE=".env .env.*"
|
|
19
|
+
DEFAULT_SYNC_EXCLUDE=".env.template .env.example .env.sample"
|
|
20
|
+
DEFAULT_AUTO_FETCH=true
|
|
21
|
+
DEFAULT_PROTECTED_BRANCHES="main master develop"
|
|
22
|
+
DEFAULT_BASE_BRANCH=""
|
|
23
|
+
DEFAULT_MIN_AGE=1
|
|
24
|
+
|
|
25
|
+
die() {
|
|
26
|
+
printf '%s: error: %s\n' "$PROGRAM" "$*" >&2
|
|
27
|
+
exit 1
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
warn() {
|
|
31
|
+
printf '%s: warning: %s\n' "$PROGRAM" "$*" >&2
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
usage() {
|
|
35
|
+
cat <<EOF
|
|
36
|
+
Usage: $PROGRAM <command> [options]
|
|
37
|
+
|
|
38
|
+
Commands:
|
|
39
|
+
add --branch=<branch> [--dir=<dir>]
|
|
40
|
+
Create a worktree for <branch> in <root>/<dir> and sync files.
|
|
41
|
+
<dir> defaults to <branch> and <branch> defaults to <dir>.
|
|
42
|
+
list [--plain] [--sort]
|
|
43
|
+
List worktrees with status markers: [main], [dirty:N], [unpushed:N],
|
|
44
|
+
[no-upstream] and [missing directory]. --plain prints git's raw output.
|
|
45
|
+
--sort lists the main worktree first, then clean worktrees.
|
|
46
|
+
find --search=<text>
|
|
47
|
+
List worktrees whose line contains <text> (case-insensitive).
|
|
48
|
+
remove --dir=<dir>
|
|
49
|
+
Remove a worktree that has no uncommitted or unpushed changes.
|
|
50
|
+
move --old-dir=<dir> --new-dir=<dir>
|
|
51
|
+
Move a worktree directory. Requires a clean, fully pushed worktree.
|
|
52
|
+
checkout --from-dir=<dir> --branch=<branch> [--to-dir=<dir>]
|
|
53
|
+
Check out <branch> in an existing worktree and move it to <to-dir>
|
|
54
|
+
(defaults to <branch>). Requires a clean, fully pushed worktree.
|
|
55
|
+
sync (--to=<dir> | --all)
|
|
56
|
+
Copy files matching syncInclude from the main worktree into one or all other worktrees.
|
|
57
|
+
cleanup [--dry-run] [--yes] [--base=<ref>] [--min-age=<days>] [--include-gone]
|
|
58
|
+
[--older-than=<days>] [--delete-branch] [--no-fetch]
|
|
59
|
+
Remove worktrees under <root> that are no longer needed. A worktree is
|
|
60
|
+
removed when it is clean and its branch is:
|
|
61
|
+
- merged into the base (merge commit, fast-forward, rebase or squash), or
|
|
62
|
+
- gone from the remote, with --include-gone, or
|
|
63
|
+
- inactive for <days> days and fully pushed, with --older-than.
|
|
64
|
+
Worktrees whose directory is missing are pruned. The main worktree, the
|
|
65
|
+
current directory, locked worktrees, protected branches, detached HEADs,
|
|
66
|
+
operations in progress and worktrees younger than --min-age days
|
|
67
|
+
(default $DEFAULT_MIN_AGE, 0 disables) are never removed.
|
|
68
|
+
The base defaults to origin's default branch. Branches are kept unless
|
|
69
|
+
--delete-branch is given (only for branches proven merged).
|
|
70
|
+
--dry-run prints the plan only. --yes skips the confirmation prompt,
|
|
71
|
+
which is required when not running in a terminal.
|
|
72
|
+
init
|
|
73
|
+
Create $CONFIG_FILE_NAME in the main worktree with the default settings
|
|
74
|
+
(commented out) and add the worktrees root to the main worktree .gitignore
|
|
75
|
+
unless it is already listed. Refuses to overwrite an existing file.
|
|
76
|
+
help
|
|
77
|
+
Show this help.
|
|
78
|
+
|
|
79
|
+
Configuration precedence: environment variables > $CONFIG_FILE_NAME > defaults.
|
|
80
|
+
The configuration file lives in the main worktree and uses git config syntax:
|
|
81
|
+
|
|
82
|
+
[worktree-manager]
|
|
83
|
+
root = .worktrees
|
|
84
|
+
syncInclude = .env .env.*
|
|
85
|
+
syncExclude = .env.template .env.example .env.sample
|
|
86
|
+
autoFetch = true
|
|
87
|
+
protectedBranches = main master develop
|
|
88
|
+
baseBranch =
|
|
89
|
+
|
|
90
|
+
Environment variables:
|
|
91
|
+
GIT_WORKTREE_MANAGER_ROOT root (relative to the main worktree)
|
|
92
|
+
GIT_WORKTREE_MANAGER_SYNC_INCLUDE syncInclude (space-separated)
|
|
93
|
+
GIT_WORKTREE_MANAGER_SYNC_EXCLUDE syncExclude (space-separated)
|
|
94
|
+
GIT_WORKTREE_MANAGER_AUTO_FETCH autoFetch (true or false)
|
|
95
|
+
GIT_WORKTREE_MANAGER_PROTECTED_BRANCHES protectedBranches (space-separated)
|
|
96
|
+
GIT_WORKTREE_MANAGER_BASE_BRANCH baseBranch (default: origin's default branch)
|
|
97
|
+
EOF
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
# Sets OPT_VALUE to the value of --<name>=<value>, or to an empty string.
|
|
101
|
+
opt() {
|
|
102
|
+
local name=$1 arg
|
|
103
|
+
shift
|
|
104
|
+
OPT_VALUE=""
|
|
105
|
+
for arg in "$@"; do
|
|
106
|
+
case $arg in
|
|
107
|
+
"--$name="*) OPT_VALUE=${arg#"--$name="} ;;
|
|
108
|
+
esac
|
|
109
|
+
done
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# Returns 0 when the bare flag --<name> is present.
|
|
113
|
+
has_flag() {
|
|
114
|
+
local name=$1 arg
|
|
115
|
+
shift
|
|
116
|
+
for arg in "$@"; do
|
|
117
|
+
if [ "$arg" = "--$name" ]; then
|
|
118
|
+
return 0
|
|
119
|
+
fi
|
|
120
|
+
done
|
|
121
|
+
return 1
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# Sets CFG_VALUE to the values of worktree-manager.<key> (space-separated).
|
|
125
|
+
# Returns 1 when the key is absent. Dies when the configuration file is invalid.
|
|
126
|
+
config_get() {
|
|
127
|
+
local key=$1 out rc=0
|
|
128
|
+
CFG_VALUE=""
|
|
129
|
+
[ -f "$CONFIG_FILE" ] || return 1
|
|
130
|
+
out=$(git config --file "$CONFIG_FILE" --get-all "worktree-manager.$key") || rc=$?
|
|
131
|
+
if [ "$rc" -eq 1 ]; then
|
|
132
|
+
return 1
|
|
133
|
+
fi
|
|
134
|
+
[ "$rc" -eq 0 ] || die "invalid configuration file: $CONFIG_FILE"
|
|
135
|
+
CFG_VALUE=$(printf '%s\n' "$out" | paste -sd ' ' -)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
# resolve_setting <env value> <config key> <default>: sets SETTING_VALUE.
|
|
139
|
+
resolve_setting() {
|
|
140
|
+
if [ -n "$1" ]; then
|
|
141
|
+
SETTING_VALUE=$1
|
|
142
|
+
elif config_get "$2"; then
|
|
143
|
+
SETTING_VALUE=$CFG_VALUE
|
|
144
|
+
else
|
|
145
|
+
SETTING_VALUE=$3
|
|
146
|
+
fi
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
load_settings() {
|
|
150
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_ROOT:-}" root "$DEFAULT_ROOT"
|
|
151
|
+
ROOT=$SETTING_VALUE
|
|
152
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_SYNC_INCLUDE:-}" syncInclude "$DEFAULT_SYNC_INCLUDE"
|
|
153
|
+
SYNC_INCLUDE=$SETTING_VALUE
|
|
154
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_SYNC_EXCLUDE:-}" syncExclude "$DEFAULT_SYNC_EXCLUDE"
|
|
155
|
+
SYNC_EXCLUDE=$SETTING_VALUE
|
|
156
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_AUTO_FETCH:-}" autoFetch "$DEFAULT_AUTO_FETCH"
|
|
157
|
+
AUTO_FETCH=$SETTING_VALUE
|
|
158
|
+
case $AUTO_FETCH in
|
|
159
|
+
true | false) ;;
|
|
160
|
+
*) die "autoFetch must be 'true' or 'false', got '$AUTO_FETCH'" ;;
|
|
161
|
+
esac
|
|
162
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_PROTECTED_BRANCHES:-}" protectedBranches "$DEFAULT_PROTECTED_BRANCHES"
|
|
163
|
+
PROTECTED_BRANCHES=$SETTING_VALUE
|
|
164
|
+
resolve_setting "${GIT_WORKTREE_MANAGER_BASE_BRANCH:-}" baseBranch "$DEFAULT_BASE_BRANCH"
|
|
165
|
+
BASE_BRANCH=$SETTING_VALUE
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
# Prints the absolute physical path of a directory, or nothing if it does not exist.
|
|
169
|
+
abs_path() {
|
|
170
|
+
(cd "$1" 2>/dev/null && pwd -P) || true
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
# Sets MAIN_ROOT, CONFIG_FILE and the settings. Must run before any other repository command.
|
|
174
|
+
require_repo() {
|
|
175
|
+
local first
|
|
176
|
+
git rev-parse --git-dir >/dev/null 2>&1 || die "not inside a git repository"
|
|
177
|
+
first=$(git worktree list --porcelain | sed -n '1s/^worktree //p')
|
|
178
|
+
MAIN_ROOT=$(abs_path "$first")
|
|
179
|
+
[ -n "$MAIN_ROOT" ] || die "cannot resolve the main worktree"
|
|
180
|
+
CONFIG_FILE="$MAIN_ROOT/$CONFIG_FILE_NAME"
|
|
181
|
+
load_settings
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
# Sets WORKTREES_ABS to the worktrees root as an absolute path. The path need not exist.
|
|
185
|
+
worktrees_root_path() {
|
|
186
|
+
case $ROOT in
|
|
187
|
+
/*) WORKTREES_ABS=$ROOT ;;
|
|
188
|
+
*) WORKTREES_ABS=$MAIN_ROOT/$ROOT ;;
|
|
189
|
+
esac
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
# Sets WT_PATH to the absolute path of <root>/<dir>, creating the root when missing.
|
|
193
|
+
resolve_worktree_path() {
|
|
194
|
+
local root
|
|
195
|
+
[ -n "$1" ] || die "a directory name is required"
|
|
196
|
+
worktrees_root_path
|
|
197
|
+
mkdir -p "$WORKTREES_ABS" || die "cannot create worktrees root: $WORKTREES_ABS"
|
|
198
|
+
root=$(abs_path "$WORKTREES_ABS")
|
|
199
|
+
WT_PATH=$root/$1
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
ref_exists() {
|
|
203
|
+
git -C "$MAIN_ROOT" show-ref --verify --quiet "$1"
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
# Prints the path of the worktree that has <branch> checked out, if any.
|
|
207
|
+
branch_worktree() {
|
|
208
|
+
git -C "$MAIN_ROOT" worktree list --porcelain | awk -v ref="branch refs/heads/$1" '
|
|
209
|
+
/^worktree / { path = substr($0, 10) }
|
|
210
|
+
$0 == ref { print path; exit }
|
|
211
|
+
'
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# Dies unless the worktree at <path> can be removed, moved or checked out safely.
|
|
215
|
+
require_removable() {
|
|
216
|
+
[ "$1" != "$MAIN_ROOT" ] || die "refusing to modify the main worktree: $1"
|
|
217
|
+
[ -z "$(git -C "$1" status --porcelain)" ] || die "worktree has uncommitted changes: $1"
|
|
218
|
+
[ -z "$(git -C "$1" rev-list HEAD --not --remotes)" ] || die "worktree has unpushed commits: $1"
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# Returns 0 when <name> matches one of the syncExclude entries.
|
|
222
|
+
is_excluded() {
|
|
223
|
+
local excluded
|
|
224
|
+
for excluded in $SYNC_EXCLUDE; do
|
|
225
|
+
if [ "$1" = "$excluded" ]; then
|
|
226
|
+
return 0
|
|
227
|
+
fi
|
|
228
|
+
done
|
|
229
|
+
return 1
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
# Copies files from every directory under <src> (skipping node_modules and .git)
|
|
233
|
+
# to the same relative location under <dst>. Only top-level files of each directory are copied.
|
|
234
|
+
sync_files() {
|
|
235
|
+
local src=$1 dst=$2 dirs dir rel pattern file name target_dir
|
|
236
|
+
worktrees_root_path
|
|
237
|
+
dirs=$(find "$src" \( -name node_modules -o -name .git \) -prune -o -type d -print)
|
|
238
|
+
while IFS= read -r dir; do
|
|
239
|
+
[ -n "$dir" ] || continue
|
|
240
|
+
case $dir in
|
|
241
|
+
"$WORKTREES_ABS" | "$WORKTREES_ABS"/*) continue ;;
|
|
242
|
+
esac
|
|
243
|
+
rel=${dir#"$src"}
|
|
244
|
+
rel=${rel#/}
|
|
245
|
+
for pattern in $SYNC_INCLUDE; do
|
|
246
|
+
while IFS= read -r file; do
|
|
247
|
+
name=${file##*/}
|
|
248
|
+
if is_excluded "$name"; then
|
|
249
|
+
continue
|
|
250
|
+
fi
|
|
251
|
+
target_dir=$dst${rel:+/$rel}
|
|
252
|
+
mkdir -p "$target_dir"
|
|
253
|
+
cp "$file" "$target_dir/$name"
|
|
254
|
+
printf ' synced %s\n' "${rel:+$rel/}$name"
|
|
255
|
+
done < <(find "$dir" -maxdepth 1 -type f -name "$pattern")
|
|
256
|
+
done
|
|
257
|
+
done < <(printf '%s\n' "$dirs")
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
# Prints the modification time of <file> in epoch seconds, or 0 when unavailable.
|
|
261
|
+
# GNU stat is tried first: BSD "stat -f" would succeed on Linux with another meaning.
|
|
262
|
+
file_mtime() {
|
|
263
|
+
stat -c %Y "$1" 2>/dev/null || stat -f %m "$1" 2>/dev/null || printf '0\n'
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
# Dies unless <value> is empty or a non-negative integer.
|
|
267
|
+
require_integer() {
|
|
268
|
+
case $2 in
|
|
269
|
+
*[!0-9]*) die "--$1 must be a non-negative integer, got '$2'" ;;
|
|
270
|
+
esac
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
# Sets BASE_REF to the ref worktrees are compared to: <requested>, baseBranch,
|
|
274
|
+
# origin's default branch, then the usual branch names. Dies when none exists.
|
|
275
|
+
resolve_base() {
|
|
276
|
+
local candidate=${1:-$BASE_BRANCH}
|
|
277
|
+
if [ -n "$candidate" ]; then
|
|
278
|
+
git -C "$MAIN_ROOT" rev-parse --verify --quiet "$candidate^{commit}" >/dev/null || die "base ref not found: $candidate"
|
|
279
|
+
BASE_REF=$candidate
|
|
280
|
+
return 0
|
|
281
|
+
fi
|
|
282
|
+
candidate=$(git -C "$MAIN_ROOT" symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null || true)
|
|
283
|
+
if [ -z "$candidate" ]; then
|
|
284
|
+
for candidate in origin/main origin/master main master; do
|
|
285
|
+
if git -C "$MAIN_ROOT" rev-parse --verify --quiet "$candidate^{commit}" >/dev/null; then
|
|
286
|
+
break
|
|
287
|
+
fi
|
|
288
|
+
candidate=""
|
|
289
|
+
done
|
|
290
|
+
fi
|
|
291
|
+
[ -n "$candidate" ] || die "cannot determine the base branch; use --base=<ref>"
|
|
292
|
+
BASE_REF=$candidate
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
# Returns 0 when <branch> is listed in protectedBranches or is the base branch.
|
|
296
|
+
is_protected() {
|
|
297
|
+
local protected
|
|
298
|
+
for protected in $PROTECTED_BRANCHES; do
|
|
299
|
+
if [ "$1" = "$protected" ]; then
|
|
300
|
+
return 0
|
|
301
|
+
fi
|
|
302
|
+
done
|
|
303
|
+
[ "$1" = "$BASE_REF" ] || [ "origin/$1" = "$BASE_REF" ]
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
# Returns 0 and sets REASON when the HEAD of the worktree at <path> is already
|
|
307
|
+
# integrated into BASE_REF: merged, squash-merged or rebase-merged.
|
|
308
|
+
integration_reason() {
|
|
309
|
+
local path=$1 base_tree merged_tree merge_base probe cherry
|
|
310
|
+
if git -C "$path" merge-base --is-ancestor HEAD "$BASE_REF"; then
|
|
311
|
+
REASON=merged
|
|
312
|
+
return 0
|
|
313
|
+
fi
|
|
314
|
+
# Rebase merge: every commit of the branch has an equivalent patch in the base.
|
|
315
|
+
cherry=$(git -C "$path" cherry "$BASE_REF" HEAD 2>/dev/null || true)
|
|
316
|
+
if [ -n "$cherry" ] && ! printf '%s\n' "$cherry" | grep -q '^+'; then
|
|
317
|
+
REASON=rebase-merged
|
|
318
|
+
return 0
|
|
319
|
+
fi
|
|
320
|
+
# Squash merge: merging the branch into the base would not change the base tree.
|
|
321
|
+
# Needs git 2.38; older versions fall through to the patch-id check below.
|
|
322
|
+
base_tree=$(git -C "$path" rev-parse "$BASE_REF^{tree}")
|
|
323
|
+
if merged_tree=$(git -C "$path" merge-tree --write-tree "$BASE_REF" HEAD 2>/dev/null) \
|
|
324
|
+
&& [ "$merged_tree" = "$base_tree" ]; then
|
|
325
|
+
REASON=squash-merged
|
|
326
|
+
return 0
|
|
327
|
+
fi
|
|
328
|
+
# Squash merge when the base touched the same files: the whole branch diff, as a
|
|
329
|
+
# single commit, has an equivalent patch in the base.
|
|
330
|
+
if merge_base=$(git -C "$path" merge-base "$BASE_REF" HEAD 2>/dev/null) \
|
|
331
|
+
&& probe=$(GIT_AUTHOR_NAME=probe GIT_AUTHOR_EMAIL=probe@localhost \
|
|
332
|
+
GIT_COMMITTER_NAME=probe GIT_COMMITTER_EMAIL=probe@localhost \
|
|
333
|
+
git -C "$path" commit-tree "HEAD^{tree}" -p "$merge_base" -m probe 2>/dev/null); then
|
|
334
|
+
cherry=$(git -C "$path" cherry "$BASE_REF" "$probe")
|
|
335
|
+
case $cherry in
|
|
336
|
+
-*)
|
|
337
|
+
REASON=squash-merged
|
|
338
|
+
return 0
|
|
339
|
+
;;
|
|
340
|
+
esac
|
|
341
|
+
fi
|
|
342
|
+
return 1
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
# Decides what to do with the worktree at <path> on <ref> (empty when detached).
|
|
346
|
+
# Sets ACTION (remove or keep), REASON and DELETABLE (true when the branch is proven merged).
|
|
347
|
+
evaluate_worktree() {
|
|
348
|
+
local path=$1 ref=$2 locked=$3 branch git_dir marker mtime commit_time
|
|
349
|
+
ACTION=keep
|
|
350
|
+
DELETABLE=false
|
|
351
|
+
branch=${ref#refs/heads/}
|
|
352
|
+
if [ "$locked" = 1 ]; then
|
|
353
|
+
REASON=locked
|
|
354
|
+
return 0
|
|
355
|
+
fi
|
|
356
|
+
if [ ! -d "$path" ]; then
|
|
357
|
+
ACTION=remove
|
|
358
|
+
REASON="missing directory"
|
|
359
|
+
return 0
|
|
360
|
+
fi
|
|
361
|
+
case $CWD in
|
|
362
|
+
"$path" | "$path"/*)
|
|
363
|
+
REASON="current directory"
|
|
364
|
+
return 0
|
|
365
|
+
;;
|
|
366
|
+
esac
|
|
367
|
+
if [ -z "$ref" ]; then
|
|
368
|
+
REASON="detached HEAD"
|
|
369
|
+
return 0
|
|
370
|
+
fi
|
|
371
|
+
if is_protected "$branch"; then
|
|
372
|
+
REASON="protected branch"
|
|
373
|
+
return 0
|
|
374
|
+
fi
|
|
375
|
+
if [ -n "$(git -C "$path" status --porcelain)" ]; then
|
|
376
|
+
REASON="uncommitted changes"
|
|
377
|
+
return 0
|
|
378
|
+
fi
|
|
379
|
+
git_dir=$(git -C "$path" rev-parse --absolute-git-dir)
|
|
380
|
+
for marker in rebase-merge rebase-apply MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_LOG; do
|
|
381
|
+
if [ -e "$git_dir/$marker" ]; then
|
|
382
|
+
REASON="operation in progress"
|
|
383
|
+
return 0
|
|
384
|
+
fi
|
|
385
|
+
done
|
|
386
|
+
# A branch that was just created from the base looks merged: leave it alone for a while.
|
|
387
|
+
if [ "$MIN_AGE" -gt 0 ]; then
|
|
388
|
+
mtime=$(file_mtime "$git_dir/gitdir")
|
|
389
|
+
if [ $((NOW - mtime)) -lt $((MIN_AGE * 86400)) ]; then
|
|
390
|
+
REASON="younger than $MIN_AGE day(s)"
|
|
391
|
+
return 0
|
|
392
|
+
fi
|
|
393
|
+
fi
|
|
394
|
+
|
|
395
|
+
if integration_reason "$path"; then
|
|
396
|
+
ACTION=remove
|
|
397
|
+
DELETABLE=true
|
|
398
|
+
return 0
|
|
399
|
+
fi
|
|
400
|
+
if [ "$INCLUDE_GONE" = true ] \
|
|
401
|
+
&& [ "$(git -C "$MAIN_ROOT" for-each-ref --format='%(upstream:track)' "refs/heads/$branch")" = "[gone]" ]; then
|
|
402
|
+
ACTION=remove
|
|
403
|
+
REASON="upstream gone"
|
|
404
|
+
return 0
|
|
405
|
+
fi
|
|
406
|
+
if [ -n "$OLDER_THAN" ]; then
|
|
407
|
+
commit_time=$(git -C "$path" log -1 --format=%ct)
|
|
408
|
+
if [ $((NOW - commit_time)) -ge $((OLDER_THAN * 86400)) ] \
|
|
409
|
+
&& [ -z "$(git -C "$path" rev-list HEAD --not --remotes)" ]; then
|
|
410
|
+
ACTION=remove
|
|
411
|
+
REASON="inactive for $OLDER_THAN day(s)"
|
|
412
|
+
return 0
|
|
413
|
+
fi
|
|
414
|
+
fi
|
|
415
|
+
REASON="not merged"
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
# Prints the worktrees root relative to the main worktree, normalized (no leading
|
|
419
|
+
# "./", no trailing "/"). Prints nothing when it is not strictly inside the main worktree.
|
|
420
|
+
worktrees_root_relative() {
|
|
421
|
+
local rel=$ROOT
|
|
422
|
+
case $rel in
|
|
423
|
+
"$MAIN_ROOT"/*) rel=${rel#"$MAIN_ROOT"/} ;;
|
|
424
|
+
/*) return 0 ;;
|
|
425
|
+
esac
|
|
426
|
+
while :; do
|
|
427
|
+
case $rel in
|
|
428
|
+
./*) rel=${rel#./} ;;
|
|
429
|
+
*/) rel=${rel%/} ;;
|
|
430
|
+
*) break ;;
|
|
431
|
+
esac
|
|
432
|
+
done
|
|
433
|
+
case $rel in
|
|
434
|
+
"" | . | .. | ../* | */.. | */../*) return 0 ;;
|
|
435
|
+
esac
|
|
436
|
+
printf '%s\n' "$rel"
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
# Appends /<root>/ to the .gitignore of the main worktree. Nothing is
|
|
440
|
+
# changed when the file already has an equivalent entry (with or without a
|
|
441
|
+
# leading or trailing slash).
|
|
442
|
+
ensure_gitignored() {
|
|
443
|
+
local rel entry line gitignore=$MAIN_ROOT/.gitignore
|
|
444
|
+
rel=$(worktrees_root_relative)
|
|
445
|
+
if [ -z "$rel" ]; then
|
|
446
|
+
printf 'Skipped .gitignore: worktrees root is outside the main worktree (%s)\n' "$ROOT"
|
|
447
|
+
return 0
|
|
448
|
+
fi
|
|
449
|
+
entry=/$rel/
|
|
450
|
+
if [ -f "$gitignore" ]; then
|
|
451
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
452
|
+
line=${line%"${line##*[![:space:]]}"}
|
|
453
|
+
case $line in
|
|
454
|
+
"$rel" | "$rel/" | "/$rel" | "/$rel/")
|
|
455
|
+
printf '%s is already in %s\n' "$line" "$gitignore"
|
|
456
|
+
return 0
|
|
457
|
+
;;
|
|
458
|
+
esac
|
|
459
|
+
done < "$gitignore"
|
|
460
|
+
# Keep the last existing entry on its own line.
|
|
461
|
+
if [ -s "$gitignore" ] && [ -n "$(tail -c 1 "$gitignore")" ]; then
|
|
462
|
+
printf '\n' >> "$gitignore"
|
|
463
|
+
fi
|
|
464
|
+
fi
|
|
465
|
+
printf '%s\n' "$entry" >> "$gitignore"
|
|
466
|
+
printf 'Added %s to %s\n' "$entry" "$gitignore"
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
cmd_add() {
|
|
470
|
+
require_repo
|
|
471
|
+
local branch dir target checked_out new_branch=false
|
|
472
|
+
opt branch "$@"
|
|
473
|
+
branch=$OPT_VALUE
|
|
474
|
+
opt dir "$@"
|
|
475
|
+
dir=$OPT_VALUE
|
|
476
|
+
[ -n "$branch$dir" ] || die "--branch or --dir is required"
|
|
477
|
+
branch=${branch:-$dir}
|
|
478
|
+
dir=${dir:-$branch}
|
|
479
|
+
|
|
480
|
+
resolve_worktree_path "$dir"
|
|
481
|
+
target=$WT_PATH
|
|
482
|
+
[ ! -e "$target" ] || die "path already exists: $target"
|
|
483
|
+
checked_out=$(branch_worktree "$branch")
|
|
484
|
+
[ -z "$checked_out" ] || die "branch '$branch' is already checked out at: $checked_out"
|
|
485
|
+
|
|
486
|
+
if [ "$AUTO_FETCH" = true ] && ! git -C "$MAIN_ROOT" fetch --quiet; then
|
|
487
|
+
warn "git fetch failed; continuing with local refs"
|
|
488
|
+
fi
|
|
489
|
+
|
|
490
|
+
if ref_exists "refs/heads/$branch"; then
|
|
491
|
+
git -C "$MAIN_ROOT" worktree add "$target" "$branch"
|
|
492
|
+
elif ref_exists "refs/remotes/origin/$branch"; then
|
|
493
|
+
git -C "$MAIN_ROOT" worktree add --track -b "$branch" "$target" "origin/$branch"
|
|
494
|
+
else
|
|
495
|
+
git -C "$MAIN_ROOT" worktree add -b "$branch" "$target"
|
|
496
|
+
new_branch=true
|
|
497
|
+
fi
|
|
498
|
+
|
|
499
|
+
sync_files "$MAIN_ROOT" "$target"
|
|
500
|
+
printf 'Created worktree %s on branch %s\n' "$target" "$branch"
|
|
501
|
+
if [ "$new_branch" = true ]; then
|
|
502
|
+
printf 'Tip: publish the branch with: git -C "%s" push -u origin %s\n' "$target" "$branch"
|
|
503
|
+
fi
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
cmd_list() {
|
|
507
|
+
require_repo
|
|
508
|
+
if has_flag plain "$@"; then
|
|
509
|
+
git -C "$MAIN_ROOT" worktree list
|
|
510
|
+
return 0
|
|
511
|
+
fi
|
|
512
|
+
local sort_wanted=false rows="" first_dirty="" first_unpushed=""
|
|
513
|
+
local path ref branch name issues rank line dirty unpushed
|
|
514
|
+
if has_flag sort "$@"; then
|
|
515
|
+
sort_wanted=true
|
|
516
|
+
fi
|
|
517
|
+
|
|
518
|
+
while IFS=$'\t' read -r path ref; do
|
|
519
|
+
[ -n "$path" ] || continue
|
|
520
|
+
name=${path##*/}
|
|
521
|
+
issues=""
|
|
522
|
+
if [ ! -d "$path" ]; then
|
|
523
|
+
issues=" [missing directory — run \`$PROGRAM remove --dir=$name\`]"
|
|
524
|
+
else
|
|
525
|
+
dirty=$(git -C "$path" status --porcelain | wc -l | tr -d ' ')
|
|
526
|
+
unpushed=$(git -C "$path" rev-list HEAD --not --remotes | wc -l | tr -d ' ')
|
|
527
|
+
if [ "$dirty" -gt 0 ]; then
|
|
528
|
+
issues="$issues [dirty:$dirty]"
|
|
529
|
+
[ -n "$first_dirty" ] || first_dirty=$path
|
|
530
|
+
fi
|
|
531
|
+
if [ "$unpushed" -gt 0 ]; then
|
|
532
|
+
issues="$issues [unpushed:$unpushed]"
|
|
533
|
+
[ -n "$first_unpushed" ] || first_unpushed=$path
|
|
534
|
+
fi
|
|
535
|
+
if ! git -C "$path" rev-parse --abbrev-ref --symbolic-full-name '@{u}' >/dev/null 2>&1; then
|
|
536
|
+
issues="$issues [no-upstream]"
|
|
537
|
+
fi
|
|
538
|
+
fi
|
|
539
|
+
|
|
540
|
+
branch=${ref#refs/heads/}
|
|
541
|
+
[ -n "$ref" ] || branch="(detached)"
|
|
542
|
+
line="$path $branch"
|
|
543
|
+
if [ "$path" = "$MAIN_ROOT" ]; then
|
|
544
|
+
line="$line [main]"
|
|
545
|
+
rank=0
|
|
546
|
+
elif [ -z "$issues" ]; then
|
|
547
|
+
rank=1
|
|
548
|
+
else
|
|
549
|
+
rank=2
|
|
550
|
+
fi
|
|
551
|
+
[ "$sort_wanted" = true ] || rank=0
|
|
552
|
+
rows="$rows$rank"$'\t'"$line$issues"$'\n'
|
|
553
|
+
done < <(git -C "$MAIN_ROOT" worktree list --porcelain | awk '
|
|
554
|
+
/^worktree / { path = substr($0, 10) }
|
|
555
|
+
/^branch / { branch = substr($0, 8) }
|
|
556
|
+
/^$/ { print path "\t" branch; branch = "" }
|
|
557
|
+
')
|
|
558
|
+
|
|
559
|
+
printf '%s' "$rows" | sort -s -k1,1n | cut -f2-
|
|
560
|
+
if [ -n "$first_dirty" ] || [ -n "$first_unpushed" ]; then
|
|
561
|
+
printf '\nTips:\n'
|
|
562
|
+
[ -z "$first_dirty" ] || printf ' inspect dirty changes: git -C "%s" status --short\n' "$first_dirty"
|
|
563
|
+
[ -z "$first_unpushed" ] || printf ' inspect unpushed commits: git -C "%s" log HEAD --not --remotes --oneline\n' "$first_unpushed"
|
|
564
|
+
fi
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
cmd_find() {
|
|
568
|
+
require_repo
|
|
569
|
+
local search matches
|
|
570
|
+
opt search "$@"
|
|
571
|
+
search=$OPT_VALUE
|
|
572
|
+
if [ -z "$search" ]; then
|
|
573
|
+
git -C "$MAIN_ROOT" worktree list
|
|
574
|
+
return 0
|
|
575
|
+
fi
|
|
576
|
+
matches=$(git -C "$MAIN_ROOT" worktree list | grep -i -F -- "$search" || true)
|
|
577
|
+
[ -n "$matches" ] || die "no worktree found matching: $search"
|
|
578
|
+
printf '%s\n' "$matches"
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
cmd_remove() {
|
|
582
|
+
require_repo
|
|
583
|
+
local dir
|
|
584
|
+
opt dir "$@"
|
|
585
|
+
dir=$OPT_VALUE
|
|
586
|
+
[ -n "$dir" ] || die "--dir is required"
|
|
587
|
+
resolve_worktree_path "$dir"
|
|
588
|
+
[ -d "$WT_PATH" ] || die "worktree directory not found: $WT_PATH"
|
|
589
|
+
require_removable "$WT_PATH"
|
|
590
|
+
git -C "$MAIN_ROOT" worktree remove "$WT_PATH"
|
|
591
|
+
printf 'Removed worktree %s\n' "$WT_PATH"
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
cmd_move() {
|
|
595
|
+
require_repo
|
|
596
|
+
local old new old_path new_path
|
|
597
|
+
opt old-dir "$@"
|
|
598
|
+
old=$OPT_VALUE
|
|
599
|
+
opt new-dir "$@"
|
|
600
|
+
new=$OPT_VALUE
|
|
601
|
+
[ -n "$old" ] && [ -n "$new" ] || die "--old-dir and --new-dir are required"
|
|
602
|
+
resolve_worktree_path "$old"
|
|
603
|
+
old_path=$WT_PATH
|
|
604
|
+
[ -d "$old_path" ] || die "worktree directory not found: $old_path"
|
|
605
|
+
require_removable "$old_path"
|
|
606
|
+
resolve_worktree_path "$new"
|
|
607
|
+
new_path=$WT_PATH
|
|
608
|
+
[ ! -e "$new_path" ] || die "path already exists: $new_path"
|
|
609
|
+
mv "$old_path" "$new_path"
|
|
610
|
+
git -C "$MAIN_ROOT" worktree repair "$new_path"
|
|
611
|
+
printf 'Moved worktree %s -> %s\n' "$old_path" "$new_path"
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
cmd_checkout() {
|
|
615
|
+
require_repo
|
|
616
|
+
local from branch to from_path to_path checked_out
|
|
617
|
+
opt from-dir "$@"
|
|
618
|
+
from=$OPT_VALUE
|
|
619
|
+
opt branch "$@"
|
|
620
|
+
branch=$OPT_VALUE
|
|
621
|
+
opt to-dir "$@"
|
|
622
|
+
to=$OPT_VALUE
|
|
623
|
+
[ -n "$from" ] && [ -n "$branch" ] || die "--from-dir and --branch are required"
|
|
624
|
+
to=${to:-$branch}
|
|
625
|
+
|
|
626
|
+
resolve_worktree_path "$from"
|
|
627
|
+
from_path=$WT_PATH
|
|
628
|
+
[ -d "$from_path" ] || die "worktree directory not found: $from_path"
|
|
629
|
+
require_removable "$from_path"
|
|
630
|
+
checked_out=$(branch_worktree "$branch")
|
|
631
|
+
[ -z "$checked_out" ] || die "branch '$branch' is already checked out at: $checked_out"
|
|
632
|
+
resolve_worktree_path "$to"
|
|
633
|
+
to_path=$WT_PATH
|
|
634
|
+
if [ "$to_path" != "$from_path" ] && [ -e "$to_path" ]; then
|
|
635
|
+
die "path already exists: $to_path"
|
|
636
|
+
fi
|
|
637
|
+
|
|
638
|
+
if ref_exists "refs/heads/$branch"; then
|
|
639
|
+
git -C "$from_path" checkout --quiet "$branch"
|
|
640
|
+
elif ref_exists "refs/remotes/origin/$branch"; then
|
|
641
|
+
git -C "$from_path" checkout --quiet --track -b "$branch" "origin/$branch"
|
|
642
|
+
else
|
|
643
|
+
git -C "$from_path" checkout --quiet -b "$branch"
|
|
644
|
+
fi
|
|
645
|
+
|
|
646
|
+
if [ "$to_path" != "$from_path" ]; then
|
|
647
|
+
mv "$from_path" "$to_path"
|
|
648
|
+
git -C "$MAIN_ROOT" worktree repair "$to_path"
|
|
649
|
+
fi
|
|
650
|
+
printf 'Checked out worktree %s to branch %s at %s\n' "$from_path" "$branch" "$to_path"
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
cmd_sync() {
|
|
654
|
+
require_repo
|
|
655
|
+
local target_name target
|
|
656
|
+
if has_flag all "$@"; then
|
|
657
|
+
while IFS= read -r target; do
|
|
658
|
+
[ "$target" != "$MAIN_ROOT" ] || continue
|
|
659
|
+
if [ ! -d "$target" ]; then
|
|
660
|
+
warn "skipping missing directory: $target"
|
|
661
|
+
continue
|
|
662
|
+
fi
|
|
663
|
+
printf 'Syncing files to %s\n' "$target"
|
|
664
|
+
sync_files "$MAIN_ROOT" "$target"
|
|
665
|
+
done < <(git -C "$MAIN_ROOT" worktree list --porcelain | sed -n 's/^worktree //p')
|
|
666
|
+
return 0
|
|
667
|
+
fi
|
|
668
|
+
opt to "$@"
|
|
669
|
+
target_name=$OPT_VALUE
|
|
670
|
+
[ -n "$target_name" ] || die "either --to=<dir> or --all is required"
|
|
671
|
+
resolve_worktree_path "$target_name"
|
|
672
|
+
target=$WT_PATH
|
|
673
|
+
[ -d "$target" ] || die "worktree directory not found: $target"
|
|
674
|
+
[ "$target" != "$MAIN_ROOT" ] || die "refusing to sync into the main worktree"
|
|
675
|
+
sync_files "$MAIN_ROOT" "$target"
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
cmd_cleanup() {
|
|
679
|
+
require_repo
|
|
680
|
+
local dry_run=false assume_yes=false delete_branch=false fetch_wanted=true
|
|
681
|
+
local root_abs path ref locked display plan="" removals="" count=0 answer
|
|
682
|
+
local branch reason deletable removed=0 failures=0 prune_missing=false inside
|
|
683
|
+
has_flag dry-run "$@" && dry_run=true
|
|
684
|
+
has_flag yes "$@" && assume_yes=true
|
|
685
|
+
has_flag delete-branch "$@" && delete_branch=true
|
|
686
|
+
has_flag no-fetch "$@" && fetch_wanted=false
|
|
687
|
+
INCLUDE_GONE=false
|
|
688
|
+
has_flag include-gone "$@" && INCLUDE_GONE=true
|
|
689
|
+
opt min-age "$@"
|
|
690
|
+
MIN_AGE=${OPT_VALUE:-$DEFAULT_MIN_AGE}
|
|
691
|
+
require_integer min-age "$MIN_AGE"
|
|
692
|
+
opt older-than "$@"
|
|
693
|
+
OLDER_THAN=$OPT_VALUE
|
|
694
|
+
require_integer older-than "$OLDER_THAN"
|
|
695
|
+
opt base "$@"
|
|
696
|
+
|
|
697
|
+
if [ "$fetch_wanted" = true ] && [ "$AUTO_FETCH" = true ] && ! git -C "$MAIN_ROOT" fetch --prune --quiet; then
|
|
698
|
+
warn "git fetch failed; continuing with local refs"
|
|
699
|
+
fi
|
|
700
|
+
resolve_base "$OPT_VALUE"
|
|
701
|
+
CWD=$(pwd -P)
|
|
702
|
+
NOW=$(date +%s)
|
|
703
|
+
worktrees_root_path
|
|
704
|
+
root_abs=$(abs_path "$WORKTREES_ABS")
|
|
705
|
+
root_abs=${root_abs:-$WORKTREES_ABS}
|
|
706
|
+
|
|
707
|
+
while IFS=$'\t' read -r path ref locked; do
|
|
708
|
+
[ -n "$path" ] || continue
|
|
709
|
+
[ "$path" != "$MAIN_ROOT" ] || continue
|
|
710
|
+
# Worktrees outside the root are not managed here. Missing directories are always stale.
|
|
711
|
+
inside=false
|
|
712
|
+
case $path in
|
|
713
|
+
"$root_abs"/*) inside=true ;;
|
|
714
|
+
esac
|
|
715
|
+
if [ "$inside" = false ] && [ -d "$path" ]; then
|
|
716
|
+
continue
|
|
717
|
+
fi
|
|
718
|
+
evaluate_worktree "$path" "$ref" "$locked"
|
|
719
|
+
display=${path#"$MAIN_ROOT"/}
|
|
720
|
+
plan="$plan$(printf ' %-7s %s (%s)' "$ACTION" "$display" "$REASON")"$'\n'
|
|
721
|
+
if [ "$ACTION" = remove ]; then
|
|
722
|
+
removals="$removals$path"$'\t'"${ref#refs/heads/}"$'\t'"$REASON"$'\t'"$DELETABLE"$'\n'
|
|
723
|
+
count=$((count + 1))
|
|
724
|
+
fi
|
|
725
|
+
done < <(git -C "$MAIN_ROOT" worktree list --porcelain | awk '
|
|
726
|
+
/^worktree / { path = substr($0, 10) }
|
|
727
|
+
/^branch / { branch = substr($0, 8) }
|
|
728
|
+
/^locked/ { locked = 1 }
|
|
729
|
+
/^$/ { print path "\t" branch "\t" locked; branch = ""; locked = 0 }
|
|
730
|
+
')
|
|
731
|
+
|
|
732
|
+
printf 'Base: %s\n' "$BASE_REF"
|
|
733
|
+
if [ -z "$plan" ]; then
|
|
734
|
+
printf 'Nothing to clean up\n'
|
|
735
|
+
return 0
|
|
736
|
+
fi
|
|
737
|
+
printf '%s' "$plan"
|
|
738
|
+
if [ "$count" -eq 0 ]; then
|
|
739
|
+
printf 'Nothing to clean up\n'
|
|
740
|
+
return 0
|
|
741
|
+
fi
|
|
742
|
+
if [ "$dry_run" = true ]; then
|
|
743
|
+
printf 'Dry run: %s worktree(s) would be removed\n' "$count"
|
|
744
|
+
return 0
|
|
745
|
+
fi
|
|
746
|
+
|
|
747
|
+
if [ "$assume_yes" != true ]; then
|
|
748
|
+
[ -t 0 ] || die "refusing to remove worktrees without --yes when not running in a terminal (use --dry-run to preview)"
|
|
749
|
+
printf 'Remove %s worktree(s)? [y/N] ' "$count"
|
|
750
|
+
read -r answer || answer=""
|
|
751
|
+
case $answer in
|
|
752
|
+
y | Y | yes | YES) ;;
|
|
753
|
+
*)
|
|
754
|
+
printf 'Aborted\n'
|
|
755
|
+
return 0
|
|
756
|
+
;;
|
|
757
|
+
esac
|
|
758
|
+
fi
|
|
759
|
+
|
|
760
|
+
while IFS=$'\t' read -r path branch reason deletable; do
|
|
761
|
+
[ -n "$path" ] || continue
|
|
762
|
+
if [ ! -d "$path" ]; then
|
|
763
|
+
prune_missing=true
|
|
764
|
+
removed=$((removed + 1))
|
|
765
|
+
continue
|
|
766
|
+
fi
|
|
767
|
+
if git -C "$MAIN_ROOT" worktree remove "$path" </dev/null; then
|
|
768
|
+
printf 'Removed worktree %s (%s)\n' "$path" "$reason"
|
|
769
|
+
removed=$((removed + 1))
|
|
770
|
+
if [ "$delete_branch" = true ] && [ "$deletable" = true ]; then
|
|
771
|
+
if git -C "$MAIN_ROOT" branch -D "$branch" >/dev/null </dev/null; then
|
|
772
|
+
printf 'Deleted branch %s\n' "$branch"
|
|
773
|
+
else
|
|
774
|
+
warn "could not delete branch: $branch"
|
|
775
|
+
fi
|
|
776
|
+
fi
|
|
777
|
+
else
|
|
778
|
+
warn "could not remove worktree: $path"
|
|
779
|
+
failures=$((failures + 1))
|
|
780
|
+
fi
|
|
781
|
+
done <<< "$removals"
|
|
782
|
+
if [ "$prune_missing" = true ]; then
|
|
783
|
+
git -C "$MAIN_ROOT" worktree prune
|
|
784
|
+
printf 'Pruned missing worktrees\n'
|
|
785
|
+
fi
|
|
786
|
+
printf 'Cleaned up %s worktree(s)\n' "$removed"
|
|
787
|
+
[ "$failures" -eq 0 ]
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
cmd_init() {
|
|
791
|
+
require_repo
|
|
792
|
+
[ ! -e "$CONFIG_FILE" ] || die "configuration file already exists: $CONFIG_FILE"
|
|
793
|
+
# '##' lines explain a setting. A single '#' disables a setting that is shown
|
|
794
|
+
# with its default value: remove the '#' to override that default.
|
|
795
|
+
cat > "$CONFIG_FILE" <<EOF
|
|
796
|
+
## git-worktree-manager configuration.
|
|
797
|
+
## Environment variables (GIT_WORKTREE_MANAGER_*) take precedence over these values.
|
|
798
|
+
##
|
|
799
|
+
## Lines starting with '##' are comments.
|
|
800
|
+
## Lines starting with a single '#' are settings set to their default value:
|
|
801
|
+
## remove the '#' to override the default.
|
|
802
|
+
[worktree-manager]
|
|
803
|
+
## Directory where worktrees are created, relative to the main worktree.
|
|
804
|
+
# root = $DEFAULT_ROOT
|
|
805
|
+
|
|
806
|
+
## Space-separated patterns of files synced into worktrees.
|
|
807
|
+
# syncInclude = $DEFAULT_SYNC_INCLUDE
|
|
808
|
+
|
|
809
|
+
## Space-separated patterns of files that are never synced.
|
|
810
|
+
# syncExclude = $DEFAULT_SYNC_EXCLUDE
|
|
811
|
+
|
|
812
|
+
## Run git fetch before add: true or false.
|
|
813
|
+
# autoFetch = $DEFAULT_AUTO_FETCH
|
|
814
|
+
|
|
815
|
+
## Space-separated branches whose worktrees cleanup never removes.
|
|
816
|
+
# protectedBranches = $DEFAULT_PROTECTED_BRANCHES
|
|
817
|
+
|
|
818
|
+
## Ref that cleanup compares worktrees to. Empty: origin's default branch.
|
|
819
|
+
# baseBranch = $DEFAULT_BASE_BRANCH
|
|
820
|
+
EOF
|
|
821
|
+
printf 'Created %s\n' "$CONFIG_FILE"
|
|
822
|
+
ensure_gitignored
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
main() {
|
|
826
|
+
local command=${1:-help}
|
|
827
|
+
if [ "$#" -gt 0 ]; then
|
|
828
|
+
shift
|
|
829
|
+
fi
|
|
830
|
+
case $command in
|
|
831
|
+
add) cmd_add "$@" ;;
|
|
832
|
+
list) cmd_list "$@" ;;
|
|
833
|
+
find) cmd_find "$@" ;;
|
|
834
|
+
remove) cmd_remove "$@" ;;
|
|
835
|
+
move) cmd_move "$@" ;;
|
|
836
|
+
checkout) cmd_checkout "$@" ;;
|
|
837
|
+
sync) cmd_sync "$@" ;;
|
|
838
|
+
cleanup) cmd_cleanup "$@" ;;
|
|
839
|
+
init) cmd_init "$@" ;;
|
|
840
|
+
help | -h | --help) usage ;;
|
|
841
|
+
*)
|
|
842
|
+
usage >&2
|
|
843
|
+
die "unknown command: $command"
|
|
844
|
+
;;
|
|
845
|
+
esac
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
main "$@"
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@w5s/git-worktree-manager",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI to manage git worktrees: create, list, move, checkout, remove, clean up and sync env files",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"git",
|
|
7
|
+
"worktree",
|
|
8
|
+
"cli",
|
|
9
|
+
"bash"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/w5s/project-config/blob/main/packages/git-worktree-manager#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/w5s/project-config/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/w5s/project-config.git",
|
|
18
|
+
"directory": "packages/git-worktree-manager"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Julien Polo <julien.polo@gmail.com>",
|
|
22
|
+
"bin": {
|
|
23
|
+
"git-worktree-manager": "bin/git-worktree-manager"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"bin/",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"postpack": "clean-package restore"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
}
|
|
35
|
+
}
|