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
13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ permissions:
contents: read

jobs:
# Rex runs only on macOS, so rexdiff is tested there.
# Rex runs only on macOS, so the Rex integration is tested there. The
# viewer also runs on Linux, without Rex.
test:
runs-on: macos-latest
steps:
Expand All @@ -20,6 +21,16 @@ jobs:
- run: go vet ./...
- run: go test -race -count=1 ./...

test-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go vet ./...
- run: go test -race -count=1 ./...

lint:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ builds:
- -trimpath
ldflags:
- -s -w -X main.version={{ .Version }}
goos: [darwin]
goos: [darwin, linux]
goarch: [arm64, amd64]

# The name has no version, so that the link of the latest release works in
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<img src="assets/logo.png" alt="rexdiff" width="360" align="right">

A diff viewer for coding agents in [Rex]. Think [herdr-reviewr], but for Rex.
It runs on macOS and Linux.


It opens next to the agent and shows the changes. You comment on lines and send
Expand All @@ -24,9 +25,9 @@ With curl:
curl -fsSL https://raw.githubusercontent.com/frinfo702/rexdiff/main/install.sh | sh
```

The script downloads the release for this Mac, checks its checksum, and puts
the binary in `~/.local/bin`, or in `REXDIFF_BIN_DIR`. It also says how to add
the directory to `PATH` if it is not there.
The script downloads the release for this system, checks its checksum, and
puts the binary in `~/.local/bin`, or in `REXDIFF_BIN_DIR`. It also says how
to add the directory to `PATH` if it is not there.

With Go:

Expand All @@ -41,7 +42,8 @@ rexdiff install
```

This adds the "Toggle rexdiff" action and binds it to Shift+Cmd+R. To remove
them, run `rexdiff uninstall`.
them, run `rexdiff uninstall`. The install command needs the `rex` command;
it says so when Rex is not installed. The viewer itself runs without Rex.

## Use

Expand Down Expand Up @@ -128,7 +130,8 @@ After a change:

## Requirements

- Rex
- Rex for the action in Rex. Rex runs on macOS; the viewer also runs on its
own, on macOS and Linux.
- git
- `gh`, `glab`, or `az` for the pull request tab. You must be logged in.

Expand Down
6 changes: 6 additions & 0 deletions cmd/rexdiff/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ func install(
"in the settings in %s)", fs.Arg(0), luaPath())
}

// The action runs in Rex, and install reloads its configuration. Without
// the rex command, there is nothing to configure.
if !rex.Available() {
return errors.New("rex is not installed: install Rex, or put the rex command in PATH")
}

// Make the new content first. Then a bad rexdiff.lua stops the install
// before it changes a file.
lua := luaPath()
Expand Down
18 changes: 18 additions & 0 deletions cmd/rexdiff/install_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -20,6 +21,23 @@ func fakeRexDir(t *testing.T, log string) string {
return dir
}

// TestInstallWithoutRex checks that install stops before it writes a file
// when rex is not installed.
func TestInstallWithoutRex(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", "")
t.Setenv("REX_CONFIG", "")
t.Setenv("REX_BIN_DIR", "")
t.Setenv("PATH", t.TempDir())
err := install(t.Context(), "/opt/rexdiff", nil, io.Discard, io.Discard)
if err == nil || !strings.Contains(err.Error(), "rex is not installed") {
t.Errorf("install without rex = %v, want the rex error", err)
}
if _, err := os.Stat(filepath.Dir(luaPath())); !os.IsNotExist(err) {
t.Errorf("install without rex made %s", filepath.Dir(luaPath()))
}
}

// TestUpgrade checks that upgrade replaces the code of rexdiff.lua if it
// differs from the code of the binary, and keeps the settings.
func TestUpgrade(t *testing.T) {
Expand Down
26 changes: 19 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
# install.sh installs the latest rexdiff release for this Mac:
# install.sh installs the latest rexdiff release for this Mac or Linux:
#
# curl -fsSL https://raw.githubusercontent.com/frinfo702/rexdiff/main/install.sh | sh
#
Expand All @@ -17,15 +17,19 @@ fail() {
exit 1
}

[ "$(uname -s)" = Darwin ] || fail "rexdiff runs on macOS only"
case $(uname -s) in
Darwin) os=darwin ;;
Linux) os=linux ;;
*) fail "unsupported system: $(uname -s)" ;;
esac

case $(uname -m) in
arm64) arch=arm64 ;;
x86_64) arch=amd64 ;;
arm64 | aarch64) arch=arm64 ;;
x86_64 | amd64) arch=amd64 ;;
*) fail "unsupported machine: $(uname -m)" ;;
esac

name="rexdiff-darwin-$arch.tar.gz"
name="rexdiff-$os-$arch.tar.gz"
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT HUP INT TERM

Expand All @@ -34,9 +38,17 @@ curl -fsSL "$base/$name" -o "$tmp/$name" ||
curl -fsSL "$base/sha256sums.txt" -o "$tmp/sha256sums.txt" ||
fail "cannot download $base/sha256sums.txt"

if command -v sha256sum >/dev/null 2>&1; then
got=$(sha256sum "$tmp/$name")
elif command -v shasum >/dev/null 2>&1; then
got=$(shasum -a 256 "$tmp/$name")
else
fail "need sha256sum or shasum to check the checksum"
fi
got=${got%% *}

want=$(awk -v name="$name" '$2 == name { print $1 }' "$tmp/sha256sums.txt")
[ -n "$want" ] || fail "the checksums have no $name"
got=$(shasum -a 256 "$tmp/$name" | awk '{ print $1 }')
[ "$want" = "$got" ] || fail "$name does not match its checksum"

tar -xzf "$tmp/$name" -C "$tmp" rexdiff || fail "cannot unpack $name"
Expand All @@ -48,7 +60,7 @@ printf 'installed %s\n' "$dir/rexdiff"
case ":$PATH:" in
*":$dir:"*) ;;
*)
printf 'add %s to PATH, for example in ~/.zshrc:\n' "$dir"
printf 'add %s to PATH, for example:\n' "$dir"
printf ' export PATH="%s:$PATH"\n' "$dir"
;;
esac
28 changes: 25 additions & 3 deletions rex/rex.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,38 @@ func binFromEnv() string {
if _, err := exec.LookPath("rex"); err == nil {
return ""
}
bin, _ := appBin()
return bin
}

// appBin returns the path of the rex command of the Rex server app on
// macOS.
func appBin() (string, error) {
dir, err := os.UserConfigDir()
if err != nil {
return ""
return "", err
}
bin := filepath.Join(dir, "com.superlogical.rex", "RexServer", "Versions",
"Current", "Rex Server.app", "Contents", "MacOS", "rex")
if _, err := os.Stat(bin); err != nil {
return ""
return "", err
}
return bin
return bin, nil
}

// Available reports whether the rex command exists: REX_BIN_DIR/rex, rex
// in PATH, or the rex of the Rex server app. On Linux, rex comes from
// REX_BIN_DIR or PATH.
func Available() bool {
if dir := os.Getenv("REX_BIN_DIR"); dir != "" {
_, err := exec.LookPath(filepath.Join(dir, "rex"))
return err == nil
}
if _, err := exec.LookPath("rex"); err == nil {
return true
}
_, err := appBin()
return err == nil
}

// command returns the command "rex args...".
Expand Down
37 changes: 37 additions & 0 deletions rex/rex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ func TestFromEnv(t *testing.T) {
}
}

// TestAvailable checks that Available finds rex in PATH and in REX_BIN_DIR,
// and that it is false without rex.
func TestAvailable(t *testing.T) {
// No rex in PATH and no Rex app in HOME.
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", "")
t.Setenv("REX_BIN_DIR", "")
t.Setenv("PATH", t.TempDir())
if rex.Available() {
t.Error("Available without rex = true")
}

// rex in PATH, as go install leaves it.
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "rex"), []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin)
if !rex.Available() {
t.Error("Available with rex in PATH = false")
}

// rex in REX_BIN_DIR, which Rex sets in its terminals.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "rex"), []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", t.TempDir())
t.Setenv("REX_BIN_DIR", dir)
if !rex.Available() {
t.Error("Available with rex in REX_BIN_DIR = false")
}
}

// TestReloadConfigOutsideRex checks that ReloadConfig runs the rex of the
// Rex server app outside a Rex terminal. There, for example under brew,
// REX_BIN_DIR is not set and rex is not in PATH.
Expand All @@ -236,6 +270,9 @@ func TestReloadConfigOutsideRex(t *testing.T) {
if err := os.WriteFile(filepath.Join(dir, "rex"), []byte(script), 0o755); err != nil {
t.Fatal(err)
}
if !rex.Available() {
t.Error("Available with the rex of the app = false")
}
out, err := rex.ReloadConfig(t.Context())
if err != nil || out != "reloaded: config reload\n" {
t.Errorf("ReloadConfig = %q, %v, want the report of the app rex", out, err)
Expand Down
20 changes: 11 additions & 9 deletions vcs/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,18 @@ func TestPullRequestErrors(t *testing.T) {
}
}

// fakePath sets PATH to dir and the system directories, and links git into
// dir. Then PATH has the fake forge tools and no real ones.
// fakePath sets PATH to dir, which has git and cat, so that PATH has the
// fake forge tools and no real ones.
func fakePath(t *testing.T, dir string) {
t.Helper()
p, err := exec.LookPath("git")
if err != nil {
t.Fatal(err)
}
if err := os.Symlink(p, filepath.Join(dir, "git")); err != nil {
t.Fatal(err)
for _, name := range []string{"cat", "git"} {
p, err := exec.LookPath(name)
if err != nil {
t.Fatal(err)
}
if err := os.Symlink(p, filepath.Join(dir, name)); err != nil {
t.Fatal(err)
}
}
t.Setenv("PATH", dir+":/bin:/usr/bin")
t.Setenv("PATH", dir)
}
Loading