Skip to content

Conversation

@damianlewis
Copy link

@damianlewis damianlewis commented Dec 16, 2025

Pull Request

Description

Use cp -RP instead of cp -r in copy_directories() to prevent symlink dereferencing, which breaks dependency manager binaries (e.g., node_modules/.bin/*).

Motivation

When copying directories like node_modules to new worktrees, symlinks were being dereferenced into regular files. This breaks executables that rely on symlinks (common in npm/pnpm/yarn).

Fixes #45

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Other (please describe):

Testing

Manual Testing Checklist

Tested on:

  • macOS
  • Linux (specify distro: _______)
  • Windows (Git Bash)

Core functionality tested:

  • git gtr new <branch> - Create worktree
  • git gtr go <branch> - Navigate to worktree
  • git gtr editor <branch> - Open in editor (if applicable)
  • git gtr ai <branch> - Start AI tool (if applicable)
  • git gtr rm <branch> - Remove worktree
  • git gtr list - List worktrees
  • git gtr config - Configuration commands (if applicable)
  • Other commands affected by this change: N/A

Test Steps

  1. Create test directory with symlinks:

    mkdir -p test-deps/real-package
    echo "console.log('test')" > test-deps/real-package/index.js
    mkdir -p test-deps/.bin
    ln -s ../real-package/index.js test-deps/.bin/my-cli
  2. Configure gtr to copy the directory:

    git gtr config set gtr.copy.includeDirs test-deps --local
  3. Create a worktree:

    git gtr new test-symlink
  4. Verify symlinks are preserved:

    ls -la "$(git gtr go test-symlink)/test-deps/.bin/"

Expected behavior: Symlink preserved (lrwxr-xr-x -> ../real-package/index.js)

Actual behavior: Symlink preserved (lrwxr-xr-x -> ../real-package/index.js)

Breaking Changes

  • This PR introduces breaking changes
  • I have discussed this in an issue first
  • Migration guide is included in documentation

Checklist

Before submitting this PR, please check:

  • I have read CONTRIBUTING.md
  • My code follows the project's style guidelines
  • I have performed manual testing on at least one platform
  • I have updated documentation (README.md, CLAUDE.md, etc.) if needed
  • My changes work on multiple platforms (or I've noted platform-specific behavior)
  • I have added/updated shell completions (if adding new commands or flags)
  • I have tested with both git gtr (production) and ./bin/gtr (development)
  • No new external dependencies are introduced (Bash + git only)
  • All existing functionality still works

Additional Context

cp -RP is POSIX-compliant and supported on macOS, Linux (GNU coreutils), FreeBSD, and OpenBSD.


License Acknowledgment

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache License 2.0.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed symlink handling during directory copy operations. Symbolic links are now preserved as symlinks rather than dereferenced during directory replication, maintaining their integrity within copied directories. This ensures better consistency when copying complex directory structures containing symbolic references and improves overall file system reliability.

✏️ Tip: You can customize this high-level summary in your review settings.

Use cp -RP instead of cp -r in copy_directories() to prevent
symlink dereferencing, which breaks dependency manager binaries.

Fixes coderabbitai#45
@coderabbitai
Copy link

coderabbitai bot commented Dec 16, 2025

Walkthrough

The copy_directories function in lib/copy.sh was modified to use cp -RP instead of cp -r, preserving symlinks as symlinks rather than dereferencing them during directory replication. This fixes corruption of directories containing symlinks, such as dependency manager binary directories.

Changes

Cohort / File(s) Change Summary
Symlink preservation fix
lib/copy.sh
Updated directory copy command from cp -r to cp -RP to preserve symlinks as symlinks instead of dereferencing them. Prevents corruption of dependency manager binary directories (e.g., node_modules/.bin/, .venv/bin/).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

  • Single-line flag change with clear, well-documented rationale
  • No logic modifications or surrounding code affected
  • Fix addresses a specific, well-understood issue with portable flag compatibility across macOS, Linux, FreeBSD, and OpenBSD

Poem

🐰 A symlink's tale, at last it's told,
No more dereferenced, bold and cold!
With -RP flags, we bind them tight,
Binary paths now work just right! ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: replacing cp -r with cp -RP to preserve symlinks during directory copying.
Linked Issues check ✅ Passed The PR successfully addresses issue #45 by replacing cp -r with cp -RP to preserve symlinks when copying directories, meeting the core coding requirement.
Out of Scope Changes check ✅ Passed All changes are within scope: the modification to lib/copy.sh line 288 and the comment update directly address the symlink preservation issue described in #45.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

copy_directories dereferences symlinks, breaking dependency manager binaries

1 participant