agentws: check out --branch from origin when it exists
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline failed

`agentws new --branch` always forked from the base branch, so a worktree for a
branch that already existed on origin started at the base's HEAD with none of
the branch's commits, and callers had to reset --hard afterwards to recover.

- Check out an existing origin/<branch> and set the branch to track it
- Fast-forward a stale local branch onto origin, keeping unpushed commits
- Fork from --from or the remote default only when origin lacks the branch
- Name which of the two paths was taken in the command's output
- Ignore --from, with a note, when the branch is already on origin
This commit is contained in:
2026-09-19 16:11:59 +10:00
parent 72adebbf8b
commit 7510187243
4 changed files with 309 additions and 10 deletions
+16 -2
View File
@@ -97,10 +97,10 @@ checkout too; the worktrees themselves live under the **worktree root**
```bash
# Clone unkin/argocd-apps into ~/src/prodenv if missing, then add a worktree for
# a new branch off the remote default branch. Prints the worktree path.
# the branch. Prints the worktree path.
agentws new argocd-apps --branch benvin/my-change
# Branch off a specific base instead of the remote default
# Branch off a specific base instead of the remote default (new branches only)
agentws new argocd-apps --branch benvin/hotfix --from release-1.2
# List managed worktrees (repo, branch, path)
@@ -124,6 +124,20 @@ agentws clean
agentws token
```
`agentws new` fetches first, then takes one of two paths and names the one it
took on its last output line. A branch that **already exists on origin** is
checked out at `origin/<branch>` and set to track it, so the worktree starts on
the branch's own commits (`branch <b> (tracking origin/<b> at <sha>)`); a local
branch left from an earlier run is fast-forwarded onto it. A branch origin does
**not** have is created from `--from`, or from the remote's default branch when
`--from` is absent (`branch <b> (new, from origin/<base>)`) — the default is read
from `origin/HEAD`, so a repo on `master` forks from `master`. `--from` is
ignored, with a note, when the branch is already on origin.
The one case the worktree does not land on `origin/<branch>` is a local branch
carrying commits origin has never seen. Those commits exist nowhere else, so the
checkout is left on them and the output says how many.
### prune
`agentws prune` finds worktrees two ways and merges the results: the managed