зеркало из https://github.com/microsoft/git.git
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>
This commit is contained in:
Родитель
b654b34c1c
Коммит
dae197f753
|
@ -1671,6 +1671,10 @@ static int parse_fragment(struct apply_state *state,
|
||||||
*
|
*
|
||||||
* The (fragment->patch, fragment->size) pair points into the memory given
|
* The (fragment->patch, fragment->size) pair points into the memory given
|
||||||
* by the caller, not a copy, when we return.
|
* by the caller, not a copy, when we return.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* -1 in case of error,
|
||||||
|
* the number of bytes in the patch otherwise.
|
||||||
*/
|
*/
|
||||||
static int parse_single_patch(struct apply_state *state,
|
static int parse_single_patch(struct apply_state *state,
|
||||||
const char *line,
|
const char *line,
|
||||||
|
@ -1688,8 +1692,10 @@ static int parse_single_patch(struct apply_state *state,
|
||||||
fragment = xcalloc(1, sizeof(*fragment));
|
fragment = xcalloc(1, sizeof(*fragment));
|
||||||
fragment->linenr = state->linenr;
|
fragment->linenr = state->linenr;
|
||||||
len = parse_fragment(state, line, size, patch, fragment);
|
len = parse_fragment(state, line, size, patch, fragment);
|
||||||
if (len <= 0)
|
if (len <= 0) {
|
||||||
die(_("corrupt patch at line %d"), state->linenr);
|
free(fragment);
|
||||||
|
return error(_("corrupt patch at line %d"), state->linenr);
|
||||||
|
}
|
||||||
fragment->patch = line;
|
fragment->patch = line;
|
||||||
fragment->size = len;
|
fragment->size = len;
|
||||||
oldlines += fragment->oldlines;
|
oldlines += fragment->oldlines;
|
||||||
|
@ -1725,9 +1731,9 @@ static int parse_single_patch(struct apply_state *state,
|
||||||
patch->is_delete = 0;
|
patch->is_delete = 0;
|
||||||
|
|
||||||
if (0 < patch->is_new && oldlines)
|
if (0 < patch->is_new && oldlines)
|
||||||
die(_("new file %s depends on old contents"), patch->new_name);
|
return error(_("new file %s depends on old contents"), patch->new_name);
|
||||||
if (0 < patch->is_delete && newlines)
|
if (0 < patch->is_delete && newlines)
|
||||||
die(_("deleted file %s still has contents"), patch->old_name);
|
return error(_("deleted file %s still has contents"), patch->old_name);
|
||||||
if (!patch->is_delete && !newlines && context)
|
if (!patch->is_delete && !newlines && context)
|
||||||
fprintf_ln(stderr,
|
fprintf_ln(stderr,
|
||||||
_("** warning: "
|
_("** warning: "
|
||||||
|
@ -2029,6 +2035,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
|
||||||
size - offset - hdrsize,
|
size - offset - hdrsize,
|
||||||
patch);
|
patch);
|
||||||
|
|
||||||
|
if (patchsize < 0)
|
||||||
|
return -128;
|
||||||
|
|
||||||
if (!patchsize) {
|
if (!patchsize) {
|
||||||
static const char git_binary[] = "GIT binary patch\n";
|
static const char git_binary[] = "GIT binary patch\n";
|
||||||
int hd = hdrsize + offset;
|
int hd = hdrsize + offset;
|
||||||
|
|
|
@ -68,7 +68,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
|
||||||
sed -e "s/-CIT/xCIT/" <output >broken &&
|
sed -e "s/-CIT/xCIT/" <output >broken &&
|
||||||
test_must_fail git apply --stat --summary broken 2>detected &&
|
test_must_fail git apply --stat --summary broken 2>detected &&
|
||||||
detected=$(cat detected) &&
|
detected=$(cat detected) &&
|
||||||
detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
|
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
|
||||||
detected=$(sed -ne "${detected}p" broken) &&
|
detected=$(sed -ne "${detected}p" broken) &&
|
||||||
test "$detected" = xCIT
|
test "$detected" = xCIT
|
||||||
'
|
'
|
||||||
|
@ -77,7 +77,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
|
||||||
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
|
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
|
||||||
test_must_fail git apply --stat --summary broken 2>detected &&
|
test_must_fail git apply --stat --summary broken 2>detected &&
|
||||||
detected=$(cat detected) &&
|
detected=$(cat detected) &&
|
||||||
detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
|
detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
|
||||||
detected=$(sed -ne "${detected}p" broken) &&
|
detected=$(sed -ne "${detected}p" broken) &&
|
||||||
test "$detected" = xCIT
|
test "$detected" = xCIT
|
||||||
'
|
'
|
||||||
|
|
Загрузка…
Ссылка в новой задаче