| 3714 | } |
| 3715 | |
| 3716 | static int try_threeway(struct apply_state *state, |
| 3717 | struct image *image, |
| 3718 | struct patch *patch, |
| 3719 | struct stat *st, |
| 3720 | const struct cache_entry *ce) |
| 3721 | { |
| 3722 | struct object_id pre_oid, post_oid, our_oid; |
| 3723 | struct strbuf buf = STRBUF_INIT; |
| 3724 | size_t len; |
| 3725 | int status; |
| 3726 | char *img; |
| 3727 | struct image tmp_image = IMAGE_INIT; |
| 3728 | |
| 3729 | /* No point falling back to 3-way merge in these cases */ |
| 3730 | if (patch->is_delete || |
| 3731 | S_ISGITLINK(patch->old_mode) || S_ISGITLINK(patch->new_mode) || |
| 3732 | (patch->is_new && !patch->direct_to_threeway) || |
| 3733 | (patch->is_rename && !patch->lines_added && !patch->lines_deleted)) |
| 3734 | return -1; |
| 3735 | |
| 3736 | /* Preimage the patch was prepared for */ |
| 3737 | if (patch->is_new) |
| 3738 | odb_write_object(the_repository->objects, "", 0, OBJ_BLOB, &pre_oid); |
| 3739 | else if (repo_get_oid(the_repository, patch->old_oid_prefix, &pre_oid) || |
| 3740 | read_blob_object(&buf, &pre_oid, patch->old_mode)) |
| 3741 | return error(_("repository lacks the necessary blob to perform 3-way merge.")); |
| 3742 | |
| 3743 | if (state->apply_verbosity > verbosity_silent && patch->direct_to_threeway) |
| 3744 | fprintf(stderr, _("Performing three-way merge...\n")); |
| 3745 | |
| 3746 | img = strbuf_detach(&buf, &len); |
| 3747 | image_prepare(&tmp_image, img, len, 1); |
| 3748 | /* Apply the patch to get the post image */ |
| 3749 | if (apply_fragments(state, &tmp_image, patch) < 0) { |
| 3750 | image_clear(&tmp_image); |
| 3751 | return -1; |
| 3752 | } |
| 3753 | /* post_oid is theirs */ |
| 3754 | odb_write_object(the_repository->objects, tmp_image.buf.buf, |
| 3755 | tmp_image.buf.len, OBJ_BLOB, &post_oid); |
| 3756 | image_clear(&tmp_image); |
| 3757 | |
| 3758 | /* our_oid is ours */ |
| 3759 | if (patch->is_new) { |
| 3760 | if (load_current(state, &tmp_image, patch)) |
| 3761 | return error(_("cannot read the current contents of '%s'"), |
| 3762 | patch->new_name); |
| 3763 | } else { |
| 3764 | if (load_preimage(state, &tmp_image, patch, st, ce)) |
| 3765 | return error(_("cannot read the current contents of '%s'"), |
| 3766 | patch->old_name); |
| 3767 | } |
| 3768 | odb_write_object(the_repository->objects, tmp_image.buf.buf, |
| 3769 | tmp_image.buf.len, OBJ_BLOB, &our_oid); |
| 3770 | image_clear(&tmp_image); |
| 3771 | |
| 3772 | /* in-core three-way merge between post and our using pre as base */ |
| 3773 | status = three_way_merge(state, image, patch->new_name, |
no test coverage detected