MCPcopy Create free account
hub / github.com/antonmedv/gitmal / decodeSubject

Function decodeSubject

pkg/gitdiff/patch_header.go:450–470  ·  view source on GitHub ↗

Decodes a subject line. Currently only supports quoted-printable UTF-8. This format is the result of a `git format-patch` when the commit title has a non-ASCII character (i.e. an emoji). See for reference: https://stackoverflow.com/questions/27695749/gmail-api-not-respecting-utf-encoding-in-subject

(encoded string)

Source from the content-addressed store, hash-verified

448// of a `git format-patch` when the commit title has a non-ASCII character (i.e. an emoji).
449// See for reference: https://stackoverflow.com/questions/27695749/gmail-api-not-respecting-utf-encoding-in-subject
450func decodeSubject(encoded string) string {
451 if !strings.HasPrefix(encoded, "=?UTF-8?q?") {
452 // not UTF-8 encoded
453 return encoded
454 }
455
456 // If the subject is too long, `git format-patch` may produce a subject line across
457 // multiple lines. When parsed, this can look like the following:
458 // <UTF8-prefix><first-line> <UTF8-prefix><second-line>
459 payload := " " + encoded
460 payload = strings.ReplaceAll(payload, " =?UTF-8?q?", "")
461 payload = strings.ReplaceAll(payload, "?=", "")
462
463 decoded, err := ioutil.ReadAll(quotedprintable.NewReader(strings.NewReader(payload)))
464 if err != nil {
465 // if err, abort decoding and return original subject
466 return encoded
467 }
468
469 return string(decoded)
470}

Callers 1

cleanSubjectFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected