3 # mfc - perl script to generate patchsets from commit mail or message-id.
5 # Copyright (c) 2006 Florent Thoumie <flz@FreeBSD.org>
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # This perl scripts only uses programs that are part of the base system.
33 # Since some people use NO_FOO options, here's the list of used programs :
36 # - perl (with getopt module)
37 # - mkdir, cat, chmod, grep (hopefully everybody has them)
38 # - cdiff or colordiff (optional)
40 # This script is using 3 environment variables :
41 # - MFCHOME: directory where patches, scripts and commit message will be stored.
42 # - MFCCVSROOT: alternative CVSROOT used to generate diffs for new/dead files.
43 # - MFCLOGIN: define this to your freefall login if you have commit rights.
45 # TODO: Look for XXX in the file.
52 use Env qw(MFCHOME MFCLOGIN MFCCVSROOT);
55 my $mfchome = $MFCHOME ? $MFCHOME : "/var/tmp/mfc";
56 my $mfclogin = $MFCLOGIN ? $MFCLOGIN : "";
57 my $cvsroot = $MFCCVSROOT ? $MFCCVSROOT : ':pserver:anoncvs@anoncvs.at.FreeBSD.org:/home/ncvs';
59 my $version = "1.1.0";
76 my $mfc_func = \&mfc_headers;
80 # Look for pre-requisites.
81 my @reqs = ( "fetch", "cvs", "mkdir", "cat", "chmod", "grep" );
85 die "$_ is missing. Please check pre-requisites." if ($cmd =~ /^$/);
87 $cdiff = `which cdiff`;
88 $cdiff = `which colordiff` if ($cdiff =~ /^$/);
90 # Parse command-line options.
91 my $opt_string = 'bf:hi:m:s:v';
92 getopts( "$opt_string", \%opt ) or usage();
93 usage() if !$opt{i} or $opt{h};
94 @msgids = split / /, $opt{m} if (defined($opt{m}));
99 print STDERR << "EOF";
103 $0 [-vb] -f file -i id
104 $0 [-vb] -m msg-id -i id
105 $0 [-vb] -s query -i id
107 -b : generate a backout patch
108 -f file : commit mail file to use ('-' for stdin)
109 -h : this (help) message
110 -i id : identifier used to save commit log message and patch
111 -m msg-id : message-id referring to the original commit (you can use more than one)
112 -s query : search commit mail archives (a filename with his revision is a good search)
113 -v : be a little more verbose
115 $0 -m 200601081417.k08EH4EN027418 -i uscanner
116 $0 -s "param.h 1.41" -i move_acpi
117 $0 -f commit.txt -i id
118 $0 -m "200601081417.k08EH4EN027418 200601110806.k0B86m9C054798" -i consecutive
120 Please report bugs to: Florent Thoumie <flz\@FreeBSD.org>
125 sub previous_revision($)
130 # XXX - I'm not sure this is working as it should.
131 return 0 if ($rev =~ /^1\.1$/);
132 @rev = split '\.', $rev;
133 return undef unless @rev;
134 if (($#rev % 2) != 1) {
136 return join ".", @rev;
140 return &previous_revision(join ".", @rev);
143 return join ".", @rev;
156 $url = `fetch -q -o - 'http://www.freebsd.org/cgi/mid.cgi?id=$msgid'| grep getmsg.cgi | head -n 1`;
159 print "No mail found for Message-Id <$msgid>.\n";
162 $url =~ s/.*href="(.*)".*/$1/;
163 $url =~ s/\n$/\+raw/;
164 $url = "http://www.freebsd.org/cgi/$url";
174 # XXX - I guess we could take 5 first results instead of just the first
175 # but it has been working correctly for each search I've made so ...
176 my $result = `fetch -q -o - 'http://www.freebsd.org/cgi/search.cgi?words=$query&max=1&sort=score&index=recent&source=cvs-all' | grep getmsg.cgi`;
178 $result =~ s/.*href="(.*)">.*/http:\/\/www.freebsd.org\/cgi\/$1+raw/;
179 if ($result =~ /^$/) {
180 print "No commit mail found for '$query'.\n";
189 my $old = $mfc_files{$name}{"from"};
190 my $new = $mfc_files{$name}{"to"};
192 # CVSWeb uses rcsdiff instead of cvs rdiff, that's a problem for deleted and new files.
193 # Need to use cvs to generate reversed diff for backout commits.
195 print " Generating reversed diff for $name using cvs diff...\n";
196 system("cvs -d $cvsroot diff -u -j$new -j$old $name >> $mfchome/$opt{i}/patch 2>/dev/null");
197 } elsif (exists($new_files{$name}) or exists($dead_files{$name})) {
198 print " Generating diff for $name using cvs rdiff...\n";
199 system("cvs -d $cvsroot rdiff -u -r$old -r$new $name >> $mfchome/$opt{i}/patch 2>/dev/null");
201 print " Fetching diff for $name from cvsweb.freebsd.org...\n";
202 system("fetch -q -o - \"http://www.freebsd.org/cgi/cvsweb.cgi/$name.diff?r1=$old&r2=$new\" >> $mfchome/$opt{i}/patch");
209 $mfc_func = \&mfc_author;
210 } elsif ($_[0] =~ /^(\w+)\s+(\S+\s\S+\s\S+)$/) {
211 # Skipped headers (probably a copy/paste from sobomax MFC reminder).
214 if ($_[0] =~ /^Message-Id:\s*(\S+)$/ and ($opt{v} or $opt{s})) {
215 print "Message-Id is $1.\n";
222 if (!($_[0] =~ /^(\w+)\s+(\S+\s\S+\s\S+)$/)) {
223 die "Can't determine commit author and date.";
228 print "Committed by $commit_author on $commit_date.\n";
230 $mfc_func = \&mfc_modified_files;
233 sub mfc_modified_files($)
235 if ($_[0] =~ /^\s+Log:/) {
236 $mfc_func = \&mfc_log;
244 if ($_[0] =~ /^\s*Revision\s+Changes\s+Path\s*$/) {
245 $mfc_func = \&mfc_revisions;
247 push(@logmsg, $_[0]);
257 return if ($_[0] =~ /^$/);
258 if (!($_[0] =~ /^\s+(\S+)\s+\S+\s+\S+\s+(\S+)/)) {
259 # Probably two consecutive cut/paste commit mails.
260 $mfc_func = \&mfc_headers;
264 $_[0] =~ /\s+(\S+)\s+\S+\s+\S+\s+(\S+)/;
268 $new_files{$name} = undef if ($_[0] =~ /\(new\)$/);
269 $dead_files{$name} = undef if ($_[0] =~ /\(dead\)$/);
271 if (defined($mfc_files{$name}{"from"})) {
272 $prev = previous_revision($rev);
273 if ($mfc_files{$name}{"to"} =~ /^$prev$/) {
274 $mfc_files{$name}{"to"} = $rev;
276 die "Non-consecutive revisions found for $name.";
279 $mfc_files{$name}{"to"} = $rev;
280 $mfc_files{$name}{"from"} = previous_revision($rev);
288 while ($#logmsg >= 0 and ($logmsg[$#logmsg] =~ /^\s*$/ or $logmsg[$#logmsg] =~ /^\s\s\w+(\s\w+)*:\s+\w+(\s+\w+)*/)) {
292 if ($tmp =~ /^PR:\s+(.*)/) {
295 if ($tmp =~ /^Submitted by:\s+(.*)/) {
296 push(@submitted_by, $1);
298 if ($tmp =~ /^Reviewed by:\s+(.*)/) {
299 push(@reviewed_by, $1);
301 if ($tmp =~ /^Obtained from:\s+(.*)/) {
302 push(@obtained_from, $1);
311 $tmp = join(", ", @prs);
313 print MSG "PR:\t\t$tmp\n";
315 if ($#submitted_by >= 0) {
316 $tmp = join(", ", @submitted_by);
318 print MSG "Submitted by:\t$tmp\n";
320 if ($#reviewed_by >= 0) {
321 $tmp = join(", ", @reviewed_by);
323 print MSG "Reviewed by:\t$tmp\n";
325 if ($#obtained_from >= 0) {
326 $tmp = join(", ", @obtained_from);
328 print MSG "Obtained from:\t$tmp\n";
335 print "Searching commit mail on www.freebsd.org...\n";
336 $commiturl = search_mail($opt{s});
337 print "Fetching commit mail from www.freebsd.org...\n";
338 @commitmail = `fetch -q -o - $commiturl`;
339 $mfc_func->($_) foreach (@commitmail);
342 open MAIL, $opt{f} || die "Can't open $opt{f} for reading.";
343 @commitmail = <MAIL>;
345 $mfc_func->($_) foreach (@commitmail);
349 print "Fetching commit mail from www.freebsd.org...\n";
350 $commiturl = fetch_mail($_);
351 @commitmail = `fetch -q -o - $commiturl`;
352 $mfc_func->($_) foreach (@commitmail);
357 die "Doesn't seem you gave me a real commit mail." if ($mfc_func == \&mfc_headers);
358 die "No file affected by commit?" if (scalar(keys(%mfc_files)) == 0);
360 # Create directory and truncate patch file.
361 system("mkdir -p $mfchome/$opt{i}");
362 system("cat /dev/null > $mfchome/$opt{i}/patch");
364 if ($opt{v} or $opt{s}) {
365 # Print files touched by commit(s).
366 print "Files touched by commit(s):\n";
367 print " ", $_, ": rev ", $mfc_files{$_}{"from"}, " -> ", $mfc_files{$_}{"to"}, "\n" foreach (keys(%mfc_files));
371 print "Is it the commit you were looking for ? [Yn] ";
374 if ($answer =~ /^[Nn]$/) {
375 print "Sorry that I couldn't help you.\n";
381 print "Processing patch...\n";
382 fetch_diff($_) foreach (keys(%mfc_files));
385 # Create commit message from previous commit message.
386 print "Processing commit message...\n";
387 # Chop empty lines Template lines like "Approved by: (might be dangerous)".
388 open MSG, "> $mfchome/$opt{i}/msg" || die "Can't open $mfchome/$opt{i}/msg for writing.";
390 print MSG "Backout this commit:\n\n";
392 print MSG "MFC:\n\n";
395 # Append merged file names and revisions to the commit message.
396 print MSG $_ foreach (@logmsg);
399 print MSG " ", $_, ": rev ", $mfc_files{$_}{"from"}, " -> ", $mfc_files{$_}{"to"}, "\n" foreach (keys(%mfc_files));
402 # Append useful info gathered from Submitted/Obtained/... lines.
407 # Create commit script.
408 print "Processing commit script...\n";
409 open SCRIPT, "> $mfchome/$opt{i}/script" || die "Can't open $mfchome/$opt{i}/script for writing.";
410 print SCRIPT "#! /bin/sh\n\n";
411 print SCRIPT "# This script has been automatically generated by $0.\n\n";
412 print SCRIPT "export CVSROOT=\"$mfclogin\@ncvs.freebsd.org:/home/ncvs\"\n\n";
414 if (scalar(keys(%new_files)) or scalar(keys(%dead_files))) {
415 if (scalar(keys(%new_files))) {
416 print SCRIPT "cvs add";
417 print SCRIPT " \\\n $_" foreach (keys(%new_files));
420 if (scalar(keys(%dead_files))) {
421 print SCRIPT "cvs rm -f";
422 print SCRIPT " \\\n $_" foreach (keys(%dead_files));
427 print SCRIPT "cvs diff";
428 print SCRIPT " \\\n $_" foreach (keys(%mfc_files));
429 if ($cdiff =~ /^$/) {
432 print SCRIPT " | $cdiff";
435 print SCRIPT "cvs ci";
436 print SCRIPT " \\\n $_" foreach (keys(%mfc_files));
440 system("chmod a+x $mfchome/$opt{i}/script");
443 print "Done, output directory is $mfchome/$opt{i}/\n";