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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Run `wt` with no arguments to open the picker:
wt
```

Run the picker from inside a Git repository. If you invoke it elsewhere, `wt`
points you to `wt init bash|zsh|fish` to install shell integration and then asks
you to change into a repository.

```
Select branch or worktree (j/k, / to filter, c to mark, ? for help)
> ● main (current) ~/proj
Expand Down
4 changes: 4 additions & 0 deletions cmd/wt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -59,6 +60,9 @@ func run(pathOnly bool, worktreeRootFlag string, noColor bool) error {

items, err := gitdata.Collect()
if err != nil {
if errors.Is(err, gitdata.ErrNotAGitRepo) {
return fmt.Errorf("%w; run `wt init bash|zsh|fish` to install shell integration, then cd into a repository", err)
}
return err
}
top, err := gitdata.Toplevel()
Expand Down
10 changes: 10 additions & 0 deletions internal/gitdata/gitdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ package gitdata

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

// ErrNotAGitRepo is returned by Collect and Toplevel when the current
// directory is outside any Git repository.
var ErrNotAGitRepo = errors.New("not a git repository (or any of the parent directories)")

// Item represents a single row in the picker: a local branch, optionally
// paired with the worktree it is checked out in.
type Item struct {
Expand Down Expand Up @@ -56,6 +62,7 @@ type runnerFunc func(args ...string) (string, error)

func runGit(args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Env = append(os.Environ(), "LC_ALL=C")
var out, errBuf bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errBuf
Expand All @@ -64,6 +71,9 @@ func runGit(args ...string) (string, error) {
if msg == "" {
msg = err.Error()
}
if strings.Contains(msg, "not a git repository") {
return "", ErrNotAGitRepo
}
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), msg)
}
return out.String(), nil
Expand Down
19 changes: 19 additions & 0 deletions internal/gitdata/gitdata_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package gitdata

import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)

func TestRunGitUsesStableLocale(t *testing.T) {
dir := t.TempDir()
git := filepath.Join(dir, "git")
script := "#!/bin/sh\nif [ \"$LC_ALL\" = C ]; then\n echo 'fatal: not a git repository' >&2\nelse\n echo 'fatal: kein Git-Repository' >&2\nfi\nexit 128\n"
if err := os.WriteFile(git, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("LC_ALL", "de_DE.UTF-8")

_, err := runGit("worktree", "list")
if !errors.Is(err, ErrNotAGitRepo) {
t.Fatalf("runGit() error = %v, want ErrNotAGitRepo", err)
}
}

func TestParseWorktreePorcelain(t *testing.T) {
out := strings.Join([]string{
"worktree /repo/proj",
Expand Down
Loading