Git development
 help / color / mirror / Atom feed
From: Aniket Gupta <aniketgupta3001@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Elijah Newren <newren@gmail.com>, Coy Geek <coygeek@gmail.com>
Subject: [PATCH] merge: honor --no-overwrite-ignore for real merges
Date: Sat,  7 Nov 2026 04:44:19 +0000	[thread overview]
Message-ID: <20261107044419.209119-1-aniketgupta3001@gmail.com> (raw)
In-Reply-To: <CACgTecNrMKHGexWTkm1bqpVzL7SVWD5Hwo17wVokVGe3_Rnx0A@mail.gmail.com>

"git merge --no-overwrite-ignore" is meant to abort instead of
overwriting an ignored file that is in the way. However, only a
fast-forward honors it. When a real merge is needed, ort and recursive
overwrite the ignored file whatever the option says. Two FIXME comments
in merge-ort.c and builtin/merge.c mark where it was never passed along.
Add a preserve_ignored bit to struct merge_options then set it from the
option and use it where merge-ort updates the working tree. The
trivial-merge path in builtin/merge.c gets the same treatment. Other
callers of merge-ort leave the bit unset so nothing will change for
them.

Add tests for a plain merge and for a directory rename.

Reported-by: Coy Geek <coygeek@gmail.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
 builtin/merge.c  |  3 ++-
 merge-ort.c      |  2 +-
 merge-ort.h      |  1 +
 t/t7600-merge.sh | 59 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 5b4eb23a83..c7cd3ce29e 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -756,7 +756,7 @@ static int read_tree_trivial(struct object_id *common, struct object_id *head,
 	opts.verbose_update = 1;
 	opts.trivial_merges_only = 1;
 	opts.merge = 1;
-	opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
+	opts.preserve_ignored = !overwrite_ignore;
 	trees[nr_trees] = repo_parse_tree_indirect(the_repository, common);
 	if (!trees[nr_trees++])
 		return -1;
@@ -812,6 +812,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 		}
 
 		init_ui_merge_options(&o, the_repository);
+		o.preserve_ignored = !overwrite_ignore;
 		if (!strcmp(strategy, "subtree"))
 			o.subtree_shift = "";
 
diff --git a/merge-ort.c b/merge-ort.c
index c410a5d353..38dd1ac99d 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -4632,7 +4632,7 @@ static int checkout(struct merge_options *opt,
 	unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
 	unpack_opts.verbose_update = (opt->verbosity > 2);
 	unpack_opts.fn = twoway_merge;
-	unpack_opts.preserve_ignored = 0; /* FIXME: !opts->overwrite_ignore */
+	unpack_opts.preserve_ignored = opt->preserve_ignored;
 	if (repo_parse_tree(opt->repo, prev) < 0)
 		return -1;
 	init_tree_desc(&trees[0], &prev->object.oid, prev->buffer, prev->size);
diff --git a/merge-ort.h b/merge-ort.h
index 6045579825..d5747f358e 100644
--- a/merge-ort.h
+++ b/merge-ort.h
@@ -85,6 +85,7 @@ struct merge_options {
 	unsigned renormalize : 1;
 	unsigned mergeability_only : 1; /* exit early, write fewer objects */
 	unsigned record_conflict_msgs_as_headers : 1;
+	unsigned preserve_ignored : 1; /* do not overwrite ignored files */
 	const char *msg_header_prefix;
 
 	/* internal fields used by the implementation */
diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
index 64fe21717d..c1150e50e0 100755
--- a/t/t7600-merge.sh
+++ b/t/t7600-merge.sh
@@ -1179,4 +1179,63 @@ test_expect_success 'merge with no argument defaults to upstream' '
 	)
 '
 
+test_expect_success 'merge --no-overwrite-ignore keeps an ignored file in the way' '
+	test_when_finished "rm -rf ignored" &&
+	git init ignored &&
+	(
+		cd ignored &&
+		echo secret >.gitignore &&
+		git add .gitignore &&
+		test_tick &&
+		git commit -m base &&
+		git checkout -b side &&
+		echo side >secret &&
+		git add -f secret &&
+		test_tick &&
+		git commit -m side &&
+		git checkout - &&
+		test_commit other &&
+		echo mine >secret &&
+
+		test_must_fail git merge --no-overwrite-ignore side &&
+		echo mine >expect &&
+		test_cmp expect secret &&
+		test_path_is_missing .git/MERGE_HEAD &&
+
+		git merge --overwrite-ignore side &&
+		echo side >expect &&
+		test_cmp expect secret
+	)
+'
+
+test_expect_success 'merge --no-overwrite-ignore with a directory rename' '
+	test_when_finished "rm -rf ignored" &&
+	git init ignored &&
+	(
+		cd ignored &&
+		echo new/private >.gitignore &&
+		mkdir old &&
+		echo seed >old/seed &&
+		git add .gitignore old/seed &&
+		test_tick &&
+		git commit -m base &&
+		git checkout -b incoming &&
+		git mv old new &&
+		test_tick &&
+		git commit -m "rename old to new" &&
+		git checkout - &&
+		echo committed >old/private &&
+		git add old/private &&
+		test_tick &&
+		git commit -m "add old/private" &&
+		mkdir new &&
+		echo mine >new/private &&
+
+		test_must_fail git merge --no-overwrite-ignore incoming &&
+		echo mine >expect &&
+		test_cmp expect new/private &&
+		test_path_is_missing .git/MERGE_HEAD
+	)
+'
+
 test_done
-- 
2.43.0


      reply	other threads:[~2026-11-07  4:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 23:54 [BUG] merge-ort: --no-overwrite-ignore overwrites an ignored file at a directory-rename destination Coy Geek
2026-11-07  4:44 ` Aniket Gupta [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261107044419.209119-1-aniketgupta3001@gmail.com \
    --to=aniketgupta3001@gmail.com \
    --cc=coygeek@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=newren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox