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 <artem.bityutskiy@linux.intel.com> Signed-off-by: Brandon Casey <drafnel@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Dec 3, 2011 at 14:35 UTC cc64b318f26c9e176c4f07b1a459a86e7a04c4eb
2 files changed +24 -5
builtin/apply.c
+5 -5
@@ -3590,15 +3590,12 @@ static int write_out_one_reject(struct patch *patch)
3590 return -1;
3591 }
3592
3593 -static int write_out_results(struct patch *list, int skipped_patch)
3593 +static int write_out_results(struct patch *list)
3594 {
3595 int phase;
3596 int errs = 0;
3597 struct patch *l;
3598
3599 - if (!list && !skipped_patch)
3600 - return error("No changes");
3601 -
3599 for (phase = 0; phase < 2; phase++) {
3600 l = list;
3601 while (l) {
@@ -3724,6 +3721,9 @@ static int apply_patch(int fd, const char *filename, int options)
3721 offset += nr;
3722 }
3723
3724 + if (!list && !skipped_patch)
3725 + die("unrecognized input");
3726 +
3727 if (whitespace_error && (ws_error_action == die_on_ws_error))
3728 apply = 0;
3729
@@ -3741,7 +3741,7 @@ static int apply_patch(int fd, const char *filename, int options)
3741 !apply_with_reject)
3742 exit(1);
3743
3744 - if (apply && write_out_results(list, skipped_patch))
3744 + if (apply && write_out_results(list))
3745 exit(1);
3746
3747 if (fake_ancestor)
t/t4136-apply-check.sh new
+19
@@ -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