Skip to content

Commit 4b35b3a

Browse files
authored
Merge pull request #2 from wasabeef/fix-create-worktree
2 parents 480b460 + 8624a51 commit 4b35b3a

20 files changed

+2217
-324
lines changed

CLAUDE.md

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,26 @@ source /path/to/git-workers/shell/gw.sh
8181
- Reduced from 3 options to 2: "Create from current HEAD" and "Select branch (smart mode)"
8282
- Smart mode automatically handles branch conflicts and offers appropriate actions
8383

84+
### Custom Path Support
85+
86+
- Added third option for first worktree creation: "Custom path (specify relative to project root)"
87+
- Allows users to specify arbitrary relative paths for worktree creation
88+
- Comprehensive path validation with security checks:
89+
- Prevents absolute paths
90+
- Validates against filesystem-incompatible characters
91+
- Blocks git reserved names in path components
92+
- Prevents excessive path traversal (max one level above project root)
93+
- Cross-platform compatibility checks
94+
8495
### Key Methods Added/Modified
8596

8697
- **`get_branch_worktree_map()`**: Maps branch names to worktree names, including main worktree detection
8798
- **`list_all_branches()`**: Returns both local and remote branches (remote without "origin/" prefix)
8899
- **`create_worktree_with_new_branch()`**: Creates worktree with new branch from base branch (supports git-flow style workflows)
89100
- **`copy_configured_files()`**: Copies files specified in config to new worktrees
101+
- **`create_worktree_from_head()`**: Fixed path resolution for non-bare repositories (converts relative paths to absolute)
102+
- **`validate_custom_path()`**: Validates custom paths for security and compatibility
103+
- **`create_worktree_internal()`**: Enhanced with custom path input option
90104

91105
## Architecture
92106

@@ -176,11 +190,35 @@ Since Git lacks native rename functionality:
176190

177191
### Testing Considerations
178192

179-
- Integration tests in `tests/` directory (27 test files)
193+
- Integration tests in `tests/` directory (30 test files)
180194
- Some tests are flaky in parallel execution (marked with `#[ignore]`)
181195
- CI sets `CI=true` environment variable to skip flaky tests
182196
- Run with `--test-threads=1` for reliable results
183197
- Use `--nocapture` to see test output for debugging
198+
- New test files added:
199+
- `worktree_path_test.rs`: Tests for path resolution and edge cases
200+
- `create_worktree_integration_test.rs`: Integration tests for worktree creation
201+
202+
### String Formatting
203+
204+
- **ALWAYS use inline variable syntax in format! macros**: `format!("{variable}")` instead of `format!("{}", variable)`
205+
- This applies to ALL format-like macros: `format!`, `println!`, `eprintln!`, `log::info!`, `log::warn!`, `log::error!`, etc.
206+
- Examples:
207+
208+
```rust
209+
// ✅ Correct
210+
format!("Device {name} created successfully")
211+
println!("Found {count} devices")
212+
log::info!("Starting device {identifier}")
213+
214+
// ❌ Incorrect
215+
format!("Device {} created successfully", name)
216+
println!("Found {} devices", count)
217+
log::info!("Starting device {}", identifier)
218+
```
219+
220+
- This rule is enforced by `clippy::uninlined_format_args` which treats violations as errors in CI
221+
- Apply this consistently across ALL files including main source, tests, examples, and binary targets
184222

185223
### Important Constraints
186224

@@ -233,3 +271,64 @@ copy = [".env", ".env.local", "config/local.json"]
233271
- Symlink detection with warnings
234272
- Maximum directory depth limit (50 levels)
235273
- Preserves file permissions
274+
275+
## Bug Fixes
276+
277+
### v0.3.0 Worktree Creation Path Resolution
278+
279+
Fixed an issue where creating worktrees from HEAD in non-bare repositories could fail when using relative paths like `../worktree-name`. The fix ensures that relative paths are resolved from the current working directory rather than from the git directory.
280+
281+
**Root Cause**: The `git worktree add` command was being executed with `current_dir` set to the git directory, causing relative paths to be interpreted incorrectly.
282+
283+
### v0.3.0 Security and Robustness Improvements
284+
285+
#### Worktree Name Validation
286+
287+
Added comprehensive validation for worktree names to prevent issues:
288+
289+
- **Invalid Characters**: Rejects filesystem-incompatible characters (`/`, `\`, `:`, `*`, `?`, `"`, `<`, `>`, `|`, `\0`)
290+
- **Reserved Names**: Prevents conflicts with Git internals (`.git`, `HEAD`, `refs`, etc.)
291+
- **Non-ASCII Warning**: Warns users about potential compatibility issues with non-ASCII characters
292+
- **Length Limits**: Enforces 255-character maximum for filesystem compatibility
293+
- **Hidden Files**: Prevents names starting with `.` to avoid hidden file conflicts
294+
295+
#### File Copy Size Limits
296+
297+
Enhanced file copy functionality with safety checks:
298+
299+
- **Large File Skipping**: Automatically skips files larger than 100MB with warnings
300+
- **Performance Protection**: Prevents accidental copying of build artifacts or large binaries
301+
- **User Feedback**: Clear warnings when files are skipped due to size
302+
303+
#### Concurrent Access Control
304+
305+
Implemented file-based locking to prevent race conditions:
306+
307+
- **Process Locking**: Uses `.git/git-workers-worktree.lock` to prevent concurrent worktree creation
308+
- **Stale Lock Cleanup**: Automatically removes locks older than 5 minutes
309+
- **Error Messages**: Clear feedback when another process is creating worktrees
310+
- **Automatic Cleanup**: Lock files are automatically removed when operations complete
311+
312+
#### Custom Path Validation
313+
314+
Added comprehensive validation for user-specified worktree paths:
315+
316+
- **Path Security**: Validates against path traversal attacks and excessive directory navigation
317+
- **Cross-Platform Compatibility**: Checks for Windows reserved characters even on non-Windows systems
318+
- **Git Reserved Names**: Prevents conflicts with git internal directories in path components
319+
- **Path Format Validation**: Ensures proper relative path format (no absolute paths, no trailing slashes)
320+
321+
**Solution**: Convert relative paths to absolute paths before passing them to the git command, ensuring consistent behavior regardless of the working directory.
322+
323+
## Test Coverage
324+
325+
The following test files have been added/updated for v0.3.0:
326+
327+
- `tests/worktree_path_test.rs`: 10 tests for path resolution edge cases
328+
- `tests/create_worktree_integration_test.rs`: 5 integration tests including bare repository scenarios
329+
- `tests/worktree_commands_test.rs`: 3 new tests for HEAD creation patterns
330+
- `tests/validate_worktree_name_test.rs`: 7 tests for name validation including edge cases
331+
- `tests/file_copy_size_test.rs`: 6 tests for file size limits and copying behavior
332+
- `tests/worktree_lock_test.rs`: 5 tests for concurrent access control
333+
- `tests/validate_custom_path_test.rs`: 9 tests for custom path validation including security checks
334+
- Enhanced `tests/create_worktree_integration_test.rs`: 2 additional tests for custom path creation

0 commit comments

Comments
 (0)