bump product version to 4.1.6.2
[LibreOffice.git] / solenv / bin / patch_sanitizer.pl
blob81c83ce4ad72c4d4db9221638e77237c3fe42546
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
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 .
22 use utf8;
23 use warnings;
24 use strict;
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'}};
48 next;
51 # either file wasn't patched before, or the patchset changed, so use the new
52 # values for it..
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'}};
58 close SORTEDPATCH;
60 ###############
61 # Helper subs
62 ###############
63 sub show_help {
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";
69 exit 1;
71 sub parse_patch {
72 my $patchfile = shift;
73 my $patchtype;
74 my $pfirst;
75 my $psecond;
77 my %hunks = ();
78 my $origfilename;
79 open PATCHFILE, "< $patchfile" or die "Cannot open file $patchfile $!";
80 my @patchfile = <PATCHFILE>;
81 close 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.*$';
91 } else {
92 die "unknown patch format\n";
95 foreach (@patchfile) {
96 if ( /$pfirst/ ) {
97 my $timestamp;
98 # extract the filename, to be able to compare the old
99 # with the new file...
100 ($origfilename, $timestamp) = split(/\t/, $_, 2);
101 chomp $timestamp;
102 # ideally convert the timestamp to iso-format...
103 $hunks{$origfilename}{'origtimestamp'} = $timestamp;
104 next;
105 } elsif ( $_ =~ /$psecond/ ) {
106 my ($filename, $timestamp) = split(/\t/, $_, 2);
107 chomp $timestamp;
108 # ideally convert the timestamp to iso-format...
109 $hunks{$origfilename}{'patchedfilename'} = $filename;
110 $hunks{$origfilename}{'patchedtimestamp'} = $timestamp;
111 next;
113 push (@{$hunks{$origfilename}{'data'}}, $_);
116 return %hunks;