Skip to content

Commit 6ddbc74

Browse files
committed
fix: worktree merge command support
1 parent 71989f4 commit 6ddbc74

File tree

6 files changed

+515
-6
lines changed

6 files changed

+515
-6
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,33 @@ Or use the `--stack` flag from anywhere:
520520
slic --stack=/path/to/worktree run wpunit
521521
```
522522

523+
#### Merging a Worktree
524+
525+
When you're done working on a feature branch, merge it back and clean up:
526+
527+
```bash
528+
# From the base stack directory (not the worktree directory)
529+
slic worktree merge fix/issue-123
530+
```
531+
532+
**Important:** This command must be run from the base stack directory, not from within the worktree directory being merged. The worktree directory will be removed by git during the merge process.
533+
534+
The merge command will:
535+
1. Checkout the base branch in the target repository
536+
2. Merge the worktree branch into the base branch
537+
3. Remove the git worktree directory
538+
4. Delete the local worktree branch
539+
5. Stop Docker containers
540+
6. Unregister the slic stack
541+
542+
Use `-y` or `--yes` to skip confirmation prompts:
543+
544+
```bash
545+
slic worktree merge fix/issue-123 -y
546+
```
547+
548+
If the merge encounters conflicts, the command will stop and provide instructions for manual resolution.
549+
523550
#### Removing a Worktree
524551

525552
To remove a worktree and clean up its slic stack:

slic.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,74 @@ As an example, if I wanted to dump the current database contents to the `tests/_
175175
slic cli db export /plugins/the-events/calendar/tests/_data/dump.sql
176176
```
177177

178+
## Git Worktree Support
179+
180+
The stack supports git worktrees with dedicated slic stacks, allowing you to work on multiple branches simultaneously with isolated environments.
181+
182+
### Creating a worktree
183+
184+
Create a new worktree for a feature branch:
185+
186+
```bash
187+
slic worktree add fix/issue-123
188+
```
189+
190+
This will:
191+
- Create a git worktree in a sibling directory
192+
- Set up a dedicated slic stack for the worktree
193+
- Allow you to run tests and develop in isolation
194+
195+
### Listing worktrees
196+
197+
View all worktrees and their associated stacks:
198+
199+
```bash
200+
slic worktree list
201+
```
202+
203+
### Merging a worktree
204+
205+
When you're done working on a feature branch, merge it back and clean up:
206+
207+
```bash
208+
# From the base stack directory (not the worktree directory)
209+
slic worktree merge fix/issue-123
210+
```
211+
212+
**Important:** This command must be run from the base stack directory, not from within the worktree directory being merged. The worktree directory will be removed by git during the merge process.
213+
214+
The merge command will:
215+
1. Checkout the base branch in the target repository
216+
2. Merge the worktree branch into the base branch
217+
3. Remove the git worktree directory
218+
4. Delete the local worktree branch
219+
5. Stop Docker containers
220+
6. Unregister the slic stack
221+
222+
Use `-y` or `--yes` to skip confirmation prompts:
223+
224+
```bash
225+
slic worktree merge fix/issue-123 -y
226+
```
227+
228+
If the merge encounters conflicts, the command will stop and provide instructions for manual resolution.
229+
230+
### Removing a worktree without merging
231+
232+
To remove a worktree without merging its changes:
233+
234+
```bash
235+
slic worktree remove fix/issue-123
236+
```
237+
238+
### Synchronizing worktrees
239+
240+
Clean up stale entries when worktrees have been manually removed:
241+
242+
```bash
243+
slic worktree sync
244+
```
245+
178246
## How the stack works, an overview
179247

180248
The stack services are defined by the `slic-stack.yml` file.

src/commands/worktree.php

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,108 @@
11
<?php
2+
/**
3+
* Handles the `worktree` command and its subcommands.
4+
*/
5+
26
namespace StellarWP\Slic;
37

48
// Load worktree utility functions
59
require_once __DIR__ . '/../worktree-utils.php';
610

11+
if ( $is_help ) {
12+
$help = <<< HELP
13+
SUMMARY:
14+
15+
Manage git worktrees with dedicated slic stacks.
16+
17+
USAGE:
18+
19+
<yellow>{$cli_name} worktree <subcommand> [<args>]</yellow>
20+
21+
SUBCOMMANDS:
22+
23+
<light_cyan>add <branch> [-y|--yes]</light_cyan>
24+
Create a new git worktree for the specified branch with a dedicated slic stack.
25+
26+
<light_cyan>list</light_cyan>
27+
List all worktrees and their associated slic stacks.
28+
29+
<light_cyan>merge <branch> [-y|--yes]</light_cyan>
30+
Merge worktree branch into base branch and remove the worktree stack.
31+
Must be run from the base stack directory, not from within the worktree.
32+
33+
<light_cyan>remove <branch> [-y|--yes]</light_cyan>
34+
Remove a worktree and its associated slic stack.
35+
36+
<light_cyan>sync</light_cyan>
37+
Synchronize git worktrees with slic registry, removing stale entries.
38+
39+
OPTIONS:
40+
41+
<light_cyan>-y, --yes</light_cyan>
42+
Skip confirmation prompt and proceed immediately. Useful for non-interactive
43+
environments like CI pipelines and automation scripts.
44+
45+
EXAMPLES:
46+
47+
<light_cyan>{$cli_name} worktree add fix/issue-123</light_cyan>
48+
Create a worktree for branch 'fix/issue-123' with a dedicated stack.
49+
50+
<light_cyan>{$cli_name} worktree add feature/new-feature -y</light_cyan>
51+
Create a worktree without confirmation prompt.
52+
53+
<light_cyan>{$cli_name} worktree list</light_cyan>
54+
Show all worktrees and their stacks.
55+
56+
<light_cyan>{$cli_name} worktree remove fix/issue-123</light_cyan>
57+
Remove the worktree for branch 'fix/issue-123' and its stack.
58+
59+
<light_cyan>{$cli_name} worktree merge fix/issue-123</light_cyan>
60+
Merge branch 'fix/issue-123' into base branch and clean up (from base stack directory).
61+
62+
<light_cyan>{$cli_name} worktree merge fix/issue-123 -y</light_cyan>
63+
Merge without confirmation prompt.
64+
65+
<light_cyan>{$cli_name} worktree sync</light_cyan>
66+
Synchronize worktrees with slic registry.
67+
68+
NOTE:
69+
70+
slic provides minimal git worktree integration. Use 'git worktree' directly
71+
for advanced operations.
72+
HELP;
73+
74+
echo colorize( $help );
75+
return;
76+
}
77+
778
$worktree_subcommands = [
879
'add' => 'Create a new git worktree with dedicated stack',
980
'list' => 'List worktrees and their stacks',
81+
'merge' => 'Merge worktree branch and remove stack',
1082
'remove' => 'Remove a worktree and its stack',
1183
'sync' => 'Synchronize git worktrees with slic registry',
1284
];
1385

1486
// Parse subcommand from args
1587
$sub_args = args( [ 'subcommand' ], $args( '...' ), 0 );
16-
$subcommand = $sub_args( 'subcommand', false );
88+
$wt_subcommand = $sub_args( 'subcommand', false );
1789

18-
if (empty($subcommand)) {
90+
if (empty($wt_subcommand)) {
1991
echo "Git Worktree Support for slic\n\n";
2092
echo "Available commands:\n";
2193
foreach ($worktree_subcommands as $cmd => $desc) {
2294
echo " slic worktree $cmd - $desc\n";
2395
}
2496
echo "\nNote: slic provides minimal git worktree integration.\n";
2597
echo "Use 'git worktree' directly for advanced operations.\n";
98+
echo "\nRun 'slic worktree help' for more information.\n";
2699
exit(0);
27100
}
28101

29-
$subcommand_file = __DIR__ . '/worktree/' . $subcommand . '.php';
102+
$subcommand_file = __DIR__ . '/worktree/' . $wt_subcommand . '.php';
30103

31104
if (!file_exists($subcommand_file)) {
32-
echo "Unknown worktree command: $subcommand\n";
105+
echo "Unknown worktree command: $wt_subcommand\n";
33106
echo "Run 'slic worktree' to see available commands.\n";
34107
exit(1);
35108
}

src/commands/worktree/add.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
}
151151
}
152152

153+
// Get the base branch before creating the worktree
154+
$base_branch = trim(shell_exec("git -C " . escapeshellarg($target_full_path) . " rev-parse --abbrev-ref HEAD 2>/dev/null"));
155+
153156
// Execute git worktree add (only if needed)
154157
if ($should_create_worktree) {
155158
echo "\nCreating git worktree...\n";
@@ -192,6 +195,7 @@
192195
'stack_id' => $worktree_stack_id,
193196
'is_worktree' => true,
194197
'base_stack_id' => $base_stack_id,
198+
'base_branch' => $base_branch,
195199
'worktree_target' => $target,
196200
'worktree_dir' => $worktree_dir,
197201
'worktree_branch' => $branch,

0 commit comments

Comments
 (0)