]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - tools/tools/mfc/mfc.pl
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / tools / tools / mfc / mfc.pl
1 #! /usr/bin/env perl
2 #
3 # mfc - perl script to generate patchsets from commit mail or message-id.
4 #
5 # Copyright (c) 2006 Florent Thoumie <flz@FreeBSD.org>
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
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.
16 #
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
27 # SUCH DAMAGE.
28 #
29 # $FreeBSD$
30 #
31
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 :
34 #  - cvs
35 #  - fetch
36 #  - perl (with getopt module)
37 #  - mkdir, cat, chmod, grep (hopefully everybody has them)
38 #  - cdiff or colordiff (optional)
39 #
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.
44 #
45 # TODO: Look for XXX in the file.
46 #
47
48 use strict;
49 use warnings;
50
51 use Env;
52 use Env qw(MFCHOME MFCLOGIN MFCCVSROOT);
53 use Getopt::Std;
54
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';
58
59 my $version = "1.1.0";
60 my %opt;
61 my $commit_author;
62 my $commit_date;
63 my %mfc_files = ( );
64 my %new_files = ( );
65 my %dead_files = ( );
66 my @msgids = ( );
67 my @logmsg = ( );
68 my @commitmail = ( );
69 my $commiturl;
70 my @prs;
71 my @submitted_by;
72 my @reviewed_by;
73 my @obtained_from;
74 my $cdiff;
75 my $answer;
76 my $mfc_func = \&mfc_headers;
77
78 sub init()
79 {
80         # Look for pre-requisites.
81         my @reqs = ( "fetch", "cvs", "mkdir", "cat", "chmod", "grep" );
82         my $cmd;
83         foreach (@reqs) {
84                 $cmd = `which $_`;
85                 die "$_ is missing. Please check pre-requisites." if ($cmd =~ /^$/);
86         }
87         $cdiff = `which cdiff`;
88         $cdiff = `which colordiff` if ($cdiff =~ /^$/);
89
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}));
95 }
96
97 sub usage()
98 {
99         print STDERR << "EOF";
100 $0 version $version 
101
102 Usage: $0 [-v] -h
103        $0 [-vb] -f file -i id
104        $0 [-vb] -m msg-id -i id
105        $0 [-vb] -s query -i id
106 Options:
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
114 Examples:
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
119
120 Please report bugs to: Florent Thoumie <flz\@FreeBSD.org>
121 EOF
122         exit 1;
123 }
124
125 sub previous_revision($)
126 {
127         my ($rev) = @_;
128         my @rev;
129
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) {
135                 pop @rev;
136                 return join ".", @rev;
137         }
138         if ($rev[-1] == 1) {
139                 pop @rev;
140                 return &previous_revision(join ".", @rev);
141         } else {
142                 $rev[-1]--;
143                 return join ".", @rev;
144         }
145 }
146
147 sub fetch_mail($)
148 {
149         my $msgid = $_[0];
150         my $url = "";
151
152         $msgid =~ s/<//;
153         $msgid =~ s/>//;
154         $msgid =~ s/@.*//;
155
156         $url = `fetch -q -o - 'http://www.freebsd.org/cgi/mid.cgi?id=$msgid'| grep getmsg.cgi | head -n 1`;
157
158         if ($url =~ /^$/) {
159                 print "No mail found for Message-Id <$msgid>.\n";
160                 exit 1;
161         }
162         $url =~ s/.*href="(.*)".*/$1/;
163         $url =~ s/\n$/\+raw/;
164         $url = "http://www.freebsd.org/cgi/$url";
165         return $url;
166 }
167
168 sub search_mail($)
169 {
170         my $query = $_[0];
171
172         $query =~ s/\s+/+/g;
173
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`;
177
178         $result =~ s/.*href="(.*)">.*/http:\/\/www.freebsd.org\/cgi\/$1+raw/;
179         if ($result =~ /^$/) {
180                 print "No commit mail found for '$query'.\n";
181                 exit 1;
182         }
183         return $result;
184 }
185
186 sub fetch_diff($)
187 {
188         my $name = $_[0];
189         my $old = $mfc_files{$name}{"from"};
190         my $new = $mfc_files{$name}{"to"};
191
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.
194         if ($opt{b}) {
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");
200         } else {
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");
203         }
204 }
205
206 sub mfc_headers($)
207 {
208         if ($_[0] =~ /^$/) {
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).
212                 mfc_author($_[0]);
213         } else {
214                 if ($_[0] =~ /^Message-Id:\s*(\S+)$/ and ($opt{v} or $opt{s})) {
215                         print "Message-Id is $1.\n";
216                 }
217         }
218 }
219
220 sub mfc_author($)
221 {
222         if (!($_[0] =~ /^(\w+)\s+(\S+\s\S+\s\S+)$/)) {
223                 die "Can't determine commit author and date.";
224         }
225         $commit_author = $1;
226         $commit_date = $2;      
227
228         print "Committed by $commit_author on $commit_date.\n";
229
230         $mfc_func = \&mfc_modified_files;
231 }
232
233 sub mfc_modified_files($)
234 {
235         if ($_[0] =~ /^\s+Log:/) {
236                 $mfc_func = \&mfc_log;
237         } else {
238                 # Nothing
239         }
240 }
241
242 sub mfc_log($)
243 {
244         if ($_[0] =~ /^\s*Revision\s+Changes\s+Path\s*$/) {
245                 $mfc_func = \&mfc_revisions;
246         } else {
247                 push(@logmsg, $_[0]);
248         }
249 }
250
251 sub mfc_revisions($)
252 {
253         my $name;
254         my $rev;
255         my $prev;
256
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;
261                 mfc_headers($_[0]);
262                 return;
263         } else {
264                 $_[0] =~ /\s+(\S+)\s+\S+\s+\S+\s+(\S+)/;
265                 $name = $2;
266                 $rev = $1;
267
268                 $new_files{$name} = undef if ($_[0] =~ /\(new\)$/);
269                 $dead_files{$name} = undef if ($_[0] =~ /\(dead\)$/);
270                 
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;
275                         } else {
276                                 die "Non-consecutive revisions found for $name.";
277                         }
278                 } else {
279                         $mfc_files{$name}{"to"} = $rev;
280                         $mfc_files{$name}{"from"} = previous_revision($rev);
281                 }
282         }
283 }
284
285 sub strip_log(@) {
286         my $tmp;
287
288         while ($#logmsg >= 0 and ($logmsg[$#logmsg] =~ /^\s*$/ or $logmsg[$#logmsg] =~ /^\s\s\w+(\s\w+)*:\s+\w+(\s+\w+)*/)) {
289                 $tmp = pop(@logmsg);
290                 $tmp =~ s/^\s*//;
291                 chomp($tmp);
292                 if ($tmp =~ /^PR:\s+(.*)/) {
293                         push(@prs, $1);
294                 }
295                 if ($tmp =~ /^Submitted by:\s+(.*)/) {
296                         push(@submitted_by, $1);
297                 }
298                 if ($tmp =~ /^Reviewed by:\s+(.*)/) {
299                         push(@reviewed_by, $1);
300                 }
301                 if ($tmp =~ /^Obtained from:\s+(.*)/) {
302                         push(@obtained_from, $1);
303                 }
304         }
305 }
306
307 sub print_epilog {
308         my $tmp;
309
310         if ($#prs >= 0) {
311                 $tmp = join(", ", @prs);
312                 chomp($tmp);
313                 print MSG "PR:\t\t$tmp\n";
314         }
315         if ($#submitted_by >= 0) {
316                 $tmp = join(", ", @submitted_by);
317                 chomp($tmp);
318                 print MSG "Submitted by:\t$tmp\n";
319         }
320         if ($#reviewed_by >= 0) {
321                 $tmp = join(", ", @reviewed_by);
322                 chomp($tmp);
323                 print MSG "Reviewed by:\t$tmp\n";
324         }
325         if ($#obtained_from >= 0) {
326                 $tmp = join(", ", @obtained_from);
327                 chomp($tmp);
328                 print MSG "Obtained from:\t$tmp\n";
329         }
330 }
331
332 init();
333
334 if ($opt{s}) {
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);
340         strip_log(@logmsg);
341 } elsif ($opt{f}) {
342         open MAIL, $opt{f} || die "Can't open $opt{f} for reading.";
343         @commitmail = <MAIL>;   
344         close MAIL;
345         $mfc_func->($_) foreach (@commitmail);
346         strip_log(@logmsg);
347 } else { # $opt{m}
348         foreach (@msgids) {
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);
353                 strip_log(@logmsg);
354         }
355 }
356
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);
359
360 # Create directory and truncate patch file.
361 system("mkdir -p $mfchome/$opt{i}");
362 system("cat /dev/null > $mfchome/$opt{i}/patch");
363
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));
368 }
369
370 if ($opt{s}) {
371         print "Is it the commit you were looking for ? [Yn] ";
372         $answer = <STDIN>;
373         chomp($answer);
374         if ($answer =~ /^[Nn]$/) {
375                 print "Sorry that I couldn't help you.\n";
376                 exit 0;
377         }
378 }
379
380 # Generating patch.
381 print "Processing patch...\n";
382 fetch_diff($_) foreach (keys(%mfc_files));
383
384 if ($mfclogin) {
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.";
389         if ($opt{b}) {
390                 print MSG "Backout this commit:\n\n";
391         } else {
392                 print MSG "MFC:\n\n";
393         }
394         
395         # Append merged file names and revisions to the commit message.
396         print MSG $_ foreach (@logmsg);
397         if (!$opt{b}) {
398                 print MSG "\n";
399                 print MSG "      ", $_, ": rev ", $mfc_files{$_}{"from"}, " -> ", $mfc_files{$_}{"to"}, "\n" foreach (keys(%mfc_files));
400         }
401
402         # Append useful info gathered from Submitted/Obtained/... lines.
403         print MSG "\n";
404         print_epilog();
405         close MSG;
406
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";
413
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));
418                         print SCRIPT "\n";
419                 }
420                 if (scalar(keys(%dead_files))) {
421                         print SCRIPT "cvs rm -f";
422                         print SCRIPT " \\\n  $_" foreach (keys(%dead_files));
423                         print SCRIPT "\n";
424                 }
425         }
426
427         print SCRIPT "cvs diff";
428         print SCRIPT " \\\n  $_" foreach (keys(%mfc_files));
429         if ($cdiff =~ /^$/) {
430                 print SCRIPT "\n";
431         } else {
432                 print SCRIPT " | $cdiff";
433         }
434
435         print SCRIPT "cvs ci";
436         print SCRIPT " \\\n  $_" foreach (keys(%mfc_files));
437         print SCRIPT "\n";
438
439         close SCRIPT;
440         system("chmod a+x $mfchome/$opt{i}/script");
441 }
442
443 print "Done, output directory is $mfchome/$opt{i}/\n";
444
445 exit 0;