Skip to content

Add emoji support in email subjects #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added multiline support
  • Loading branch information
jmcampanini committed Jul 19, 2021
commit 8ab13c82b8373b71c960dadb183c48971006d1e4
25 changes: 17 additions & 8 deletions gitdiff/patch_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,25 +467,34 @@ func decodeUTF8Subject(encoded string) string {
return encoded
}

payload := strings.TrimPrefix(encoded, "=?UTF-8?q?")
payload = strings.TrimSuffix(payload, "?=")
// If the subject is too long, `git format-patch` maty produce a subject line across
// multiple lines. This will lead to the next line starting with <space><UTF-prefix>
// Adding the space to the beginning of `encoded` ensures the replace catches all
// instances, including the first line.
payload := strings.ReplaceAll(" "+encoded, " =?UTF-8?q?", "")
payload = strings.ReplaceAll(payload, "?=", "")

at := 0
subject := ""
var subject []byte

for at < len(payload) {
if payload[at] == '=' {
// detected a hex value
hexx := payload[at+1 : at+3]
hexbytes, _ := hex.DecodeString(hexx)
subject += string(hexbytes)
hexString := payload[at+1 : at+3]
hexByte, err := hex.DecodeString(hexString)
if err != nil {
// if err, abort decoding and return original subject
return encoded
}

subject = append(subject, hexByte...)
at += 3

} else {
subject += string(payload[at])
subject = append(subject, payload[at])
at++
}
}

return subject
return string(subject)
}
21 changes: 21 additions & 0 deletions gitdiff/patch_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func TestParsePatchHeader(t *testing.T) {
expectedDate := time.Date(2020, 04, 11, 15, 21, 23, 0, time.FixedZone("PDT", -7*60*60))
expectedTitle := "A sample commit to test header parsing"
expectedEmojiOneLineTitle := "🤖 Enabling auto-merging"
expectedEmojiMultiLineTitle := "🤖 Enabling auto-merging of certain PRs"
expectedBody := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
expectedBodyAppendix := "CC: Joe Smith <joe.smith@company.com>"

Expand Down Expand Up @@ -287,6 +288,26 @@ Another body line.
Body: expectedBody,
},
},
"mailboxEmojiMultiLine": {
Input: `From 61f5cd90bed4d204ee3feb3aa41ee91d4734855b Mon Sep 17 00:00:00 2001
From: Morton Haypenny <mhaypenny@example.com>
Date: Sat, 11 Apr 2020 15:21:23 -0700
Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Enabling=20auto-merging=20of=20c?=
=?UTF-8?q?ertain=20PRs?=

The medium format shows the body, which
may wrap on to multiple lines.

Another body line.
`,
Header: PatchHeader{
SHA: expectedSHA,
Author: expectedIdentity,
AuthorDate: expectedDate,
Title: expectedEmojiMultiLineTitle,
Body: expectedBody,
},
},
"mailboxAppendix": {
Input: `From 61f5cd90bed4d204ee3feb3aa41ee91d4734855b Mon Sep 17 00:00:00 2001
From: Morton Haypenny <mhaypenny@example.com>
Expand Down