--- /dev/null
+#!/usr/bin/perl -w
+=pod
+
+=head1 NAME
+
+ bugstogitformatpatch
+
+=head1 DESCRIPTION
+
+ Searches through the given file (or STDIN) for things that look like moodle
+ bug ids eg MDL-1234, and greps a git log for patches that contain these bug
+ numbers and generates git patches for them, ready to be applied with git am.
+
+ Needs to be run in the git checkout directory.
+
+=head1 USAGE
+
+ --grepfile FILE (otherwise STDIN will be used)
+ --revlist REVLIST eg origin/cvshead (default) or frompoint..topoint or 123abc
+ --patchdir DIR (must be empty) to write patches to. Default: .patches
+ --help Print help and then exit
+
+=head1 AUTHORS
+
+ Penny Leach <penny@mjollnir.org>
+
+=cut
+
+use strict;
+use warnings;
+use Getopt::Long;
+use Pod::Usage;
+
+my $grepfile = undef;
+my $revlist = 'origin/cvshead';
+my $patchdir = '.patches';
+my $help = 0;
+
+GetOptions(
+ 'grepfile=s', \$grepfile,
+ 'revlist=s', \$revlist,
+ 'patchdir=s', \$patchdir,
+ 'help', \$help,
+) or &usage;
+
+$help && &usage;
+
+die "Target directory $patchdir was non empty" if (<$patchdir/*>);
+
+my $bugs;
+if ($grepfile) {
+ $bugs = `grep MDL $grepfile`;
+ die "No bugs found in $grepfile" unless $bugs;
+} else {
+ my @input;
+ while (<>) {
+ push (@input, $_);
+ }
+ $bugs = join ('', grep (/MDL/, @input));
+ die "No bugs found in file read in STDIN" unless $bugs;
+}
+$bugs =~ s/.*?(MDL\-\d+).*/$1/g;
+$bugs =~ s/\n/\\|/g;
+$bugs =~ s/(.*?)..$/$1/;
+
+if (! -d $patchdir ) {
+ `mkdir $patchdir`;
+}
+
+my $hashes = `git log --reverse -r $revlist -i --grep="$bugs" | grep ^commit | awk ' { print \$2 } '`;
+
+my $count = 0;
+
+my @hashes = split(/\n/, $hashes);
+
+foreach my $hash (@hashes) {
+ `git format-patch -o $patchdir --start-number $count $hash^..$hash`;
+ $count++;
+}
+
+print "wrote $count patches to $patchdir\n";
+exit 0;
+
+sub usage {
+ pod2usage( {
+ -sections => "USAGE",
+ -exitval => 0,
+ -verbose => 99,
+ -output => \*STDOUT,
+ });
+}