From a381630604066cde5b791bfadb92de8c35e1cd1c Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Sun, 23 Oct 2022 16:01:21 +0200 Subject: pipe: reorder patches based on email subjects The Message-Id header cannot be relied upon as users can tweak it before sending. The subject seems a more reliable basis to reorder patches. Change the logic of sorting. Previously, all messages were required to look like patches to be sorted. Now if at least one message looks like a patch, all messages will be sorted by Subject before piping them. Since `git am` ignores non-patch messages, it should allow piping series of emails including reviews and comments without getting confusing errors. I have tested that this works on multiple series that appeared out of order in my INBOX with the following command (after marking the messages): :pipe -m sed -n 's/^Subject: //p' Signed-off-by: Robin Jarry Tested-by: Bence Ferdinandy --- commands/msg/pipe.go | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) (limited to 'commands/msg/pipe.go') diff --git a/commands/msg/pipe.go b/commands/msg/pipe.go index 89dbc47c..d0101675 100644 --- a/commands/msg/pipe.go +++ b/commands/msg/pipe.go @@ -7,7 +7,6 @@ import ( "os/exec" "regexp" "sort" - "strconv" "time" "git.sr.ht/~rjarry/aerc/commands" @@ -169,11 +168,11 @@ func (Pipe) Execute(aerc *widgets.Aerc, args []string) error { } } - is_git_patches := true + is_git_patches := false for _, msg := range messages { info := store.Messages[msg.Content.Uid] - if info == nil || !gitMessageIdRe.MatchString(info.Envelope.MessageId) { - is_git_patches = false + if info != nil && patchSeriesRe.MatchString(info.Envelope.Subject) { + is_git_patches = true break } } @@ -186,9 +185,7 @@ func (Pipe) Execute(aerc *widgets.Aerc, args []string) error { if infoi == nil || infoj == nil { return false } - msgidi := padGitMessageId(infoi.Envelope.MessageId) - msgidj := padGitMessageId(infoj.Envelope.MessageId) - return msgidi < msgidj + return infoi.Envelope.Subject < infoj.Envelope.Subject }) } @@ -241,21 +238,6 @@ func newMessagesReader(messages []*types.FullMessage, useMbox bool) io.Reader { return pr } -var gitMessageIdRe = regexp.MustCompile(`^(\d+\.\d+)-(\d+)-(.+)$`) - -// Git send-email Message-Id headers have the following format: -// -// DATETIME.PID-NUM-COMMITTER -// -// Return a copy of the message id with NUM zero-padded to three characters. -func padGitMessageId(msgId string) string { - matches := gitMessageIdRe.FindStringSubmatch(msgId) - if matches == nil { - return msgId - } - number, err := strconv.Atoi(matches[2]) - if err != nil { - return msgId - } - return fmt.Sprintf("%s-%03d-%s", matches[1], number, matches[3]) -} +var patchSeriesRe = regexp.MustCompile( + `^.*\[(RFC )?PATCH( [^\]]+)? \d+/\d+] .+$`, +) -- cgit