Skip to content

Commit 6ee9759

Browse files
authored
patch: standard exit code (#779)
* Patch::error() returns $ERROR * In Patch::print_rejects(), $ERROR is incremented if rejected pieces are found during processing * No other function updates $ERROR, so Patch::error() indicates if rejects are present * die() cals are wrapped by an END-block which sets exit code to 1; follow standard and set it to 2 [1] * Failure to detect a diff in the input is considered an error; call die() to indicate this * test1: "perl patch -x" ---> bad option, exit(2) * test2: "echo "BOGUS-DIFF-123" > bogus.diff && perl patch xargs bogus.diff" ---> no diff found, exit(2) * test3: "perl patch dirname sort.diff" ---> mismatched diff which can't be applied, exit(1) * test4: "perl patch a.c correct.diff" ---> diff applies with no failed parts, exit(0) 1. https://pubs.opengroup.org/onlinepubs/007904975/utilities/patch.html
1 parent 4fe8aad commit 6ee9759

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

bin/patch

+15-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ use strict;
2020

2121
use File::Temp qw();
2222

23-
my $VERSION = '0.28';
23+
use constant EX_SUCCESS => 0;
24+
use constant EX_REJECTS => 1;
25+
use constant EX_FAILURE => 2;
26+
27+
my $VERSION = '0.29';
2428

2529
$|++;
2630

@@ -33,7 +37,7 @@ sub version {
3337
You may play with this software in accordance with the
3438
Perl Artistic License.
3539
];
36-
exit;
40+
exit EX_SUCCESS;
3741
}
3842
3943
my ($patchfile, @options);
@@ -252,17 +256,17 @@ while (<PATCH>) {
252256
close PATCH;
253257

254258
if (ref $patch eq 'Patch') {
255-
$patch->note("Hmm... I can't seem to find a patch in there anywhere.\n");
259+
die "Hmm... I can't seem to find a patch in there anywhere.\n";
256260
} else {
257261
$patch->end;
258262
}
259263

260264
$patch->note("done\n");
261-
exit($patch->error ? 1 : 0);
265+
exit($patch->error ? EX_REJECTS : EX_SUCCESS);
262266

263267
END {
264268
close STDOUT || die "$0: can't close stdout: $!\n";
265-
$? = 1 if $? == 255; # from die
269+
$? = EX_FAILURE if $? == 255; # from die
266270
}
267271

268272

@@ -1512,10 +1516,12 @@ text in the patch file and that I<patch> is attempting to
15121516
intuit whether there is a patch in that text and, if so,
15131517
what kind of patch it is.
15141518
1515-
I<Patch> will exit with a non-zero status if any reject files
1516-
were created. When applying a set of patches in a loop it
1517-
behooves you to check this exit status so you don't apply
1518-
a later patch to a partially patched file.
1519+
=head1 EXIT STATUS
1520+
1521+
The patch utility exits with status of 0 if the input diff
1522+
was applied successfully. If part of the input was rejected
1523+
an exit status of 1 is used. Exit status 2 indicates an error
1524+
occurred.
15191525
15201526
=head1 CAVEATS
15211527

0 commit comments

Comments
 (0)