worktree_manager 0.2.1
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.
- checksums.yaml +7 -0
- data/.github/workflows/test.yml +52 -0
- data/.gitignore +37 -0
- data/.mise.toml +9 -0
- data/.rspec +3 -0
- data/.version +1 -0
- data/.worktree.yml.example +45 -0
- data/CHANGELOG.md +50 -0
- data/CLAUDE.md +17 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +53 -0
- data/LICENSE +9 -0
- data/README.md +391 -0
- data/Rakefile +6 -0
- data/bin/wm +6 -0
- data/bin/worktree_manager +6 -0
- data/lib/worktree_manager/cli.rb +794 -0
- data/lib/worktree_manager/config_manager.rb +64 -0
- data/lib/worktree_manager/hook_manager.rb +269 -0
- data/lib/worktree_manager/manager.rb +126 -0
- data/lib/worktree_manager/version.rb +3 -0
- data/lib/worktree_manager/worktree.rb +56 -0
- data/lib/worktree_manager.rb +11 -0
- data/mise/release.sh +120 -0
- data/pkg/worktree_manager-0.1.0.gem +0 -0
- data/spec/integration/worktree_integration_spec.rb +288 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/worktree_manager/cli_help_spec.rb +154 -0
- data/spec/worktree_manager/cli_spec.rb +976 -0
- data/spec/worktree_manager/config_manager_spec.rb +172 -0
- data/spec/worktree_manager/hook_manager_spec.rb +581 -0
- data/spec/worktree_manager/manager_spec.rb +95 -0
- data/spec/worktree_manager/worktree_spec.rb +83 -0
- data/spec/worktree_manager_spec.rb +14 -0
- data/worktree_manager.gemspec +33 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f1d77801499b0a0039a83fd38626cc70dd4ee886956457143eb184d855f893f7
|
4
|
+
data.tar.gz: 698b04a2042f8c441e462e47837d8290e812d7263b7fbd4bf6aee5879e91c294
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d89c295082ad33003c03a1d07217a085c216706f63ff8078aef142f520b01ffb1e135eae8a422c39a7f17090747f20e02b7a90333f3983b15ac35c73b85cfae
|
7
|
+
data.tar.gz: 1df27c192fc56bf9a0e730f935b54e4a34aa2c30e436f64558f1c7f98819c23bf0045e782d46e6b38c659bb14d4b0bb4e04ba93a76bef1d1694e9179fad2509e
|
@@ -0,0 +1,52 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main, master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ main, master ]
|
8
|
+
workflow_dispatch:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
test:
|
12
|
+
name: Ruby ${{ matrix.ruby }} on ${{ matrix.os }}
|
13
|
+
runs-on: ${{ matrix.os }}
|
14
|
+
continue-on-error: ${{ matrix.experimental }}
|
15
|
+
|
16
|
+
strategy:
|
17
|
+
fail-fast: false
|
18
|
+
matrix:
|
19
|
+
os: [ubuntu-latest, macos-latest]
|
20
|
+
ruby: ['3.1', '3.2', '3.3', '3.4']
|
21
|
+
experimental: [false]
|
22
|
+
include:
|
23
|
+
# Test on Windows with each Ruby version
|
24
|
+
- os: windows-latest
|
25
|
+
ruby: '3.1'
|
26
|
+
experimental: true
|
27
|
+
- os: windows-latest
|
28
|
+
ruby: '3.2'
|
29
|
+
experimental: true
|
30
|
+
- os: windows-latest
|
31
|
+
ruby: '3.3'
|
32
|
+
experimental: true
|
33
|
+
- os: windows-latest
|
34
|
+
ruby: '3.4'
|
35
|
+
experimental: true
|
36
|
+
# Test with Ruby head on Ubuntu
|
37
|
+
- os: ubuntu-latest
|
38
|
+
ruby: 'head'
|
39
|
+
experimental: true
|
40
|
+
|
41
|
+
steps:
|
42
|
+
- uses: actions/checkout@v4
|
43
|
+
|
44
|
+
- name: Set up Ruby
|
45
|
+
uses: ruby/setup-ruby@v1
|
46
|
+
with:
|
47
|
+
ruby-version: ${{ matrix.ruby }}
|
48
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems
|
49
|
+
|
50
|
+
- name: Run tests
|
51
|
+
run: bundle exec rspec --format documentation
|
52
|
+
continue-on-error: ${{ matrix.experimental }}
|
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Ignore test and example directories
|
2
|
+
tmp/
|
3
|
+
|
4
|
+
# Ruby/Bundler files
|
5
|
+
.bundle/
|
6
|
+
vendor/bundle/
|
7
|
+
|
8
|
+
# RSpec coverage
|
9
|
+
coverage/
|
10
|
+
|
11
|
+
# Local configuration
|
12
|
+
.env
|
13
|
+
.env.local
|
14
|
+
|
15
|
+
# OS generated files
|
16
|
+
.DS_Store
|
17
|
+
.DS_Store?
|
18
|
+
._*
|
19
|
+
.Spotlight-V100
|
20
|
+
.Trashes
|
21
|
+
ehthumbs.db
|
22
|
+
Thumbs.db
|
23
|
+
|
24
|
+
# Editor files
|
25
|
+
*.swp
|
26
|
+
*.swo
|
27
|
+
*~
|
28
|
+
.vscode/
|
29
|
+
.idea/
|
30
|
+
|
31
|
+
# Log files
|
32
|
+
*.log
|
33
|
+
|
34
|
+
# Temporary files
|
35
|
+
*.tmp
|
36
|
+
*.temp
|
37
|
+
*.gem
|
data/.mise.toml
ADDED
data/.rspec
ADDED
data/.version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Worktree Manager Configuration
|
2
|
+
|
3
|
+
# Directory where worktrees will be created
|
4
|
+
# When you run 'wm add feature', it creates at '../feature'
|
5
|
+
worktrees_dir: "../"
|
6
|
+
|
7
|
+
# Main branch name for 'wm reset' command
|
8
|
+
# Change to "master" if your main branch is different
|
9
|
+
main_branch_name: "main"
|
10
|
+
|
11
|
+
# Hooks: Commands to run during worktree operations
|
12
|
+
# Available environment variables:
|
13
|
+
# $WORKTREE_PATH - Worktree path (e.g., ../feature)
|
14
|
+
# $WORKTREE_BRANCH - Branch name
|
15
|
+
# $WORKTREE_MAIN - Main repository path
|
16
|
+
# $WORKTREE_ABSOLUTE_PATH - Full path to worktree
|
17
|
+
# $WORKTREE_FORCE - "true" if --force was used
|
18
|
+
|
19
|
+
# Simple format (single command)
|
20
|
+
# hooks:
|
21
|
+
# pre_add: "echo 'Creating worktree for $WORKTREE_BRANCH'"
|
22
|
+
# post_remove: "echo 'Removed $WORKTREE_PATH'"
|
23
|
+
|
24
|
+
# Array format (multiple commands)
|
25
|
+
# hooks:
|
26
|
+
# post_add:
|
27
|
+
# - "bundle install"
|
28
|
+
# - "cp .env.example .env"
|
29
|
+
# - "echo 'Setup complete!'"
|
30
|
+
|
31
|
+
# Advanced format with options
|
32
|
+
# hooks:
|
33
|
+
# post_add:
|
34
|
+
# commands:
|
35
|
+
# - "bundle install"
|
36
|
+
# - "yarn install"
|
37
|
+
# - "rake db:create db:migrate"
|
38
|
+
# pwd: "$WORKTREE_ABSOLUTE_PATH" # Working directory (default: worktree for post_add)
|
39
|
+
# stop_on_error: false # Continue even if a command fails (default: true)
|
40
|
+
#
|
41
|
+
# pre_remove:
|
42
|
+
# commands:
|
43
|
+
# - "git add -A"
|
44
|
+
# - "git stash push -m 'Auto-stash: $WORKTREE_BRANCH'"
|
45
|
+
# stop_on_error: false
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.2.0] - 2025-07-23
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- GitHub Actions CI/CD pipeline with Ruby 3.1-3.4 test matrix
|
12
|
+
- Windows CI testing support for all Ruby versions
|
13
|
+
- MIT LICENSE file
|
14
|
+
- RubyGems metadata including MFA requirement
|
15
|
+
- This CHANGELOG file
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
- Minimum Ruby version requirement from 3.0 to 3.1
|
19
|
+
- Updated gemspec to use `git ls-files` for better file inclusion
|
20
|
+
- Fixed `wm reset` to always perform hard reset, preventing dirty working directory issues
|
21
|
+
- Release script now updates Gemfile.lock to prevent version mismatch in CI
|
22
|
+
|
23
|
+
### Fixed
|
24
|
+
- CI failures caused by version mismatch between .version and Gemfile.lock
|
25
|
+
- Reset command leaving uncommitted changes in working directory
|
26
|
+
|
27
|
+
## [0.1.8] - 2025-07-21
|
28
|
+
|
29
|
+
### Added
|
30
|
+
- `wm init` command to initialize worktree configuration file
|
31
|
+
- `wm reset` command to reset worktree branch to origin/main
|
32
|
+
- Support for `--help` flag on all commands (e.g., `wm add --help`)
|
33
|
+
|
34
|
+
### Changed
|
35
|
+
- Refactored configuration example file from `.worktree.yml.sample` to `.worktree.yml.example`
|
36
|
+
|
37
|
+
## [0.1.7] - 2025-07-21
|
38
|
+
|
39
|
+
### Added
|
40
|
+
- Interactive force removal prompt for worktrees with uncommitted changes
|
41
|
+
|
42
|
+
## [0.1.6] - 2025-07-21
|
43
|
+
|
44
|
+
### Added
|
45
|
+
- `--no-hooks` option to skip hook execution during worktree operations
|
46
|
+
- Comprehensive test coverage for main repository protection
|
47
|
+
|
48
|
+
## Previous versions
|
49
|
+
|
50
|
+
Earlier versions were released to GitHub Packages only. This is the first public release to RubyGems.org.
|
data/CLAUDE.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
## General Working Rules
|
2
|
+
|
3
|
+
- **Follow development guidelines as the top priority.**
|
4
|
+
- Unless specifically requested, think and respond in English.
|
5
|
+
- Do not leave spaces in blank lines. Do not leave trailing spaces at the end of lines. Files must end with a newline character.
|
6
|
+
- Test implemented code directly or write tests using TDD approach.
|
7
|
+
- After completing work, run newly written tests or related tests.
|
8
|
+
- Check changes since the last commit using git before starting work.
|
9
|
+
- Look for similar implementations in the project first, and check with gh if related issues or PRs are mentioned.
|
10
|
+
- New features and tests should be written with minimal code, and do not make modifications beyond what was directly requested.
|
11
|
+
|
12
|
+
## Not Goals
|
13
|
+
|
14
|
+
Do not perform the following tasks unless explicitly requested:
|
15
|
+
|
16
|
+
- Code refactoring work
|
17
|
+
- Project configuration or structure changes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
worktree_manager (0.2.1)
|
5
|
+
thor (~> 1.0)
|
6
|
+
tty-prompt (~> 0.23)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.6.2)
|
12
|
+
pastel (0.8.0)
|
13
|
+
tty-color (~> 0.5)
|
14
|
+
rake (13.3.0)
|
15
|
+
rspec (3.13.1)
|
16
|
+
rspec-core (~> 3.13.0)
|
17
|
+
rspec-expectations (~> 3.13.0)
|
18
|
+
rspec-mocks (~> 3.13.0)
|
19
|
+
rspec-core (3.13.5)
|
20
|
+
rspec-support (~> 3.13.0)
|
21
|
+
rspec-expectations (3.13.5)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.13.0)
|
24
|
+
rspec-mocks (3.13.5)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.13.0)
|
27
|
+
rspec-support (3.13.4)
|
28
|
+
thor (1.3.2)
|
29
|
+
tty-color (0.6.0)
|
30
|
+
tty-cursor (0.7.1)
|
31
|
+
tty-prompt (0.23.1)
|
32
|
+
pastel (~> 0.8)
|
33
|
+
tty-reader (~> 0.8)
|
34
|
+
tty-reader (0.9.0)
|
35
|
+
tty-cursor (~> 0.7)
|
36
|
+
tty-screen (~> 0.8)
|
37
|
+
wisper (~> 2.0)
|
38
|
+
tty-screen (0.8.2)
|
39
|
+
wisper (2.0.1)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
arm64-darwin-24
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
rake (~> 13.0)
|
47
|
+
rspec (~> 3.0)
|
48
|
+
thor (~> 1.0)
|
49
|
+
tty-prompt (~> 0.23)
|
50
|
+
worktree_manager!
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
2.6.9
|
data/LICENSE
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2025 nacyot
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,391 @@
|
|
1
|
+
# WorktreeManager
|
2
|
+
|
3
|
+
A Ruby gem for managing Git worktrees with ease. WorktreeManager provides a simple and intuitive interface for creating, managing, and removing Git worktrees with built-in hook support.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- **Easy worktree management**: Create, list, and remove Git worktrees
|
8
|
+
- **Branch operations**: Create new branches or checkout existing ones
|
9
|
+
- **Hook system**: Execute custom scripts before/after worktree operations
|
10
|
+
- **Conflict detection**: Automatic validation to prevent path and branch conflicts
|
11
|
+
- **CLI interface**: Simple command-line tool for quick operations
|
12
|
+
- **Ruby API**: Programmatic access for integration with other tools
|
13
|
+
- **Configuration initialization**: Easy setup with `wm init` command
|
14
|
+
- **Branch reset**: Reset worktree branches to origin/main with `wm reset`
|
15
|
+
- **Help support**: All commands support `--help` flag for usage information
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'worktree_manager'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
bundle install
|
29
|
+
```
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
gem install worktree_manager
|
35
|
+
```
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Command Line Interface
|
40
|
+
|
41
|
+
WorktreeManager provides a CLI tool called `wm` for managing worktrees:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
# Initialize configuration file
|
45
|
+
wm init
|
46
|
+
|
47
|
+
# List all worktrees
|
48
|
+
wm list
|
49
|
+
|
50
|
+
# Create a new worktree using just a name (uses worktrees_dir)
|
51
|
+
wm add feature-branch
|
52
|
+
|
53
|
+
# Create a worktree with a relative path
|
54
|
+
wm add ../feature-branch
|
55
|
+
|
56
|
+
# Create a worktree with an absolute path
|
57
|
+
wm add /path/to/feature-branch
|
58
|
+
|
59
|
+
# Create a worktree with an existing branch
|
60
|
+
wm add feature-branch existing-branch
|
61
|
+
|
62
|
+
# Create a worktree with a new branch
|
63
|
+
wm add feature-branch -b new-feature-branch
|
64
|
+
|
65
|
+
# Remove a worktree using just a name
|
66
|
+
wm remove feature-branch
|
67
|
+
|
68
|
+
# Remove a worktree with a path
|
69
|
+
wm remove ../feature-branch
|
70
|
+
|
71
|
+
# Force operations (bypass safety checks)
|
72
|
+
wm add existing-dir -f
|
73
|
+
wm remove worktree-with-changes -f
|
74
|
+
|
75
|
+
# Track remote branches
|
76
|
+
wm add pr-154 -t origin/pr-154 # Create local pr-154 tracking origin/pr-154
|
77
|
+
wm add pr-154 origin/pr-154 # Auto-detect remote branch
|
78
|
+
wm add hotfix -t upstream/hotfix-123 # Track from different remote
|
79
|
+
|
80
|
+
# Reset worktree branch to origin/main (must be run from worktree)
|
81
|
+
wm reset # Reset current branch to origin/main
|
82
|
+
wm reset -f # Force reset (discard uncommitted changes)
|
83
|
+
|
84
|
+
# Get help for any command
|
85
|
+
wm --help # Show all commands
|
86
|
+
wm add --help # Show help for add command
|
87
|
+
wm remove -h # Short help flag also works
|
88
|
+
```
|
89
|
+
|
90
|
+
#### Working with Remote Branches
|
91
|
+
|
92
|
+
WorktreeManager makes it easy to work with remote branches:
|
93
|
+
|
94
|
+
```bash
|
95
|
+
# Method 1: Using --track (-t) option
|
96
|
+
wm add pr-154 -t origin/pr-154
|
97
|
+
# This will:
|
98
|
+
# 1. Fetch origin/pr-154
|
99
|
+
# 2. Create a new local branch 'pr-154' tracking 'origin/pr-154'
|
100
|
+
# 3. Create worktree at '../worktrees/pr-154' (or configured location)
|
101
|
+
|
102
|
+
# Method 2: Auto-detection (when branch name contains '/')
|
103
|
+
wm add pr-154 origin/pr-154
|
104
|
+
# Automatically detects that origin/pr-154 is a remote branch
|
105
|
+
|
106
|
+
# Method 3: Different local and remote names
|
107
|
+
wm add my-fix -t origin/pr-154
|
108
|
+
# Creates local branch 'my-fix' tracking 'origin/pr-154'
|
109
|
+
|
110
|
+
# Working with different remotes
|
111
|
+
wm add upstream-fix -t upstream/fix-123
|
112
|
+
wm add fork-feature -t fork/new-feature
|
113
|
+
```
|
114
|
+
|
115
|
+
Example workflow for Pull Request review:
|
116
|
+
|
117
|
+
```bash
|
118
|
+
# Review PR #154
|
119
|
+
wm add pr-154 -t origin/pr-154
|
120
|
+
cd ../worktrees/pr-154
|
121
|
+
# Make changes, test, etc.
|
122
|
+
|
123
|
+
# When done, remove the worktree
|
124
|
+
cd ../main-repo
|
125
|
+
wm remove pr-154
|
126
|
+
```
|
127
|
+
|
128
|
+
### Ruby API
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
require 'worktree_manager'
|
132
|
+
|
133
|
+
# Create a manager instance
|
134
|
+
manager = WorktreeManager.new
|
135
|
+
|
136
|
+
# List existing worktrees
|
137
|
+
worktrees = manager.list
|
138
|
+
worktrees.each do |worktree|
|
139
|
+
puts "Path: #{worktree.path}"
|
140
|
+
puts "Branch: #{worktree.branch}"
|
141
|
+
puts "Detached: #{worktree.detached?}"
|
142
|
+
end
|
143
|
+
|
144
|
+
# Add a new worktree
|
145
|
+
worktree = manager.add("../feature-branch", "feature-branch")
|
146
|
+
|
147
|
+
# Add a worktree with a new branch
|
148
|
+
worktree = manager.add_with_new_branch("../new-feature", "new-feature-branch")
|
149
|
+
|
150
|
+
# Remove a worktree
|
151
|
+
manager.remove("../feature-branch")
|
152
|
+
|
153
|
+
# Clean up removed worktrees
|
154
|
+
manager.prune
|
155
|
+
```
|
156
|
+
|
157
|
+
## Hook System
|
158
|
+
|
159
|
+
WorktreeManager supports hooks that execute before and after worktree operations:
|
160
|
+
|
161
|
+
### Hook Types
|
162
|
+
|
163
|
+
- `pre_add`: Execute before creating a worktree
|
164
|
+
- `post_add`: Execute after creating a worktree
|
165
|
+
- `pre_remove`: Execute before removing a worktree
|
166
|
+
- `post_remove`: Execute after removing a worktree
|
167
|
+
|
168
|
+
### Configuration
|
169
|
+
|
170
|
+
Create a `.worktree.yml` file in your repository root to configure WorktreeManager:
|
171
|
+
|
172
|
+
```yaml
|
173
|
+
# Base directory for worktrees (default: "../")
|
174
|
+
# When you run 'wm add feature', it creates worktree at '../feature' by default
|
175
|
+
# With worktrees_dir set to "../worktrees", it creates at '../worktrees/feature'
|
176
|
+
worktrees_dir: "../worktrees"
|
177
|
+
|
178
|
+
# Main branch name for reset command (default: "main")
|
179
|
+
# Used by 'wm reset' to determine which branch to reset to
|
180
|
+
main_branch_name: "main" # or "master" for older repositories
|
181
|
+
|
182
|
+
# Hook configuration (see below)
|
183
|
+
hooks:
|
184
|
+
# ...
|
185
|
+
```
|
186
|
+
|
187
|
+
#### Worktrees Directory
|
188
|
+
|
189
|
+
The `worktrees_dir` option allows you to specify a default location for your worktrees:
|
190
|
+
|
191
|
+
- **Default value**: `../` (parent directory of your main repository)
|
192
|
+
- **Purpose**: Organize all worktrees in a specific directory
|
193
|
+
- **Usage**: When you use `wm add <name>` with just a name (no path), it creates the worktree in `<worktrees_dir>/<name>`
|
194
|
+
|
195
|
+
Example configurations:
|
196
|
+
|
197
|
+
```yaml
|
198
|
+
# Keep worktrees in a sibling directory
|
199
|
+
worktrees_dir: "../worktrees"
|
200
|
+
|
201
|
+
# Keep worktrees in a subdirectory of the parent
|
202
|
+
worktrees_dir: "../../git-worktrees"
|
203
|
+
|
204
|
+
# Use absolute path
|
205
|
+
worktrees_dir: "/home/user/projects/worktrees"
|
206
|
+
```
|
207
|
+
|
208
|
+
### Hook Configuration
|
209
|
+
|
210
|
+
Hooks allow you to execute custom scripts during worktree operations:
|
211
|
+
|
212
|
+
```yaml
|
213
|
+
hooks:
|
214
|
+
# Execute before creating a worktree (runs in main repository)
|
215
|
+
pre_add:
|
216
|
+
commands:
|
217
|
+
- "echo 'Creating worktree at: $WORKTREE_PATH'"
|
218
|
+
- "echo 'Branch: $WORKTREE_BRANCH'"
|
219
|
+
stop_on_error: true # Stop if any command fails (default: true)
|
220
|
+
|
221
|
+
# Execute after creating a worktree (runs in new worktree directory)
|
222
|
+
post_add:
|
223
|
+
commands:
|
224
|
+
- "bundle install"
|
225
|
+
- "echo 'Setup complete: $WORKTREE_BRANCH'"
|
226
|
+
# Override default working directory if needed
|
227
|
+
# pwd: "$WORKTREE_MAIN" # Run in main repository instead
|
228
|
+
|
229
|
+
# Execute before removing a worktree (runs in worktree directory)
|
230
|
+
pre_remove:
|
231
|
+
commands:
|
232
|
+
- "git add -A"
|
233
|
+
- "git stash push -m 'Auto stash before removal'"
|
234
|
+
stop_on_error: false # Continue even if commands fail
|
235
|
+
|
236
|
+
# Execute after removing a worktree (runs in main repository)
|
237
|
+
post_remove:
|
238
|
+
commands:
|
239
|
+
- "echo 'Cleanup complete: $WORKTREE_PATH'"
|
240
|
+
```
|
241
|
+
|
242
|
+
### Available Environment Variables
|
243
|
+
|
244
|
+
- `$WORKTREE_PATH`: Path where the worktree will be created/removed (relative path)
|
245
|
+
- `$WORKTREE_ABSOLUTE_PATH`: Absolute path to the worktree
|
246
|
+
- `$WORKTREE_BRANCH`: Branch name (if specified)
|
247
|
+
- `$WORKTREE_MAIN`: Main repository path
|
248
|
+
- `$WORKTREE_MANAGER_ROOT`: Main repository path (legacy, same as `$WORKTREE_MAIN`)
|
249
|
+
- `$WORKTREE_FORCE`: Whether force option is enabled ("true" or "")
|
250
|
+
- `$WORKTREE_SUCCESS`: Whether the operation succeeded (post hooks only, "true" or "false")
|
251
|
+
|
252
|
+
### Practical Hook Examples
|
253
|
+
|
254
|
+
```yaml
|
255
|
+
hooks:
|
256
|
+
# Automatic development environment setup
|
257
|
+
post_add:
|
258
|
+
commands:
|
259
|
+
- "bundle install" # Install dependencies
|
260
|
+
- "yarn install" # Install JS dependencies
|
261
|
+
- "cp .env.example .env" # Copy environment variables
|
262
|
+
- "code ." # Open in VS Code
|
263
|
+
# Default pwd is the new worktree directory
|
264
|
+
|
265
|
+
# Automatic backup of work
|
266
|
+
pre_remove:
|
267
|
+
commands:
|
268
|
+
- "git add -A"
|
269
|
+
- "git stash push -m 'Auto backup: $WORKTREE_BRANCH'"
|
270
|
+
stop_on_error: false # Continue even if nothing to stash
|
271
|
+
|
272
|
+
# Notification system (run in main repository)
|
273
|
+
post_add:
|
274
|
+
commands:
|
275
|
+
- "osascript -e 'display notification \"Workspace ready: $WORKTREE_BRANCH\" with title \"WorktreeManager\"'"
|
276
|
+
pwd: "$WORKTREE_MAIN" # Run notification from main repo
|
277
|
+
|
278
|
+
# CI/CD integration
|
279
|
+
post_add:
|
280
|
+
commands:
|
281
|
+
- "gh pr create --draft --title 'WIP: $WORKTREE_BRANCH' --body 'Auto-created by worktree manager'"
|
282
|
+
pwd: "$WORKTREE_ABSOLUTE_PATH"
|
283
|
+
```
|
284
|
+
|
285
|
+
## CLI Command Reference
|
286
|
+
|
287
|
+
All commands support help flags (`--help`, `-h`, `-?`, `--usage`) to display usage information.
|
288
|
+
|
289
|
+
### `wm version`
|
290
|
+
Display the current installed version.
|
291
|
+
|
292
|
+
### `wm init`
|
293
|
+
Initialize a `.worktree.yml` configuration file in your repository.
|
294
|
+
|
295
|
+
**Options**:
|
296
|
+
- `-f, --force`: Overwrite existing configuration file
|
297
|
+
|
298
|
+
**Requirements**: Must be run from the main Git repository
|
299
|
+
|
300
|
+
**Examples**:
|
301
|
+
```bash
|
302
|
+
wm init # Create .worktree.yml from example
|
303
|
+
wm init --force # Overwrite existing .worktree.yml
|
304
|
+
```
|
305
|
+
|
306
|
+
### `wm list`
|
307
|
+
List all worktrees in the current Git repository. Can be run from either the main repository or any worktree.
|
308
|
+
|
309
|
+
### `wm add PATH [BRANCH]`
|
310
|
+
Create a new worktree.
|
311
|
+
|
312
|
+
**Arguments**:
|
313
|
+
- `PATH`: Path where the worktree will be created
|
314
|
+
- `BRANCH`: Branch to use (optional)
|
315
|
+
|
316
|
+
**Options**:
|
317
|
+
- `-b, --branch BRANCH`: Create a new branch for the worktree
|
318
|
+
- `-f, --force`: Force creation even if directory exists
|
319
|
+
- `-v, --verbose`: Enable verbose output for debugging
|
320
|
+
|
321
|
+
**Examples**:
|
322
|
+
```bash
|
323
|
+
wm add ../feature-api feature/api # Use existing branch
|
324
|
+
wm add ../new-feature -b feature/new # Create new branch
|
325
|
+
wm add ../override --force # Force creation
|
326
|
+
```
|
327
|
+
|
328
|
+
### `wm remove PATH`
|
329
|
+
Remove an existing worktree.
|
330
|
+
|
331
|
+
**Arguments**:
|
332
|
+
- `PATH`: Path of the worktree to remove
|
333
|
+
|
334
|
+
**Options**:
|
335
|
+
- `-f, --force`: Force removal even if worktree has changes
|
336
|
+
- `-v, --verbose`: Enable verbose output for debugging
|
337
|
+
|
338
|
+
**Examples**:
|
339
|
+
```bash
|
340
|
+
wm remove ../feature-api # Normal removal
|
341
|
+
wm remove ../old-feature --force # Force removal
|
342
|
+
```
|
343
|
+
|
344
|
+
### `wm reset`
|
345
|
+
Reset the current worktree branch to origin/main (or configured main branch).
|
346
|
+
|
347
|
+
**Options**:
|
348
|
+
- `-f, --force`: Force reset even if there are uncommitted changes
|
349
|
+
|
350
|
+
**Requirements**: Must be run from a worktree (not from the main repository)
|
351
|
+
|
352
|
+
**Configuration**: The target branch can be configured via `main_branch_name` in `.worktree.yml`
|
353
|
+
|
354
|
+
**Examples**:
|
355
|
+
```bash
|
356
|
+
wm reset # Reset to origin/main
|
357
|
+
wm reset --force # Force reset, discarding changes
|
358
|
+
```
|
359
|
+
|
360
|
+
## Requirements
|
361
|
+
|
362
|
+
- Ruby 3.0.0 or higher
|
363
|
+
- Git 2.5.0 or higher (for worktree support)
|
364
|
+
|
365
|
+
## Development
|
366
|
+
|
367
|
+
After checking out the repo, run:
|
368
|
+
|
369
|
+
```bash
|
370
|
+
# Install dependencies
|
371
|
+
bundle install
|
372
|
+
|
373
|
+
# Run tests
|
374
|
+
bundle exec rspec
|
375
|
+
|
376
|
+
# Build gem
|
377
|
+
gem build worktree_manager.gemspec
|
378
|
+
|
379
|
+
# Install locally
|
380
|
+
gem install worktree_manager-*.gem
|
381
|
+
```
|
382
|
+
|
383
|
+
## Contributing
|
384
|
+
|
385
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/nacyot/worktree_manager.
|
386
|
+
|
387
|
+
1. Fork the repository
|
388
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
389
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
390
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
391
|
+
5. Open a Pull Request
|