2 eval 'exec perl -wS $0 ${1+"$@"}'
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
26 # command line arguments
27 my $oldpatchfile = shift;
28 my $newpatchfile = shift;
29 my $sortedfile = shift;
31 show_help
() unless defined $oldpatchfile and defined $newpatchfile and defined $sortedfile;
33 my %oldpatchfile = parse_patch
($oldpatchfile);
34 my %newpatchfile = parse_patch
($newpatchfile);
36 open SORTEDPATCH
, "> $sortedfile";
38 foreach my $file (sort (keys %newpatchfile)) {
39 print SORTEDPATCH
$file."\t";
40 if (defined($oldpatchfile{$file})) {
41 if ( (join '', @
{$oldpatchfile{$file}{'data'}}) eq (join '', @
{$newpatchfile{$file}{'data'}}) ) {
42 # patch data for the file hasn't been modified, use the header from
43 # the old patch, to reduce noise (keep the old timestamps)
44 print SORTEDPATCH
$oldpatchfile{$file}{'origtimestamp'}."\n";
45 print SORTEDPATCH
$oldpatchfile{$file}{'patchedfilename'}."\t";
46 print SORTEDPATCH
$oldpatchfile{$file}{'patchedtimestamp'}."\n";
47 print SORTEDPATCH @
{$oldpatchfile{$file}{'data'}};
51 # either file wasn't patched before, or the patchset changed, so use the new
53 print SORTEDPATCH
$newpatchfile{$file}{'origtimestamp'}."\n";
54 print SORTEDPATCH
$newpatchfile{$file}{'patchedfilename'}."\t";
55 print SORTEDPATCH
$newpatchfile{$file}{'patchedtimestamp'}."\n";
56 print SORTEDPATCH @
{$newpatchfile{$file}{'data'}};
64 print "Usage: $0 oldpatch newpatch outputfilename\n";
65 print "oldpatch and newpatch can be the very same file\n";
66 print "will output a sanitized form of newpatch to outputfilename\n";
67 print "if outputfilename is '-', the patch will be printed to stdout\n";
68 print "sanitized means: It will avoid all unnecessary changes\n";
72 my $patchfile = shift;
79 open PATCHFILE
, "< $patchfile" or die "Cannot open file $patchfile $!";
80 my @patchfile = <PATCHFILE
>;
82 return %hunks if ( $#patchfile == -1 );
83 if ( $patchfile[0] =~ /^---/ ) {
84 $patchtype = "unified";
85 $pfirst = '^--- [^\*]*$';
86 $psecond = '^\+\+\+ [^\*]*$';
87 } elsif ( $patchfile[0] =~ /^\*\*\*/ ) {
88 $patchtype = "content";
89 $pfirst = '^\*\*\* [^\*]*$';
90 $psecond = '^--- .*\t.*$';
92 die "unknown patch format\n";
95 foreach (@patchfile) {
98 # extract the filename, to be able to compare the old
99 # with the new file...
100 ($origfilename, $timestamp) = split(/\t/, $_, 2);
102 # ideally convert the timestamp to iso-format...
103 $hunks{$origfilename}{'origtimestamp'} = $timestamp;
105 } elsif ( $_ =~ /$psecond/ ) {
106 my ($filename, $timestamp) = split(/\t/, $_, 2);
108 # ideally convert the timestamp to iso-format...
109 $hunks{$origfilename}{'patchedfilename'} = $filename;
110 $hunks{$origfilename}{'patchedtimestamp'} = $timestamp;
113 push (@
{$hunks{$origfilename}{'data'}}, $_);