xref: /trunk/main/solenv/bin/patch_sanitizer.pl (revision cdf0e10c)
1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30
31use utf8;
32use warnings;
33use strict;
34
35# command line arguments
36my $oldpatchfile = shift;
37my $newpatchfile = shift;
38my $sortedfile = shift;
39
40show_help() unless defined $oldpatchfile and defined $newpatchfile and defined $sortedfile;
41
42my %oldpatchfile = parse_patch($oldpatchfile);
43my %newpatchfile = parse_patch($newpatchfile);
44
45open SORTEDPATCH, "> $sortedfile";
46
47foreach my $file (sort (keys %newpatchfile)) {
48	print SORTEDPATCH $file."\t";
49	if (defined($oldpatchfile{$file})) {
50		if ( (join '', @{$oldpatchfile{$file}{'data'}}) eq (join '', @{$newpatchfile{$file}{'data'}}) ) {
51			# patch data for the file hasn't been modified, use the header from
52			# the old patch, to reduce noise (keep the old timestamps)
53			print SORTEDPATCH $oldpatchfile{$file}{'origtimestamp'}."\n";
54			print SORTEDPATCH $oldpatchfile{$file}{'patchedfilename'}."\t";
55			print SORTEDPATCH $oldpatchfile{$file}{'patchedtimestamp'}."\n";
56			print SORTEDPATCH @{$oldpatchfile{$file}{'data'}};
57			next;
58		}
59	}
60	# either file wasn't patched before, or the patchset changed, so use the new
61	# values for it..
62	print SORTEDPATCH $newpatchfile{$file}{'origtimestamp'}."\n";
63	print SORTEDPATCH $newpatchfile{$file}{'patchedfilename'}."\t";
64	print SORTEDPATCH $newpatchfile{$file}{'patchedtimestamp'}."\n";
65	print SORTEDPATCH @{$newpatchfile{$file}{'data'}};
66}
67close SORTEDPATCH;
68
69###############
70# Helper subs
71###############
72sub show_help {
73	print "Usage: $0 oldpatch newpatch outputfilename\n";
74	print "oldpatch and newpatch can be the very same file\n";
75	print "will output a sanitized form of newpatch to outputfilename\n";
76	print "if outputfilename is '-', the patch will be printed to stdout\n";
77	print "sanitized means: It will avoid all unnecessary changes\n";
78	exit 1;
79}
80sub parse_patch {
81	my $patchfile = shift;
82	my $patchtype;
83	my $pfirst;
84	my $psecond;
85
86	my %hunks = ();
87	my $origfilename;
88	open PATCHFILE, "< $patchfile" or die "Cannot open file $patchfile $!";
89	my @patchfile = <PATCHFILE>;
90	close PATCHFILE;
91	return %hunks if ( $#patchfile == -1 );
92	if ( $patchfile[0] =~ /^---/ ) {
93		$patchtype = "unified";
94		$pfirst = '^--- [^\*]*$';
95		$psecond = '^\+\+\+ [^\*]*$';
96	} elsif ( $patchfile[0] =~ /^\*\*\*/ ) {
97		$patchtype = "content";
98		$pfirst = '^\*\*\* [^\*]*$';
99		$psecond = '^--- .*\t.*$';
100	} else {
101		die "unknown patch format\n";
102	}
103
104	foreach (@patchfile) {
105		if ( /$pfirst/ ) {
106			my $timestamp;
107			# extract the filename, to be able to compare the old
108			# with the new file...
109			($origfilename, $timestamp) = split(/\t/, $_, 2);
110			chomp $timestamp;
111			# ideally convert the timestamp to iso-format...
112			$hunks{$origfilename}{'origtimestamp'} = $timestamp;
113			next;
114		} elsif ( $_ =~ /$psecond/ ) {
115			my ($filename, $timestamp) = split(/\t/, $_, 2);
116			chomp $timestamp;
117			# ideally convert the timestamp to iso-format...
118			$hunks{$origfilename}{'patchedfilename'} = $filename;
119			$hunks{$origfilename}{'patchedtimestamp'} = $timestamp;
120			next;
121		}
122		push (@{$hunks{$origfilename}{'data'}}, $_);
123
124	}
125	return %hunks;
126}
127