#!/usr/bin/perl # # lkmlify - Rewrite a mercurial patchbomb mailbox to be lkml-compliant # # This mangles an mbox written by hg email. The diffstat section is # moved from the beginning of each message to after the --- separator. # # Copyright (C) 2008 Martin K. Petersen # Released under the terms of the GNU General Public License v2. # use File::Basename; use File::Spec; die "Must specify arg\n" if ($#ARGV < 0); $in = File::Spec->rel2abs(shift); ($file, $path, $suffix) = fileparse($in, '.mbox'); $out = "$path/$file-lkml$suffix"; open(IN, "$in") || die "Can't open $in\n"; open(OUT, ">$out") || die "Can't open $out\n"; $sob = 0; # Signed-off-by: found $dsh = 0; # Diffstat header found # Fun RS-altering state machine while () { if ($dsh == 1) { $dsh = 0; $diffstat = $_; $/ = "\n"; } elsif (/^\d+ file[s]* changed, \d+ insertions/) { $dsh = 1; $header = $_; $/ = ""; } elsif (/^(Signed-off|Acked|Reviewed)-by:/) { print OUT; $sob = 1; } elsif (/^---\n$/ && $sob == 1) { print OUT "---\n$header\n$diffstat"; $sob = 0; } else { print OUT; if ($sob == 1 && $_ ne "\n") { die "Warning: Gunk found between signoff and ---\n"; } } } # Sanity check open(STATS, "diff -u $in $out | diffstat |") || die "Can't run diffstat"; while () { if (/^ \d+ file[s]* changed, (\d+) insertions\(\+\), (\d+) deletions/) { print; die "$1 insertions != $2 deletions" if ($1 != $2); } } close IN; close OUT; close STATS; # EOF