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