diff --git a/README.md b/README.md index 4090a45..49e1dc6 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,10 @@ fast-forward ref update for a plain branch). A branch that has diverged is reported as an error and left untouched; a missing local branch is created from the remote tip without asking. Switching then follows the usual rules: a branch with a worktree returns its path, a plain branch shows the -`[s]witch here / [w]orktree` prompt. `--no-color`, `--worktree-root DIR` and -`--path-only` work here too. PR heads are resolved as `origin/`, so +`[s]witch here / [w]orktree` prompt. The row whose branch is checked out in +the current worktree shows ● and `(current)`, just like the normal picker. +`--no-color`, `--worktree-root DIR` and `--path-only` work here too. PR heads +are resolved as `origin/`, so this covers branches pushed to the repository's `origin`, not fork PR heads. Optional config file at `~/.config/wt/config.toml`: diff --git a/SPEC.md b/SPEC.md index 6f4c964..89d46c8 100644 --- a/SPEC.md +++ b/SPEC.md @@ -163,7 +163,9 @@ wt --no-color wt --version / --help ``` -**Pull requests:** `wt pr` runs `gh pr list --state open --json +**Pull requests:** the PR row for the branch checked out in the current +worktree sets `IsCurrent`, so the picker shows the same ● marker and +`(current)` suffix as the normal branch list. `wt pr` runs `gh pr list --state open --json number,title,headRefName` and shows a picker with `#number branch title` rows; `wt pr 123` runs `gh pr view 123 --json ...` and skips the picker. Both then call `action.PreparePRBranch(branch, worktreePath, confirm)`: fetch that diff --git a/cmd/wt/main.go b/cmd/wt/main.go index 2bbf52d..f200457 100644 --- a/cmd/wt/main.go +++ b/cmd/wt/main.go @@ -194,13 +194,11 @@ func runPR(number string, pathOnly bool, worktreeRootFlag string, noColor bool) fmt.Fprintln(os.Stderr, "wt: no open pull requests") return nil } - items := make([]gitdata.Item, 0, len(prs)) - for _, pr := range prs { - items = append(items, gitdata.Item{ - Branch: pr.Branch, - Display: prDisplay(pr), - }) + current, err := currentBranch() + if err != nil { + return err } + items := prItems(prs, current) top, err := gitdata.Toplevel() if err != nil { return err @@ -258,6 +256,34 @@ func prDisplay(pr github.PR) string { return fmt.Sprintf("#%d %s %s", pr.Number, pr.Branch, pr.Title) } +// prItems turns PRs into picker rows. The row matching the checked-out +// branch is marked current, so the picker renders it with the same ● and +// (current) suffix as the normal branch list. +func prItems(prs []github.PR, current string) []gitdata.Item { + items := make([]gitdata.Item, 0, len(prs)) + for _, pr := range prs { + items = append(items, gitdata.Item{ + Branch: pr.Branch, + Display: prDisplay(pr), + IsCurrent: pr.Branch == current, + }) + } + return items +} + +func currentBranch() (string, error) { + items, err := gitdata.Collect() + if err != nil { + return "", err + } + for _, item := range items { + if item.IsCurrent { + return item.Branch, nil + } + } + return "", nil +} + // itemForBranch returns the picker item for branch, or a bare item when the // branch does not exist locally yet. func itemForBranch(branch string) (gitdata.Item, error) { diff --git a/cmd/wt/main_test.go b/cmd/wt/main_test.go index 59fe68e..c79fa06 100644 --- a/cmd/wt/main_test.go +++ b/cmd/wt/main_test.go @@ -8,6 +8,19 @@ import ( "github.com/AgileIndustrialComplex/wt/internal/github" ) +func TestPRItemsMarkCurrentBranch(t *testing.T) { + items := prItems([]github.PR{ + {Number: 17, Branch: "feature/force-update", Title: "A"}, + {Number: 18, Branch: "feature/pr", Title: "B"}, + }, "feature/pr") + if items[0].IsCurrent { + t.Fatalf("row for feature/force-update marked current, want only feature/pr") + } + if !items[1].IsCurrent { + t.Fatalf("row for feature/pr not marked current: %+v", items[1]) + } +} + func TestPRDisplayOrder(t *testing.T) { got := prDisplay(github.PR{Number: 17, Branch: "feature/x", Title: "Add F key"}) if want := "#17 feature/x Add F key"; got != want { diff --git a/internal/ui/ui_test.go b/internal/ui/ui_test.go index 6064dce..b9bc4c5 100644 --- a/internal/ui/ui_test.go +++ b/internal/ui/ui_test.go @@ -720,6 +720,19 @@ func TestPRPickerShowsNumberAndTitle(t *testing.T) { } } +func TestPRPickerShowsCurrentMarker(t *testing.T) { + items := prItems() + items[1].IsCurrent = true + m := NewPR(items, "/repo/proj", "", true, config.ActionPrompt, config.KeymapVim) + line := lineContaining(m.View(), "#18") + if !strings.Contains(line, "●") || !strings.Contains(line, "(current)") { + t.Fatalf("current PR row = %q, want ● and (current)", line) + } + if other := lineContaining(m.View(), "#17"); strings.Contains(other, "(current)") { + t.Fatalf("non-current PR row = %q, want no (current) suffix", other) + } +} + func TestPRPickerFiltersOnTitle(t *testing.T) { m := NewPR(prItems(), "/repo/proj", "", true, config.ActionPrompt, config.KeymapVim) m = send(t, m, key('/'), key('p'), key('r'), key(' '), keyType(tea.KeyEnter))