fix: use origin/ ref when branching from default branch#149
fix: use origin/ ref when branching from default branch#149averyjennings wants to merge 1 commit intomainfrom
Conversation
resolve_default_branch returns a bare branch name (e.g. "main") which
points to the local branch. After git fetch, origin/main is up to date
but the local main may be stale — causing new worktrees to start from
an old commit.
The resolved_ref logic in create_worktree compounds this: it tries
git rev-parse "main^{commit}" first, which succeeds with the stale
local SHA, so the origin/ fallback never runs.
Prefix with origin/ so new branches always start from the latest remote.
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@lib/commands/create.sh`:
- Around line 76-81: The code unconditionally prefixes origin/ onto the value
returned by resolve_default_branch which can produce origin/origin/main; update
the logic around from_ref assignment in create.sh to normalize the resolved
branch: check the string returned by resolve_default_branch (used where from_ref
is set) and if it already starts with "origin/" (or a full ref like
"refs/heads/"), use it as-is, otherwise prepend "origin/"; apply this
normalization in both places where from_ref is assigned so existing configs like
"origin/main" remain unchanged and new values get the correct prefix.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f01bef52-b39c-4c49-8777-d44cb1cef59b
📒 Files selected for processing (1)
lib/commands/create.sh
| from_ref="origin/$(resolve_default_branch "$repo_root")" | ||
| else | ||
| log_info "Creating from current branch: $from_ref" | ||
| fi | ||
| else | ||
| from_ref=$(resolve_default_branch "$repo_root") | ||
| from_ref="origin/$(resolve_default_branch "$repo_root")" |
There was a problem hiding this comment.
Avoid unconditional origin/ prefixing for default branch values.
On Line 76 and Line 81, if resolve_default_branch already returns origin/main (from existing config), this becomes origin/origin/main and ref resolution can fail. Normalize the value before prefixing.
Proposed fix
_create_resolve_from_ref() {
local from_ref="$1" from_current="$2" repo_root="$3"
+ local default_branch
if [ -z "$from_ref" ]; then
if [ "$from_current" -eq 1 ]; then
from_ref=$(get_current_branch)
if [ -z "$from_ref" ] || [ "$from_ref" = "HEAD" ]; then
log_warn "Currently in detached HEAD state - falling back to default branch"
- from_ref="origin/$(resolve_default_branch "$repo_root")"
+ default_branch=$(resolve_default_branch "$repo_root")
+ case "$default_branch" in
+ origin/*) from_ref="$default_branch" ;;
+ refs/remotes/origin/*) from_ref="${default_branch#refs/remotes/}" ;;
+ *) from_ref="origin/$default_branch" ;;
+ esac
else
log_info "Creating from current branch: $from_ref"
fi
else
- from_ref="origin/$(resolve_default_branch "$repo_root")"
+ default_branch=$(resolve_default_branch "$repo_root")
+ case "$default_branch" in
+ origin/*) from_ref="$default_branch" ;;
+ refs/remotes/origin/*) from_ref="${default_branch#refs/remotes/}" ;;
+ *) from_ref="origin/$default_branch" ;;
+ esac
fi
fi
printf "%s" "$from_ref"
}As per coding guidelines, "Maintain backwards compatibility with existing configs in shell scripts."
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| from_ref="origin/$(resolve_default_branch "$repo_root")" | |
| else | |
| log_info "Creating from current branch: $from_ref" | |
| fi | |
| else | |
| from_ref=$(resolve_default_branch "$repo_root") | |
| from_ref="origin/$(resolve_default_branch "$repo_root")" | |
| default_branch=$(resolve_default_branch "$repo_root") | |
| case "$default_branch" in | |
| origin/*) from_ref="$default_branch" ;; | |
| refs/remotes/origin/*) from_ref="${default_branch#refs/remotes/}" ;; | |
| *) from_ref="origin/$default_branch" ;; | |
| esac | |
| else | |
| log_info "Creating from current branch: $from_ref" | |
| fi | |
| else | |
| default_branch=$(resolve_default_branch "$repo_root") | |
| case "$default_branch" in | |
| origin/*) from_ref="$default_branch" ;; | |
| refs/remotes/origin/*) from_ref="${default_branch#refs/remotes/}" ;; | |
| *) from_ref="origin/$default_branch" ;; | |
| esac |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@lib/commands/create.sh` around lines 76 - 81, The code unconditionally
prefixes origin/ onto the value returned by resolve_default_branch which can
produce origin/origin/main; update the logic around from_ref assignment in
create.sh to normalize the resolved branch: check the string returned by
resolve_default_branch (used where from_ref is set) and if it already starts
with "origin/" (or a full ref like "refs/heads/"), use it as-is, otherwise
prepend "origin/"; apply this normalization in both places where from_ref is
assigned so existing configs like "origin/main" remain unchanged and new values
get the correct prefix.
Summary
git gtr new <name>branch from the local default branch (e.g.main), which may be stalegit fetchupdatesorigin/mainbut doesn't fast-forward the localmainbranchresolved_reflogic increate_worktreecompounds this:git rev-parse "main^{commit}"succeeds with the stale local SHA, so theorigin/fallback never runsFix: Prefix
resolve_default_branch()result withorigin/in_create_resolve_from_ref(), so new branches always start from the freshly-fetched remote state.Test plan
git gtr new test-branchcreates worktree atorigin/mainHEAD (not stale localmain)git gtr new test-branch --from-currentstill branches from current local branch (not affected)git gtr new test-branch --from some-refstill uses the explicit ref (not affected)origin/mainSummary by CodeRabbit