Update ooo320-m1
[ooovba.git] / solenv / bin / patch_sanitizer.pl
blobd07745ab6efcf2ff8d3630795314fbe83912a175
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 #
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: patch_sanitizer.pl,v $
14 # $Revision: 1.6 $
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 #*************************************************************************
35 use utf8;
36 use warnings;
37 use strict;
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'}};
61 next;
64 # either file wasn't patched before, or the patchset changed, so use the new
65 # values for it..
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'}};
71 close SORTEDPATCH;
73 ###############
74 # Helper subs
75 ###############
76 sub show_help {
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";
82 exit 1;
84 sub parse_patch {
85 my $patchfile = shift;
86 my $patchtype;
87 my $pfirst;
88 my $psecond;
90 my %hunks = ();
91 my $origfilename;
92 open PATCHFILE, "< $patchfile" or die "Cannot open file $patchfile $!";
93 my @patchfile = <PATCHFILE>;
94 close 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.*$';
104 } else {
105 die "unknown patch format\n";
108 foreach (@patchfile) {
109 if ( /$pfirst/ ) {
110 my $timestamp;
111 # extract the filename, to be able to compare the old
112 # with the new file...
113 ($origfilename, $timestamp) = split(/\t/, $_, 2);
114 chomp $timestamp;
115 # ideally convert the timestamp to iso-format...
116 $hunks{$origfilename}{'origtimestamp'} = $timestamp;
117 next;
118 } elsif ( $_ =~ /$psecond/ ) {
119 my ($filename, $timestamp) = split(/\t/, $_, 2);
120 chomp $timestamp;
121 # ideally convert the timestamp to iso-format...
122 $hunks{$origfilename}{'patchedfilename'} = $filename;
123 $hunks{$origfilename}{'patchedtimestamp'} = $timestamp;
124 next;
126 push (@{$hunks{$origfilename}{'data'}}, $_);
129 return %hunks;