Skip to content

Commit 0ee6b03

Browse files
authored
patch: raise error for incompatible format options
* The Getopt library makes the original order of options invisible, i.e. we can't tell if -e comes before or after -c * By default, patch attempts to guess the type of the input diff * Options -c, -e, -n & -u direct the program to look for a certain diff format * Specifying -c and -e results in undefined behaviour in this version so raise an error * In GNU patch, the order of -c and -e matters, i.e. -e will win if it appears after -c * type_conflict() function will print the long-option names of the conflicting diff types
1 parent 26ba18f commit 0ee6b03

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

bin/patch

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use constant EX_SUCCESS => 0;
2424
use constant EX_REJECTS => 1;
2525
use constant EX_FAILURE => 2;
2626

27-
my $VERSION = '0.32';
27+
my $VERSION = '0.33';
2828

2929
$|++;
3030

@@ -40,6 +40,20 @@ sub version {
4040
exit EX_SUCCESS;
4141
}
4242
43+
sub type_conflict {
44+
my $opts = shift;
45+
my @types;
46+
my $i = 0;
47+
for (qw(context ed normal unified)) {
48+
push @types, '--' . $_ if $opts->{$_};
49+
}
50+
if (scalar(@types) > 1) {
51+
warn "$0: incompatible input type specifiers: @types\n";
52+
exit EX_FAILURE;
53+
}
54+
return;
55+
}
56+
4357
my ($patchfile, @options);
4458

4559
if (@ARGV) {
@@ -76,7 +90,9 @@ if (@ARGV) {
7690
my $next = 0;
7791
for (@options) {
7892
local @ARGV = @$_;
79-
Getopt::Long::GetOptions(\my %opts, @desc) or die("Bad options\n");
93+
my %opts;
94+
Getopt::Long::GetOptions(\%opts, @desc) or die("Bad options\n");
95+
type_conflict(\%opts);
8096
$opts{origfile} = shift;
8197
$_ = \%opts;
8298
$patchfile = shift unless $next++;

0 commit comments

Comments
 (0)