Home Home > GIT Browse
summaryrefslogtreecommitdiff
blob: 325631282f861d43ff0736d279f042d9c602df87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl

#############################################################################
# Copyright (c) 2015 Micro Focus
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#############################################################################

# Check that the a patch is in the format produced by scripts/refresh_patch.sh

use strict;
use warnings;

my $errors = 0;
my $git_renames = 0;
my %reported;
our $file;

sub err {
	return if ($reported{"$file:$_[0]"}++);
	print STDERR "$file:$.: error: @_\n";
	$errors = 1;
}

sub check_filename {
	my $filename = shift;

	if ($filename !~ m:^(/dev/null\b|[ab]/):) {
		err("Patch not in -p ab format");
	}
	if ($filename =~ /\s\S/) {
		err("Timestamp after filename");
	}
	if ($filename =~ /\/\//) {
		err("Double slash in filename");
	}
}

sub do_patch {
	my $fh = shift;
	my $fn = shift;
	my ($in_payload, $in_hunk, $maybe_eof, $last_noncontext);
	my $xen_prefix = "patches.xen/";
	my $is_xen = $xen_prefix eq substr($fn,0,length($xen_prefix));

	my @lines = <$fh>;
	chomp(@lines);
	for (my $i = 1; $i < scalar(@lines); $i++) {
		my $cur = $lines[$i];
		$. = $i + 1;
		if ($cur =~ /^similarity index \d+%/ &&
					$lines[$i-1] =~ /^diff --git/) {
			$in_payload = 1;
			$in_hunk = 0;
			$maybe_eof = 0;
			err("git diff -M patches fail with sequences-patch.sh --fast");
			$git_renames = 1;
		}
		if ($cur =~ /^\+\+\+ / && $lines[$i-1] =~ /^--- /) {
			$in_payload = 1;
			$in_hunk = 0;
			$maybe_eof = 0;
			(my $new = $cur) =~ s/^\+\+\+ //;
			(my $old = $lines[$i-1]) =~ s/^--- //;
			if (!$is_xen) { check_filename($new); }
			if (!$is_xen) { check_filename($old); }
			if (!$is_xen && $i > 2 && $lines[$i-2] =~ /^={20}/ &&
						$lines[$i-3] =~ /^Index: /) {
				err("Superfluous Index: line in patch");
			}
			next;
		}
		next unless $in_payload;
		if ($cur =~ /^(diff |Index: |--- |\\ No newline at end of file)/) {
			$in_hunk = 0;
			$maybe_eof = 0;
			next;
		} elsif ($cur =~ /^@@ /) {
			$in_hunk = 1;
			$last_noncontext = $i;
			$maybe_eof = 0;
			next;
		}
		next unless $in_hunk;
		# "-- " can be used as signature delimiter and is sometimes
		# written as "--"
		if ($cur =~ /^-- ?$/) {
			$maybe_eof = 1;
			next;
		}
		if ($cur =~ /^[-+]/) {
			$last_noncontext = $i;
			next;
		}
		# Blank lines seem to be OK for git am
		if ($cur =~ /^$|^ /) {
			next;
		}
		# Try to ignore junk at the end of patch files
		if ($i - $last_noncontext <= 3 && !$maybe_eof) {
			err("Malformed patch (missing leading space in context line?)");
		}
	}
}

if (!@ARGV) {
	die "Usage: $0 <patch>...\n";
}
if ($ARGV[0] eq "--stdin") {
	shift;
	$file = "<stdin>";
	if (scalar(@ARGV)) {
		$file = $ARGV[0];
	}
	open(my $fh, '-');
	do_patch($fh, $file);
	close($fh);
} else {
	for $file (@ARGV) {
		open(my $fh, '<', $file) or die "$file: $!\n";
		do_patch($fh, $file);
		close($fh);
	}
}

if ($errors) {
	printf STDERR ("Please refresh the patch%s using scripts/refresh_patch.sh\n", (scalar(@ARGV) > 1 ? "es" : ""));
	if ($git_renames) {
		print STDERR "Use --no-renames when generating patches from git\n";
	}
	exit(1);
}
exit(0);