-
Notifications
You must be signed in to change notification settings - Fork 583
chore(scripts): Add git worktree management tools #5497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ericapisani
wants to merge
1
commit into
master
Choose a base branch
from
ep/add-worktree-scripts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,3 +34,5 @@ pip-wheel-metadata | |
|
|
||
| # for running AWS Lambda tests using AWS SAM | ||
| sam.template.yaml | ||
|
|
||
| .worktrees/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| echo "Usage: $0 <name>" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| POSITIONAL_ARGS=("$@") | ||
|
|
||
| # For simplicity, we use the same value for both the worktree name and branch name. | ||
| WORKTREE_NAME="${POSITIONAL_ARGS[0]}" | ||
| BRANCH_NAME="${POSITIONAL_ARGS[0]}" | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
| WORKTREE_DIR="$REPO_ROOT/.worktrees/$WORKTREE_NAME" | ||
|
Check warning on line 17 in scripts/worktree-create.sh
|
||
|
|
||
| if [[ -d "$WORKTREE_DIR" ]]; then | ||
| echo "Error: worktree directory already exists: $WORKTREE_DIR" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| if git -C "$REPO_ROOT" branch --list "$BRANCH_NAME" | grep -q .; then | ||
| echo "Error: branch '$BRANCH_NAME' already exists. Delete it first or choose a different name." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Creating worktree '$WORKTREE_NAME' on branch '$BRANCH_NAME'..." | ||
| git -C "$REPO_ROOT" worktree add "$WORKTREE_DIR" -b "$BRANCH_NAME" | ||
|
|
||
| if command -v uv &>/dev/null; then | ||
| echo "Setting up virtual environment with uv..." | ||
| uv venv "$WORKTREE_DIR/.venv" | ||
| else | ||
| echo "uv not found — falling back to python -m venv..." | ||
| python -m venv "$WORKTREE_DIR/.venv" | ||
| fi | ||
|
|
||
| echo "" | ||
| echo "Worktree ready!" | ||
| echo " Path: $WORKTREE_DIR" | ||
| echo " Branch: $BRANCH_NAME" | ||
| echo "" | ||
| echo "To start working:" | ||
| echo " cd $WORKTREE_DIR" | ||
| echo " source .venv/bin/activate" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| echo "Usage: $0 <name>" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
|
|
||
| for WORKTREE_NAME in "$@"; do | ||
| WORKTREE_DIR="$REPO_ROOT/.worktrees/$WORKTREE_NAME" | ||
|
Check warning on line 13 in scripts/worktree-delete.sh
|
||
|
|
||
| if [[ ! -d "$WORKTREE_DIR" ]]; then | ||
| echo "Warning: worktree directory not found: $WORKTREE_DIR — skipping" >&2 | ||
| continue | ||
| fi | ||
|
|
||
| # Capture branch name before removal | ||
| BRANCH_NAME="$(git -C "$WORKTREE_DIR" branch --show-current 2>/dev/null || true)" | ||
|
|
||
| echo "Removing worktree '$WORKTREE_NAME'..." | ||
| git -C "$REPO_ROOT" worktree remove "$WORKTREE_DIR" | ||
| echo " Removed: $WORKTREE_DIR" | ||
|
|
||
| if [[ -n "$BRANCH_NAME" ]]; then | ||
| if [[ -t 0 ]]; then | ||
| read -r -p " Delete branch '$BRANCH_NAME'? [y/N] " REPLY | ||
| echo | ||
| else | ||
| REPLY="n" | ||
| fi | ||
| if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then | ||
| git -C "$REPO_ROOT" branch -d "$BRANCH_NAME" | ||
| echo " Deleted branch: $BRANCH_NAME" | ||
| fi | ||
| fi | ||
|
|
||
| echo " Done." | ||
| done | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path traversal allows worktree creation outside intended directory
The Makefile regex validation
^[a-zA-Z0-9_/-]+The Makefile regex validation permits forward slashes in the NAME parameter. This allows path traversal sequences like../../footo create worktrees outside the intended.worktrees` directory. An attacker with access to the Makefile target could create worktrees in arbitrary locations within the filesystem (relative to repo root), potentially overwriting or polluting other directories.Suggested fix: Add validation in the script to reject names containing path traversal patterns, or modify the Makefile regex to disallow forward slashes.
Also found at 1 additional location
scripts/worktree-delete.sh:13-13Identified by Warden [find-bugs] · QA3-AZZ