| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | // Classes of a failed push. |
| 10 | const ( |
| 11 | PushRefused = "refused" // the remote, such as its receive hook, declined the ref |
| 12 | PushAuthentication = "authentication" // the remote did not accept the credentials |
| 13 | PushNetwork = "network" // the remote could not be reached, or the connection broke |
| 14 | PushNotFastForward = "not_fast_forward" // the remote's ref is not an ancestor of the pushed commit |
| 15 | PushUnconfirmed = "unconfirmed" // the remote did not report the ref's status, so it may have updated it |
| 16 | PushOther = "other" |
| 17 | ) |
| 18 | |
| 19 | // MaxPushMessage bounds a PushError's Message, cut marker included, so a |
| 20 | // remote that prints megabytes stays far below the event log reader's line |
| 21 | // limit. |
| 22 | const MaxPushMessage = 4096 |
| 23 | |
| 24 | // PushMessageCut ends a Message cut at MaxPushMessage. |
| 25 | const PushMessageCut = "[truncated]" |
| 26 | |
| 27 | // PushWithheld stands, whole, for a kept remote line, status or reason that |
| 28 | // contains "://" or "@". Such text can carry an address with credentials, and |
| 29 | // no part of it is kept, since cutting an address out reliably needs a URL |
| 30 | // parser. |
| 31 | const PushWithheld = "[withheld: it contains an address]" |
| 32 | |
| 33 | // PushError is a failed push. Message holds the lines the remote sent |
| 34 | // ("remote: ...") on git's stderr, then the pushed ref's status as git push |
| 35 | // --porcelain reports it on stdout ("[remote rejected] (reason)"), without |
| 36 | // control characters and with PushWithheld for any line, status or reason |
| 37 | // holding an address. Every other line of git's output is dropped, because |
| 38 | // git echoes the remote's URL, credentials and all, in its own lines. |
| 39 | type PushError struct { |
| 40 | Class string |
| 41 | Message string |
| 42 | } |
| 43 | |
| 44 | func (e *PushError) Error() string { |
| 45 | msg := "push failed (" + PushClassText(e.Class) + ")" |
| 46 | if e.Message != "" { |
| 47 | msg += ": " + strings.ReplaceAll(e.Message, "\n", "; ") |
| 48 | } |
| 49 | return msg |
| 50 | } |
| 51 | |
| 52 | // PushClassText describes a push failure class in a few words. |
| 53 | func PushClassText(class string) string { |
| 54 | switch class { |
| 55 | case PushRefused: |
| 56 | return "refused by the remote" |
| 57 | case PushAuthentication: |
| 58 | return "authentication" |
| 59 | case PushNetwork: |
| 60 | return "network" |
| 61 | case PushNotFastForward: |
| 62 | return "not a fast-forward" |
| 63 | case PushUnconfirmed: |
| 64 | return "unconfirmed by the remote" |
| 65 | } |
| 66 | return "other" |
| 67 | } |
| 68 | |
| 69 | // ParsePushError describes a failed git push --porcelain of ref from its |
| 70 | // stdout and stderr. |
| 71 | func ParsePushError(ref, stdout, stderr string) *PushError { |
| 72 | var kept []string |
| 73 | for _, line := range pushLines(stderr) { |
| 74 | if strings.HasPrefix(line, "remote:") { |
| 75 | kept = append(kept, keptLine(line)) |
| 76 | } |
| 77 | } |
| 78 | class := "" |
| 79 | for _, line := range strings.Split(stdout, "\n") { |
| 80 | // A porcelain ref line is <flag> TAB <from>:<to> TAB <summary>, and a |
| 81 | // failed ref's flag is "!". Only the line for ref counts: the |
| 82 | // destination git prints before it can hold a newline and so forge a |
| 83 | // line of its own. |
| 84 | flag, rest, _ := strings.Cut(line, "\t") |
| 85 | refs, summary, ok := strings.Cut(rest, "\t") |
| 86 | _, to, _ := strings.Cut(refs, ":") |
| 87 | if flag != "!" || !ok || to != ref { |
| 88 | continue |
| 89 | } |
| 90 | status, reason := splitPushSummary(strings.TrimSpace(strings.Map(dropControl, summary))) |
| 91 | if class == "" { |
| 92 | class = statusClass(status, reason) |
| 93 | } |
| 94 | if reason == "" { |
| 95 | kept = append(kept, keptLine(status)) |
| 96 | } else { |
| 97 | kept = append(kept, keptLine(status, "("+reason+")")) |
| 98 | } |
| 99 | } |
| 100 | if class == "" { |
| 101 | class = connectionClass(stderr) |
| 102 | } |
| 103 | return &PushError{Class: class, Message: boundPushMessage(strings.Join(kept, "\n"))} |
| 104 | } |
| 105 | |
| 106 | // pushLines splits git's stderr into lines without control characters. |
| 107 | func pushLines(s string) []string { |
| 108 | var lines []string |
| 109 | for _, raw := range strings.FieldsFunc(s, func(r rune) bool { return r == '\n' || r == '\r' }) { |
| 110 | lines = append(lines, strings.TrimSpace(strings.Map(dropControl, raw))) |
| 111 | } |
| 112 | return lines |
| 113 | } |
| 114 | |
| 115 | // splitPushSummary splits a porcelain summary such as "[remote rejected] |
| 116 | // (pre-receive hook declined)" into its status and the reason in parentheses. |
| 117 | func splitPushSummary(summary string) (status, reason string) { |
| 118 | end := strings.Index(summary, "]") |
| 119 | if !strings.HasPrefix(summary, "[") || end < 0 { |
| 120 | return summary, "" |
| 121 | } |
| 122 | reason = strings.TrimSpace(summary[end+1:]) |
| 123 | reason = strings.TrimSuffix(strings.TrimPrefix(reason, "("), ")") |
| 124 | return summary[:end+1], reason |
| 125 | } |
| 126 | |
| 127 | // statusClass classes a failed ref from its porcelain status and reason. |
| 128 | func statusClass(status, reason string) string { |
| 129 | switch { |
| 130 | case status == "[rejected]" && (reason == "non-fast-forward" || reason == "fetch first"): |
| 131 | return PushNotFastForward |
| 132 | case status == "[remote failure]": |
| 133 | return PushUnconfirmed |
| 134 | } |
| 135 | return PushRefused |
| 136 | } |
| 137 | |
| 138 | // connectionClass classes a push that failed before any ref's status, from |
| 139 | // what git and the remote said about the connection. |
| 140 | func connectionClass(stderr string) string { |
| 141 | lower := strings.ToLower(stderr) |
| 142 | for _, s := range []string{"authentication failed", "permission denied", "could not read username", |
| 143 | "could not read password", "terminal prompts disabled", "invalid username or password", |
| 144 | "returned error: 401", "returned error: 403"} { |
| 145 | if strings.Contains(lower, s) { |
| 146 | return PushAuthentication |
| 147 | } |
| 148 | } |
| 149 | for _, s := range []string{"could not resolve host", "connection refused", "connection timed out", |
| 150 | "connection reset", "network is unreachable", "no route to host", "failed to connect", |
| 151 | "the remote end hung up", "could not read from remote repository", "unable to access", "early eof"} { |
| 152 | if strings.Contains(lower, s) { |
| 153 | return PushNetwork |
| 154 | } |
| 155 | } |
| 156 | return PushOther |
| 157 | } |
| 158 | |
| 159 | // keptLine joins the fields of a line Message keeps, each replaced whole by |
| 160 | // PushWithheld when it contains an address. Every kept line is built here. |
| 161 | func keptLine(fields ...string) string { |
| 162 | for i, f := range fields { |
| 163 | if strings.Contains(f, "://") || strings.Contains(f, "@") { |
| 164 | fields[i] = PushWithheld |
| 165 | } |
| 166 | } |
| 167 | return strings.Join(fields, " ") |
| 168 | } |
| 169 | |
| 170 | func dropControl(r rune) rune { |
| 171 | if r == '\t' { |
| 172 | return ' ' |
| 173 | } |
| 174 | if unicode.IsControl(r) { |
| 175 | return -1 |
| 176 | } |
| 177 | return r |
| 178 | } |
| 179 | |
| 180 | func boundPushMessage(s string) string { |
| 181 | if len(s) <= MaxPushMessage { |
| 182 | return s |
| 183 | } |
| 184 | cut := MaxPushMessage - len(PushMessageCut) - 1 |
| 185 | for cut > 0 && !utf8.RuneStart(s[cut]) { |
| 186 | cut-- |
| 187 | } |
| 188 | return s[:cut] + "\n" + PushMessageCut |
| 189 | } |