Skip to content

Conversation

@KazushiMatsuda
Copy link

@KazushiMatsudaKazushiMatsuda commented Dec 14, 2025

Summary

  • Add # to the list of characters sanitized in branch names
  • Prevents Python venv shebang truncation when # appears in folder paths

Related Issue

Closes#43

Changes

lib/core.sh - sanitize_branch_name():

- printf "%s" "$branch" | sed -e 's/[\/\\ :*?"<>|]/-/g' -e 's/^-*//' -e 's/-*$//'+ printf "%s" "$branch" | sed -e 's/[\/\\ :*?"<>|#]/-/g' -e 's/^-*//' -e 's/-*$//'

Test performed

  • Created worktree with branch feature/issue#123
  • Confirmed folder name is feature-issue-123
  • Verified Python venv works correctly in the worktree

Summary by CodeRabbit

  • Bug Fixes
    • Improved branch name handling to properly format names containing special characters.

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

# in folder names breaks Python venv shebang lines because shell interprets # as comment start, truncating the path. Closescoderabbitai#43
@coderabbitai
Copy link

coderabbitaibot commented Dec 14, 2025

Walkthrough

The sanitize_branch_name function in lib/core.sh is updated to replace the hash symbol (#) with hyphens when converting branch names to worktree folder names. This prevents shell comment interpretation issues in Python virtual environment shebangs.

Changes

Cohort / File(s)Change Summary
Branch name sanitization
lib/core.sh
Updated character replacement pattern in sanitize_branch_name() to include # in the set of characters converted to hyphens, preventing shell interpretation errors in venv shebangs

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

  • Single character addition to a sed regex pattern with minimal scope and no logic changes

Poem

A hash in the branch caused quite the affair,
Python scripts failed with errors to spare!
But our rabbit friend fixed it with care,
Now worktrees work smoothly, beyond all compare! 🐰✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check nameStatusExplanation
Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
Title check✅ PassedThe title accurately describes the main change: adding # to the sanitize_branch_name function to prevent shebang issues with Python venv in worktree folders.
Linked Issues check✅ PassedThe PR implementation matches issue #43 requirements exactly: adding # to the character class in sanitize_branch_name() ensures branch names containing # are converted to safe folder names.
Out of Scope Changes check✅ PassedAll changes are scoped to the single requirement in #43; only the sanitize_branch_name function was modified to add # to the character replacement set.
Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9fd4e85 and 365db45.

📒 Files selected for processing (1)
  • lib/core.sh (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
lib/**/*.sh

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

lib/**/*.sh: Core libraries (lib/core.sh, lib/config.sh, lib/ui.sh, lib/copy.sh, lib/hooks.sh, lib/platform.sh) must be sourced at startup and implement specific functionality
Maintain backwards compatibility with Git <2.22 by using fallback rev-parse --abbrev-ref HEAD instead of branch --show-current

lib/**/*.sh: Maintain backwards compatibility with existing configs in shell scripts
Quote all paths to support spaces in directory names
Use log_error / log_info from lib/ui.sh for user messages
Implement Git version fallbacks (e.g., Git 2.22+ --show-current vs older rev-parse); check lib/core.sh:97-100 for example
Add new config keys with gtr.<name> prefix to avoid collisions
For performance-sensitive loops (e.g., directory scans), prefer built-ins (find, grep) with minimal subshells
For any new Git command, add fallback for older versions or guard with detection

Files:

  • lib/core.sh
**/*.sh

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.sh: Always quote paths to handle spaces and special characters; avoid unguarded globbing
Keep set -e active in shell scripts; ensure non-critical failures are guarded with command || true

**/*.sh: Use shebang #!/usr/bin/env bash (not /bin/bash or /bin/sh)
Use snake_case naming for functions
Use snake_case for local variables and UPPER_CASE for constants/environment variables
Use 2-space indentation (no tabs)
Always quote variables and paths to handle spaces and special characters
Check return codes and use || exit 1 or || return 1 for error handling
Use local for function-scoped variables to avoid polluting global namespace
Target Bash 3.2+ compatibility in shell scripts

Files:

  • lib/core.sh
**/*.{bash,fish,sh}

📄 CodeRabbit inference engine (.github/instructions/sh.instructions.md)

**/*.{bash,fish,sh}: Bash 3.2+ compatible (macOS default), but 4.0+ features allowed where appropriate
Always quote variables: use "$var" not $var
Use function-scoped variables: local var="value"
Check return codes; functions return 1 on failure
Use snake_case for functions and variables, UPPER_CASE for constants
Prefer [ ] over [[ ]] for POSIX portability; use [[ only when needed
Always quote glob inputs; disable unintended globbing with set -f temporarily if required
Avoid associative arrays in shell scripts; use simple string/loop constructs for Bash 3.2+ compatibility
Avoid readarray and process substitution unsupported in older Bash
Debug with bash -x ./bin/gtr <cmd> or wrap suspicious blocks with set -x / set +x
Check function presence with declare -f create_worktree or declare -f resolve_target
Use stderr for variable inspection: echo "DEBUG var=$var" >&2 to keep stdout clean for command substitution
Keep dependencies minimal: only use git, sed, awk, find, grep; avoid jq/curl unless justified
Check availability of external commands before use when adding new tools
Use "${var}" for variable expansion; for line loops use while IFS= read -r line; do ... done to preserve spaces
Sanitize branch names via sanitize_branch_name function; do NOT duplicate logic elsewhere
Everything is sourced at startup with set -e enabled; functions call each other directly; no subshells except for hooks and AI tools

Files:

  • lib/core.sh
{bin/gtr,lib/**/*.sh,adapters/**/*.sh}

📄 CodeRabbit inference engine (.github/instructions/testing.instructions.md)

{bin/gtr,lib/**/*.sh,adapters/**/*.sh}: All commands must exit 0 (except intentional failures) and produce expected side-effects
No unquoted path errors; spaces must be handled in file paths
Hooks must run only once per creation/removal event

Files:

  • lib/core.sh
**/lib/*.sh

📄 CodeRabbit inference engine (CLAUDE.md)

Use set -e for error handling in library files

Files:

  • lib/core.sh
lib/*.sh

📄 CodeRabbit inference engine (CLAUDE.md)

Maintain backwards compatibility with existing configurations when modifying lib/*.sh files

Files:

  • lib/core.sh
🔇 Additional comments (1)
lib/core.sh (1)

22-28: Correct fix to prevent # in directory names. Line 27 safely adds # to the character replacement set, which prevents directory names from containing shell comment characters that could cause issues during script execution (e.g., in shebangs). The sed regex is portable across BSD and GNU sed implementations.

Ensure this sanitization is applied consistently to all branch names before directory creation.


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 freeto 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.

fix: # in branch names breaks Python venv shebang

1 participant

@KazushiMatsuda