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