Skip to content

Comments

Copy gitignored files when creating worktree via lazygit#164

Open
rikuson wants to merge 2 commits intomasterfrom
ecstatic-brahmagupta
Open

Copy gitignored files when creating worktree via lazygit#164
rikuson wants to merge 2 commits intomasterfrom
ecstatic-brahmagupta

Conversation

@rikuson
Copy link
Owner

@rikuson rikuson commented Feb 10, 2026

Summary

  • After creating a new worktree with <C-n> in lazygit, copy files ignored by .gitignore (e.g., .env, node_modules) from the source repo to the new worktree using rsync

Test plan

  • Open lazygit and press <C-n> in the local branches view
  • Verify the new worktree contains gitignored files from the original repo

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@claude
Copy link

claude bot commented Feb 10, 2026

Pull Request Review

Summary

This PR adds functionality to copy gitignored files from the source repository to new worktrees created via the lazygit <C-n> command. The implementation uses rsync with git ls-files to identify and copy ignored files.

Code Quality & Best Practices

Positive aspects:

  • Clean shell pipeline using standard Git commands
  • Proper quoting of variables ("$REPO_ROOT", "$WORKTREE_PATH/")
  • Maintains existing error handling pattern (worktree creation already suppresses errors)

Concerns:

  1. Silent failure - The rsync command doesn't have error handling:

    && (cd "$REPO_ROOT" && git ls-files --others --ignored --exclude-standard | rsync -a --files-from=- . "$WORKTREE_PATH/")

    If rsync fails (permissions, disk space, missing rsync), the command chain continues silently. Consider adding || true to explicitly ignore errors, or add error output redirection.

  2. Incomplete implementation - The rsync command only copies untracked ignored files (--others --ignored). It won't copy:

    • Ignored files that were tracked before being added to .gitignore
    • Empty directories (rsync skips empty dirs by default)
    • Symlinks to ignored files (needs --links flag)

Potential Bugs & Issues

Critical:

  1. Path handling with spaces - While variables are quoted, the rsync --files-from reads from stdin and may not handle filenames with newlines correctly. Consider using -print0 with --from0:

    git ls-files -z --others --ignored --exclude-standard | rsync -a --files-from=- --from0 . "$WORKTREE_PATH/"
  2. Race condition - If the worktree creation fails but returns 0, or if files are being actively written during the rsync, you could get partial/corrupted copies.

  3. Submodule handling - If the repo has submodules with their own gitignored files, these won't be copied (git ls-files doesn't recurse into submodules by default).

Minor:

  1. Trailing slash inconsistency - "$WORKTREE_PATH/" has a trailing slash while other uses don't. This is correct for rsync, but worth noting for consistency.

Performance Considerations

  1. Large ignored directories - Copying node_modules (often 100K+ files) or build artifacts could take significant time, blocking lazygit UI. No progress indication for the rsync operation.

  2. Disk space - No check for available disk space before copying potentially GB-sized node_modules directories.

  3. Optimization opportunity - Consider adding rsync flags:

    • --info=progress2 for progress indication
    • --exclude-from= for patterns that shouldn't be copied (e.g., .DS_Store, temp files)
    • --size-only or --checksum depending on use case

Security Concerns

Medium severity:

  1. Arbitrary file copying - If a malicious .gitignore pattern is crafted, it could potentially expose sensitive files. This is low risk since you control the repo, but worth noting.

  2. No path traversal protection - The command assumes git ls-files output is safe, which it should be, but there's no explicit validation.

Test Coverage

The test plan is minimal:

  • ✅ Manual testing specified
  • ❌ No unit/integration tests (hard to automate for GUI tools)
  • ❌ No edge case testing specified (empty repos, repos with no gitignored files, large node_modules)

Suggested test scenarios:

  1. Repo with no gitignored files
  2. Repo with large node_modules (performance test)
  3. Repo with filenames containing spaces/special chars
  4. Repo with nested gitignored directories
  5. Verify .env files are copied correctly (the stated use case)

Recommendations

High priority:

  1. Add null-terminated input handling for filenames with special characters:

    git ls-files -z --others --ignored --exclude-standard | rsync -a --files-from=- --from0 . "$WORKTREE_PATH/"
  2. Consider explicit error handling:

    && (cd "$REPO_ROOT" && git ls-files -z --others --ignored --exclude-standard | rsync -a --files-from=- --from0 . "$WORKTREE_PATH/" 2>/dev/null || echo "Warning: Could not copy gitignored files")

Medium priority:
3. Add progress indication or make rsync quieter to avoid noise on stderr
4. Document the behavior in comments (what gets copied, what doesn't)

Low priority:
5. Consider adding --exclude patterns for files that shouldn't be copied between worktrees (OS-specific files like .DS_Store)

Verdict

Conditional approval - The core implementation is sound, but the lack of null-termination handling could cause issues with special filenames. The silent failure mode is acceptable for this use case (nice-to-have feature), but the special character handling should be fixed before merge.

The feature adds genuine value for development workflows, especially for .env files and local dependencies. Once the null-termination fix is applied, this is ready to merge.

🤖 Review generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant