Skip to content

Commit 88b5a3b

Browse files
author
Nate Smith
authored
Merge pull request cli#1534 from mmontes11/pr-diff-use-default-pager
Using default pager in gh pr diff
2 parents e441ea6 + df66a8f commit 88b5a3b

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

pkg/cmd/pr/diff/diff.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"os"
9+
"os/exec"
810
"strings"
911

1012
"github.com/cli/cli/api"
@@ -13,6 +15,7 @@ import (
1315
"github.com/cli/cli/pkg/cmd/pr/shared"
1416
"github.com/cli/cli/pkg/cmdutil"
1517
"github.com/cli/cli/pkg/iostreams"
18+
"github.com/google/shlex"
1619
"github.com/spf13/cobra"
1720
)
1821

@@ -90,6 +93,12 @@ func diffRun(opts *DiffOptions) error {
9093
return err
9194
}
9295

96+
if opts.IO.IsStdoutTTY() {
97+
if pager := os.Getenv("PAGER"); pager != "" {
98+
return runPager(pager, diff, opts.IO.Out)
99+
}
100+
}
101+
93102
diffLines := bufio.NewScanner(diff)
94103
for diffLines.Scan() {
95104
diffLine := diffLines.Text()
@@ -134,3 +143,14 @@ func isRemovalLine(dl string) bool {
134143
func validColorFlag(c string) bool {
135144
return c == "auto" || c == "always" || c == "never"
136145
}
146+
147+
var runPager = func(pager string, diff io.Reader, out io.Writer) error {
148+
args, err := shlex.Split(pager)
149+
if err != nil {
150+
return err
151+
}
152+
pagerCmd := exec.Command(args[0], args[1:]...)
153+
pagerCmd.Stdin = diff
154+
pagerCmd.Stdout = out
155+
return pagerCmd.Run()
156+
}

pkg/cmd/pr/diff/diff_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package diff
22

33
import (
44
"bytes"
5+
"io"
56
"io/ioutil"
67
"net/http"
8+
"os"
79
"testing"
810

911
"github.com/cli/cli/context"
@@ -131,8 +133,13 @@ func TestPRDiff_notty(t *testing.T) {
131133
}
132134

133135
func TestPRDiff_tty(t *testing.T) {
136+
pager := os.Getenv("PAGER")
134137
http := &httpmock.Registry{}
135-
defer http.Verify(t)
138+
defer func() {
139+
os.Setenv("PAGER", pager)
140+
http.Verify(t)
141+
}()
142+
os.Setenv("PAGER", "")
136143
http.StubResponse(200, bytes.NewBufferString(`
137144
{ "data": { "repository": { "pullRequests": { "nodes": [
138145
{ "url": "https://github.com/OWNER/REPO/pull/123",
@@ -149,6 +156,38 @@ func TestPRDiff_tty(t *testing.T) {
149156
assert.Contains(t, output.String(), "\x1b[32m+site: bin/gh\x1b[m")
150157
}
151158

159+
func TestPRDiff_pager(t *testing.T) {
160+
realRunPager := runPager
161+
pager := os.Getenv("PAGER")
162+
http := &httpmock.Registry{}
163+
defer func() {
164+
runPager = realRunPager
165+
os.Setenv("PAGER", pager)
166+
http.Verify(t)
167+
}()
168+
runPager = func(pager string, diff io.Reader, out io.Writer) error {
169+
_, err := io.Copy(out, diff)
170+
return err
171+
}
172+
os.Setenv("PAGER", "fakepager")
173+
http.StubResponse(200, bytes.NewBufferString(`
174+
{ "data": { "repository": { "pullRequests": { "nodes": [
175+
{ "url": "https://github.com/OWNER/REPO/pull/123",
176+
"number": 123,
177+
"id": "foobar123",
178+
"headRefName": "feature",
179+
"baseRefName": "master" }
180+
] } } } }`))
181+
http.StubResponse(200, bytes.NewBufferString(testDiff))
182+
output, err := runCommand(http, nil, true, "")
183+
if err != nil {
184+
t.Fatalf("unexpected error: %s", err)
185+
}
186+
if diff := cmp.Diff(testDiff, output.String()); diff != "" {
187+
t.Errorf("command output did not match:\n%s", diff)
188+
}
189+
}
190+
152191
const testDiff = `diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml
153192
index 73974448..b7fc0154 100644
154193
--- a/.github/workflows/releases.yml

0 commit comments

Comments
 (0)