Skip to content

Commit 5950851

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: make find_header() return -128 instead of die()ing
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, let's make find_header() return -128 instead of calling die(). We could make it return -1, unfortunately find_header() already returns -1 when no header is found. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3bee345 commit 5950851

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

builtin/apply.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,14 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
14191419
return offset;
14201420
}
14211421

1422+
/*
1423+
* Find file diff header
1424+
*
1425+
* Returns:
1426+
* -1 if no header was found
1427+
* -128 in case of error
1428+
* the size of the header in bytes (called "offset") otherwise
1429+
*/
14221430
static int find_header(struct apply_state *state,
14231431
const char *line,
14241432
unsigned long size,
@@ -1452,8 +1460,9 @@ static int find_header(struct apply_state *state,
14521460
struct fragment dummy;
14531461
if (parse_fragment_header(line, len, &dummy) < 0)
14541462
continue;
1455-
die(_("patch fragment without header at line %d: %.*s"),
1456-
state->linenr, (int)len-1, line);
1463+
error(_("patch fragment without header at line %d: %.*s"),
1464+
state->linenr, (int)len-1, line);
1465+
return -128;
14571466
}
14581467

14591468
if (size < len + 6)
@@ -1468,19 +1477,23 @@ static int find_header(struct apply_state *state,
14681477
if (git_hdr_len <= len)
14691478
continue;
14701479
if (!patch->old_name && !patch->new_name) {
1471-
if (!patch->def_name)
1472-
die(Q_("git diff header lacks filename information when removing "
1473-
"%d leading pathname component (line %d)",
1474-
"git diff header lacks filename information when removing "
1475-
"%d leading pathname components (line %d)",
1476-
state->p_value),
1477-
state->p_value, state->linenr);
1480+
if (!patch->def_name) {
1481+
error(Q_("git diff header lacks filename information when removing "
1482+
"%d leading pathname component (line %d)",
1483+
"git diff header lacks filename information when removing "
1484+
"%d leading pathname components (line %d)",
1485+
state->p_value),
1486+
state->p_value, state->linenr);
1487+
return -128;
1488+
}
14781489
patch->old_name = xstrdup(patch->def_name);
14791490
patch->new_name = xstrdup(patch->def_name);
14801491
}
1481-
if (!patch->is_delete && !patch->new_name)
1482-
die("git diff header lacks filename information "
1483-
"(line %d)", state->linenr);
1492+
if (!patch->is_delete && !patch->new_name) {
1493+
error("git diff header lacks filename information "
1494+
"(line %d)", state->linenr);
1495+
return -128;
1496+
}
14841497
patch->is_toplevel_relative = 1;
14851498
*hdrsize = git_hdr_len;
14861499
return offset;
@@ -1996,6 +2009,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
19962009
int hdrsize, patchsize;
19972010
int offset = find_header(state, buffer, size, &hdrsize, patch);
19982011

2012+
if (offset == -128)
2013+
exit(128);
2014+
19992015
if (offset < 0)
20002016
return offset;
20012017

t/t4254-am-corrupt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test_expect_success 'try to apply corrupted patch' '
2929
'
3030

3131
test_expect_success 'compare diagnostic; ensure file is still here' '
32-
echo "fatal: git diff header lacks filename information (line 4)" >expected &&
32+
echo "error: git diff header lacks filename information (line 4)" >expected &&
3333
test_path_is_file f &&
3434
test_cmp expected actual
3535
'

0 commit comments

Comments
 (0)