1cdf0e10cSrcweir: 2cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}' 3cdf0e10cSrcweir if 0; 4*7e90fac2SAndrew Rist#************************************************************** 5*7e90fac2SAndrew Rist# 6*7e90fac2SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 7*7e90fac2SAndrew Rist# or more contributor license agreements. See the NOTICE file 8*7e90fac2SAndrew Rist# distributed with this work for additional information 9*7e90fac2SAndrew Rist# regarding copyright ownership. The ASF licenses this file 10*7e90fac2SAndrew Rist# to you under the Apache License, Version 2.0 (the 11*7e90fac2SAndrew Rist# "License"); you may not use this file except in compliance 12*7e90fac2SAndrew Rist# with the License. You may obtain a copy of the License at 13*7e90fac2SAndrew Rist# 14*7e90fac2SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 15*7e90fac2SAndrew Rist# 16*7e90fac2SAndrew Rist# Unless required by applicable law or agreed to in writing, 17*7e90fac2SAndrew Rist# software distributed under the License is distributed on an 18*7e90fac2SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19*7e90fac2SAndrew Rist# KIND, either express or implied. See the License for the 20*7e90fac2SAndrew Rist# specific language governing permissions and limitations 21*7e90fac2SAndrew Rist# under the License. 22*7e90fac2SAndrew Rist# 23*7e90fac2SAndrew Rist#************************************************************** 24*7e90fac2SAndrew Rist 25*7e90fac2SAndrew Rist 26cdf0e10cSrcweir 27cdf0e10cSrcweiruse utf8; 28cdf0e10cSrcweiruse warnings; 29cdf0e10cSrcweiruse strict; 30cdf0e10cSrcweir 31cdf0e10cSrcweir# command line arguments 32cdf0e10cSrcweirmy $oldpatchfile = shift; 33cdf0e10cSrcweirmy $newpatchfile = shift; 34cdf0e10cSrcweirmy $sortedfile = shift; 35cdf0e10cSrcweir 36cdf0e10cSrcweirshow_help() unless defined $oldpatchfile and defined $newpatchfile and defined $sortedfile; 37cdf0e10cSrcweir 38cdf0e10cSrcweirmy %oldpatchfile = parse_patch($oldpatchfile); 39cdf0e10cSrcweirmy %newpatchfile = parse_patch($newpatchfile); 40cdf0e10cSrcweir 41cdf0e10cSrcweiropen SORTEDPATCH, "> $sortedfile"; 42cdf0e10cSrcweir 43cdf0e10cSrcweirforeach my $file (sort (keys %newpatchfile)) { 44cdf0e10cSrcweir print SORTEDPATCH $file."\t"; 45cdf0e10cSrcweir if (defined($oldpatchfile{$file})) { 46cdf0e10cSrcweir if ( (join '', @{$oldpatchfile{$file}{'data'}}) eq (join '', @{$newpatchfile{$file}{'data'}}) ) { 47cdf0e10cSrcweir # patch data for the file hasn't been modified, use the header from 48cdf0e10cSrcweir # the old patch, to reduce noise (keep the old timestamps) 49cdf0e10cSrcweir print SORTEDPATCH $oldpatchfile{$file}{'origtimestamp'}."\n"; 50cdf0e10cSrcweir print SORTEDPATCH $oldpatchfile{$file}{'patchedfilename'}."\t"; 51cdf0e10cSrcweir print SORTEDPATCH $oldpatchfile{$file}{'patchedtimestamp'}."\n"; 52cdf0e10cSrcweir print SORTEDPATCH @{$oldpatchfile{$file}{'data'}}; 53cdf0e10cSrcweir next; 54cdf0e10cSrcweir } 55cdf0e10cSrcweir } 56cdf0e10cSrcweir # either file wasn't patched before, or the patchset changed, so use the new 57cdf0e10cSrcweir # values for it.. 58cdf0e10cSrcweir print SORTEDPATCH $newpatchfile{$file}{'origtimestamp'}."\n"; 59cdf0e10cSrcweir print SORTEDPATCH $newpatchfile{$file}{'patchedfilename'}."\t"; 60cdf0e10cSrcweir print SORTEDPATCH $newpatchfile{$file}{'patchedtimestamp'}."\n"; 61cdf0e10cSrcweir print SORTEDPATCH @{$newpatchfile{$file}{'data'}}; 62cdf0e10cSrcweir} 63cdf0e10cSrcweirclose SORTEDPATCH; 64cdf0e10cSrcweir 65cdf0e10cSrcweir############### 66cdf0e10cSrcweir# Helper subs 67cdf0e10cSrcweir############### 68cdf0e10cSrcweirsub show_help { 69cdf0e10cSrcweir print "Usage: $0 oldpatch newpatch outputfilename\n"; 70cdf0e10cSrcweir print "oldpatch and newpatch can be the very same file\n"; 71cdf0e10cSrcweir print "will output a sanitized form of newpatch to outputfilename\n"; 72cdf0e10cSrcweir print "if outputfilename is '-', the patch will be printed to stdout\n"; 73cdf0e10cSrcweir print "sanitized means: It will avoid all unnecessary changes\n"; 74cdf0e10cSrcweir exit 1; 75cdf0e10cSrcweir} 76cdf0e10cSrcweirsub parse_patch { 77cdf0e10cSrcweir my $patchfile = shift; 78cdf0e10cSrcweir my $patchtype; 79cdf0e10cSrcweir my $pfirst; 80cdf0e10cSrcweir my $psecond; 81cdf0e10cSrcweir 82cdf0e10cSrcweir my %hunks = (); 83cdf0e10cSrcweir my $origfilename; 84cdf0e10cSrcweir open PATCHFILE, "< $patchfile" or die "Cannot open file $patchfile $!"; 85cdf0e10cSrcweir my @patchfile = <PATCHFILE>; 86cdf0e10cSrcweir close PATCHFILE; 87cdf0e10cSrcweir return %hunks if ( $#patchfile == -1 ); 88cdf0e10cSrcweir if ( $patchfile[0] =~ /^---/ ) { 89cdf0e10cSrcweir $patchtype = "unified"; 90cdf0e10cSrcweir $pfirst = '^--- [^\*]*$'; 91cdf0e10cSrcweir $psecond = '^\+\+\+ [^\*]*$'; 92cdf0e10cSrcweir } elsif ( $patchfile[0] =~ /^\*\*\*/ ) { 93cdf0e10cSrcweir $patchtype = "content"; 94cdf0e10cSrcweir $pfirst = '^\*\*\* [^\*]*$'; 95cdf0e10cSrcweir $psecond = '^--- .*\t.*$'; 96cdf0e10cSrcweir } else { 97cdf0e10cSrcweir die "unknown patch format\n"; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir foreach (@patchfile) { 101cdf0e10cSrcweir if ( /$pfirst/ ) { 102cdf0e10cSrcweir my $timestamp; 103cdf0e10cSrcweir # extract the filename, to be able to compare the old 104cdf0e10cSrcweir # with the new file... 105cdf0e10cSrcweir ($origfilename, $timestamp) = split(/\t/, $_, 2); 106cdf0e10cSrcweir chomp $timestamp; 107cdf0e10cSrcweir # ideally convert the timestamp to iso-format... 108cdf0e10cSrcweir $hunks{$origfilename}{'origtimestamp'} = $timestamp; 109cdf0e10cSrcweir next; 110cdf0e10cSrcweir } elsif ( $_ =~ /$psecond/ ) { 111cdf0e10cSrcweir my ($filename, $timestamp) = split(/\t/, $_, 2); 112cdf0e10cSrcweir chomp $timestamp; 113cdf0e10cSrcweir # ideally convert the timestamp to iso-format... 114cdf0e10cSrcweir $hunks{$origfilename}{'patchedfilename'} = $filename; 115cdf0e10cSrcweir $hunks{$origfilename}{'patchedtimestamp'} = $timestamp; 116cdf0e10cSrcweir next; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir push (@{$hunks{$origfilename}{'data'}}, $_); 119cdf0e10cSrcweir 120cdf0e10cSrcweir } 121cdf0e10cSrcweir return %hunks; 122cdf0e10cSrcweir} 123