apply: factor out checkout_target() helper function

When a patch wants to touch a path, if the path exists in the index but is missing in the working tree, "git apply --index" checks out the file to the working tree from the index automatically and then applies the patch. Split this logic out to a separate helper function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 12, 2012 at 22:47 UTC f4c66eeddd37d3e5f4043df89c2679fbadeb57bd
1 file changed +13 -7
builtin/apply.c
+13 -7
@@ -3034,6 +3034,18 @@ static void prepare_fn_table(struct patch *patch)
3034 }
3035 }
3036
3037 +static int checkout_target(struct cache_entry *ce, struct stat *st)
3038 +{
3039 + struct checkout costate;
3040 +
3041 + memset(&costate, 0, sizeof(costate));
3042 + costate.base_dir = "";
3043 + costate.refresh_cache = 1;
3044 + if (checkout_entry(ce, &costate, NULL) || lstat(ce->name, st))
3045 + return error(_("cannot checkout %s"), ce->name);
3046 + return 0;
3047 +}
3048 +
3049 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
3050 {
3051 struct strbuf buf = STRBUF_INIT;
@@ -3163,13 +3175,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
3175 }
3176 *ce = active_cache[pos];
3177 if (stat_ret < 0) {
3166 - struct checkout costate;
3167 - /* checkout */
3168 - memset(&costate, 0, sizeof(costate));
3169 - costate.base_dir = "";
3170 - costate.refresh_cache = 1;
3171 - if (checkout_entry(*ce, &costate, NULL) ||
3172 - lstat(old_name, st))
3178 + if (checkout_target(*ce, st))
3179 return -1;
3180 }
3181 if (!cached && verify_index_match(*ce, st))