]> git.mjollnir.org Git - scripts.git/commitdiff
Add bugstogitformatpatch, small helper for moodle work
authorPenny Leach <penny@mjollnir.org>
Tue, 15 Sep 2009 10:54:30 +0000 (12:54 +0200)
committerPenny Leach <penny@mjollnir.org>
Tue, 15 Sep 2009 10:54:30 +0000 (12:54 +0200)
bin/bugstogitformatpatch [new file with mode: 0755]

diff --git a/bin/bugstogitformatpatch b/bin/bugstogitformatpatch
new file mode 100755 (executable)
index 0000000..fd76071
--- /dev/null
@@ -0,0 +1,91 @@
+#!/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,
+    });
+}