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
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/mattn/go-isatty"
)
Expand Down Expand Up @@ -1443,7 +1444,8 @@ func findMergedPRsWithGh(cfg runConfig, remote Remote, base string, heads []stri
return nil, fmt.Errorf("gh api graphql: timed out after %s", ghTimeout)
}
if err != nil {
return nil, fmt.Errorf("gh api graphql: %w", err)
// gh's stderr quotes the server's error body.
return nil, fmt.Errorf("gh api graphql: %s", sanitizeForTerminal(err.Error()))
}
return parseMergedPRResponse(output, heads)
}
Expand Down Expand Up @@ -1506,7 +1508,7 @@ func parseMergedPRResponse(output []byte, heads []string) (map[string][]mergedPR
return nil, fmt.Errorf("unexpected gh api graphql output: %w", err)
}
if len(resp.Errors) > 0 {
return nil, fmt.Errorf("GraphQL error: %s", resp.Errors[0].Message)
return nil, fmt.Errorf("GraphQL error: %s", sanitizeForTerminal(resp.Errors[0].Message))
}
if resp.Data.Repository == nil {
return nil, errors.New("repository not found")
Expand All @@ -1530,6 +1532,26 @@ func parseMergedPRResponse(output []byte, heads []string) (map[string][]mergedPR
return prs, nil
}

// sanitizeForTerminal makes text that came from a server safe to print: control
// characters (C0 including ESC, DEL and C1) are shown as Go escapes such as
// \x1b, so a response can't move the cursor, recolor or erase what the terminal
// shows. Line breaks and tabs are kept, and so is every other character, so an
// ordinary message reads as it was sent. Bytes that aren't valid UTF-8 become
// U+FFFD.
func sanitizeForTerminal(s string) string {
s = strings.ReplaceAll(s, "\r\n", "\n")
var b strings.Builder
for _, r := range s {
if unicode.IsControl(r) && r != '\n' && r != '\t' {
quoted := strconv.QuoteRune(r)
b.WriteString(quoted[1 : len(quoted)-1])
continue
}
b.WriteRune(r)
}
return b.String()
}

// gitHubRepo identifies a repository on a GitHub host (github.com or GitHub
// Enterprise Server).
type gitHubRepo struct {
Expand Down
42 changes: 42 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"slices"
"strings"
"testing"
"unicode"
)

// TestMain isolates every git invocation in this package (including the ones
Expand Down Expand Up @@ -966,6 +967,47 @@ func TestParseMergedPRResponse(t *testing.T) {
t.Errorf("%s: want error", name)
}
}

// A message from the server can't carry escape sequences to the terminal.
_, err = parseMergedPRResponse([]byte(`{"errors":[{"message":"\u001b[2K\u001b[1Aall good\u009b31m\u0007"}]}`), heads)
if err == nil {
t.Fatal("GraphQL error with escape sequences: want error")
}
if want := `GraphQL error: \x1b[2K\x1b[1Aall good\u009b31m\a`; err.Error() != want {
t.Errorf("parseMergedPRResponse() error = %q, want %q", err.Error(), want)
}
if i := strings.IndexFunc(err.Error(), unicode.IsControl); i >= 0 {
t.Errorf("parseMergedPRResponse() error keeps a control character at %d: %q", i, err.Error())
}
}

func TestSanitizeForTerminal(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{"plain", "Something went wrong", "Something went wrong"},
{"non-ASCII", "リポジトリが見つかりません — café ✓", "リポジトリが見つかりません — café ✓"},
{"quotes and backslashes", `Could not resolve "a\b"`, `Could not resolve "a\b"`},
{"lines and tabs", "first\n\tsecond\r\nthird", "first\n\tsecond\nthird"},
{"ESC sequence", "\x1b[31mred\x1b[0m", `\x1b[31mred\x1b[0m`},
{"OSC title", "\x1b]0;title\a", `\x1b]0;title\a`},
{"carriage return", "real\rfake", `real\rfake`},
{"backspace and NUL", "a\b\x00", `a\b\x00`},
{"DEL", "a\x7f", `a\x7f`},
{"C1 CSI", "\u009b31m", `\u009b31m`},
{"raw C1 byte", "\x9b31m", "\ufffd31m"},
}
for _, tt := range tests {
got := sanitizeForTerminal(tt.in)
if got != tt.want {
t.Errorf("%s: sanitizeForTerminal(%q) = %q, want %q", tt.name, tt.in, got, tt.want)
}
if i := strings.IndexFunc(strings.NewReplacer("\n", "", "\t", "").Replace(got), unicode.IsControl); i >= 0 {
t.Errorf("%s: sanitizeForTerminal(%q) keeps a control character: %q", tt.name, tt.in, got)
}
}
}

func TestSSHResolvedHost(t *testing.T) {
Expand Down
Loading