apply: --3way with add/add conflict

When a patch wants to create a path, but we already have it in our current state, pretend as if the patch and we independently added the same path and cause add/add conflict, so that the user can resolve it just like "git merge" in the same situation. For that purpose, implement load_current() in terms of the load_patch_target() helper introduced earlier to read the current contents from the path given by patch->new_name (patch->old_name is NULL for a creation patch). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 7, 2012 at 15:04 UTC 099f3c421a446e2eef3a1bb6aea0e451711ddd88
1 file changed +62 -8
builtin/apply.c
+62 -8
@@ -197,6 +197,7 @@ struct patch {
197 unsigned int is_rename:1;
198 unsigned int recount:1;
199 unsigned int conflicted_threeway:1;
200 + unsigned int direct_to_threeway:1;
201 struct fragment *fragments;
202 char *result;
203 size_t resultsize;
@@ -3187,6 +3188,48 @@ static int three_way_merge(struct image *image,
3188 return status;
3189 }
3190
3191 +/*
3192 + * When directly falling back to add/add three-way merge, we read from
3193 + * the current contents of the new_name. In no cases other than that
3194 + * this function will be called.
3195 + */
3196 +static int load_current(struct image *image, struct patch *patch)
3197 +{
3198 + struct strbuf buf = STRBUF_INIT;
3199 + int status, pos;
3200 + size_t len;
3201 + char *img;
3202 + struct stat st;
3203 + struct cache_entry *ce;
3204 + char *name = patch->new_name;
3205 + unsigned mode = patch->new_mode;
3206 +
3207 + if (!patch->is_new)
3208 + die("BUG: patch to %s is not a creation", patch->old_name);
3209 +
3210 + pos = cache_name_pos(name, strlen(name));
3211 + if (pos < 0)
3212 + return error(_("%s: does not exist in index"), name);
3213 + ce = active_cache[pos];
3214 + if (lstat(name, &st)) {
3215 + if (errno != ENOENT)
3216 + return error(_("%s: %s"), name, strerror(errno));
3217 + if (checkout_target(ce, &st))
3218 + return -1;
3219 + }
3220 + if (verify_index_match(ce, &st))
3221 + return error(_("%s: does not match index"), name);
3222 +
3223 + status = load_patch_target(&buf, ce, &st, name, mode);
3224 + if (status < 0)
3225 + return status;
3226 + else if (status)
3227 + return -1;
3228 + img = strbuf_detach(&buf, &len);
3229 + prepare_image(image, img, len, !patch->is_binary);
3230 + return 0;
3231 +}
3232 +
3233 static int try_threeway(struct image *image, struct patch *patch,
3234 struct stat *st, struct cache_entry *ce)
3235 {
@@ -3198,13 +3241,15 @@ static int try_threeway(struct image *image, struct patch *patch,
3241 struct image tmp_image;
3242
3243 /* No point falling back to 3-way merge in these cases */
3201 - if (patch->is_new || patch->is_delete ||
3244 + if (patch->is_delete ||
3245 S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode))
3246 return -1;
3247
3248 /* Preimage the patch was prepared for */
3206 - if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3207 - read_blob_object(&buf, pre_sha1, patch->old_mode))
3249 + if (patch->is_new)
3250 + write_sha1_file("", 0, blob_type, pre_sha1);
3251 + else if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
3252 + read_blob_object(&buf, pre_sha1, patch->old_mode))
3253 return error("repository lacks the necessary blob to fall back on 3-way merge.");
3254
3255 fprintf(stderr, "Falling back to three-way merge...\n");
@@ -3221,9 +3266,15 @@ static int try_threeway(struct image *image, struct patch *patch,
3266 clear_image(&tmp_image);
3267
3268 /* our_sha1[] is ours */
3224 - if (load_preimage(&tmp_image, patch, st, ce))
3225 - return error("cannot read the current contents of '%s'",
3226 - patch->old_name);
3269 + if (patch->is_new) {
3270 + if (load_current(&tmp_image, patch))
3271 + return error("cannot read the current contents of '%s'",
3272 + patch->new_name);
3273 + } else {
3274 + if (load_preimage(&tmp_image, patch, st, ce))
3275 + return error("cannot read the current contents of '%s'",
3276 + patch->old_name);
3277 + }
3278 write_sha1_file(tmp_image.buf, tmp_image.len, blob_type, our_sha1);
3279 clear_image(&tmp_image);
3280
@@ -3254,7 +3305,8 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
3305 if (load_preimage(&image, patch, st, ce) < 0)
3306 return -1;
3307
3257 - if (apply_fragments(&image, patch) < 0) {
3308 + if (patch->direct_to_threeway ||
3309 + apply_fragments(&image, patch) < 0) {
3310 /* Note: with --reject, apply_fragments() returns 0 */
3311 if (!threeway || try_threeway(&image, patch, st, ce) < 0)
3312 return -1;
@@ -3431,7 +3483,9 @@ static int check_patch(struct patch *patch)
3483 ((0 < patch->is_new) | (0 < patch->is_rename) | patch->is_copy)) {
3484 int err = check_to_create(new_name, ok_if_exists);
3485
3434 - switch (err) {
3486 + if (err && threeway) {
3487 + patch->direct_to_threeway = 1;
3488 + } else switch (err) {
3489 case 0:
3490 break; /* happy */
3491 case EXISTS_IN_INDEX: