2 eval 'exec perl -wS $0 ${1+"$@"}'
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: patch_sanitizer.pl,v $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
39 # command line arguments
40 my $oldpatchfile = shift;
41 my $newpatchfile = shift;
42 my $sortedfile = shift;
44 show_help
() unless defined $oldpatchfile and defined $newpatchfile and defined $sortedfile;
46 my %oldpatchfile = parse_patch
($oldpatchfile);
47 my %newpatchfile = parse_patch
($newpatchfile);
49 open SORTEDPATCH
, "> $sortedfile";
51 foreach my $file (sort (keys %newpatchfile)) {
52 print SORTEDPATCH
$file."\t";
53 if (defined($oldpatchfile{$file})) {
54 if ( (join '', @
{$oldpatchfile{$file}{'data'}}) eq (join '', @
{$newpatchfile{$file}{'data'}}) ) {
55 # patch data for the file hasn't been modified, use the header from
56 # the old patch, to reduce noise (keep the old timestamps)
57 print SORTEDPATCH
$oldpatchfile{$file}{'origtimestamp'}."\n";
58 print SORTEDPATCH
$oldpatchfile{$file}{'patchedfilename'}."\t";
59 print SORTEDPATCH
$oldpatchfile{$file}{'patchedtimestamp'}."\n";
60 print SORTEDPATCH @
{$oldpatchfile{$file}{'data'}};
64 # either file wasn't patched before, or the patchset changed, so use the new
66 print SORTEDPATCH
$newpatchfile{$file}{'origtimestamp'}."\n";
67 print SORTEDPATCH
$newpatchfile{$file}{'patchedfilename'}."\t";
68 print SORTEDPATCH
$newpatchfile{$file}{'patchedtimestamp'}."\n";
69 print SORTEDPATCH @
{$newpatchfile{$file}{'data'}};
77 print "Usage: $0 oldpatch newpatch outputfilename\n";
78 print "oldpatch and newpatch can be the very same file\n";
79 print "will output a sanitized form of newpatch to outputfilename\n";
80 print "if outputfilename is '-', the patch will be printed to stdout\n";
81 print "sanitized means: It will avoid all unnecessary changes\n";
85 my $patchfile = shift;
92 open PATCHFILE
, "< $patchfile" or die "Cannot open file $patchfile $!";
93 my @patchfile = <PATCHFILE
>;
95 return %hunks if ( $#patchfile == -1 );
96 if ( $patchfile[0] =~ /^---/ ) {
97 $patchtype = "unified";
98 $pfirst = '^--- [^\*]*$';
99 $psecond = '^\+\+\+ [^\*]*$';
100 } elsif ( $patchfile[0] =~ /^\*\*\*/ ) {
101 $patchtype = "content";
102 $pfirst = '^\*\*\* [^\*]*$';
103 $psecond = '^--- .*\t.*$';
105 die "unknown patch format\n";
108 foreach (@patchfile) {
111 # extract the filename, to be able to compare the old
112 # with the new file...
113 ($origfilename, $timestamp) = split(/\t/, $_, 2);
115 # ideally convert the timestamp to iso-format...
116 $hunks{$origfilename}{'origtimestamp'} = $timestamp;
118 } elsif ( $_ =~ /$psecond/ ) {
119 my ($filename, $timestamp) = split(/\t/, $_, 2);
121 # ideally convert the timestamp to iso-format...
122 $hunks{$origfilename}{'patchedfilename'} = $filename;
123 $hunks{$origfilename}{'patchedtimestamp'} = $timestamp;
126 push (@
{$hunks{$origfilename}{'data'}}, $_);