| 400 | } |
| 401 | |
| 402 | func cleanSubject(s string, mode SubjectCleanMode) (prefix string, subject string) { |
| 403 | switch mode { |
| 404 | case SubjectCleanAll, SubjectCleanPatchOnly: |
| 405 | case SubjectCleanWhitespace: |
| 406 | return "", strings.TrimSpace(decodeSubject(s)) |
| 407 | default: |
| 408 | panic(fmt.Sprintf("unknown clean mode: %d", mode)) |
| 409 | } |
| 410 | |
| 411 | // Based on the algorithm from Git in mailinfo.c:cleanup_subject() |
| 412 | // If compatibility with `git am` drifts, go there to see if there are any updates. |
| 413 | |
| 414 | at := 0 |
| 415 | for at < len(s) { |
| 416 | switch s[at] { |
| 417 | case 'r', 'R': |
| 418 | // Detect re:, Re:, rE: and RE: |
| 419 | if at+2 < len(s) && (s[at+1] == 'e' || s[at+1] == 'E') && s[at+2] == ':' { |
| 420 | at += 3 |
| 421 | continue |
| 422 | } |
| 423 | |
| 424 | case ' ', '\t', ':': |
| 425 | // Delete whitespace and duplicate ':' characters |
| 426 | at++ |
| 427 | continue |
| 428 | |
| 429 | case '[': |
| 430 | if i := strings.IndexByte(s[at:], ']'); i > 0 { |
| 431 | if mode == SubjectCleanAll || strings.Contains(s[at:at+i+1], "PATCH") { |
| 432 | at += i + 1 |
| 433 | continue |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // Nothing was removed, end processing |
| 439 | break |
| 440 | } |
| 441 | |
| 442 | prefix = strings.TrimLeftFunc(s[:at], unicode.IsSpace) |
| 443 | subject = strings.TrimRightFunc(decodeSubject(s[at:]), unicode.IsSpace) |
| 444 | return |
| 445 | } |
| 446 | |
| 447 | // Decodes a subject line. Currently only supports quoted-printable UTF-8. This format is the result |
| 448 | // of a `git format-patch` when the commit title has a non-ASCII character (i.e. an emoji). |