-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(worktree): add --dir flag to decouple folder names from branches #39607
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
Conversation
Adds optional --dir parameter to create, checkout, and pr commands. Enables theme-based workflows where multiple related branches can be worked on in the same worktree directory while maintaining separate branch names. Previously, worktree folder names were always tied to branch names, forcing a 1:1 relationship. Now you can specify a custom directory name that persists across branch switches within that worktree.
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.
1 file reviewed, 1 comment
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--dir) | ||
CUSTOM_DIR="$2" | ||
shift 2 | ||
;; | ||
*) | ||
# First non-flag arg is branch name, second is base branch | ||
if [[ -z "$BRANCH_NAME" ]]; then | ||
BRANCH_NAME="$1" | ||
elif [[ "$BASE_BRANCH" == "master" ]]; then | ||
BASE_BRANCH="$1" | ||
fi | ||
shift | ||
;; | ||
esac | ||
done |
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.
logic: argument parsing bug: --dir
requires an argument but doesn't validate that $2
exists before using it. If user runs posthog-worktree create my-branch --dir
, the script will set CUSTOM_DIR=""
and then shift 2
will fail or consume the next argument incorrectly.
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--dir) | |
CUSTOM_DIR="$2" | |
shift 2 | |
;; | |
*) | |
# First non-flag arg is branch name, second is base branch | |
if [[ -z "$BRANCH_NAME" ]]; then | |
BRANCH_NAME="$1" | |
elif [[ "$BASE_BRANCH" == "master" ]]; then | |
BASE_BRANCH="$1" | |
fi | |
shift | |
;; | |
esac | |
done | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--dir) | |
if [[ -z "$2" ]]; then | |
error "Missing argument for --dir flag" | |
exit 1 | |
fi | |
CUSTOM_DIR="$2" | |
shift 2 | |
;; | |
*) | |
# First non-flag arg is branch name, second is base branch | |
if [[ -z "$BRANCH_NAME" ]]; then | |
BRANCH_NAME="$1" | |
elif [[ "$BASE_BRANCH" == "master" ]]; then | |
BASE_BRANCH="$1" | |
fi | |
shift | |
;; | |
esac | |
done |
Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/posthog-worktree
Line: 17:33
Comment:
**logic:** argument parsing bug: `--dir` requires an argument but doesn't validate that `$2` exists before using it. If user runs `posthog-worktree create my-branch --dir`, the script will set `CUSTOM_DIR=""` and then `shift 2` will fail or consume the next argument incorrectly.
```suggestion
while [[ $# -gt 0 ]]; do
case $1 in
--dir)
if [[ -z "$2" ]]; then
error "Missing argument for --dir flag"
exit 1
fi
CUSTOM_DIR="$2"
shift 2
;;
*)
# First non-flag arg is branch name, second is base branch
if [[ -z "$BRANCH_NAME" ]]; then
BRANCH_NAME="$1"
elif [[ "$BASE_BRANCH" == "master" ]]; then
BASE_BRANCH="$1"
fi
shift
;;
esac
done
```
How can I resolve this? If you propose a fix, please make it concise.
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.
This seems legit.
Size Change: 0 B Total Size: 3.05 MB ℹ️ View Unchanged
|
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.
Nice! I think the issue that Greptile points out is legit, but not a blocker.
Adds validation to ensure --dir flag receives an argument. Prevents errors when --dir is used without a value.
Summary
--dir <dirname>
parameter tocreate
,checkout
, andpr
commandsMotivation
Previously, worktree folder names were always tied to branch names (1:1 relationship). For multi-PR workflows around a theme, this meant creating new worktrees for each branch, losing context and setup.
With
--dir
, you can:Changes
--dir
flag alongside positional argscreate_worktree()
,checkout_worktree()
, andcheckout_pr_worktree()
to use custom dirname when providedremove
command unchanged (already finds worktrees by branch name)Test plan
create
with--dir
flagcheckout
with--dir
pr
with--dir
--dir
still work