Skip to content

feat: wt add accepts a pull request from a fork - #10

Merged
lorenzolfm merged 1 commit into
mainfrom
add-fork-pr
Oct 22, 2026
Merged

lorenzolfm merged 1 commit into
mainfrom
add-fork-pr

Conversation

@lorenzolfm

Copy link
Copy Markdown
Owner

wt add <link> refused a pull request from a fork, because origin has no branch at its commit. It now makes the worktree.

  • Git cannot name the fork: refs/pull/<n>/head holds only the commit. When origin has no branch at that commit, the command asks GitHub with gh api repos/<owner>/<repo>/pulls/<n> for the owner and the branch of the fork. A pull request from origin does not use gh, as before.
  • The command uses the remote that has the URL of the fork, in any form. Else it adds a remote with the name of the owner. The URL has the form of the URL of origin, so SSH stays SSH, and a push of a maintainer edit uses the same key as a push to origin.
  • The local branch has the name of the branch in the fork and tracks <owner>/<branch>. When a local branch has that name and tracks a different branch, for example main, the name is <owner>-<branch>. The same pull request always gives the same branch, so a second link goes to the worktree.
  • A link whose branch the command cannot fetch is now an error, also for a pull request from origin. Before, the command made a new branch from the base branch, and the worktree had none of the commits of the pull request.
  • A deleted fork, or a gh that is absent or has no login, gives an error.

wt prune does not change. It fetches from origin only, so it keeps the worktree of a pull request from a fork. The README says to remove it with wt delete.

Output for cashubtc/cdk#2736, from crodas/cdk:

  pull       #2736 (asking origin for its branch)
  pull       #2736 (asking GitHub for its fork)
  remote     crodas (git@github.com:crodas/cdk.git)
  branch     fix/deactivate-vanished-keysets (tracks crodas/fix/deactivate-vanished-keysets, from a new fetch)

Test

The four CI gates (nix build .#wt .#wt-clippy .#wt-test .#wt-fmt) pass. cargo test runs 48 tests, 6 new. Three of them run the command in a temporary repository with an origin and a fork:

  • A pull request from a fork gives a worktree that tracks the fork, at the commit of the fork.
  • A fork branch master next to a local master that tracks origin/master gives alice-master, and master keeps its upstream. A second link gives the same branch and adds no remote.
  • A link whose branch cannot be fetched makes no branch and no worktree.

The other three read the output of gh, also for a deleted fork, and choose the URL form from origin. The test for the clash and the test for the failed fetch fail when the behavior that they protect is removed from the code.

A manual test in a .bare copy of cashubtc/cdk:

Case Result
wt add .../pull/2736 Remote crodas (SSH). The branch tracks crodas/fix/deactivate-vanished-keysets. Worktree at 8415f1f5.
The same link again Goes to the worktree. No change.
Remote crodas with HTTPS, and a local fix/deactivate-vanished-keysets that tracks origin/main Uses crodas. Branch crodas-fix/deactivate-vanished-keysets.
wt add .../pull/2710, from origin Tracks origin/meeting-agenda-2026-10-14. No call to gh.
wt prune --dry-run Keeps the worktree of the fork.

Origin has no branch for a pull request from a fork, and wt add refused
it. Git cannot name the fork: refs/pull/<n>/head holds only the commit.
When origin has no branch at that commit, wt add now asks GitHub with
`gh api` for the owner and the branch of the fork. A pull request from
origin does not need gh.

wt add uses the remote that has the URL of the fork, or it adds a remote
with the name of the owner. The URL has the form of the URL of origin,
so SSH stays SSH. The local branch has the name of the branch in the
fork and tracks <owner>/<branch>. When a local branch has that name and
tracks a different branch, for example main, the name is
<owner>-<branch>. The same pull request therefore always gives the same
branch.

A link whose branch wt cannot fetch is now an error. Before, wt add made
a new branch from the base branch, and the worktree had none of the
commits of the pull request.

wt prune does not change. It fetches from origin only, so it keeps the
worktree of a pull request from a fork.
@lorenzolfm
lorenzolfm merged commit 316d01f into main Oct 22, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant