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 <pclouds@gmail.com> 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 dbf1b5fb6a86acafd8294e98b464e2aa370fdde0
1 file changed +21 -19
builtin/apply.c
+21 -19
@@ -827,54 +827,56 @@ static int gitdiff_hdrend(struct apply_state *state,
827 #define DIFF_OLD_NAME 0
828 #define DIFF_NEW_NAME 1
829
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)
835 {
836 if (!*name && !isnull) {
837 *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
838 - return;
838 + return 0;
839 }
840
841 if (*name) {
842 int len = strlen(*name);
843 char *another;
844 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);
847 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) ?
851 _("git apply: bad git-diff - inconsistent new filename on line %d") :
852 _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
853 + }
854 free(another);
855 } else {
856 /* expect "/dev/null" */
857 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);
859 }
860 +
861 + return 0;
862 }
863
864 static int gitdiff_oldname(struct apply_state *state,
865 const char *line,
866 struct patch *patch)
867 {
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);
871 }
872
873 static int gitdiff_newname(struct apply_state *state,
874 const char *line,
875 struct patch *patch)
876 {
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);
880 }
881
882 static int gitdiff_oldmode(struct apply_state *state,