Thread (35 messages) flat view 35 messages, 5 authors, 2022-03-15
STALE1648d

[PATCH v9 3/3] rev-parse: short-circuit superproject worktree when config unset

From: Emily Shaffer <hidden>
Date: 2022-03-10 00:44:44
Subsystem: the rest · Maintainer: Linus Torvalds

In the previous commit, submodules learned a config
'submodule.hasSuperproject' to indicate whether or not we should attempt
to traverse the filesystem to find their superproject. To help test that
this config was added everywhere it should have been, begin using it to
decide whether to exit early from 'git rev-parse
--show-superproject-working-dir'. Because that command is fairly old,
only short-circuit if the new config was explicitly set to false.

Signed-off-by: Emily Shaffer <redacted>
---
 submodule.c          | 18 ++++++++++++++++++
 t/t1500-rev-parse.sh | 10 +++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/submodule.c b/submodule.c
index aafbd628ad..64760f1e3a 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2231,6 +2231,7 @@ int get_superproject_working_tree(struct strbuf *buf)
 	struct strbuf sb = STRBUF_INIT;
 	struct strbuf one_up = STRBUF_INIT;
 	const char *cwd = xgetcwd();
+	int has_superproject_cfg = 0;
 	int ret = 0;
 	const char *subpath;
 	int code;
@@ -2244,6 +2245,23 @@ int get_superproject_working_tree(struct strbuf *buf)
 		 */
 		return 0;
 
+	/*
+	 * Because get_superproject_working_tree() is older than
+	 * submodule.hasSuperproject, don't rely on the default "unset = false"
+	 * - instead, only rely on if submodule.hasSuperproject was explicitly
+	 * set to false.
+	 */
+	if (! git_config_get_bool("submodule.hassuperproject", &has_superproject_cfg)
+	    && !has_superproject_cfg) {
+		/*
+		 * If we don't have a superproject, then we're probably not a
+		 * submodule. If this is failing and shouldn't be, investigate
+		 * why the config was set to false.
+		 */
+		error(_("Asked to find a superproject, but submodule.hasSuperproject == false"));
+		return 0;
+	}
+
 	if (!strbuf_realpath(&one_up, "../", 0))
 		return 0;
 
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 1c2df08333..dd35036bd6 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -244,7 +244,15 @@ test_expect_success 'showing the superproject correctly' '
 	test_must_fail git -C super merge branch1 &&
 
 	git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
-	test_cmp expect out
+	test_cmp expect out &&
+
+	# When submodule.hasSuperproject=false, --show-superproject-working-tree
+	# should fail instead of checking the filesystem.
+	test_config -C super/dir/sub submodule.hasSuperproject false &&
+	git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
+	# --show-superproject-working-tree should print an error about the
+	# broken config
+	! grep "error:.*hasSuperproject" out
 '
 
 # at least one external project depends on this behavior:
-- 
2.35.1.616.g0bdcbb4464-goog
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help