Skip to content

Instantly share code, notes, and snippets.

@nasamuffin
Created May 8, 2019 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasamuffin/af69fb2bb76ec66c1bc4f42932183539 to your computer and use it in GitHub Desktop.
Save nasamuffin/af69fb2bb76ec66c1bc4f42932183539 to your computer and use it in GitHub Desktop.
midam - "Message-Id git am"
#!/usr/bin/perl
# midam: Message-Id git am
# Author: Emily Shaffer (@nasamuffin)
#
# Applies patches based on a Message-Id.
# If the Message-Id provided is a cover letter ([PATCH 0/]), apply all
# messages with In-Reply-To: <ID> instead.
use autodie;
# Only take one argument.
$#ARGV+1==1 or die "Usage: midam \"<message-id>\"";
my $mid = $ARGV[0];
# The mailing list name.
my $maildir = '/usr/local/google/home/emilyshaffer/git-maillist';
# Fetch the latest mail.
my $fetcher = join('', 'git -C ', $maildir, ' pull');
(system $fetcher == 0) or warn "Couldn't fetch mail; using stale repo";
# git-grep the message with provided ID from the archive
my @patches = find_messages("Message-Id:");
# We should only have one message match, using the Message-Id prefix.
$#patches+1==1 or die "More than one message with ID $mid!";
# Check whether it's a cover letter
my $topPatch = join('/', $maildir, $patches[0]);
open(my $fh, "<", $topPatch);
my @lines = <$fh>;
my $series = 0;
foreach (@lines) {
# Cover letters contain "[PATCH 0/n]"
if (m|Subject: \[(PATCH \|RFC )+0+/\d\]|) {
print "Cover letter supplied, applying all replies.\n";
@patches = find_messages("In-Reply-To:");
last;
}
}
# apply the patch(es) with --interactive so user has a last chance to stop it
foreach $patch (@patches) {
my $patcher = join('', 'git am --interactive ', $maildir, '/', $patch);
system $patcher
}
#### Subroutines ####
# git-grep messages from the archive matching a header and message-id
sub find_messages {
my $header_pfx = shift;
my $grepper = join('', 'git -C ', $maildir,
' --no-pager grep -Fl --full-name "', $header_pfx, ' ', $mid,
'" |');
open(PATCHES, $grepper);
chomp(my @patches = <PATCHES>);
close(PATCHES);
return @patches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment