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