git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 13/19] apply: plug the three-way merge logic in

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 10, 2012, 07:04 UTC
Message-ID
<1341903852-4815-14-git-send-email-gitster@pobox.com>
In-Reply-To
<1341903852-4815-1-git-send-email-gitster@pobox.com>

When a patch does not apply to what we have, but we know the preimage the patch was made against, we apply the patch to the preimage to compute what the patch author wanted the result to look like, and attempt a three-way merge between the result and our version, using the intended preimage as the base version.

When we are applying the patch using the index, we would additionally need to add the object names of these three blobs involved in the merge, which is not yet done in this step, but we add a field to "struct patch" so that later write-out step can use it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/apply.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index 5a7201f..d84958b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -16,6 +16,8 @@
 #include "dir.h"
 #include "diff.h"
 #include "parse-options.h"
+#include "xdiff-interface.h"
+#include "ll-merge.h"
 
 /*
  *  --check turns on checking that the working tree matches the
@@ -194,12 +196,16 @@ struct patch {
 	unsigned int is_copy:1;
 	unsigned int is_rename:1;
 	unsigned int recount:1;
+	unsigned int conflicted_threeway:1;
 	struct fragment *fragments;
 	char *result;
 	size_t resultsize;
 	char old_sha1_prefix[41];
 	char new_sha1_prefix[41];
 	struct patch *next;
+
+	/* three-way fallback result */
+	unsigned char threeway_stage[3][20];
 };
 
 static void free_fragment_list(struct fragment *list)
@@ -3146,7 +3152,29 @@ static int three_way_merge(struct image *image,
 			   const unsigned char *ours,
 			   const unsigned char *theirs)
 {
-	return -1; /* for now */
+	mmfile_t base_file, our_file, their_file;
+	mmbuffer_t result = { NULL };
+	int status;
+
+	read_mmblob(&base_file, base);
+	read_mmblob(&our_file, ours);
+	read_mmblob(&their_file, theirs);
+	status = ll_merge(&result, path,
+			  &base_file, "base",
+			  &our_file, "ours",
+			  &their_file, "theirs", NULL);
+	free(base_file.ptr);
+	free(our_file.ptr);
+	free(their_file.ptr);
+	if (status < 0 || !result.ptr) {
+		free(result.ptr);
+		return -1;
+	}
+	clear_image(image);
+	image->buf = result.ptr;
+	image->len = result.size;
+
+	return status;
 }
 
 static int try_threeway(struct image *image, struct patch *patch,
@@ -3155,6 +3183,7 @@ static int try_threeway(struct image *image, struct patch *patch,
 	unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
 	struct strbuf buf = STRBUF_INIT;
 	size_t len;
+	int status;
 	char *img;
 	struct image tmp_image;
 
@@ -3167,6 +3196,9 @@ static int try_threeway(struct image *image, struct patch *patch,
 	if (get_sha1(patch->old_sha1_prefix, pre_sha1) ||
 	    read_blob_object(&buf, pre_sha1, patch->old_mode))
 		return error("repository lacks the necessary blob to fall back on 3-way merge.");
+
+	fprintf(stderr, "Falling back to three-way merge...\n");
+
 	img = strbuf_detach(&buf, &len);
 	prepare_image(&tmp_image, img, len, 1);
 	/* Apply the patch to get the post image */
@@ -3186,8 +3218,23 @@ static int try_threeway(struct image *image, struct patch *patch,
 	clear_image(&tmp_image);
 
 	/* in-core three-way merge between post and our using pre as base */
-	return three_way_merge(image,
-			       patch->new_name, pre_sha1, our_sha1, post_sha1);
+	status = three_way_merge(image, patch->new_name,
+				 pre_sha1, our_sha1, post_sha1);
+	if (status < 0) {
+		fprintf(stderr, "Failed to fall back on three-way merge...\n");
+		return status;
+	}
+
+	if (status) {
+		patch->conflicted_threeway = 1;
+		hashcpy(patch->threeway_stage[0], pre_sha1);
+		hashcpy(patch->threeway_stage[1], our_sha1);
+		hashcpy(patch->threeway_stage[2], post_sha1);
+		fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
+	} else {
+		fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
+	}
+	return 0;
 }
 
 static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *ce)
-- 
1.7.11.1.294.g68a9409
Previous: Junio C HamanoNext: Junio C Hamano
Message 14 of 20 in “"git apply --3way"”
  1. 00/19 "git apply --3way"Junio C Hamano, Jul 10, 2012
  2. 01/19 apply: fix an incomplete comment in check_patch()Junio C Hamano, Jul 10, 2012
  3. 02/19 apply: a bit more comments on PATH_TO_BE_DELETEDJunio C Hamano, Jul 10, 2012
  4. 03/19 apply: clear_image() clears things a bit moreJunio C Hamano, Jul 10, 2012
  5. 04/19 apply: refactor read_file_or_gitlink()Junio C Hamano, Jul 10, 2012
  6. 05/19 apply: factor out checkout_target() helper functionJunio C Hamano, Jul 10, 2012
  7. 06/19 apply: split load_preimage() helper function outJunio C Hamano, Jul 10, 2012
  8. 07/19 apply: refactor "previous patch" logicJunio C Hamano, Jul 10, 2012
  9. 08/19 apply: further split load_preimage()Junio C Hamano, Jul 10, 2012
  10. 09/19 apply: move check_to_create_blob() closer to its sole callerJunio C Hamano, Jul 10, 2012
  11. 10/19 apply: move "already exists" logic to check_to_create()Junio C Hamano, Jul 10, 2012
  12. 11/19 apply: accept -3/--3way command line optionJunio C Hamano, Jul 10, 2012
  13. 12/19 apply: fall back on three-way mergeJunio C Hamano, Jul 10, 2012
  14. 13/19 apply: plug the three-way merge logic inJunio C Hamano, Jul 10, 2012
  15. 14/19 apply: move verify_index_match() higherJunio C Hamano, Jul 10, 2012
  16. 15/19 apply: --3way with add/add conflictJunio C Hamano, Jul 10, 2012
  17. 16/19 apply: register conflicted stages to the indexJunio C Hamano, Jul 10, 2012
  18. 17/19 apply: allow rerere() to work on --3way resultsJunio C Hamano, Jul 10, 2012
  19. 18/19 apply: document --3way optionJunio C Hamano, Jul 10, 2012
  20. 19/19 apply: tests for the --3way optionJunio C Hamano, Jul 10, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.