Skip to content

Commit 8964018

Browse files
dev-ankitclaude
andauthored
Fix wt run command worktree error (#7)
The wt run command now properly handles the '^' symbol to reference the default worktree, consistent with how wt switch handles it. Changes: - Added special handling for '^' in run command (cli.py) - Added three new tests for run command functionality - Updated notes.md with implementation details Fixes issue where 'wt run ^ "git status"' would error with "Worktree '^' not found" instead of running in the default worktree. All 61 tests passing. Co-authored-by: Claude <noreply@anthropic.com>
1 parent a4a6416 commit 8964018

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

tools/wt-worktree/notes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ wt-worktree/
118118
- worktree.py: 62% (worktree operations tested)
119119
- cli.py: 54% (CLI commands tested including secondary worktree usage)
120120

121+
7. **Missing Special Symbol Support in `wt run`**
122+
- Problem: The `wt run` command didn't support the `^` symbol for the default worktree, while `wt switch ^` did
123+
- Error: Running `wt run ^ "git status"` resulted in "Error: Worktree '^' not found"
124+
- Solution: Added special handling for the `^` symbol in the `run` command (cli.py:373-380) to resolve it to the default worktree name before looking up the worktree
125+
- Implementation: Added check `if name == "^":` to get the default worktree using `ctx.manager.get_default_worktree()` and then use its name
126+
- Tests: Added three new tests in test_cli.py:
127+
- `test_run_command`: Tests running a command in a normal worktree
128+
- `test_run_command_with_default_symbol`: Tests running a command using `^` symbol
129+
- `test_run_command_nonexistent_worktree`: Tests error handling for non-existent worktrees
130+
- Lesson: Always ensure consistency across commands - if a special symbol works in one command, users will expect it to work in related commands too
131+
121132
### Future Improvements
122133

123134
1. **Increase CLI Test Coverage**: Add more edge case tests for CLI commands

tools/wt-worktree/tests/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,24 @@ def test_commands_from_secondary_worktree(runner, initialized_repo, no_prompt):
208208
os.chdir(original_dir)
209209
except (OSError, FileNotFoundError):
210210
os.chdir("/tmp")
211+
212+
213+
def test_run_command(runner, initialized_repo):
214+
"""Test wt run command."""
215+
# Run a simple command in main worktree
216+
result = runner.invoke(cli, ["run", "main", "echo hello"])
217+
assert result.exit_code == 0
218+
219+
220+
def test_run_command_with_default_symbol(runner, initialized_repo):
221+
"""Test wt run command with ^ symbol for default worktree."""
222+
# Run a command in default worktree using ^
223+
result = runner.invoke(cli, ["run", "^", "echo hello"])
224+
assert result.exit_code == 0
225+
226+
227+
def test_run_command_nonexistent_worktree(runner, initialized_repo):
228+
"""Test wt run command with non-existent worktree."""
229+
result = runner.invoke(cli, ["run", "nonexistent", "echo hello"])
230+
assert result.exit_code == 4
231+
assert "not found" in result.output

tools/wt-worktree/wt/cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ def run(ctx: Context, name: str, command: str):
376376
error("Not in a git repository", EXIT_GIT_ERROR)
377377
return
378378

379+
# Handle special names
380+
if name == "^":
381+
# Use default worktree
382+
default_wt = ctx.manager.get_default_worktree()
383+
if not default_wt:
384+
error("Cannot determine default worktree", EXIT_ERROR)
385+
return
386+
name = default_wt["name"]
387+
379388
# Find worktree
380389
wt = ctx.manager.find_worktree_by_name(name)
381390
if not wt:

0 commit comments

Comments
 (0)