Skip to content

Commit 60bad79

Browse files
committed
mbox: fix signature and multi-message formatting
Add a blank line before the signature so that git am does not treat it as part of the diff. Trim trailing newlines before writing the body since mbox.Writer.Close() already appends the message terminator. Write multi-message mbox directly to the response writer instead of collecting parts into a slice since mbox.Writer.Close() already separates messages. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent b296c17 commit 60bad79

3 files changed

Lines changed: 23 additions & 41 deletions

File tree

pkg/api/mbox.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package api
77

88
import (
9-
"bytes"
109
"fmt"
1110
"net/http"
1211
"strconv"
@@ -118,12 +117,6 @@ func (h *handler) seriesMbox(w http.ResponseWriter, r *http.Request) {
118117
return
119118
}
120119

121-
var parts [][]byte
122-
for i := range patches {
123-
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
124-
parts = append(parts, mbox.Format(sub))
125-
}
126-
127120
name := "series"
128121
if series.Name != nil {
129122
name = *series.Name
@@ -133,7 +126,10 @@ func (h *handler) seriesMbox(w http.ResponseWriter, r *http.Request) {
133126
w.Header().Set("Content-Disposition",
134127
fmt.Sprintf("attachment; filename=%s.patch",
135128
mbox.SanitizeFilename(name)))
136-
_, _ = w.Write(bytes.Join(parts, []byte("\n")))
129+
for i := range patches {
130+
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
131+
_, _ = w.Write(mbox.Format(sub))
132+
}
137133
}
138134

139135
func (h *handler) bundleMbox(w http.ResponseWriter, r *http.Request) {
@@ -169,15 +165,12 @@ func (h *handler) bundleMbox(w http.ResponseWriter, r *http.Request) {
169165
return
170166
}
171167

172-
var parts [][]byte
173-
for i := range patches {
174-
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
175-
parts = append(parts, mbox.Format(sub))
176-
}
177-
178168
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
179169
w.Header().Set("Content-Disposition",
180170
fmt.Sprintf("attachment; filename=bundle-%d-%s.mbox",
181171
bundle.ID, mbox.SanitizeFilename(bundle.Name)))
182-
_, _ = w.Write(bytes.Join(parts, []byte("\n")))
172+
for i := range patches {
173+
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
174+
_, _ = w.Write(mbox.Format(sub))
175+
}
183176
}

pkg/mbox/mbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ func Format(sub Submission) []byte {
114114
}
115115

116116
if Version != "" && !signatureRe.MatchString(body) {
117-
body += "-- \npatchwork " + Version + "\n"
117+
body += "\n-- \npatchwork " + Version
118118
}
119119

120-
_, _ = w.Write([]byte(body))
120+
_, _ = w.Write([]byte(strings.TrimRight(body, "\n")))
121121
mw.Close()
122122

123123
return buf.Bytes()

pkg/web/mbox.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package web
77

88
import (
9-
"bytes"
109
"context"
1110
"fmt"
1211
"net/http"
@@ -139,12 +138,6 @@ func (h *webHandler) SeriesMbox(w http.ResponseWriter, r *http.Request) {
139138
return
140139
}
141140

142-
var parts [][]byte
143-
for i := range patches {
144-
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
145-
parts = append(parts, mbox.Format(sub))
146-
}
147-
148141
name := "series"
149142
if series.Name != nil {
150143
name = *series.Name
@@ -153,7 +146,10 @@ func (h *webHandler) SeriesMbox(w http.ResponseWriter, r *http.Request) {
153146
w.Header().Set("Content-Type", "text/plain")
154147
w.Header().Set("Content-Disposition",
155148
fmt.Sprintf("attachment; filename=%s.patch", mbox.SanitizeFilename(name)))
156-
_, _ = w.Write(bytes.Join(parts, []byte("\n")))
149+
for i := range patches {
150+
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
151+
_, _ = w.Write(mbox.Format(sub))
152+
}
157153
}
158154

159155
func (h *webHandler) seriesPatchMbox(w http.ResponseWriter, r *http.Request, patch db.Patch, project db.Project, seriesParam string) {
@@ -182,19 +178,15 @@ func (h *webHandler) seriesPatchMbox(w http.ResponseWriter, r *http.Request, pat
182178
Scan(ctx)
183179
}
184180

185-
var parts [][]byte
181+
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
182+
w.Header().Set("Content-Disposition",
183+
fmt.Sprintf("attachment; filename=%s.patch", mbox.SanitizeFilename(patch.Name)))
186184
for i := range deps {
187185
sub := mbox.BuildPatchSubmission(ctx, q.DB, &deps[i], project.Listemail)
188-
parts = append(parts, mbox.Format(sub))
186+
_, _ = w.Write(mbox.Format(sub))
189187
}
190-
191188
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patch, project.Listemail)
192-
parts = append(parts, mbox.Format(sub))
193-
194-
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
195-
w.Header().Set("Content-Disposition",
196-
fmt.Sprintf("attachment; filename=%s.patch", mbox.SanitizeFilename(patch.Name)))
197-
_, _ = w.Write(bytes.Join(parts, []byte("\n")))
189+
_, _ = w.Write(mbox.Format(sub))
198190
}
199191

200192
func (h *webHandler) BundleMbox(w http.ResponseWriter, r *http.Request) {
@@ -238,17 +230,14 @@ func (h *webHandler) BundleMbox(w http.ResponseWriter, r *http.Request) {
238230
return
239231
}
240232

241-
var parts [][]byte
242-
for i := range patches {
243-
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
244-
parts = append(parts, mbox.Format(sub))
245-
}
246-
247233
w.Header().Set("Content-Type", "text/plain")
248234
w.Header().Set("Content-Disposition",
249235
fmt.Sprintf("attachment; filename=bundle-%d-%s.mbox",
250236
bundle.ID, mbox.SanitizeFilename(bundle.Name)))
251-
_, _ = w.Write(bytes.Join(parts, []byte("\n")))
237+
for i := range patches {
238+
sub := mbox.BuildPatchSubmission(ctx, q.DB, &patches[i], project.Listemail)
239+
_, _ = w.Write(mbox.Format(sub))
240+
}
252241
}
253242

254243
func (h *webHandler) CommentRedirect(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)