gunk-dev/the-valley

main.go

source28abc5737165main's published tip history
4.7 KiB
1// Command pushreason makes what git printed for a failed push into the one
2// line a valley host logs as the reason. It reads git's error output on
3// stdin and writes the line on stdout.
4//
5// The text is the remote's to choose, and the line lands in the host's
6// journal and in telemetry, so it is made fit to log there:
7//
8// - invalid UTF-8 is replaced, a tab becomes a space, and other control
9// characters are deleted, since they would rewrite an operator's
10// terminal and make the journal store the line as binary;
11// - a line longer than lineLimit is replaced whole, without being held
12// whole, since a cut could leave part of a credential without the mark
13// that would drop it;
14// - git's own framing is dropped, which leaves the remote's refusal (its
15// remote: lines and the rejected ref) or what stopped the connection;
16// - any word holding one of the marks, the signs of an address or a
17// parameter, is replaced whole, so no part of a credential such a
18// word carries is kept. Nothing tells a reference's shape apart from
19// another's: every shape git, ssh or a remote can print holds a mark
20// where it holds a credential. A word ends only at a space, since any
21// other whitespace, such as a no-break space, can sit inside a
22// password;
23// - the last keptLines lines are kept, joined, and cut to their last
24// reasonLimit bytes, starting on a whole character, where a long
25// refusal ends with its conclusion.
26//
27// A secret the remote prints as a plain word passes through: the line is
28// for diagnosis, and whoever reads the journal reads it.
29package main
30
31import (
32 "bufio"
33 "fmt"
34 "io"
35 "os"
36 "strings"
37 "unicode"
38 "unicode/utf8"
39)
40
41const (
42 // lineLimit is the most of one line kept, in bytes.
43 lineLimit = 1024
44 // keptLines is how many of the last lines make the reason.
45 keptLines = 5
46 // reasonLimit is the most of the reason kept, in bytes.
47 reasonLimit = 600
48
49 tooLong = "(a line too long to keep)"
50 separator = " · "
51 dropped = "[…]"
52)
53
54// marks are what make a word an address or a parameter: a scheme's ://,
55// a user's @, and a query's ?, = and the % of an encoded value.
56var marks = []string{"://", "@", "?", "=", "%"}
57
58func main() {
59 line, err := reason(os.Stdin)
60 if err != nil {
61 fmt.Fprintf(os.Stderr, "pushreason: reading git's output: %v\n", err)
62 os.Exit(1)
63 }
64 fmt.Println(line)
65}
66
67// reason reads r to its end and returns the reason line.
68func reason(r io.Reader) (string, error) {
69 br := bufio.NewReader(r)
70 var kept []string
71 for {
72 raw, cut, err := readLine(br)
73 if len(raw) > 0 || cut {
74 if line := clean(raw, cut); line != "" {
75 kept = append(kept, line)
76 if len(kept) > keptLines {
77 kept = kept[1:]
78 }
79 }
80 }
81 if err == io.EOF {
82 break
83 }
84 if err != nil {
85 return "", err
86 }
87 }
88 return tail(strings.Join(kept, separator)), nil
89}
90
91// readLine returns the next line of r without its newline. It holds at
92// most lineLimit bytes of it, and cut reports a line longer than that.
93func readLine(r *bufio.Reader) (line []byte, cut bool, err error) {
94 for {
95 chunk, err := r.ReadSlice('\n')
96 chunk = trimNewline(chunk)
97 if !cut && len(line)+len(chunk) <= lineLimit {
98 line = append(line, chunk...)
99 } else {
100 cut, line = true, nil
101 }
102 if err != bufio.ErrBufferFull {
103 return line, cut, err
104 }
105 }
106}
107
108func trimNewline(b []byte) []byte {
109 if n := len(b); n > 0 && b[n-1] == '\n' {
110 return b[:n-1]
111 }
112 return b
113}
114
115// clean makes one line fit to log, or returns "" for a line that says
116// nothing of the remote's.
117func clean(raw []byte, cut bool) string {
118 if cut {
119 return tooLong
120 }
121 text := strings.Map(func(r rune) rune {
122 switch {
123 case r == '\t':
124 return ' '
125 case unicode.IsControl(r):
126 return -1
127 }
128 return r
129 }, strings.ToValidUTF8(string(raw), "\uFFFD"))
130 var words []string
131 for _, w := range strings.Split(text, " ") {
132 if w != "" {
133 words = append(words, w)
134 }
135 }
136 line := strings.Join(words, " ")
137 if framing(line) {
138 return ""
139 }
140 for i, w := range words {
141 for _, m := range marks {
142 if strings.Contains(w, m) {
143 words[i] = dropped
144 break
145 }
146 }
147 }
148 return strings.Join(words, " ")
149}
150
151// framing reports a line git prints around the remote's words: the
152// destination, its hints, and its closing error. A blank line too.
153func framing(line string) bool {
154 return line == "" ||
155 strings.HasPrefix(line, "To ") ||
156 strings.HasPrefix(line, "hint:") ||
157 strings.HasPrefix(line, "error: failed to push")
158}
159
160// tail keeps the last reasonLimit bytes of s, from the start of a whole
161// character.
162func tail(s string) string {
163 if len(s) <= reasonLimit {
164 return s
165 }
166 s = s[len(s)-reasonLimit:]
167 for len(s) > 0 && !utf8.RuneStart(s[0]) {
168 s = s[1:]
169 }
170 return strings.TrimLeft(s, " ")
171}