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

[PATCH v2 17/20] merge-ort: add implementation of checkout()

From
Elijah Newren <newren@gmail.com>
Date
Nov 2, 2020, 20:43 UTC
Message-ID
<20201102204344.342633-18-newren@gmail.com>
In-Reply-To
<20201102204344.342633-1-newren@gmail.com>

Since merge-ort creates a tree for its output, when there are no conflicts, updating the working tree and index is as simple as using the unpack_trees() machinery with a twoway_merge (i.e. doing the equivalent of a "checkout" operation).

If there were conflicts in the merge, then since the tree we created included all the conflict markers, then using the unpack_trees machinery in this manner will still update the working tree correctly. Further, all index entries corresponding to cleanly merged files will also be updated correctly by this procedure. Index entries corresponding to unmerged entries will appear as though the user had run "git add -u" after the merge to accept all files as-is with conflict markers.

Thus, after running unpack_trees(), there needs to be a separate step for updating the entries in the index corresponding to unmerged files. This will be the job for the function record_unmerged_index_entries(), which will be implemented in a subsequent commit.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/merge-ort.c b/merge-ort.c
index 2a60d84f1d..b7c5973d4d 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -19,9 +19,11 @@
 
 #include "diff.h"
 #include "diffcore.h"
+#include "dir.h"
 #include "object-store.h"
 #include "strmap.h"
 #include "tree.h"
+#include "unpack-trees.h"
 #include "xdiff-interface.h"
 
 struct merge_options_internal {
@@ -662,7 +664,48 @@ static int checkout(struct merge_options *opt,
 		    struct tree *prev,
 		    struct tree *next)
 {
-	die("Not yet implemented.");
+	/* Switch the index/working copy from old to new */
+	int ret;
+	struct tree_desc trees[2];
+	struct unpack_trees_options unpack_opts;
+
+	memset(&unpack_opts, 0, sizeof(unpack_opts));
+	unpack_opts.head_idx = -1;
+	unpack_opts.src_index = opt->repo->index;
+	unpack_opts.dst_index = opt->repo->index;
+
+	setup_unpack_trees_porcelain(&unpack_opts, "merge");
+
+	/*
+	 * NOTE: if this were just "git checkout" code, we would probably
+	 * read or refresh the cache and check for an unmerged index, but
+	 * builtin/merge.c or sequencer.c really needs to read the index
+	 * and check for unmerged entries before starting merging for a
+	 * good user experience (no sense waiting for merges/rebases before
+	 * erroring out), so there's no reason to duplicate that work here.
+	 */
+
+	/* 2-way merge to the new branch */
+	unpack_opts.update = 1;
+	unpack_opts.merge = 1;
+	unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
+	unpack_opts.verbose_update = (opt->verbosity > 2);
+	unpack_opts.fn = twoway_merge;
+	if (1/* FIXME: opts->overwrite_ignore*/) {
+		unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
+		unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
+		setup_standard_excludes(unpack_opts.dir);
+	}
+	parse_tree(prev);
+	init_tree_desc(&trees[0], prev->buffer, prev->size);
+	parse_tree(next);
+	init_tree_desc(&trees[1], next->buffer, next->size);
+
+	ret = unpack_trees(2, trees, &unpack_opts);
+	clear_unpack_trees_porcelain(&unpack_opts);
+	dir_clear(unpack_opts.dir);
+	FREE_AND_NULL(unpack_opts.dir);
+	return ret;
 }
 
 static int record_unmerged_index_entries(struct merge_options *opt,
-- 
2.29.0.471.ga4f56089c0
Previous: Jonathan TanNext: Elijah Newren
Message 29 of 83 in “fundamentals of merge-ort implementation”
  1. 00/20 fundamentals of merge-ort implementationElijah Newren, Nov 2, 2020
  2. 10/20 merge-ort: avoid recursing into identical treesElijah Newren, Nov 2, 2020
  3. Derrick StoleeNov 11, 2020
  4. 08/20 merge-ort: compute a few more useful fields for collect_merge_infoElijah Newren, Nov 2, 2020
  5. Jonathan TanNov 6, 2020
  6. Elijah NewrenNov 6, 2020
  7. Jonathan TanNov 9, 2020
  8. Elijah NewrenNov 9, 2020
  9. 11/20 merge-ort: add a preliminary simple process_entries() implementationElijah Newren, Nov 2, 2020
  10. Jonathan TanNov 11, 2020
  11. Elijah NewrenNov 12, 2020
  12. 09/20 merge-ort: record stage and auxiliary info for every pathElijah Newren, Nov 2, 2020
  13. Jonathan TanNov 6, 2020
  14. Elijah NewrenNov 7, 2020
  15. Jonathan TanNov 9, 2020
  16. Elijah NewrenNov 9, 2020
  17. Derrick StoleeNov 11, 2020
  18. Elijah NewrenNov 11, 2020
  19. Elijah NewrenNov 11, 2020
  20. Derrick StoleeNov 12, 2020
  21. Derrick StoleeNov 12, 2020
  22. 15/20 merge-ort: step 3 of tree writing -- handling subdirectories as we goElijah Newren, Nov 2, 2020
  23. Jonathan TanNov 12, 2020
  24. Elijah NewrenNov 12, 2020
  25. Elijah NewrenNov 24, 2020
  26. Jonathan TanNov 25, 2020
  27. Elijah NewrenNov 26, 2020
  28. Jonathan TanNov 30, 2020
  29. 17/20 merge-ort: add implementation of checkout()Elijah Newren, Nov 2, 2020
  30. 18/20 tree: enable cmp_cache_name_compare() to be used elsewhereElijah Newren, Nov 2, 2020
  31. 14/20 merge-ort: step 2 of tree writing -- function to create tree objectElijah Newren, Nov 2, 2020
  32. Jonathan TanNov 11, 2020
  33. Elijah NewrenNov 11, 2020
  34. 19/20 merge-ort: add implementation of record_unmerged_index_entries()Elijah Newren, Nov 2, 2020
  35. 20/20 merge-ort: free data structures in merge_finalize()Elijah Newren, Nov 2, 2020
  36. 16/20 merge-ort: basic outline for merge_switch_to_result()Elijah Newren, Nov 2, 2020
  37. 12/20 merge-ort: have process_entries operate in a defined orderElijah Newren, Nov 2, 2020
  38. Derrick StoleeNov 11, 2020
  39. Elijah NewrenNov 11, 2020
  40. 13/20 merge-ort: step 1 of tree writing -- record basenames, modes, and oidsElijah Newren, Nov 2, 2020
  41. Jonathan TanNov 11, 2020
  42. Elijah NewrenNov 11, 2020
  43. Jonathan TanNov 12, 2020
  44. 07/20 merge-ort: avoid repeating fill_tree_descriptor() on the same treeElijah Newren, Nov 2, 2020
  45. Derrick StoleeNov 11, 2020
  46. Elijah NewrenNov 11, 2020
  47. Eric SunshineNov 11, 2020
  48. 05/20 merge-ort: add an err() function similar to one from merge-recursiveElijah Newren, Nov 2, 2020
  49. Derrick StoleeNov 11, 2020
  50. Elijah NewrenNov 11, 2020
  51. Derrick StoleeNov 11, 2020
  52. 01/20 merge-ort: setup basic internal data structuresElijah Newren, Nov 2, 2020
  53. Jonathan TanNov 6, 2020
  54. Elijah NewrenNov 6, 2020
  55. Jonathan TanNov 9, 2020
  56. 02/20 merge-ort: add some high-level algorithm structureElijah Newren, Nov 2, 2020
  57. 03/20 merge-ort: port merge_start() from merge-recursiveElijah Newren, Nov 2, 2020
  58. Derrick StoleeNov 11, 2020
  59. Elijah NewrenNov 11, 2020
  60. 04/20 merge-ort: use histogram diffElijah Newren, Nov 2, 2020
  61. Derrick StoleeNov 11, 2020
  62. Elijah NewrenNov 11, 2020
  63. Derrick StoleeNov 11, 2020
  64. Elijah NewrenNov 11, 2020
  65. 06/20 merge-ort: implement a very basic collect_merge_info()Elijah Newren, Nov 2, 2020
  66. Jonathan TanNov 6, 2020
  67. Elijah NewrenNov 6, 2020
  68. Jonathan TanNov 9, 2020
  69. Derrick StoleeNov 11, 2020
  70. Elijah NewrenNov 11, 2020
  71. Derrick StoleeNov 3, 2020
  72. Elijah NewrenNov 3, 2020
  73. Elijah NewrenNov 7, 2020
  74. Derrick StoleeNov 7, 2020
  75. Elijah NewrenNov 7, 2020
  76. Derrick StoleeNov 9, 2020
  77. Elijah NewrenNov 9, 2020
  78. Derrick StoleeNov 9, 2020
  79. Elijah NewrenNov 9, 2020
  80. Derrick StoleeNov 11, 2020
  81. Elijah NewrenNov 11, 2020
  82. Derrick StoleeNov 11, 2020
  83. Elijah NewrenNov 11, 2020

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.