apply: further split load_preimage()

load_preimage() is very specific to grab the current contents for the path given by patch->old_name. Split the logic that grabs the contents for a path out of it into a separate load_patch_target() function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 12, 2012 at 15:23 UTC 5a812661694efed28b4446724278bcc21fe6d6d6
1 file changed +40 -19
builtin/apply.c
+40 -19
@@ -3067,6 +3067,31 @@ static struct patch *previous_patch(struct patch *patch, int *gone)
3067 return previous;
3068 }
3069
3070 +#define SUBMODULE_PATCH_WITHOUT_INDEX 1
3071 +
3072 +static int load_patch_target(struct strbuf *buf,
3073 + struct cache_entry *ce,
3074 + struct stat *st,
3075 + const char *name,
3076 + unsigned expected_mode)
3077 +{
3078 + if (cached) {
3079 + if (read_file_or_gitlink(ce, buf))
3080 + return error(_("read of %s failed"), name);
3081 + } else if (name) {
3082 + if (S_ISGITLINK(expected_mode)) {
3083 + if (ce)
3084 + return read_file_or_gitlink(ce, buf);
3085 + else
3086 + return SUBMODULE_PATCH_WITHOUT_INDEX;
3087 + } else {
3088 + if (read_old_data(st, name, buf))
3089 + return error(_("read of %s failed"), name);
3090 + }
3091 + }
3092 + return 0;
3093 +}
3094 +
3095 /*
3096 * We are about to apply "patch"; populate the "image" with the
3097 * current version we have, from the working tree or from the index,
@@ -3090,26 +3115,22 @@ static int load_preimage(struct image *image,
3115 if (previous) {
3116 /* We have a patched copy in memory; use that. */
3117 strbuf_add(&buf, previous->result, previous->resultsize);
3093 - } else if (cached) {
3094 - if (read_file_or_gitlink(ce, &buf))
3118 + } else {
3119 + status = load_patch_target(&buf, ce, st,
3120 + patch->old_name, patch->old_mode);
3121 + if (status < 0)
3122 + return status;
3123 + else if (status == SUBMODULE_PATCH_WITHOUT_INDEX) {
3124 + /*
3125 + * There is no way to apply subproject
3126 + * patch without looking at the index.
3127 + * NEEDSWORK: shouldn't this be flagged
3128 + * as an error???
3129 + */
3130 + free_fragment_list(patch->fragments);
3131 + patch->fragments = NULL;
3132 + } else if (status) {
3133 return error(_("read of %s failed"), patch->old_name);
3096 - } else if (patch->old_name) {
3097 - if (S_ISGITLINK(patch->old_mode)) {
3098 - if (ce) {
3099 - read_file_or_gitlink(ce, &buf);
3100 - } else {
3101 - /*
3102 - * There is no way to apply subproject
3103 - * patch without looking at the index.
3104 - * NEEDSWORK: shouldn't this be flagged
3105 - * as an error???
3106 - */
3107 - free_fragment_list(patch->fragments);
3108 - patch->fragments = NULL;
3109 - }
3110 - } else {
3111 - if (read_old_data(st, patch->old_name, &buf))
3112 - return error(_("read of %s failed"), patch->old_name);
3134 }
3135 }
3136