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>
Christian Couder committed
Aug 8, 2016 at 23:03 UTC
5950851e44fc6f19e9fc9261bac4a61e59fc5121
2 files changed
+29
-13
builtin/apply.c
+28
-12
@@ -1419,6 +1419,14 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
1419
return offset;
1420
}
1421
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
+ */
1430
static int find_header(struct apply_state *state,
1431
const char *line,
1432
unsigned long size,
@@ -1452,8 +1460,9 @@ static int find_header(struct apply_state *state,
1460
struct fragment dummy;
1461
if (parse_fragment_header(line, len, &dummy) < 0)
1462
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;
1466
}
1467
1468
if (size < len + 6)
@@ -1468,19 +1477,23 @@ static int find_header(struct apply_state *state,
1477
if (git_hdr_len <= len)
1478
continue;
1479
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
+ }
1489
patch->old_name = xstrdup(patch->def_name);
1490
patch->new_name = xstrdup(patch->def_name);
1491
}
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
+ }
1497
patch->is_toplevel_relative = 1;
1498
*hdrsize = git_hdr_len;
1499
return offset;
@@ -1996,6 +2009,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2009
int hdrsize, patchsize;
2010
int offset = find_header(state, buffer, size, &hdrsize, patch);
2011
2012
+ if (offset == -128)
2013
+ exit(128);
2014
+
2015
if (offset < 0)
2016
return offset;
2017
t/t4254-am-corrupt.sh
+1
-1
@@ -29,7 +29,7 @@ test_expect_success 'try to apply corrupted patch' '
29
'
30
31
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 &&
33
test_path_is_file f &&
34
test_cmp expected actual
35
'