Skip to content

Commit dae197f

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: make parse_single_patch() return -1 on error
To libify `git apply` functionality we have to signal errors to the caller instead of die()ing. To do that in a compatible manner with the rest of the error handling in builtin/apply.c, parse_single_patch() should return a negative integer instead of calling die(). Let's do that by using error() and let's adjust the related test cases accordingly. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b654b34 commit dae197f

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

builtin/apply.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,10 @@ static int parse_fragment(struct apply_state *state,
16711671
*
16721672
* The (fragment->patch, fragment->size) pair points into the memory given
16731673
* by the caller, not a copy, when we return.
1674+
*
1675+
* Returns:
1676+
* -1 in case of error,
1677+
* the number of bytes in the patch otherwise.
16741678
*/
16751679
static int parse_single_patch(struct apply_state *state,
16761680
const char *line,
@@ -1688,8 +1692,10 @@ static int parse_single_patch(struct apply_state *state,
16881692
fragment = xcalloc(1, sizeof(*fragment));
16891693
fragment->linenr = state->linenr;
16901694
len = parse_fragment(state, line, size, patch, fragment);
1691-
if (len <= 0)
1692-
die(_("corrupt patch at line %d"), state->linenr);
1695+
if (len <= 0) {
1696+
free(fragment);
1697+
return error(_("corrupt patch at line %d"), state->linenr);
1698+
}
16931699
fragment->patch = line;
16941700
fragment->size = len;
16951701
oldlines += fragment->oldlines;
@@ -1725,9 +1731,9 @@ static int parse_single_patch(struct apply_state *state,
17251731
patch->is_delete = 0;
17261732

17271733
if (0 < patch->is_new && oldlines)
1728-
die(_("new file %s depends on old contents"), patch->new_name);
1734+
return error(_("new file %s depends on old contents"), patch->new_name);
17291735
if (0 < patch->is_delete && newlines)
1730-
die(_("deleted file %s still has contents"), patch->old_name);
1736+
return error(_("deleted file %s still has contents"), patch->old_name);
17311737
if (!patch->is_delete && !newlines && context)
17321738
fprintf_ln(stderr,
17331739
_("** warning: "
@@ -2029,6 +2035,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
20292035
size - offset - hdrsize,
20302036
patch);
20312037

2038+
if (patchsize < 0)
2039+
return -128;
2040+
20322041
if (!patchsize) {
20332042
static const char git_binary[] = "GIT binary patch\n";
20342043
int hd = hdrsize + offset;

t/t4012-diff-binary.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
6868
sed -e "s/-CIT/xCIT/" <output >broken &&
6969
test_must_fail git apply --stat --summary broken 2>detected &&
7070
detected=$(cat detected) &&
71-
detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
71+
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
7272
detected=$(sed -ne "${detected}p" broken) &&
7373
test "$detected" = xCIT
7474
'
@@ -77,7 +77,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
7777
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
7878
test_must_fail git apply --stat --summary broken 2>detected &&
7979
detected=$(cat detected) &&
80-
detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
80+
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
8181
detected=$(sed -ne "${detected}p" broken) &&
8282
test "$detected" = xCIT
8383
'

0 commit comments

Comments
 (0)