Skip to content

Commit dbf1b5f

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: make gitdiff_*() 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", gitdiff_*() functions should return -1 instead of calling die(). A previous patch made it possible for gitdiff_*() functions to return -1 in case of error. Let's take advantage of that to make gitdiff_verify_name() return -1 on error, and to have gitdiff_oldname() and gitdiff_newname() directly return what gitdiff_verify_name() returns. Helped-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 70af766 commit dbf1b5f

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

builtin/apply.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -827,54 +827,56 @@ static int gitdiff_hdrend(struct apply_state *state,
827827
#define DIFF_OLD_NAME 0
828828
#define DIFF_NEW_NAME 1
829829

830-
static void gitdiff_verify_name(struct apply_state *state,
831-
const char *line,
832-
int isnull,
833-
char **name,
834-
int side)
830+
static int gitdiff_verify_name(struct apply_state *state,
831+
const char *line,
832+
int isnull,
833+
char **name,
834+
int side)
835835
{
836836
if (!*name && !isnull) {
837837
*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
838-
return;
838+
return 0;
839839
}
840840

841841
if (*name) {
842842
int len = strlen(*name);
843843
char *another;
844844
if (isnull)
845-
die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
846-
*name, state->linenr);
845+
return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
846+
*name, state->linenr);
847847
another = find_name(state, line, NULL, state->p_value, TERM_TAB);
848-
if (!another || memcmp(another, *name, len + 1))
849-
die((side == DIFF_NEW_NAME) ?
848+
if (!another || memcmp(another, *name, len + 1)) {
849+
free(another);
850+
return error((side == DIFF_NEW_NAME) ?
850851
_("git apply: bad git-diff - inconsistent new filename on line %d") :
851852
_("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
853+
}
852854
free(another);
853855
} else {
854856
/* expect "/dev/null" */
855857
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
856-
die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
858+
return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
857859
}
860+
861+
return 0;
858862
}
859863

860864
static int gitdiff_oldname(struct apply_state *state,
861865
const char *line,
862866
struct patch *patch)
863867
{
864-
gitdiff_verify_name(state, line,
865-
patch->is_new, &patch->old_name,
866-
DIFF_OLD_NAME);
867-
return 0;
868+
return gitdiff_verify_name(state, line,
869+
patch->is_new, &patch->old_name,
870+
DIFF_OLD_NAME);
868871
}
869872

870873
static int gitdiff_newname(struct apply_state *state,
871874
const char *line,
872875
struct patch *patch)
873876
{
874-
gitdiff_verify_name(state, line,
875-
patch->is_delete, &patch->new_name,
876-
DIFF_NEW_NAME);
877-
return 0;
877+
return gitdiff_verify_name(state, line,
878+
patch->is_delete, &patch->new_name,
879+
DIFF_NEW_NAME);
878880
}
879881

880882
static int gitdiff_oldmode(struct apply_state *state,

0 commit comments

Comments
 (0)