diff --git a/README.md b/README.md index ce64b33..21d5db0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/wt/main.go b/cmd/wt/main.go index de8eb4d..b98213e 100644 --- a/cmd/wt/main.go +++ b/cmd/wt/main.go @@ -3,6 +3,7 @@ package main import ( + "errors" "flag" "fmt" "os" @@ -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() diff --git a/internal/gitdata/gitdata.go b/internal/gitdata/gitdata.go index 8b596a8..2588726 100644 --- a/internal/gitdata/gitdata.go +++ b/internal/gitdata/gitdata.go @@ -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 { @@ -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 @@ -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 diff --git a/internal/gitdata/gitdata_test.go b/internal/gitdata/gitdata_test.go index 1f230a1..3e233cb 100644 --- a/internal/gitdata/gitdata_test.go +++ b/internal/gitdata/gitdata_test.go @@ -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",