Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<branch>`, 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/<branch>`, so
this covers branches pushed to the repository's `origin`, not fork PR heads.

Optional config file at `~/.config/wt/config.toml`:
Expand Down
4 changes: 3 additions & 1 deletion SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 32 additions & 6 deletions cmd/wt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions cmd/wt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions internal/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading