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

[PATCH 4/4] merge: introduce merge.allowUnrelatedhistories configuration option

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 21, 2016, 19:25 UTC
Message-ID
<20160421192500.23563-5-gitster@pobox.com>
In-Reply-To
<20160421192500.23563-1-gitster@pobox.com>

It was rumored that in an unspecified workflow it is common to create what Kernel folks call "crazy" and "insane" merges of two unrelated histories, and having to give --allow-unrelated-histories option every time is cumbersome.

Just in case the rumor will substanticated with a proper rationale in the future, prepare a change to allow disabling the safety by default.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/git.txt          |  4 ++++
 Documentation/merge-config.txt |  7 +++++++
 builtin/merge.c                |  3 +++
 t/t3033-merge-toplevel.sh      | 29 +++++++++++++++++++++++++++++
 t/t5521-pull-options.sh        |  9 ++++++++-
 5 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/Documentation/git.txt b/Documentation/git.txt
index 5c9380d..f2edac1 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -1140,6 +1140,10 @@ of clones and fetches.
 
 'GIT_MERGE_ALLOW_UNRELATED_HISTORIES'::
 	Allow "git merge" to merge unrelated histories by default.
+	It is recommended that a script that regularly wants to
+	create such a merge to set and export this environment
+	variable upfront, instead of forcing its users to set
+	merge.allowunrelatedhistories configuration variable.
 
 
 Discussion[[Discussion]]
diff --git a/Documentation/merge-config.txt b/Documentation/merge-config.txt
index 002ca58..8b3d14b 100644
--- a/Documentation/merge-config.txt
+++ b/Documentation/merge-config.txt
@@ -47,6 +47,13 @@ merge.stat::
 	Whether to print the diffstat between ORIG_HEAD and the merge result
 	at the end of the merge.  True by default.
 
+merge.allowUnrelatedhistories::
+	Setting this option to true (false) makes `git merge` and `git
+	pull` to pretend as if the `--allow-unrelated-histories`
+	(`--no-allow-unrelated-histories`) option was given from the
+	command line.  The configuration is ignored when one of these
+	options is explicitly given from the command line.
+
 merge.tool::
 	Controls which merge tool is used by linkgit:git-mergetool[1].
 	The list below shows the valid built-in values.
diff --git a/builtin/merge.c b/builtin/merge.c
index 4e8b1a1..e979c68 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -583,6 +583,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
 	} else if (!strcmp(k, "commit.gpgsign")) {
 		sign_commit = git_config_bool(k, v) ? "" : NULL;
 		return 0;
+	} else if (!strcmp(k, "merge.allowunrelatedhistories")) {
+		allow_unrelated_histories = git_config_bool(k, v);
+		return 0;
 	}
 
 	status = fmt_merge_msg_config(k, v, cb);
diff --git a/t/t3033-merge-toplevel.sh b/t/t3033-merge-toplevel.sh
index d314599..583b837 100755
--- a/t/t3033-merge-toplevel.sh
+++ b/t/t3033-merge-toplevel.sh
@@ -149,4 +149,33 @@ test_expect_success 'two-project merge with --allow-unrelated-histories' '
 	git diff --exit-code five
 '
 
+test_expect_success 'two-project merge with merge.allowunrelatedhistories' '
+	t3033_reset &&
+
+	# make sure configuration parser works
+	git reset --hard four &&
+	test_config merge.allowunrelatedhistories notabool &&
+	test_must_fail git merge . HEAD &&
+
+	# disabled explicitly and redundantly by configuration
+	git reset --hard four &&
+	test_config merge.allowunrelatedhistories false &&
+	test_must_fail git merge five &&
+
+	# disabled explicitly by configuration, overridden by command line
+	git reset --hard four &&
+	test_config merge.allowunrelatedhistories false &&
+	git merge --allow-unrelated-histories five &&
+
+	# enabled by configuration but explicitly disabled
+	git reset --hard four &&
+	test_config merge.allowunrelatedhistories true &&
+	test_must_fail git merge --no-allow-unrelated-histories five &&
+
+	# enabled by configuration
+	git reset --hard four &&
+	test_config merge.allowunrelatedhistories true &&
+	git merge five
+'
+
 test_done
diff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh
index ded8f98..50f0887 100755
--- a/t/t5521-pull-options.sh
+++ b/t/t5521-pull-options.sh
@@ -161,7 +161,14 @@ test_expect_success 'git pull --allow-unrelated-histories' '
 	(
 		cd dst &&
 		test_must_fail git pull ../src side &&
-		git pull --allow-unrelated-histories ../src side
+		git pull --allow-unrelated-histories ../src side &&
+
+		git reset --hard one &&
+		git config merge.allowunrelatedhistories no &&
+		test_must_fail git pull ../src side &&
+		git config merge.allowunrelatedhistories yes &&
+		test_must_fail git pull --no-allow-unrelated-histories ../src side &&
+		git pull ../src side
 	)
 '
 
-- 
2.8.1-422-g6d9b748
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 8 in “Loosening "two project merge" safety”
  1. 0/4 Loosening "two project merge" safetyJunio C Hamano, Apr 21, 2016
  2. 1/4 t3033: avoid 'ambiguous refs' warningJunio C Hamano, Apr 21, 2016
  3. 2/4 pull: pass --allow-unrelated-histories to "git merge"Junio C Hamano, Apr 21, 2016
  4. Stefan BellerApr 21, 2016
  5. Junio C HamanoApr 21, 2016
  6. 3/4 merge: GIT_MERGE_ALLOW_UNRELATED_HISTORIES environmentJunio C Hamano, Apr 21, 2016
  7. 4/4 merge: introduce merge.allowUnrelatedhistories configuration optionJunio C Hamano, Apr 21, 2016
  8. Junio C HamanoApr 21, 2016

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.