apply: refactor "previous patch" logic

The code to grab the result of application of a previous patch in the input was mixed with error message generation for a case where a later patch tries to modify contents of a path that has been removed. The same code is duplicated elsewhere in the code. Introduce a helper to clarify what is going on. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 16, 2012 at 14:03 UTC ccf998b297c6d04053eaa9d61e9cb4187bccd0a9
1 file changed +56 -26
builtin/apply.c
+56 -26
@@ -3046,22 +3046,50 @@ static int checkout_target(struct cache_entry *ce, struct stat *st)
3046 return 0;
3047 }
3048
3049 +static struct patch *previous_patch(struct patch *patch, int *gone)
3050 +{
3051 + struct patch *previous;
3052 +
3053 + *gone = 0;
3054 + if (patch->is_copy || patch->is_rename)
3055 + return NULL; /* "git" patches do not depend on the order */
3056 +
3057 + previous = in_fn_table(patch->old_name);
3058 + if (!previous)
3059 + return NULL;
3060 +
3061 + if (to_be_deleted(previous))
3062 + return NULL; /* the deletion hasn't happened yet */
3063 +
3064 + if (was_deleted(previous))
3065 + *gone = 1;
3066 +
3067 + return previous;
3068 +}
3069 +
3070 +/*
3071 + * We are about to apply "patch"; populate the "image" with the
3072 + * current version we have, from the working tree or from the index,
3073 + * depending on the situation e.g. --cached/--index. If we are
3074 + * applying a non-git patch that incrementally updates the tree,
3075 + * we read from the result of a previous diff.
3076 + */
3077 static int load_preimage(struct image *image,
3078 struct patch *patch, struct stat *st, struct cache_entry *ce)
3079 {
3080 struct strbuf buf = STRBUF_INIT;
3081 size_t len;
3082 char *img;
3055 - struct patch *tpatch;
3083 + struct patch *previous;
3084 + int status;
3085
3057 - if (!(patch->is_copy || patch->is_rename) &&
3058 - (tpatch = in_fn_table(patch->old_name)) != NULL && !to_be_deleted(tpatch)) {
3059 - if (was_deleted(tpatch)) {
3060 - return error(_("patch %s has been renamed/deleted"),
3061 - patch->old_name);
3062 - }
3086 + previous = previous_patch(patch, &status);
3087 + if (status)
3088 + return error(_("path %s has been renamed/deleted"),
3089 + patch->old_name);
3090 + if (previous) {
3091 /* We have a patched copy in memory; use that. */
3064 - strbuf_add(&buf, tpatch->result, tpatch->resultsize);
3092 + strbuf_add(&buf, previous->result, previous->resultsize);
3093 } else if (cached) {
3094 if (read_file_or_gitlink(ce, &buf))
3095 return error(_("read of %s failed"), patch->old_name);
@@ -3143,39 +3171,41 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
3171 return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
3172 }
3173
3174 +/*
3175 + * If "patch" that we are looking at modifies or deletes what we have,
3176 + * we would want it not to lose any local modification we have, either
3177 + * in the working tree or in the index.
3178 + *
3179 + * This also decides if a non-git patch is a creation patch or a
3180 + * modification to an existing empty file. We do not check the state
3181 + * of the current tree for a creation patch in this function; the caller
3182 + * check_patch() separately makes sure (and errors out otherwise) that
3183 + * the path the patch creates does not exist in the current tree.
3184 + */
3185 static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
3186 {
3187 const char *old_name = patch->old_name;
3149 - struct patch *tpatch = NULL;
3150 - int stat_ret = 0;
3188 + struct patch *previous = NULL;
3189 + int stat_ret = 0, status;
3190 unsigned st_mode = 0;
3191
3153 - /*
3154 - * Make sure that we do not have local modifications from the
3155 - * index when we are looking at the index. Also make sure
3156 - * we have the preimage file to be patched in the work tree,
3157 - * unless --cached, which tells git to apply only in the index.
3158 - */
3192 if (!old_name)
3193 return 0;
3194
3195 assert(patch->is_new <= 0);
3196 + previous = previous_patch(patch, &status);
3197
3164 - if (!(patch->is_copy || patch->is_rename) &&
3165 - (tpatch = in_fn_table(old_name)) != NULL && !to_be_deleted(tpatch)) {
3166 - if (was_deleted(tpatch))
3167 - return error(_("%s: has been deleted/renamed"), old_name);
3168 - st_mode = tpatch->new_mode;
3198 + if (status)
3199 + return error(_("path %s has been renamed/deleted"), old_name);
3200 + if (previous) {
3201 + st_mode = previous->new_mode;
3202 } else if (!cached) {
3203 stat_ret = lstat(old_name, st);
3204 if (stat_ret && errno != ENOENT)
3205 return error(_("%s: %s"), old_name, strerror(errno));
3206 }
3207
3175 - if (to_be_deleted(tpatch))
3176 - tpatch = NULL;
3177 -
3178 - if (check_index && !tpatch) {
3208 + if (check_index && !previous) {
3209 int pos = cache_name_pos(old_name, strlen(old_name));
3210 if (pos < 0) {
3211 if (patch->is_new < 0)
@@ -3197,7 +3227,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
3227 return error(_("%s: %s"), old_name, strerror(errno));
3228 }
3229
3200 - if (!cached && !tpatch)
3230 + if (!cached && !previous)
3231 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3232
3233 if (patch->is_new < 0)