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
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ git diff → diff.ParseUnifiedDiff() → []DiffLine
- Highlighted lines are pre-computed once per file load, stored parallel to `diffLines`
- `DiffLine.Content` has no `+`/`-` prefix - prefix is re-added at render time
- Tab replacement happens at render time in `renderDiffLine`, not in diff parsing
- `run()` resolves git repo root via `git rev-parse --show-toplevel` so revdiff works from any subdirectory
22 changes: 21 additions & 1 deletion cmd/revdiff/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -161,7 +163,11 @@ func defaultConfigPath() string {
}

func run(opts options) error {
renderer := diff.NewGit(".")
repoRoot, err := gitTopLevel()
if err != nil {
return fmt.Errorf("find git root: %w", err)
}
renderer := diff.NewGit(repoRoot)
store := annotation.NewStore()
hl := highlight.New(opts.ChromaStyle, !opts.NoColors)
model := ui.NewModel(renderer, store, hl, ui.ModelConfig{
Expand Down Expand Up @@ -216,3 +222,17 @@ func run(opts options) error {
fmt.Print(output)
return nil
}

// gitTopLevel returns the root directory of the current git repository.
func gitTopLevel() (string, error) {
cmd := exec.CommandContext(context.Background(), "git", "rev-parse", "--show-toplevel")
out, err := cmd.Output()
if err != nil {
Comment on lines +229 to +230

Copilot AI Apr 1, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gitTopLevel wraps cmd.Output() errors directly, which typically loses the useful git error text (e.g. you’ll end up with just exit status 128). Consider mirroring diff.Git.runGit’s handling by detecting *exec.ExitError and including exitErr.Stderr in the returned error message (or using CombinedOutput and formatting stderr on failure).

Suggested change
out, err := cmd.Output()
if err != nil {
out, err := cmd.CombinedOutput()
if err != nil {
msg := strings.TrimSpace(string(out))
if msg != "" {
return "", fmt.Errorf("git rev-parse --show-toplevel: %w: %s", err, msg)
}

Copilot uses AI. Check for mistakes.
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return "", fmt.Errorf("git rev-parse --show-toplevel: %s", strings.TrimSpace(string(exitErr.Stderr)))
}
return "", fmt.Errorf("git rev-parse --show-toplevel: %w", err)
}
return strings.TrimSpace(string(out)), nil
}
16 changes: 16 additions & 0 deletions cmd/revdiff/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,19 @@ func TestDefaultConfigPath(t *testing.T) {
assert.Contains(t, path, "revdiff")
assert.Contains(t, path, "config")
}

func TestGitTopLevel(t *testing.T) {
t.Run("inside repo", func(t *testing.T) {
root, err := gitTopLevel()
require.NoError(t, err)
assert.DirExists(t, root)
assert.NotEmpty(t, root)
})

t.Run("outside repo", func(t *testing.T) {
t.Chdir(t.TempDir())
_, err := gitTopLevel()
require.Error(t, err)
assert.Contains(t, err.Error(), "git rev-parse --show-toplevel")
})
}
Loading