fix p4genpatch breakage (missing slash in patch headers)
[p5sagit/p5-mst-13.2.git] / Porting / p4genpatch
CommitLineData
a619eb9e 1#!/usr/bin/perl -w
2
3
4# p4genpatch - Generate a perl patch from the repository
5
6# Usage: $0 -h
7
8# andreas.koenig@anima.de
9
10use strict;
11use File::Temp qw(tempdir);
12use File::Compare;
00082bef 13use File::Spec;
14use File::Spec::Unix;
a619eb9e 15use Time::Local;
667f40ee 16use Getopt::Long;
17use Cwd qw(cwd);
a619eb9e 18
19sub correctmtime ($$$);
20sub Usage ();
21
667f40ee 22$0 =~ s|^.*[\\/]||;
23my $VERSION = '0.05';
24my $TOPDIR = cwd();
25my @P4opt;
26our %OPT = ( "d" => "u", b => "//depot/perl", "D" => "diff" );
a619eb9e 27Getopt::Long::Configure("no_ignore_case");
2d6becbd 28GetOptions(\%OPT, "b=s", "p=s", "d=s", "D=s", "h", "v", "V") or die Usage;
a619eb9e 29print Usage and exit if $OPT{h};
30print "$VERSION\n" and exit if $OPT{V};
fc867767 31die Usage unless @ARGV == 1 && $ARGV[0] =~ /^\d+$/;
32my $CHANGE = shift;
a619eb9e 33
34for my $p4opt (qw(p)) {
35 push @P4opt, "-$p4opt $OPT{$p4opt}" if $OPT{$p4opt};
36}
37
fc867767 38my $system = "p4 @P4opt describe -s $CHANGE |";
a619eb9e 39open my $p4, $system or die "Could not run $system";
40my @action;
41while (<$p4>) {
42 print;
ca8a8491 43 next unless m|($OPT{b})|;
44 my($prefix) = $1;
45 $prefix =~ s|/[^/]+$||; # up to the last "/" in the match is to be stripped
a619eb9e 46 if (my($file,$action) = m|^\.\.\. (//depot.*)\s(\w+)$|) {
47 next if $action eq "delete";
ca8a8491 48 push @action, [$action, $file, $prefix];
a619eb9e 49 }
50}
51close $p4;
52
53my $tempdir;
54print "Differences ...\n";
55for my $a (@action) {
8d7ecf55 56 $tempdir ||= tempdir( "tmp-XXXX", CLEANUP => 1, TMPDIR => 1 );
ca8a8491 57 my($action,$file,$prefix) = @$a;
58 my($path,$basename,$number) = $file =~ m|\Q$prefix\E/(.+/)?([^/]+)#(\d+)|;
00082bef 59
60 my @splitdir = File::Spec::Unix->splitdir($path);
61 $path = File::Spec->catdir(@splitdir);
62
fc867767 63 my($depotfile) = $file =~ m|^(.+)#\d+\z|;
ca8a8491 64 die "Panic: Could not parse file[$file]" unless $number;
a619eb9e 65 $path = "" unless defined $path;
00082bef 66 my($d1,$d2,$prev,$prevchange,$prevfile,$doadd,$t1,$t2);
a619eb9e 67 $prev = $number-1;
fc867767 68 $prevchange = $CHANGE-1;
69 # can't assume previous rev == $number-1 due to obliterated revisions
70 $prevfile = "$depotfile\@$prevchange";
71 if ($number == 1 or $action =~ /^(add|branch)$/) {
00082bef 72 $d1 = File::Spec->devnull;
73 $t1 = $d1;
cbdeeddd 74 ++$doadd;
a619eb9e 75 } elsif ($action =~ /^(edit|integrate)$/) {
00082bef 76 $d1 = File::Spec->catfile($path, "$basename-$prevchange");
77 $t1 = File::Spec->catfile($tempdir, $d1);
a619eb9e 78 warn "==> $d1 <==\n" if $OPT{v};
00082bef 79 my $system = qq[p4 @P4opt print -o "$t1" "$prevfile"];
a619eb9e 80 my $status = `$system`;
81 if ($?) {
82 warn "$0: system[$system] failed, status[$?]\n";
83 next;
84 }
00082bef 85 chmod 0644, $t1;
fc867767 86 if ($status =~ /\#(\d+) \s - \s \w+ \s change \s (\d+) \s /x) {
87 ($prev,$prevchange) = ($1,$2);
88 $prevfile = "$depotfile#$prev";
a619eb9e 89 my $oldd1 = $d1;
fc867767 90 $d1 =~ s/-\d+$/#$prev~$prevchange~/;
00082bef 91 my $oldt1 = $t1;
92 $t1 = File::Spec->catfile($tempdir, $d1);
93 rename $oldt1, $t1;
a619eb9e 94 }
95 } else {
96 die "Unknown action[$action]";
97 }
38dcbcb1 98 $d2 = File::Spec->catfile($path, $basename);
00082bef 99 $t2 = File::Spec->catfile($tempdir, $d2);
a619eb9e 100 warn "==> $d2#$number <==\n" if $OPT{v};
00082bef 101 my $system = qq[p4 @P4opt print -o "$t2" "$file"];
a619eb9e 102 # warn "system[$system]";
103 my $type = `$system`;
104 if ($?) {
105 warn "$0: `$system` failed, status[$?]\n";
106 next;
107 }
00082bef 108 chmod 0644, $t2;
a619eb9e 109 $type =~ m|^//.*\((.+)\)$| or next;
110 $type = $1;
00082bef 111 if ($doadd or File::Compare::compare($t1, $t2)) {
667f40ee 112 print "\n==== $file ($type) ====\n";
a619eb9e 113 unless ($type =~ /text/) {
114 next;
115 }
38dcbcb1 116 print "Index: $d2\n";
00082bef 117 correctmtime($prevfile,$prev,$t1) unless $doadd;
118 correctmtime($file,$number,$t2);
667f40ee 119 chdir $tempdir or warn "Could not chdir '$tempdir': $!";
120 $system = qq[$OPT{D} -$OPT{d} "$d1" "$d2"];
a619eb9e 121 system($system); # no return check because diff doesn't always return 0
667f40ee 122 chdir $TOPDIR or warn "Could not chdir '$TOPDIR': $!";
a619eb9e 123 }
00082bef 124 for ($t1, $t2) {
a619eb9e 125 unlink or warn "Could not unlink $_: $!" if -f;
126 }
127}
128print "End of Patch.\n";
129
00082bef 130my($tz_offset);
a619eb9e 131sub correctmtime ($$$) {
fc867767 132 my($depotfile,$nr,$localfile) = @_;
cbdeeddd 133 my %fstat = map { /^\.\.\. (\w+) (.*)$/ } `p4 @P4opt fstat -s "$depotfile"`;
134 return unless exists($fstat{headRev}) and $fstat{headRev} == $nr;
00082bef 135
136 if ($^O eq 'MacOS') { # fix epoch ... still off by three hours (EDT->PDT)
137 require Time::Local;
138 $tz_offset ||= sprintf "%+0.4d\n", (
139 Time::Local::timelocal(localtime) - Time::Local::timelocal(gmtime)
140 );
141 $fstat{headTime} += 2082844801 + $tz_offset;
142 }
143
fc867767 144 utime $fstat{headTime}, $fstat{headTime}, $localfile;
a619eb9e 145}
146
147sub Usage () {
148 qq{Usage: $0 [OPTIONS] patchnumber
149
ca8a8491 150 -p host:port p4 port (e.g. myhost:1666)
151 -d diffopt option to pass to diff(1)
152 -D diff diff(1) to use
153 -b branch(es) which branches to include (regex); everything up
154 to the last slash of matched portion of path is
155 stripped on local copy (default: //depot/perl)
156 -v verbose
157 -h print this help and exit
158 -V print version number and exit
a619eb9e 159
160Fetches all required files from the repository, puts them into a
161temporary directory with sensible names and sensible modification
162times and composes a patch to STDOUT using external diff command.
163Requires repository access.
164
165Examples:
166 perl $0 12345 | gzip -c > 12345.gz
167 perl $0 -dc 12345 > change-12345.patch
ca8a8491 168 perl $0 -b //depot/maint-5.6/perl -v 8571 > 8571
a619eb9e 169};
170}