feat(desktop): auto-fetch remote base branch before worktree add

When the base is an origin/… ref, fetch just that branch so the
local tracking ref is fresh before `git worktree add -b new origin/main`.
Fetch failures (offline / no remote) are silently ignored — git uses
whatever local ref exists, or raises a clear error if it's missing.
This commit is contained in:
ethernet 2026-07-10 13:40:25 -04:00
parent 6f7ee72be5
commit f6d1fd511c
2 changed files with 28 additions and 2 deletions

View file

@ -251,7 +251,25 @@ async function addWorktree(repoPath, options, gitBin) {
const args = ['worktree', 'add', '-b', branch, dir]
if (opts.base) {
args.push(String(opts.base))
// Remote-tracking branches may be stale or missing if the user hasn't
// fetched recently. When the base is an `origin/…` ref, fetch just that
// branch so `git worktree add -b new origin/main` works against the
// latest remote commit. Local branches are used as-is.
const base = String(opts.base)
if (base.startsWith('origin/')) {
const remoteBranch = base.slice('origin/'.length)
try {
await runGit(gitBin, ['fetch', 'origin', remoteBranch], root)
} catch {
// The fetch isn't mandatory, but it would be nice to do if possible.
// If it's not possible, just use the local ref of the remote branch.
// If it doesn't exist locally, we'll get an error anyways
}
}
args.push(base)
}
try {

View file

@ -597,7 +597,15 @@ def worktree_add(cwd: str, options: dict) -> dict:
target = _unique_dir(os.path.join(root, ".worktrees", slug))
args = ["worktree", "add", "-b", branch, target]
if options.get("base"):
args.append(str(options["base"]))
base = str(options["base"])
# Remote-tracking branches may be stale or missing; fetch just that
# branch so the local ref is up to date before branching. Ignore fetch
# failures (offline / no remote) — git will use whatever local ref
# exists, or raise a clear error below if the ref is entirely missing.
if base.startswith("origin/"):
remote_branch = base[len("origin/"):]
_git(root, ["fetch", "origin", remote_branch])
args.append(base)
code, _, err = _git(root, args)
if code != 0:
if "already exists" in (err or "").lower():