Skip to content

Commit cc64b31

Browse files
drafnelgitster
authored andcommitted
builtin/apply.c: report error on failure to recognize input
When git apply is passed something that is not a patch, it does not produce an error message or exit with a non-zero status if it was not actually "applying" the patch i.e. --check or --numstat etc were supplied on the command line. Fix this by producing an error when apply fails to find any hunks whatsoever while parsing the patch. This will cause some of the output formats (--numstat, --diffstat, etc) to produce an error when they formerly would have reported zero changes and exited successfully. That seems like the correct behavior though. Failure to recognize the input as a patch should be an error. Plus, add a test. Reported-by: Artem Bityutskiy <[email protected]> Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 590a472 commit cc64b31

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

builtin/apply.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,15 +3590,12 @@ static int write_out_one_reject(struct patch *patch)
35903590
return -1;
35913591
}
35923592

3593-
static int write_out_results(struct patch *list, int skipped_patch)
3593+
static int write_out_results(struct patch *list)
35943594
{
35953595
int phase;
35963596
int errs = 0;
35973597
struct patch *l;
35983598

3599-
if (!list && !skipped_patch)
3600-
return error("No changes");
3601-
36023599
for (phase = 0; phase < 2; phase++) {
36033600
l = list;
36043601
while (l) {
@@ -3724,6 +3721,9 @@ static int apply_patch(int fd, const char *filename, int options)
37243721
offset += nr;
37253722
}
37263723

3724+
if (!list && !skipped_patch)
3725+
die("unrecognized input");
3726+
37273727
if (whitespace_error && (ws_error_action == die_on_ws_error))
37283728
apply = 0;
37293729

@@ -3741,7 +3741,7 @@ static int apply_patch(int fd, const char *filename, int options)
37413741
!apply_with_reject)
37423742
exit(1);
37433743

3744-
if (apply && write_out_results(list, skipped_patch))
3744+
if (apply && write_out_results(list))
37453745
exit(1);
37463746

37473747
if (fake_ancestor)

t/t4136-apply-check.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/sh
2+
3+
test_description='git apply should exit non-zero with unrecognized input.'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
test_commit 1
9+
'
10+
11+
test_expect_success 'apply --check exits non-zero with unrecognized input' '
12+
test_must_fail git apply --check - <<-\EOF
13+
I am not a patch
14+
I look nothing like a patch
15+
git apply must fail
16+
EOF
17+
'
18+
19+
test_done

0 commit comments

Comments
 (0)