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