Commit | Line | Data |
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 | |
10 | use strict; |
11 | use File::Temp qw(tempdir); |
12 | use File::Compare; |
00082bef |
13 | use File::Spec; |
14 | use File::Spec::Unix; |
a619eb9e |
15 | use Time::Local; |
667f40ee |
16 | use Getopt::Long; |
17 | use Cwd qw(cwd); |
a619eb9e |
18 | |
19 | sub correctmtime ($$$); |
20 | sub Usage (); |
21 | |
667f40ee |
22 | $0 =~ s|^.*[\\/]||; |
23 | my $VERSION = '0.05'; |
24 | my $TOPDIR = cwd(); |
25 | my @P4opt; |
26 | our %OPT = ( "d" => "u", b => "//depot/perl", "D" => "diff" ); |
a619eb9e |
27 | Getopt::Long::Configure("no_ignore_case"); |
2d6becbd |
28 | GetOptions(\%OPT, "b=s", "p=s", "d=s", "D=s", "h", "v", "V") or die Usage; |
a619eb9e |
29 | print Usage and exit if $OPT{h}; |
30 | print "$VERSION\n" and exit if $OPT{V}; |
fc867767 |
31 | die Usage unless @ARGV == 1 && $ARGV[0] =~ /^\d+$/; |
32 | my $CHANGE = shift; |
a619eb9e |
33 | |
34 | for my $p4opt (qw(p)) { |
35 | push @P4opt, "-$p4opt $OPT{$p4opt}" if $OPT{$p4opt}; |
36 | } |
37 | |
fc867767 |
38 | my $system = "p4 @P4opt describe -s $CHANGE |"; |
a619eb9e |
39 | open my $p4, $system or die "Could not run $system"; |
40 | my @action; |
41 | while (<$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 | } |
51 | close $p4; |
52 | |
53 | my $tempdir; |
54 | print "Differences ...\n"; |
55 | for 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 | } |
128 | print "End of Patch.\n"; |
129 | |
00082bef |
130 | my($tz_offset); |
a619eb9e |
131 | sub 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 | |
147 | sub 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 | |
160 | Fetches all required files from the repository, puts them into a |
161 | temporary directory with sensible names and sensible modification |
162 | times and composes a patch to STDOUT using external diff command. |
163 | Requires repository access. |
164 | |
165 | Examples: |
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 | } |