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

[PATCH v2 07/10] merge-ort: implement CE_SKIP_WORKTREE handling with conflicted entries

From
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 9, 2021, 06:24 UTC
Message-ID
<90a57ade629d66e557f6f7b142e87cbeba8ca25e.1615271086.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.973.v2.git.git.1615271086.gitgitgadget@gmail.com>
From: Elijah Newren <newren@gmail.com>

When merge conflicts occur in paths removed by a sparse-checkout, we need to unsparsify those paths (clear the SKIP_WORKTREE bit), and write out the conflicted file to the working copy. In the very unlikely case that someone manually put a file into the working copy at the location of the SKIP_WORKTREE file, we need to avoid overwriting whatever edits they have made and move that file to a different location first.

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 merge-ort.c                       | 43 +++++++++++++++++++++----------
 t/t6428-merge-conflicts-sparse.sh |  4 +--
 2 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/merge-ort.c b/merge-ort.c
index cfd956a3f435..040287e3a998 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -3234,23 +3234,27 @@ static int checkout(struct merge_options *opt,
 	return ret;
 }
 
-static int record_conflicted_index_entries(struct merge_options *opt,
-					   struct index_state *index,
-					   struct strmap *paths,
-					   struct strmap *conflicted)
+static int record_conflicted_index_entries(struct merge_options *opt)
 {
 	struct hashmap_iter iter;
 	struct strmap_entry *e;
+	struct index_state *index = opt->repo->index;
+	struct checkout state = CHECKOUT_INIT;
 	int errs = 0;
 	int original_cache_nr;
 
-	if (strmap_empty(conflicted))
+	if (strmap_empty(&opt->priv->conflicted))
 		return 0;
 
+	/* If any entries have skip_worktree set, we'll have to check 'em out */
+	state.force = 1;
+	state.quiet = 1;
+	state.refresh_cache = 1;
+	state.istate = index;
 	original_cache_nr = index->cache_nr;
 
 	/* Put every entry from paths into plist, then sort */
-	strmap_for_each_entry(conflicted, &iter, e) {
+	strmap_for_each_entry(&opt->priv->conflicted, &iter, e) {
 		const char *path = e->key;
 		struct conflict_info *ci = e->value;
 		int pos;
@@ -3291,9 +3295,23 @@ static int record_conflicted_index_entries(struct merge_options *opt,
 			 * the higher order stages.  Thus, we need override
 			 * the CE_SKIP_WORKTREE bit and manually write those
 			 * files to the working disk here.
-			 *
-			 * TODO: Implement this CE_SKIP_WORKTREE fixup.
 			 */
+			if (ce_skip_worktree(ce)) {
+				struct stat st;
+
+				if (!lstat(path, &st)) {
+					char *new_name = unique_path(&opt->priv->paths,
+								     path,
+								     "cruft");
+
+					path_msg(opt, path, 1,
+						 _("Note: %s not up to date and in way of checking out conflicted version; old copy renamed to %s"),
+						 path, new_name);
+					errs |= rename(path, new_name);
+					free(new_name);
+				}
+				errs |= checkout_entry(ce, &state, NULL, NULL);
+			}
 
 			/*
 			 * Mark this cache entry for removal and instead add
@@ -3343,8 +3361,6 @@ void merge_switch_to_result(struct merge_options *opt,
 {
 	assert(opt->priv == NULL);
 	if (result->clean >= 0 && update_worktree_and_index) {
-		struct merge_options_internal *opti = result->priv;
-
 		trace2_region_enter("merge", "checkout", opt->repo);
 		if (checkout(opt, head, result->tree)) {
 			/* failure to function */
@@ -3354,13 +3370,14 @@ void merge_switch_to_result(struct merge_options *opt,
 		trace2_region_leave("merge", "checkout", opt->repo);
 
 		trace2_region_enter("merge", "record_conflicted", opt->repo);
-		if (record_conflicted_index_entries(opt, opt->repo->index,
-						    &opti->paths,
-						    &opti->conflicted)) {
+		opt->priv = result->priv;
+		if (record_conflicted_index_entries(opt)) {
 			/* failure to function */
+			opt->priv = NULL;
 			result->clean = -1;
 			return;
 		}
+		opt->priv = NULL;
 		trace2_region_leave("merge", "record_conflicted", opt->repo);
 	}
 
diff --git a/t/t6428-merge-conflicts-sparse.sh b/t/t6428-merge-conflicts-sparse.sh
index 1bb52ff6f38c..7e8bf497f821 100755
--- a/t/t6428-merge-conflicts-sparse.sh
+++ b/t/t6428-merge-conflicts-sparse.sh
@@ -76,7 +76,7 @@ test_setup_numerals () {
 	)
 }
 
-test_expect_merge_algorithm success failure 'conflicting entries written to worktree even if sparse' '
+test_expect_success 'conflicting entries written to worktree even if sparse' '
 	test_setup_numerals plain &&
 	(
 		cd numerals_plain &&
@@ -112,7 +112,7 @@ test_expect_merge_algorithm success failure 'conflicting entries written to work
 	)
 '
 
-test_expect_merge_algorithm failure failure 'present-despite-SKIP_WORKTREE handled reasonably' '
+test_expect_merge_algorithm failure success 'present-despite-SKIP_WORKTREE handled reasonably' '
 	test_setup_numerals in_the_way &&
 	(
 		cd numerals_in_the_way &&
-- 
gitgitgadget
Previous: Derrick StoleeNext: Elijah Newren via GitGitGadget
Message 37 of 44 in “Complete merge-ort implementation...almost”
  1. 00/11 Complete merge-ort implementation...almostElijah Newren via GitGitGadget, Mar 5, 2021
  2. 01/11 merge-ort: use STABLE_QSORT instead of QSORT where requiredElijah Newren via GitGitGadget, Mar 5, 2021
  3. 02/11 merge-ort: add a special minimal index just for renormalizationElijah Newren via GitGitGadget, Mar 5, 2021
  4. 03/11 merge-ort: add a function for initializing our special attr_indexElijah Newren via GitGitGadget, Mar 5, 2021
  5. Ævar Arnfjörð BjarmasonMar 8, 2021
  6. 04/11 merge-ort: have ll_merge() calls use the attr_index for renormalizationElijah Newren via GitGitGadget, Mar 5, 2021
  7. Ævar Arnfjörð BjarmasonMar 8, 2021
  8. Elijah NewrenMar 9, 2021
  9. 05/11 merge-ort: let renormalization change modify/delete into clean deleteElijah Newren via GitGitGadget, Mar 5, 2021
  10. Ævar Arnfjörð BjarmasonMar 8, 2021
  11. 06/11 merge-ort: support subtree shiftingElijah Newren via GitGitGadget, Mar 5, 2021
  12. 07/11 t6428: new test for SKIP_WORKTREE handling and conflictsElijah Newren via GitGitGadget, Mar 5, 2021
  13. Ævar Arnfjörð BjarmasonMar 8, 2021
  14. Elijah NewrenMar 8, 2021
  15. 08/11 merge-ort: implement CE_SKIP_WORKTREE handling with conflicted entriesElijah Newren via GitGitGadget, Mar 5, 2021
  16. Ævar Arnfjörð BjarmasonMar 8, 2021
  17. Elijah NewrenMar 8, 2021
  18. 09/11 t: mark several submodule merging tests as fixed under merge-ortElijah Newren via GitGitGadget, Mar 5, 2021
  19. Ævar Arnfjörð BjarmasonMar 8, 2021
  20. 10/11 merge-ort: write $GIT_DIR/AUTO_MERGE whenever we hit a conflictElijah Newren via GitGitGadget, Mar 5, 2021
  21. Ævar Arnfjörð BjarmasonMar 8, 2021
  22. Elijah NewrenMar 8, 2021
  23. 11/11 merge-recursive: add a bunch of FIXME comments documenting known bugsElijah Newren via GitGitGadget, Mar 5, 2021
  24. Ævar Arnfjörð BjarmasonMar 8, 2021
  25. Ævar Arnfjörð BjarmasonMar 8, 2021
  26. Elijah NewrenMar 8, 2021
  27. 00/10 Complete merge-ort implementation...almostElijah Newren via GitGitGadget, Mar 9, 2021
  28. 01/10 merge-ort: use STABLE_QSORT instead of QSORT where requiredElijah Newren via GitGitGadget, Mar 9, 2021
  29. 02/10 merge-ort: add a special minimal index just for renormalizationElijah Newren via GitGitGadget, Mar 9, 2021
  30. Derrick StoleeMar 11, 2021
  31. 05/10 merge-ort: support subtree shiftingElijah Newren via GitGitGadget, Mar 9, 2021
  32. 04/10 merge-ort: let renormalization change modify/delete into clean deleteElijah Newren via GitGitGadget, Mar 9, 2021
  33. 06/10 t6428: new test for SKIP_WORKTREE handling and conflictsElijah Newren via GitGitGadget, Mar 9, 2021
  34. Derrick StoleeMar 11, 2021
  35. 03/10 merge-ort: have ll_merge() use a special attr_index for renormalizationElijah Newren via GitGitGadget, Mar 9, 2021
  36. Derrick StoleeMar 11, 2021
  37. 07/10 merge-ort: implement CE_SKIP_WORKTREE handling with conflicted entriesElijah Newren via GitGitGadget, Mar 9, 2021
  38. 09/10 merge-ort: write $GIT_DIR/AUTO_MERGE whenever we hit a conflictElijah Newren via GitGitGadget, Mar 9, 2021
  39. 08/10 t: mark several submodule merging tests as fixed under merge-ortElijah Newren via GitGitGadget, Mar 9, 2021
  40. Derrick StoleeMar 11, 2021
  41. 10/10 merge-recursive: add a bunch of FIXME comments documenting known bugsElijah Newren via GitGitGadget, Mar 9, 2021
  42. Derrick StoleeMar 11, 2021
  43. Elijah NewrenMar 11, 2021
  44. Elijah NewrenMar 17, 2021

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.