Skip to contentSkip to conversation

teach submodules to know they're submodules

PATCH Superseded Emily Shaffer 7 participants 64 messages Nov 17, 2021 – Mar 15, 2022 Documentation

lore
Share & tools
lore
#0 lore raw

v6 cover teach submodules to know they're submodules

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

CI run: https://github.com/nasamuffin/git/actions/runs/1469463328

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Since v4:

The only real change here is a slight semantics change to map from <submodule gitdir> to <superproject common git dir>. In every case *except* for when the superproject has a worktree, this changes nothing. For the case when the superproject has a worktree, this means that now submodules will refer to the general superproject common dir (e.g. no worktree-specific refs or configs or whatnot).

I *think* that because a submodule should exist in the context of the common dir, not the worktree gitdir, that is ok. However, it does mean it would be difficult to do something like sharing a config specific to the worktree (the initial goal of this series).

$ROOT/.git
$ROOT/.git/config.superproject <- shared by $ROOT/.git/modules/sub $ROOT/.git/modules/sub <- points to $ROOT/.git
$ROOT/.git/worktrees/wt
$ROOT/.git/worktrees/wt/config.superproject <- contains a certain config-based pre-commit hook

If the submodule only knows about the common dir, that is tough, because the submodule would basically have to guess which worktree it's in from its own path. There would be no way for '$WT/sub' to inherit '$ROOT/.git/worktrees/wt/config.superproject'.

That said... right now, we don't support submodules in worktrees very well at all. A submodule in a worktree will get a brand new gitdir in $ROOT/.git/worktrees/modules/ (and that brand new gitdir would point to the super's common dir). So I think we can punt on this entire question until we teach submodules and worktrees to play more gracefully together (it's on my long list...), and at that time we can probably introduce a pointer from $ROOT/.git/modules/sub/worktrees/wt/ to $ROOT/.git/worktrees/wt/....

Or, to summarize the long ramble above: "this is still kind of weird with worktrees, but let's fix it later when we fix worktrees more thoroughly".

(More rambling about worktree weirdness here:
https://lore.kernel.org/git/YYRaII8YWVxlBqsF%40google.com )

Since v3, a pretty major change: the semantics of submodule.superprojectGitDir has changed, to point from the submodule's gitdir to the superproject's gitdir (in v3 and earlier, we kept a path from the submodule's *worktree* to the superproject's gitdir instead). This cleans up some of the confusions about the behavior when a submodule worktree moves around in the superproject's tree, or in a future when we support submodules having multiple worktrees.

I also tried to simplify the tests to use 'test-tool path-utils relative_path' everywhere - I think that makes them much more clear for a test reader, but if you're reviewing and it isn't obvious what we're testing for, please speak up.

I think this is pretty mature and there was a lot of general agreement that the gitdir->gitdir association was the way to go, so please be brutal and look for nits, leaks, etc. this round ;)
[/v4 cover letter]

Emily Shaffer (5):
t7400-submodule-basic: modernize inspect() helper introduce submodule.superprojectGitDir record
submodule: record superproject gitdir during absorbgitdirs
submodule: record superproject gitdir during 'update'
submodule: use config to find superproject worktree

 Documentation/config/submodule.txt |  12 ++++
 builtin/submodule--helper.c        |  11 +++
 git-submodule.sh                   |  15 ++++
 submodule.c                        | 108 ++++++++++++++++++++++++++++-
 t/t1500-rev-parse.sh               |   9 +++
 t/t7400-submodule-basic.sh         |  54 ++++++++-------
 t/t7406-submodule-update.sh        |  27 ++++++++
 t/t7412-submodule-absorbgitdirs.sh |  82 +++++++++++++++++++++-
 8 files changed, 290 insertions(+), 28 deletions(-)

Range-diff against v5:

1:  6ff10beaf2 = 1:  f1b08a7057 t7400-submodule-basic: modernize inspect() helper
2:  d4f4627585 ! 2:  d46c8439ab introduce submodule.superprojectGitDir record

@@ Commit message

By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the
- submodule's cache. And by using the path from gitdir to gitdir, we can
+ submodule's pointer. And by using the path from gitdir to gitdir, we can
move the submodule within the superproject's tree structure without
- breaking the submodule's cache, too. Finally, by pointing at
- "get_git_common_dir()" instead of "get_git_dir()", we ensure the link
- will refer to the parent repo, not to a specific worktree.
+ breaking the submodule's pointer, too. Finally, by pointing at the
+ superproject's worktree gitdir (if it exists), we ensure that we can
+ tell which worktree contains our submodule.

Since this hint value is only introduced during new submodule creation via git submodule add, though, there is more work to do to allow the record to be created at other times.

- If the new config is present, we can do some optional value-added
- behavior, like letting "git status" print additional info about the
- submodule's status in relation to its superproject, or like letting the
- superproject and submodule share an additional config file separate from
- either one's local config.
+ Once this new config is reliably in place, we can use it to know
+ definitively that we are working in a submodule, and to know which
+ superproject we are a submodule of. This allows us to do some
+ value-added behavior, like letting "git status" print additional info
+ about the submodule's status in relation to its superproject, or like
+ letting the superproject and submodule share an additional config file
+ separate from either one's local config.

@@ Documentation/config/submodule.txt: submodule.alternateErrorStrategy::
+
+submodule.superprojectGitDir::
+ The relative path from the submodule's gitdir to its superproject's
-+ common dir. When Git is run in a repository, it usually makes no
++ gitdir. When Git is run in a repository, it usually makes no
+ difference whether this repository is standalone or a submodule, but if
+ this configuration variable is present, additional behavior may be
+ possible, such as "git status" printing additional information about
@@ builtin/submodule--helper.c: static int clone_submodule(struct module_clone_data
git_config_set_in_file(p, "submodule.alternateErrorStrategy",
error_strategy);

-+ git_config_set_in_file(p, "submodule.superprojectGitdir",
-+ relative_path(absolute_path(get_git_common_dir()),
++ /*
++ * Set the path from submodule's new gitdir to superproject's gitdir.
++ * The latter may be a worktree gitdir. However, it is not possible for
++ * the submodule to have a worktree-specific gitdir or config at clone
++ * time, because "extensions.worktreeConfig" is only valid when set in
++ * the local gitconfig, which the brand new submodule does not have yet.
++ */
++ git_config_set_in_file(p, "submodule.superprojectGitDir",
++ relative_path(absolute_path(get_git_dir()),
+ sm_gitdir, &sb));
+
free(sm_alternate);
@@ t/t7400-submodule-basic.sh: submodurl=$(pwd -P)
+ # Ensure that submodule.superprojectGitDir contains the path from the
+ # submodule's gitdir to the superproject's gitdir.
+
-+ super_abs_gitdir=$(git -C "$super_dir" rev-parse --path-format=absolute --git-common-dir) &&
-+ sub_abs_gitdir=$(git -C "$sub_dir" rev-parse --path-format=absolute --git-common-dir) &&
++ super_abs_gitdir=$(git -C "$super_dir" rev-parse --absolute-git-dir) &&
++ sub_abs_gitdir=$(git -C "$sub_dir" rev-parse --absolute-git-dir) &&
+
+ [ "$(git -C "$sub_dir" config --get submodule.superprojectGitDir)" = \
+ "$(test-tool path-utils relative_path "$super_abs_gitdir" \
3: 2dae297943 ! 3: 63ddaf5608 submodule: record superproject gitdir during absorbgitdirs

    @@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
      	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
      	struct strbuf new_gitdir = STRBUF_INIT;
      	const struct submodule *sub;
    ++	struct config_set sub_cs;
     +	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
    ++	int tmp;
      
      	if (submodule_uses_worktrees(path))
      		die(_("relocate_gitdir for submodule '%s' with "
    @@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
      
      	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
      
    -+	/* cache pointer to superproject's gitdir */
    ++	/*
    ++	 * Note location of superproject's gitdir. Because the submodule already
    ++	 * has a gitdir and local config, we can store this pointer from
    ++	 * worktree config to worktree config, if the submodule has
    ++	 * extensions.worktreeConfig set.
    ++	 */
     +	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
    ++	git_configset_init(&sub_cs);
    ++	git_configset_add_file(&sub_cs, config_path.buf);
    ++	/* return 0 indicates config was found - we have a worktree config */
    ++	if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp))
    ++		strbuf_addstr(&config_path, ".worktree");
    ++
     +	git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
    -+			       relative_path(absolute_path(get_git_common_dir()),
    ++			       relative_path(absolute_path(get_git_dir()),
     +					     real_new_git_dir, &sb));
     +
    ++	git_configset_clear(&sub_cs);
     +	strbuf_release(&config_path);
     +	strbuf_release(&sb);
      	free(old_git_dir);
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
     +	# absorb the git dir
     +	git submodule absorbgitdirs sub4 &&
     +
    -+	# make sure the submodule cached the superproject gitdir correctly
    -+	submodule_gitdir="$(git -C sub4 rev-parse --path-format=absolute --git-common-dir)" &&
    -+	superproject_gitdir="$(git rev-parse --path-format=absolute --git-common-dir)" &&
    ++	# make sure the submodule noted the superproject gitdir correctly
    ++	submodule_gitdir="$(git -C sub4 rev-parse --absolute-git-dir)" &&
    ++	superproject_gitdir="$(git rev-parse --absolute-git-dir)" &&
     +
     +	test-tool path-utils relative_path "$superproject_gitdir" \
     +		"$submodule_gitdir" >expect &&
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
     +	test_cmp expect actual
     +	)
     +'
    ++
    ++test_expect_success 'absorbgitdirs works with a submodule with worktree config' '
    ++	# reuse the worktree of the superproject
    ++	(
    ++	cd wt &&
    ++
    ++	# create a new unembedded git dir
    ++	git init sub5 &&
    ++	test_commit -C sub5 first &&
    ++	git submodule add ./sub5 &&
    ++	test_tick &&
    ++
    ++	# turn on worktree configs for submodule
    ++	git -C sub5 config extensions.worktreeConfig true &&
    ++
    ++	# absorb the git dir
    ++	git submodule absorbgitdirs sub5 &&
    ++
    ++	# make sure the submodule noted the superproject gitdir correctly
    ++	submodule_gitdir="$(git -C sub5 rev-parse --absolute-git-dir)" &&
    ++	superproject_gitdir="$(git rev-parse --absolute-git-dir)" &&
    ++
    ++	test-tool path-utils relative_path "$superproject_gitdir" \
    ++		"$submodule_gitdir" >expect &&
    ++	git -C sub5 config submodule.superprojectGitDir >actual &&
    ++
    ++	test_cmp expect actual &&
    ++
    ++	# make sure the config went into the submodule config.worktree
    ++	test_file_not_empty "$submodule_gitdir/config.worktree"
    ++	)
    ++'
     +
      test_done
4:  f0412d6d34 ! 4:  33a582ef13 submodule: record superproject gitdir during 'update'
    @@ Metadata
      ## Commit message ##
         submodule: record superproject gitdir during 'update'
     
    -    A recorded hint path to the superproject's gitdir might be added during
    +    A recorded path to the superproject's gitdir might be added during
         'git submodule add', but in some cases - like submodules which were
         created before 'git submodule add' learned to record that info - it might
         be useful to update the hint. Let's do it during 'git submodule
    @@ git-submodule.sh: cmd_update()
      			;;
      		esac
      
    -+		# Cache a pointer to the superproject's common dir. This may have
    -+		# changed, unless it's a fresh clone. Writes it to worktree
    -+		# if applicable, otherwise to local.
    ++		# Store a poitner to the superproject's gitdir. This may have
    ++		# changed, unless it's a fresh clone. Write to worktree if
    ++		# applicable, and point to superproject's worktree gitdir if
    ++		# applicable.
     +		if test -z "$just_cloned"
     +		then
     +			sm_gitdir="$(git -C "$sm_path" rev-parse --absolute-git-dir)"
     +			relative_gitdir="$(git rev-parse --path-format=relative \
     +							 --prefix "${sm_gitdir}" \
    -+							 --git-common-dir)"
    ++							 --git-dir)"
     +
     +			git -C "$sm_path" config --worktree \
     +				submodule.superprojectgitdir "$relative_gitdir"
    @@ t/t7406-submodule-update.sh: test_expect_success 'submodule update --quiet passe
     +	 git -C submodule config --unset submodule.superprojectGitdir &&
     +	 git submodule update &&
     +	 test-tool path-utils relative_path \
    -+		"$(git rev-parse --path-format=absolute --git-common-dir)" \
    -+		"$(git -C submodule rev-parse --path-format=absolute --git-common-dir)" >expect &&
    ++		"$(git rev-parse --absolute-git-dir)" \
    ++		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
     +	 git -C submodule config submodule.superprojectGitdir >actual &&
     +	 test_cmp expect actual
     +	)
     +'
    ++
    ++test_expect_success 'submodule update uses config.worktree if applicable' '
    ++	(cd super &&
    ++	 git -C submodule config --unset submodule.superprojectGitDir &&
    ++	 git -C submodule config extensions.worktreeConfig true &&
    ++	 git submodule update &&
    ++	 test-tool path-utils relative_path \
    ++		"$(git rev-parse --absolute-git-dir)" \
    ++		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
    ++	 git -C submodule config submodule.superprojectGitdir >actual &&
    ++	 test_cmp expect actual &&
    ++
    ++	 test_file_not_empty "$(git -C submodule rev-parse --absolute-git-dir)/config.worktree"
    ++	)
    ++'
     +
      test_done

-: ---------- > 5: a8b5d40a77 submodule: use config to find superproject worktree

Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#1 lore raw

PATCH v6 1/5 t7400-submodule-basic: modernize inspect() helper

Since the inspect() helper in the submodule-basic test suite was written, 'git -C <dir>' was added. By using -C, we no longer need a reference to the base directory for the test. This simplifies callsites, and will make the addition of other arguments in later patches more readable.

---
 t/t7400-submodule-basic.sh | 40 +++++++++++++++-----------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
t/t7400-submodule-basic.sh +16 -24
index cb1b8e35db..d69a5c0032 100755
@@ -107,23 +107,15 @@ test_expect_success 'setup - repository to add submodules to' '
 # generates, which will expand symbolic links.
 submodurl=$(pwd -P)
 
-listbranches() {
-	git for-each-ref --format='%(refname)' 'refs/heads/*'
-}
-
 inspect() {
-	dir=$1 &&
-	dotdot="${2:-..}" &&
-
-	(
-		cd "$dir" &&
-		listbranches >"$dotdot/heads" &&
-		{ git symbolic-ref HEAD || :; } >"$dotdot/head" &&
-		git rev-parse HEAD >"$dotdot/head-sha1" &&
-		git update-index --refresh &&
-		git diff-files --exit-code &&
-		git clean -n -d -x >"$dotdot/untracked"
-	)
+	sub_dir=$1 &&
+
+	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
+	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
+	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
+	git -C "$sub_dir" update-index --refresh &&
+	git -C "$sub_dir" diff-files --exit-code &&
+	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
 test_expect_success 'submodule add' '
@@ -146,7 +138,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod ../.. &&
+	inspect addtest/submod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -248,7 +240,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch ../.. &&
+	inspect addtest/submod-branch &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -264,7 +256,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz ../../.. &&
+	inspect addtest/dotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -280,7 +272,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+	inspect addtest/dotslashdotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -296,7 +288,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz ../../.. &&
+	inspect addtest/slashslashsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -312,7 +304,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod ../.. &&
+	inspect addtest/realsubmod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -328,7 +320,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 ../.. &&
+	inspect addtest/realsubmod2 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -359,7 +351,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 ../.. &&
+	inspect addtest/realsubmod3 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#2 lore raw

PATCH v6 2/5 introduce submodule.superprojectGitDir record

In reply to Emily Shaffer

Teach submodules a reference to their superproject's gitdir. This allows us to A) know that we're running from a submodule, and B) have a shortcut to the superproject's vitals, for example, configs.

By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the submodule's pointer. And by using the path from gitdir to gitdir, we can move the submodule within the superproject's tree structure without breaking the submodule's pointer, too. Finally, by pointing at the superproject's worktree gitdir (if it exists), we ensure that we can tell which worktree contains our submodule.

Since this hint value is only introduced during new submodule creation via git submodule add, though, there is more work to do to allow the record to be created at other times.

Once this new config is reliably in place, we can use it to know definitively that we are working in a submodule, and to know which superproject we are a submodule of. This allows us to do some value-added behavior, like letting "git status" print additional info about the submodule's status in relation to its superproject, or like letting the superproject and submodule share an additional config file separate from either one's local config.

---
 Documentation/config/submodule.txt | 12 +++++++++++
 builtin/submodule--helper.c        | 11 ++++++++++
 t/t7400-submodule-basic.sh         | 32 ++++++++++++++++++++----------
 3 files changed, 45 insertions(+), 10 deletions(-)
Documentation/config/submodule.txt +12 -0
index ee454f8126..61d975745e 100644
@@ -91,3 +91,15 @@ submodule.alternateErrorStrategy::
 	`ignore`, `info`, `die`. Default is `die`. Note that if set to `ignore`
 	or `info`, and if there is an error with the computed alternate, the
 	clone proceeds as if no alternate was specified.
+
+submodule.superprojectGitDir::
+	The relative path from the submodule's gitdir to its superproject's
+	gitdir. When Git is run in a repository, it usually makes no
+	difference whether this repository is standalone or a submodule, but if
+	this configuration variable is present, additional behavior may be
+	possible, such as "git status" printing additional information about
+	this submodule's status with respect to its superproject. This config
+	should only be present in projects which are submodules, but is not
+	guaranteed to be present in every submodule, so only optional
+	value-added behavior should be linked to it. It is set automatically
+	during submodule creation.
builtin/submodule--helper.c +11 -0
index e630f0c730..24f0ef2a78 100644
@@ -1838,6 +1838,17 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Set the path from submodule's new gitdir to superproject's gitdir.
+	 * The latter may be a worktree gitdir. However, it is not possible for
+	 * the submodule to have a worktree-specific gitdir or config at clone
+	 * time, because "extensions.worktreeConfig" is only valid when set in
+	 * the local gitconfig, which the brand new submodule does not have yet.
+	 */
+	git_config_set_in_file(p, "submodule.superprojectGitDir",
+			       relative_path(absolute_path(get_git_dir()),
+					     sm_gitdir, &sb));
+
 	free(sm_alternate);
 	free(error_strategy);
 
t/t7400-submodule-basic.sh +22 -10
index d69a5c0032..3d146491df 100755
@@ -109,12 +109,24 @@ submodurl=$(pwd -P)
 
 inspect() {
 	sub_dir=$1 &&
+	super_dir=$2 &&
 
 	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
 	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.superprojectGitDir contains the path from the
+	# submodule's gitdir to the superproject's gitdir.
+
+	super_abs_gitdir=$(git -C "$super_dir" rev-parse --absolute-git-dir) &&
+	sub_abs_gitdir=$(git -C "$sub_dir" rev-parse --absolute-git-dir) &&
+
+	[ "$(git -C "$sub_dir" config --get submodule.superprojectGitDir)" = \
+	  "$(test-tool path-utils relative_path "$super_abs_gitdir" \
+						"$sub_abs_gitdir")" ] &&
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
@@ -138,7 +150,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod &&
+	inspect addtest/submod addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -240,7 +252,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch &&
+	inspect addtest/submod-branch addtest &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -256,7 +268,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz &&
+	inspect addtest/dotsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -272,7 +284,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz &&
+	inspect addtest/dotslashdotsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -288,7 +300,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz &&
+	inspect addtest/slashslashsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -304,7 +316,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod &&
+	inspect addtest/realsubmod addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -320,7 +332,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 &&
+	inspect addtest/realsubmod2 addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -351,7 +363,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 &&
+	inspect addtest/realsubmod3 addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -492,7 +504,7 @@ test_expect_success 'update should work when path is an empty dir' '
 	git submodule update -q >update.out &&
 	test_must_be_empty update.out &&
 
-	inspect init &&
+	inspect init . &&
 	test_cmp expect head-sha1
 '
 
@@ -551,7 +563,7 @@ test_expect_success 'update should checkout rev1' '
 	echo "$rev1" >expect &&
 
 	git submodule update init &&
-	inspect init &&
+	inspect init . &&
 
 	test_cmp expect head-sha1
 '
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#4 lore raw

PATCH v6 3/5 submodule: record superproject gitdir during absorbgitdirs

In reply to Emily Shaffer

Already during 'git submodule add' we record a pointer to the superproject's gitdir. However, this doesn't help brand-new submodules created with 'git init' and later absorbed with 'git submodule absorbgitdirs'. Let's start adding that pointer during 'git submodule absorbgitdirs' too.

---
 submodule.c                        | 23 +++++++++
 t/t7412-submodule-absorbgitdirs.sh | 82 +++++++++++++++++++++++++++++-
 2 files changed, 103 insertions(+), 2 deletions(-)
submodule.c +23 -0
index c689070524..d7395c7551 100644
@@ -2097,6 +2097,9 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
 	struct strbuf new_gitdir = STRBUF_INIT;
 	const struct submodule *sub;
+	struct config_set sub_cs;
+	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
+	int tmp;
 
 	if (submodule_uses_worktrees(path))
 		die(_("relocate_gitdir for submodule '%s' with "
@@ -2127,6 +2130,26 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 
 	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
 
+	/*
+	 * Note location of superproject's gitdir. Because the submodule already
+	 * has a gitdir and local config, we can store this pointer from
+	 * worktree config to worktree config, if the submodule has
+	 * extensions.worktreeConfig set.
+	 */
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+	/* return 0 indicates config was found - we have a worktree config */
+	if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp))
+		strbuf_addstr(&config_path, ".worktree");
+
+	git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
+			       relative_path(absolute_path(get_git_dir()),
+					     real_new_git_dir, &sb));
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7412-submodule-absorbgitdirs.sh +80 -2

98 lines not on this page · raw message

Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#5 lore raw

PATCH v6 4/5 submodule: record superproject gitdir during 'update'

In reply to Emily Shaffer

A recorded path to the superproject's gitdir might be added during 'git submodule add', but in some cases - like submodules which were created before 'git submodule add' learned to record that info - it might be useful to update the hint. Let's do it during 'git submodule update', when we already have a handle to the superproject while calling operations on the submodules.

---
 git-submodule.sh            | 15 +++++++++++++++
 t/t7406-submodule-update.sh | 27 +++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
git-submodule.sh +15 -0
index 652861aa66..7c247bee7f 100755
@@ -449,6 +449,21 @@ cmd_update()
 			;;
 		esac
 
+		# Store a poitner to the superproject's gitdir. This may have
+		# changed, unless it's a fresh clone. Write to worktree if
+		# applicable, and point to superproject's worktree gitdir if
+		# applicable.
+		if test -z "$just_cloned"
+		then
+			sm_gitdir="$(git -C "$sm_path" rev-parse --absolute-git-dir)"
+			relative_gitdir="$(git rev-parse --path-format=relative \
+							 --prefix "${sm_gitdir}" \
+							 --git-dir)"
+
+			git -C "$sm_path" config --worktree \
+				submodule.superprojectgitdir "$relative_gitdir"
+		fi
+
 		if test -n "$recursive"
 		then
 			(
t/t7406-submodule-update.sh +27 -0
index 11cccbb333..b42a339982 100755
@@ -1061,4 +1061,31 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds superproject gitdir to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.superprojectGitdir &&
+	 git submodule update &&
+	 test-tool path-utils relative_path \
+		"$(git rev-parse --absolute-git-dir)" \
+		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
+	 git -C submodule config submodule.superprojectGitdir >actual &&
+	 test_cmp expect actual
+	)
+'
+
+test_expect_success 'submodule update uses config.worktree if applicable' '
+	(cd super &&
+	 git -C submodule config --unset submodule.superprojectGitDir &&
+	 git -C submodule config extensions.worktreeConfig true &&
+	 git submodule update &&
+	 test-tool path-utils relative_path \
+		"$(git rev-parse --absolute-git-dir)" \
+		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
+	 git -C submodule config submodule.superprojectGitdir >actual &&
+	 test_cmp expect actual &&
+
+	 test_file_not_empty "$(git -C submodule rev-parse --absolute-git-dir)/config.worktree"
+	)
+'
+
 test_done
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#6 lore raw

PATCH v6 5/5 submodule: use config to find superproject worktree

In reply to Emily Shaffer

Now that submodule.superprojectGitDir is being treated as the point of truth for whether a repo is a submodule or not, let's use it in git rev-parse --show-superproject-working-tree.

---

This commit may be more of an RFC - to demonstrate what life looks like if we use submodule.superprojectGitDir as the source of truth. But since 'git rev-parse --show-superproject-working-tree' is used in a lot of scripts in the wild[1], I'm not so sure it's a great example.

To be honest, I'd prefer to die("Try running 'git submodule update'") here, but I don't think that's very script-friendly. However, falling back on the old implementation kind of undermines the idea of treating submodule.superprojectGitDir as the point of truth. One thought I did have was to put that error message in builtin/rev-parse.c instead, and print it to stderr (per usual with user-visible messages) so it doesn't interfere with scripts, but gives a hint for debugging.

Another thought - captured by the NEEDSWORK in the diff - was that we could "heal" by adding that config after we know the worktree of the superproject.

Or, it could be that it won't be a problem for a long time, as anybody running 'git submodule update' will eventually have that config specified - that's why I included the traces, so we could try and get an understanding of how long repos remain in this state where they have submodules but nobody ran 'git submodule update'. But that will only give me visibility into submodule users at Google, who we expect to be making a lot of workflow changes soon, anyway.

1: https://github.com/search?q=%22--show-superproject-working-tree%22&type=code

---
 submodule.c          | 85 +++++++++++++++++++++++++++++++++++++++++++-
 t/t1500-rev-parse.sh |  9 +++++
 2 files changed, 93 insertions(+), 1 deletion(-)
submodule.c +84 -1
index d7395c7551..ad95cdda07 100644
@@ -2236,7 +2236,7 @@ void absorb_git_dir_into_superproject(const char *path,
 	}
 }
 
-int get_superproject_working_tree(struct strbuf *buf)
+static int get_superproject_working_tree_from_fs(struct strbuf *buf)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct strbuf sb = STRBUF_INIT;
@@ -2320,6 +2320,89 @@ int get_superproject_working_tree(struct strbuf *buf)
 	return ret;
 }
 
+int get_superproject_working_tree(struct strbuf *buf)
+{
+	char *super_gitdir = NULL;
+	const char *cwd = xgetcwd();
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct strbuf absolute_super_gitdir = STRBUF_INIT;
+	struct strbuf out = STRBUF_INIT;
+	struct string_list lines = STRING_LIST_INIT_NODUP;
+	struct string_list_item *it = NULL;
+	const char *wt_prefix = "worktree ";
+	int rc = 0;
+
+
+	/* Do we know we have a superproject? */
+	if (git_config_get_string("submodule.superprojectgitdir", &super_gitdir))
+		goto fallback;
+
+	strbuf_addf(&absolute_super_gitdir, "%s/%s", get_git_dir(), super_gitdir);
+
+	/*
+	 * NEEDSWORK: This is a child process call because worktree.c still
+	 * relies heavily on the_repository. If we can make worktree.c work with
+	 * a repository object - or, better yet, a gitdir path alone - then we
+	 * can drop the child process and ask worktree.c directly.
+	 *
+	 * Alternatively, if 'git worktree' learns a way to say 'the worktree
+	 * associated with this gitdir' instead of 'all worktrees', that would
+	 * be clearer because we could skip the foreach below.
+	 */
+
+	/* Get the output of `git worktree list` from that superproject */
+	prepare_other_repo_env(&cp.env_array, absolute_super_gitdir.buf);
+	strvec_pushl(&cp.args, "-C", absolute_super_gitdir.buf, "worktree", "list",
+		    "--porcelain", NULL);
+
+	cp.git_cmd = 1;
+	if (capture_command(&cp, &out, 0) ||
+	    !string_list_split_in_place(&lines, out.buf, '\n', -1))
+		die("submodule.superprojectGitDir is stale; run 'git submodule "
+		    "update' from the superproject.");
+
+	for_each_string_list_item(it, &lines) {
+		char *trimmed;
+		/*
+		 * Lines containing worktree dirs look like
+		 * 'worktree /some/path'
+		 */
+		if (strncmp(it->string, wt_prefix, strlen(wt_prefix)))
+			continue;
+		trimmed = it->string + strlen(wt_prefix);
+
+		/*
+		 * '/some/path/to/sub' is a prefix of '/some/path' - that's our
+		 * worktree
+		 */
+		if (!strncmp(cwd, trimmed, strlen(trimmed))) {
+			strbuf_addstr(buf, trimmed);
+			rc = 1;
+			goto cleanup;
+		}
+	}
+
+fallback:
+	/*
+	 * Because a submodule may have been created before
+	 * submodule.superprojectGitDir was introduced, fall back on checking
+	 * whether ../ is the superproject.
+	 */
+	trace2_data_intmax("submodule", the_repository,
+			   "get_superproject_wt/config_missing", rc);
+	rc = get_superproject_working_tree_from_fs(buf);
+
+	/*
+	 * NEEDSWORK: Is it possible to teach a submodule the path to its
+	 * superproject when this happens?
+	 */
+
+cleanup:
+	string_list_clear(&lines, 0);
+	strbuf_release(&out);
+	return rc;
+}
+
 /*
  * Put the gitdir for a submodule (given relative to the main
  * repository worktree) into `buf`, or return -1 on error.
t/t1500-rev-parse.sh +9 -0
index 1c2df08333..e569910289 100755
@@ -247,6 +247,15 @@ test_expect_success 'showing the superproject correctly' '
 	test_cmp expect out
 '
 
+test_expect_success 'show the superproject correctly without superprojectGitDir' '
+	# repos created before submodule.superprojectGitDir was introduced which
+	# have not been `git submodule update`-ed lately must not break
+	git -C super/dir/sub config --unset submodule.superprojectGitDir &&
+	echo $(pwd)/super >expect &&
+	git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
+	test_cmp expect out
+'
+
 # at least one external project depends on this behavior:
 test_expect_success 'rev-parse --since= unsqueezed ordering' '
 	x1=--since=1970-01-01T00:00:01Z &&
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
#7 lore raw

cover submodule: test what happens if submodule.superprojectGitDir isn't around

In reply to Emily Shaffer
Quoted 16 lines · Emily Shaffer

[...]
A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

Quoted 8 lines

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Did you try running the ad-hoc benchmark I included in [1] on that Google NFS? I've dealt with some slow-ish network filesystems, but if it's slower than AIX's local FS (where I couldn't see a difference) I'd put money on it being a cross-Atlantic mount or something :)

Re your:

"this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules."

In your 5/5 it says, in seeming contradiction to this:

This commit may be more of an RFC - to demonstrate what life looks like if we use submodule.superprojectGitDir as the source of truth. But since 'git rev-parse --show-superproject-working-tree' is used in a lot of scripts in the wild[1], I'm not so sure it's a great example.

To be honest, I'd prefer to die("Try running 'git submodule update'") here, but I don't think that's very script-friendly. However, falling back on the old implementation kind of undermines the idea of treating submodule.superprojectGitDir as the point of truth.

Most of what I've been suggesting in my [1] and related is that I'm confused about if & how this is a pure caching mechanism.

Removing mentions of it being a cache but it seemingly still being a cache at the tip of this series has just added to that confusion for me :)

Anyway. While I do think this caching mechanism is probably unnecessary in the short to medium term, i.e. it seems to the extent that it was ever needed was due to some bridging of *.sh<->*.c that we're *this* close to eliminating anyway.

But maybe I'm wrong. The benchmark I suggested above on that Google NFS might be indicative. I don't really see how something that'll be doing a bunch of FS ops anyway is going to be noticeably slower with that approach, but maybe opening the index/tree of the superproject is more expensive than I'm expecting.

In any case, all of that's not the hill I'm picking to die on. If you'd like to go ahead with this cache-or-not-a-cache then sure, I won't belabor that point.

I *do* strongly think if we're doing so though that we should have something like this on top. I.e. let's test wha happens if we do and don't have this "caching" variable, which is demonstrably easy to do.

Benchmarking the two gives me:

    $ git hyperfine -L rev HEAD~0 -L s true,false -s 'make -j8 all' '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR={s} ./t7412-submodule-absorbgitdirs.sh)'
    Benchmark 1: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     545.9 ms ±   1.6 ms    [User: 490.3 ms, System: 114.0 ms]
      Range (min … max):   543.5 ms … 548.1 ms    10 runs
    Benchmark 2: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     537.9 ms ±  11.4 ms    [User: 476.8 ms, System: 117.6 ms]
      Range (min … max):   532.7 ms … 570.1 ms    10 runs
     
    Summary
      '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0' ran
        1.01 ± 0.02 times faster than '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0'

I.e. not using the cache is either indistinguishable or a bit faster (the "a bit faster" is definitely due to just running less test code though).

I'm sending this before the CI run[2] finishes (which now tests both modes), but both of these work for me locally on a full test suite run.

1. https://lore.kernel.org/git/[email protected]/
2. https://github.com/avar/git/runs/4237446991?check_suite_focus=true

Ævar Arnfjörð Bjarmason (2):
submodule tests: fix potentially broken "config .. --unset"
submodule: add test mode for checking absence of "superProjectGitDir"

 ci/run-build-and-tests.sh          |  1 +
 git-submodule.sh                   |  2 +-
 submodule.c                        |  7 +++++++
 t/lib-submodule-superproject.sh    | 24 ++++++++++++++++++++++++
 t/t7406-submodule-update.sh        | 13 ++++++-------
 t/t7412-submodule-absorbgitdirs.sh | 19 ++++++-------------
 6 files changed, 45 insertions(+), 21 deletions(-)
 create mode 100644 t/lib-submodule-superproject.sh
Signature · 1 line
2.34.0.796.g2c87ed6146a
#8 lore raw

RFC PATCH 1/2 submodule tests: fix potentially broken "config .. --unset"

These "config ... --unset" at the start must be guarded by something like a test_might_fail, or we'll fail if a previous test didn't run, e.g. due to the --run option.

---
 t/t7406-submodule-update.sh | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
t/t7406-submodule-update.sh +2 -2
index b42a339982b..01e1acaf300 100755
@@ -1063,7 +1063,7 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 
 test_expect_success 'submodule update adds superproject gitdir to older repos' '
 	(cd super &&
-	 git -C submodule config --unset submodule.superprojectGitdir &&
+	 test_might_fail git -C submodule config --unset submodule.superprojectGitdir &&
 	 git submodule update &&
 	 test-tool path-utils relative_path \
 		"$(git rev-parse --absolute-git-dir)" \
@@ -1075,7 +1075,7 @@ test_expect_success 'submodule update adds superproject gitdir to older repos' '
 
 test_expect_success 'submodule update uses config.worktree if applicable' '
 	(cd super &&
-	 git -C submodule config --unset submodule.superprojectGitDir &&
+	 test_might_fail git -C submodule config --unset submodule.superprojectGitDir &&
 	 git -C submodule config extensions.worktreeConfig true &&
 	 git submodule update &&
 	 test-tool path-utils relative_path \
Signature · 1 line
2.34.0.796.g2c87ed6146a
#9 lore raw

RFC PATCH 2/2 submodule: add test mode for checking absence of "superProjectGitDir"

In reply to Ævar Arnfjörð Bjarmason

Add a GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false test mode to assert what happens if "submodule.superProjectGitDir" is absent or missing, this checks if the "fallback" codepath in get_superproject_working_tree() is equivalent to the config lookup.

---
 ci/run-build-and-tests.sh          |  1 +
 git-submodule.sh                   |  2 +-
 submodule.c                        |  7 +++++++
 t/lib-submodule-superproject.sh    | 24 ++++++++++++++++++++++++
 t/t7406-submodule-update.sh        |  9 ++++-----
 t/t7412-submodule-absorbgitdirs.sh | 19 ++++++-------------
 6 files changed, 43 insertions(+), 19 deletions(-)
 create mode 100644 t/lib-submodule-superproject.sh
ci/run-build-and-tests.sh +1 -0
index cc62616d806..5132a210057 100755
@@ -33,6 +33,7 @@ linux-gcc)
 	export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
 	export GIT_TEST_WRITE_REV_INDEX=1
 	export GIT_TEST_CHECKOUT_WORKERS=2
+	export GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false
 	make test
 	;;
 linux-clang)
git-submodule.sh +1 -1
index 7c247bee7f6..2b423ee05bc 100755
@@ -453,7 +453,7 @@ cmd_update()
 		# changed, unless it's a fresh clone. Write to worktree if
 		# applicable, and point to superproject's worktree gitdir if
 		# applicable.
-		if test -z "$just_cloned"
+		if test -z "$just_cloned" && test "$GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR" != "false"
 		then
 			sm_gitdir="$(git -C "$sm_path" rev-parse --absolute-git-dir)"
 			relative_gitdir="$(git rev-parse --path-format=relative \
submodule.c +7 -0
index ad95cdda07d..f0411a320a8 100644
@@ -2143,6 +2143,9 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp))
 		strbuf_addstr(&config_path, ".worktree");
 
+	if (!git_env_bool("GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR", 1))
+		goto fallback;
+
 	git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
 			       relative_path(absolute_path(get_git_dir()),
 					     real_new_git_dir, &sb));
@@ -2150,6 +2153,8 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	git_configset_clear(&sub_cs);
 	strbuf_release(&config_path);
 	strbuf_release(&sb);
+
+fallback:
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
@@ -2332,6 +2337,8 @@ int get_superproject_working_tree(struct strbuf *buf)
 	const char *wt_prefix = "worktree ";
 	int rc = 0;
 
+	if (!git_env_bool("GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR", 1))
+		goto fallback;
 
 	/* Do we know we have a superproject? */
 	if (git_config_get_string("submodule.superprojectgitdir", &super_gitdir))
t/lib-submodule-superproject.sh new file +24 -0
new file mode 100644
index 00000000000..4d49dd3782e
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_lazy_prereq SUBMODULE_CACHE_SUPERPROJECT_DIR '
+	test_bool_env GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR true
+'
+
+test_cmp_submodule_superprojectgitdir () {
+	if ! test_have_prereq SUBMODULE_CACHE_SUPERPROJECT_DIR
+	then
+		return 0
+	fi
+
+	git -C "$1" config submodule.superprojectGitDir >actual &&
+	test_cmp expect actual
+}
+
+test_file_not_empty_superprojectgitdir () {
+	if ! test_have_prereq SUBMODULE_CACHE_SUPERPROJECT_DIR
+	then
+		return 0
+	fi
+
+	test_file_not_empty "$(git -C $1 rev-parse --absolute-git-dir)/$2"
+}
t/t7406-submodule-update.sh +4 -5
index 01e1acaf300..f362f8d0ef0 100755
@@ -13,6 +13,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-superproject.sh
 
 
 compare_head()
@@ -1068,8 +1069,7 @@ test_expect_success 'submodule update adds superproject gitdir to older repos' '
 	 test-tool path-utils relative_path \
 		"$(git rev-parse --absolute-git-dir)" \
 		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
-	 git -C submodule config submodule.superprojectGitdir >actual &&
-	 test_cmp expect actual
+	 test_cmp_submodule_superprojectgitdir submodule
 	)
 '
 
@@ -1081,10 +1081,9 @@ test_expect_success 'submodule update uses config.worktree if applicable' '
 	 test-tool path-utils relative_path \
 		"$(git rev-parse --absolute-git-dir)" \
 		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
-	 git -C submodule config submodule.superprojectGitdir >actual &&
-	 test_cmp expect actual &&
+	 test_cmp_submodule_superprojectgitdir submodule &&
 
-	 test_file_not_empty "$(git -C submodule rev-parse --absolute-git-dir)/config.worktree"
+	 test_file_not_empty_superprojectgitdir submodule config.worktree
 	)
 '
 
t/t7412-submodule-absorbgitdirs.sh +6 -13
index 5753f902687..6faab7e56e9 100755
@@ -7,6 +7,7 @@ directory into the superproject.
 '
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-superproject.sh
 
 test_expect_success 'setup a real submodule' '
 	git init sub1 &&
@@ -38,9 +39,7 @@ test_expect_success 'absorb the git dir' '
 
 	test-tool path-utils relative_path "$superproject_gitdir" \
 		"$submodule_gitdir" >expect &&
-	git -C sub1 config submodule.superprojectGitDir >actual &&
-
-	test_cmp expect actual
+	test_cmp_submodule_superprojectgitdir sub1
 '
 
 test_expect_success 'absorbing does not fail for deinitialized submodules' '
@@ -78,9 +77,7 @@ test_expect_success 'absorb the git dir in a nested submodule' '
 
 	test-tool path-utils relative_path "$sub1_gitdir" "$sub1_nested_gitdir" \
 		>expect &&
-	git -C sub1/nested config submodule.superprojectGitDir >actual &&
-
-	test_cmp expect actual
+	test_cmp_submodule_superprojectgitdir sub1
 '
 
 test_expect_success 're-setup nested submodule' '
@@ -170,9 +167,7 @@ test_expect_success 'absorbgitdirs works when called from a superproject worktre
 
 	test-tool path-utils relative_path "$superproject_gitdir" \
 		"$submodule_gitdir" >expect &&
-	git -C sub4 config submodule.superprojectGitDir >actual &&
-
-	test_cmp expect actual
+	test_cmp_submodule_superprojectgitdir sub4
 	)
 '
 
@@ -199,12 +194,10 @@ test_expect_success 'absorbgitdirs works with a submodule with worktree config'
 
 	test-tool path-utils relative_path "$superproject_gitdir" \
 		"$submodule_gitdir" >expect &&
-	git -C sub5 config submodule.superprojectGitDir >actual &&
-
-	test_cmp expect actual &&
+	test_cmp_submodule_superprojectgitdir sub5 &&
 
 	# make sure the config went into the submodule config.worktree
-	test_file_not_empty "$submodule_gitdir/config.worktree"
+	test_file_not_empty_superprojectgitdir sub5 config.worktree
 	)
 '
 
Signature · 1 line
2.34.0.796.g2c87ed6146a
#12 lore raw
In reply to Emily Shaffer
Quoted 2 lines · Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

Also for reference, v4 and v5 (as a reply to v4) can be found here:
https://lore.kernel.org/git/[email protected]/

Quoted 8 lines

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Makes sense. Also, thanks for the tests covering what happens when this is run from worktrees.

Quoted 7 lines

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

OK - this is not the "this variable being missing is OK" idea that I had [1], but we want to be able to depend on it to some extent. (And it is not a cache either - we are not planning to perform an operation to obtain the superproject gitdir if the cache is missing, but we are just going to assume that there is no superproject.)

To that end, the 5th patch is misleading - it is behaving exactly like a cache. I think it's better to drop it.

What would make sense to me (and seems to be in the spirit of this patch set) is to describe this as something that Git commands can rely on to determine if the current repo is a submodule, for performance reasons. So maybe Git commands/parameters that directly reference the submodule concept like "--show-superproject-working-tree" will work hard to find the superproject (by searching the filesystem), but those that do not (e.g. "git status") can make assumptions.

Making this variable a source of truth wouldn't work, I think, because the source of truth is whether this repo appears in a .gitmodules file (and that hasn't changed).

To this end, I'll comment on the changes I'd like to see on the individual patches too.

[1] https://lore.kernel.org/git/[email protected]/

#3 lore raw

Re: [PATCH v6 2/5] introduce submodule.superprojectGitDir record

In reply to Emily Shaffer
Quoted 3 lines · Emily Shaffer

Teach submodules a reference to their superproject's gitdir. This allows us to A) know that we're running from a submodule, and B) have a shortcut to the superproject's vitals, for example, configs.

If we're going with my proposal [1], I think it's worth further explaining the concept right at the beginning of the commit message:

Teach submodules a config variable referencing their superproject's gitdir. Git commands may rely on this reference to determine if the current repo is a submodule to another repo: if this reference is absent, Git may assume that the current repo is not a submodule. In practice, commands and arguments that specifially reference the submodule relationship (like "rev-parse --show-superproject-working-tree") will still search the ancestor directory, but others (say, a "git status" that prints the submodule's status in relation to its superproject or a config option that lets the superproject and submodule share config) would not.

[1] https://lore.kernel.org/git/[email protected]/

Quoted 7 lines

By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the submodule's pointer. And by using the path from gitdir to gitdir, we can move the submodule within the superproject's tree structure without breaking the submodule's pointer, too. Finally, by pointing at the superproject's worktree gitdir (if it exists), we ensure that we can tell which worktree contains our submodule.

OK.

Quoted 3 lines

Since this hint value is only introduced during new submodule creation via git submodule add, though, there is more work to do to allow the record to be created at other times.

This is not a hint anymore. I would reword as:

This commit teaches "git submodule add" to add the aforementioned config variable. Subsequent commits will teach other commands to do so.

Quoted 7 lines

Once this new config is reliably in place, we can use it to know definitively that we are working in a submodule, and to know which superproject we are a submodule of. This allows us to do some value-added behavior, like letting "git status" print additional info about the submodule's status in relation to its superproject, or like letting the superproject and submodule share an additional config file separate from either one's local config.

I folded this into the first paragraph above, so this would no longer needed if you used my suggestion above.

Documentation/config/submodule.txt submodule.alternateErrorStrategy:: · line 105

Quoted 11 lines
Documentation/config/submodule.txt +11 -0
+submodule.superprojectGitDir::
+	The relative path from the submodule's gitdir to its superproject's
+	gitdir. When Git is run in a repository, it usually makes no
+	difference whether this repository is standalone or a submodule, but if
+	this configuration variable is present, additional behavior may be
+	possible, such as "git status" printing additional information about
+	this submodule's status with respect to its superproject. This config
+	should only be present in projects which are submodules, but is not
+	guaranteed to be present in every submodule, so only optional
+	value-added behavior should be linked to it. It is set automatically
+	during submodule creation.

If we're going to rely on the variable more, this needs to be updated. Maybe:

If this repository is a submodule, the relative path from this repo's gitdir to its superproject's gitdir. Git commands may rely on this reference to determine if the current repo is a submodule to another
repo: if this reference is absent, Git may assume that the current repo is not a submodule (this does not make a difference to most Git commands). It is set automatically during ??

(and probably each subsequent patch will need to update the ??)

#10 lore raw

Re: [RFC PATCH 0/2] submodule: test what happens if submodule.superprojectGitDir isn't around

In reply to Ævar Arnfjörð Bjarmason
Quoted 49 lines · Ævar Arnfjörð Bjarmason
Quoted 16 lines · Emily Shaffer

[...]
A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

Quoted 8 lines

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Did you try running the ad-hoc benchmark I included in [1] on that Google NFS? I've dealt with some slow-ish network filesystems, but if it's slower than AIX's local FS (where I couldn't see a difference) I'd put money on it being a cross-Atlantic mount or something :)

Re your:

"this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules."

In your 5/5 it says, in seeming contradiction to this:

This commit may be more of an RFC - to demonstrate what life looks like if we use submodule.superprojectGitDir as the source of truth. But since 'git rev-parse --show-superproject-working-tree' is used in a lot of scripts in the wild[1], I'm not so sure it's a great example.

To be honest, I'd prefer to die("Try running 'git submodule update'") here, but I don't think that's very script-friendly. However, falling back on the old implementation kind of undermines the idea of treating submodule.superprojectGitDir as the point of truth.

Quoted 6 lines

Most of what I've been suggesting in my [1] and related is that I'm confused about if & how this is a pure caching mechanism.

Removing mentions of it being a cache but it seemingly still being a cache at the tip of this series has just added to that confusion for me :)

Yeah, I think this was a bad choice for me to include that patch. I was really hopeful that I could show off "look, we don't need to ever hunt in the FS above us", but for established repos, that's a bad idea (because lots of people are already using this 'git rev-parse --show-superproject-work-tree' thing in scripts, like I mentioned). So I think it was a mistake to include it at all. Rather, I think it's probably a better idea to treat that particular entry point as "legacy" and implement other things using 'submodule.superprojectGitDir' directly.

Because the patch 5 illustrates: "I'm saying that this new config isn't a cache, but look, here's how I can treat it like a cache that might be invalid and here's how I can fall back on a potentially expensive operation anyways." I think I could have illustrated it a little better with something like "here's a brand new 'git rev-parse --show-superproject-gitdir'" which directly calls on the new config.

So, sorry about that.

Quoted 12 lines

Anyway. While I do think this caching mechanism is probably unnecessary in the short to medium term, i.e. it seems to the extent that it was ever needed was due to some bridging of *.sh<->*.c that we're *this* close to eliminating anyway.

But maybe I'm wrong. The benchmark I suggested above on that Google NFS might be indicative. I don't really see how something that'll be doing a bunch of FS ops anyway is going to be noticeably slower with that approach, but maybe opening the index/tree of the superproject is more expensive than I'm expecting.

Quoted 3 lines

In any case, all of that's not the hill I'm picking to die on. If you'd like to go ahead with this cache-or-not-a-cache then sure, I won't belabor that point.

Yeah, I think I would. I've heard some serious reservations from others on my team about trying to use filesystem traversal here at all, so I think that would be an uphill battle.

Quoted 20 lines

I *do* strongly think if we're doing so though that we should have something like this on top. I.e. let's test wha happens if we do and don't have this "caching" variable, which is demonstrably easy to do.

Benchmarking the two gives me:

    $ git hyperfine -L rev HEAD~0 -L s true,false -s 'make -j8 all' '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR={s} ./t7412-submodule-absorbgitdirs.sh)'
    Benchmark 1: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     545.9 ms ±   1.6 ms    [User: 490.3 ms, System: 114.0 ms]
      Range (min … max):   543.5 ms … 548.1 ms    10 runs
    Benchmark 2: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     537.9 ms ±  11.4 ms    [User: 476.8 ms, System: 117.6 ms]
      Range (min … max):   532.7 ms … 570.1 ms    10 runs
     
    Summary
      '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0' ran
        1.01 ± 0.02 times faster than '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0'
Quoted 3 lines

I.e. not using the cache is either indistinguishable or a bit faster (the "a bit faster" is definitely due to just running less test code though).

Yeah, once again, I think it is better to treat "git rev-parse --show-superproject-work-tree" as "legacy" and to rely solely on the config for new options, meaning that "what happens without this variable" is as simple as "we treat it like it's a standalone repository with no superproject", rather than a performance difference at all.

- Emily

Quoted 24 lines

I'm sending this before the CI run[2] finishes (which now tests both modes), but both of these work for me locally on a full test suite run.

1. https://lore.kernel.org/git/[email protected]/
2. https://github.com/avar/git/runs/4237446991?check_suite_focus=true

Ævar Arnfjörð Bjarmason (2):
submodule tests: fix potentially broken "config .. --unset"
submodule: add test mode for checking absence of "superProjectGitDir"

 ci/run-build-and-tests.sh          |  1 +
 git-submodule.sh                   |  2 +-
 submodule.c                        |  7 +++++++
 t/lib-submodule-superproject.sh    | 24 ++++++++++++++++++++++++
 t/t7406-submodule-update.sh        | 13 ++++++-------
 t/t7412-submodule-absorbgitdirs.sh | 19 ++++++-------------
 6 files changed, 45 insertions(+), 21 deletions(-)
 create mode 100644 t/lib-submodule-superproject.sh
Signature · 1 line
2.34.0.796.g2c87ed6146a
#13 lore raw
In reply to Jonathan Tan
Quoted 28 lines · Jonathan Tan
Quoted 2 lines · Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

Also for reference, v4 and v5 (as a reply to v4) can be found here:
https://lore.kernel.org/git/[email protected]/

Quoted 8 lines

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Makes sense. Also, thanks for the tests covering what happens when this is run from worktrees.

Quoted 7 lines

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

Quoted 8 lines

OK - this is not the "this variable being missing is OK" idea that I had [1], but we want to be able to depend on it to some extent. (And it is not a cache either - we are not planning to perform an operation to obtain the superproject gitdir if the cache is missing, but we are just going to assume that there is no superproject.)

To that end, the 5th patch is misleading - it is behaving exactly like a cache. I think it's better to drop it.

Yeah, I think you are right.

Quoted 8 lines

What would make sense to me (and seems to be in the spirit of this patch set) is to describe this as something that Git commands can rely on to determine if the current repo is a submodule, for performance reasons. So maybe Git commands/parameters that directly reference the submodule concept like "--show-superproject-working-tree" will work hard to find the superproject (by searching the filesystem), but those that do not (e.g. "git status") can make assumptions.

Oh interesting, I like the idea of that distinction. Thanks for the suggestion.

I wonder, though, how best to delineate. I'd almost rather say like I suggested to Ævar
(https://lore.kernel.org/git/YZ1KLNwsxx7IR1%2B5%40google.com), that we can treat "--show-superproject-working-tree" as a "legacy" option people are already using, and treat anything added from now on as "if it doesn't think it is a submodule, you should 'git submodule update' in the superproject". That relies on us being able to reliably keep the value of submodule.superprojectGitDir correct, but I think the gitdir->gitdir linking helps with that (as opposed to earlier iterations).

Quoted 4 lines

Making this variable a source of truth wouldn't work, I think, because the source of truth is whether this repo appears in a .gitmodules file (and that hasn't changed).

Interestingly, '--show-superproject-working-tree' doesn't check the .gitmodules file at all. It checks whether the project found in the filesystem above it knows about an object named path/to/superproject/ which is a gitlink - that is, it asks the index, not .gitmodules, as far as I understand it. So if the only existing alternative to submodule.superprojectGitDir isn't treating .gitmodules as source of truth, then what does that mean?

Quoted 5 lines

To this end, I'll comment on the changes I'd like to see on the individual patches too.

[1] https://lore.kernel.org/git/[email protected]/

#11 lore raw

Re: [RFC PATCH 0/2] submodule: test what happens if submodule.superprojectGitDir isn't around

In reply to Emily Shaffer
Quoted 69 lines · Emily Shaffer
Quoted 51 lines · Ævar Arnfjörð Bjarmason
Quoted 16 lines · Emily Shaffer

[...]
A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

Quoted 9 lines

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going
in, rather than trying to guess from the filesystem going forward.

Did you try running the ad-hoc benchmark I included in [1] on that Google NFS? I've dealt with some slow-ish network filesystems, but if it's slower than AIX's local FS (where I couldn't see a difference) I'd put money on it being a cross-Atlantic mount or something :)

Re your:

"this isn't a cache of some expensive operation, but rather
intended to be the source of truth for all submodules."

In your 5/5 it says, in seeming contradiction to this:

This commit may be more of an RFC - to demonstrate what life looks like if we use submodule.superprojectGitDir as the source of truth. But since 'git rev-parse --show-superproject-working-tree' is used in a lot of scripts in the wild[1], I'm not so sure it's a great example.

To be honest, I'd prefer to die("Try running 'git submodule update'") here, but I don't think that's very script-friendly. However, falling back on the old implementation kind of undermines the idea of treating submodule.superprojectGitDir as the point of truth.

Quoted 6 lines

Most of what I've been suggesting in my [1] and related is that I'm confused about if & how this is a pure caching mechanism.

Removing mentions of it being a cache but it seemingly still being a cache at the tip of this series has just added to that confusion for me :)

Yeah, I think this was a bad choice for me to include that patch. I was really hopeful that I could show off "look, we don't need to ever hunt in the FS above us", but for established repos, that's a bad idea (because lots of people are already using this 'git rev-parse --show-superproject-work-tree' thing in scripts, like I mentioned). So I think it was a mistake to include it at all. Rather, I think it's probably a better idea to treat that particular entry point as "legacy" and implement other things using 'submodule.superprojectGitDir' directly.

Quoted 8 lines

Because the patch 5 illustrates: "I'm saying that this new config isn't a cache, but look, here's how I can treat it like a cache that might be invalid and here's how I can fall back on a potentially expensive operation anyways." I think I could have illustrated it a little better with something like "here's a brand new 'git rev-parse --show-superproject-gitdir'" which directly calls on the new config.

So, sorry about that.

The RFC series here shows that the config & the on-the-fly discovery 1=1 map to each other (at least in terms of the tests your series adds).

Why wouldn't the same be the case with a --show-superproject-gitdir? Unless the fallback implementation was intentionally removed.

Quoted 12 lines
Quoted 12 lines

Anyway. While I do think this caching mechanism is probably unnecessary in the short to medium term, i.e. it seems to the extent that it was ever needed was due to some bridging of *.sh<->*.c that we're *this* close to eliminating anyway.

But maybe I'm wrong. The benchmark I suggested above on that Google NFS might be indicative. I don't really see how something that'll be doing a bunch of FS ops anyway is going to be noticeably slower with that approach, but maybe opening the index/tree of the superproject is more expensive than I'm expecting.

Quoted 7 lines
Quoted 3 lines

In any case, all of that's not the hill I'm picking to die on. If you'd like to go ahead with this cache-or-not-a-cache then sure, I won't belabor that point.

Yeah, I think I would. I've heard some serious reservations from others on my team about trying to use filesystem traversal here at all, so I think that would be an uphill battle.

Do they have any benchmarks to share? :)

Quoted 24 lines
Quoted 20 lines

I *do* strongly think if we're doing so though that we should have something like this on top. I.e. let's test wha happens if we do and don't have this "caching" variable, which is demonstrably easy to do.

Benchmarking the two gives me:

    $ git hyperfine -L rev HEAD~0 -L s true,false -s 'make -j8 all' '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR={s} ./t7412-submodule-absorbgitdirs.sh)'
    Benchmark 1: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     545.9 ms ±   1.6 ms    [User: 490.3 ms, System: 114.0 ms]
      Range (min … max):   543.5 ms … 548.1 ms    10 runs
    Benchmark 2: (cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0
      Time (mean ± σ):     537.9 ms ±  11.4 ms    [User: 476.8 ms, System: 117.6 ms]
      Range (min … max):   532.7 ms … 570.1 ms    10 runs
     
    Summary
      '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=false ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0' ran
        1.01 ± 0.02 times faster than '(cd t && GIT_TEST_SUBMODULE_CACHE_SUPERPROJECT_DIR=true ./t7412-submodule-absorbgitdirs.sh)' in 'HEAD~0'
Quoted 3 lines

I.e. not using the cache is either indistinguishable or a bit faster (the "a bit faster" is definitely due to just running less test code though).

Quoted 5 lines

Yeah, once again, I think it is better to treat "git rev-parse --show-superproject-work-tree" as "legacy" and to rely solely on the config for new options, meaning that "what happens without this variable" is as simple as "we treat it like it's a standalone repository with no superproject", rather than a performance difference at all.

Why would it be better to have a hard reliance on the config for new features or options? Your original CL says[1]:

It's expensive and non-definitive to try and guess whether or not the current repo is a submodule.

Maybe that's the case somewhere, I haven't been able to dig up the "expensive" part (aside from the fixable case of exec-ing rev-parse in a loop).

Which leaves "non-definitive", that may be the case, but as the RFC patches here show if there is such a case, it's not covered by any of your existing tests in this series.

I'd think if those two things hold true (not expensive, and we can unambiguously discover it on the fly) our bias should be towards not introducing an indirection in the config, as such a thing needs to be kept up-to-date, and which may become inaccurate for whatever reason. Whereas the FS relationship between the won't ever be out-of-date.

1. https://lore.kernel.org/git/[email protected]

#14 lore raw

v6 cover teach submodules to know they're submodules

In reply to Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

CI run: https://github.com/nasamuffin/git/actions/runs/1780282431

Since v6:

I've dropped the fifth commit to use this new config for git rev-parse --show-superproject-working-tree. I think it did more harm than good - that tool uses an odd way to determine whether the superproject is actually the superproject, anyways.

I poked a little bit at trying to find some benchmark to demonstrate that "submodule.superprojectGitDir" is actually faster - but I didn't end up able to write one without writing a ton of new code to traverse the filesystem. To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

So, the only real changes between v6 and v7 are some documentation changes suggested by Jonathan Tan
(https://lore.kernel.org/git/20211117234300.2598132-1-jonathantanmy%40google.com).

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Since v4:

The only real change here is a slight semantics change to map from <submodule gitdir> to <superproject common git dir>. In every case *except* for when the superproject has a worktree, this changes nothing. For the case when the superproject has a worktree, this means that now submodules will refer to the general superproject common dir (e.g. no worktree-specific refs or configs or whatnot).

I *think* that because a submodule should exist in the context of the common dir, not the worktree gitdir, that is ok. However, it does mean it would be difficult to do something like sharing a config specific to the worktree (the initial goal of this series).

$ROOT/.git
$ROOT/.git/config.superproject <- shared by $ROOT/.git/modules/sub $ROOT/.git/modules/sub <- points to $ROOT/.git
$ROOT/.git/worktrees/wt
$ROOT/.git/worktrees/wt/config.superproject <- contains a certain config-based pre-commit hook

If the submodule only knows about the common dir, that is tough, because the submodule would basically have to guess which worktree it's in from its own path. There would be no way for '$WT/sub' to inherit '$ROOT/.git/worktrees/wt/config.superproject'.

That said... right now, we don't support submodules in worktrees very well at all. A submodule in a worktree will get a brand new gitdir in $ROOT/.git/worktrees/modules/ (and that brand new gitdir would point to the super's common dir). So I think we can punt on this entire question until we teach submodules and worktrees to play more gracefully together (it's on my long list...), and at that time we can probably introduce a pointer from $ROOT/.git/modules/sub/worktrees/wt/ to $ROOT/.git/worktrees/wt/....

Or, to summarize the long ramble above: "this is still kind of weird with worktrees, but let's fix it later when we fix worktrees more thoroughly".

(More rambling about worktree weirdness here:
https://lore.kernel.org/git/YYRaII8YWVxlBqsF%40google.com )

Since v3, a pretty major change: the semantics of submodule.superprojectGitDir has changed, to point from the submodule's gitdir to the superproject's gitdir (in v3 and earlier, we kept a path from the submodule's *worktree* to the superproject's gitdir instead). This cleans up some of the confusions about the behavior when a submodule worktree moves around in the superproject's tree, or in a future when we support submodules having multiple worktrees.

I also tried to simplify the tests to use 'test-tool path-utils relative_path' everywhere - I think that makes them much more clear for a test reader, but if you're reviewing and it isn't obvious what we're testing for, please speak up.

I think this is pretty mature and there was a lot of general agreement that the gitdir->gitdir association was the way to go, so please be brutal and look for nits, leaks, etc. this round ;)
[/v4 cover letter]

Emily Shaffer (4):
t7400-submodule-basic: modernize inspect() helper introduce submodule.superprojectGitDir record
submodule: record superproject gitdir during absorbgitdirs
submodule: record superproject gitdir during 'update'

 Documentation/config/submodule.txt |  9 ++++
 builtin/submodule--helper.c        | 11 ++++
 git-submodule.sh                   | 15 ++++++
 submodule.c                        | 23 +++++++++
 t/t7400-submodule-basic.sh         | 54 +++++++++++---------
 t/t7406-submodule-update.sh        | 27 ++++++++++
 t/t7412-submodule-absorbgitdirs.sh | 82 +++++++++++++++++++++++++++++-
 7 files changed, 194 insertions(+), 27 deletions(-)

Range-diff against v6:

1:  f1b08a7057 = 1:  251510c687 t7400-submodule-basic: modernize inspect() helper
2:  d46c8439ab ! 2:  1a85deb1c5 introduce submodule.superprojectGitDir record

@@ Metadata
## Commit message ##
introduce submodule.superprojectGitDir record

- Teach submodules a reference to their superproject's gitdir. This allows
- us to A) know that we're running from a submodule, and B) have a
- shortcut to the superproject's vitals, for example, configs.
+ Teach submodules a config variable referencing to their superproject's
+ gitdir. Git commands can rely on this reference to determine whether the
+ current repo is a submodule to another repo. If this reference is
+ absent, Git may assume the current repo is not a submodule.
+
+ In practice, some commands which specifically reference the submodule
+ relationship, like 'git rev-parse --show-superproject-working-tree',
+ will still search the parent directory. Other newly added or implicit
+ behavior, such as "git status" showing the submodule's status in
+ relation to the superproject, or a config which is shared between the
+ superproject and the submodule, should not search the parent directory
+ and instead rely on this "submodule.superprojectGitDir" config.

By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the
@@ Commit message
superproject's worktree gitdir (if it exists), we ensure that we can tell which worktree contains our submodule.

- Since this hint value is only introduced during new submodule creation
- via git submodule add, though, there is more work to do to allow the
- record to be created at other times.
-
- Once this new config is reliably in place, we can use it to know
- definitively that we are working in a submodule, and to know which
- superproject we are a submodule of. This allows us to do some
- value-added behavior, like letting "git status" print additional info
- about the submodule's status in relation to its superproject, or like
- letting the superproject and submodule share an additional config file
- separate from either one's local config.
+ This commit teaches "git submodule add" to add the aformentioned config
+ variable. Subsequent commits will teach other commands to do so.

@@ Documentation/config/submodule.txt: submodule.alternateErrorStrategy::
clone proceeds as if no alternate was specified.
+
+submodule.superprojectGitDir::
-+ The relative path from the submodule's gitdir to its superproject's
-+ gitdir. When Git is run in a repository, it usually makes no
-+ difference whether this repository is standalone or a submodule, but if
-+ this configuration variable is present, additional behavior may be
-+ possible, such as "git status" printing additional information about
-+ this submodule's status with respect to its superproject. This config
-+ should only be present in projects which are submodules, but is not
-+ guaranteed to be present in every submodule, so only optional
-+ value-added behavior should be linked to it. It is set automatically
-+ during submodule creation.
++ If this repository is a submodule, the relative path from this repo's
++ gitdir to its superproject's gitdir. Git commands may rely on this
++ reference to determine whether the current repo is a submodule to
++ another repo; if this reference is absent, Git will treat the current
++ repo as though it is not a submodule (this does not make a difference to
++ most Git commands). It is set automatically during submodule creation.

## builtin/submodule--helper.c ##
@@ builtin/submodule--helper.c: static int clone_submodule(struct module_clone_data *clone_data)
3: 63ddaf5608 ! 3: 7a44b0edf9 submodule: record superproject gitdir during absorbgitdirs
@@ Commit message

+ ## Documentation/config/submodule.txt ##
+@@ Documentation/config/submodule.txt: submodule.superprojectGitDir::
+ reference to determine whether the current repo is a submodule to
+ another repo; if this reference is absent, Git will treat the current
+ repo as though it is not a submodule (this does not make a difference to
+- most Git commands). It is set automatically during submodule creation.
++ most Git commands). It is set automatically during submodule creation
++ and 'git submodule absorbgitdir'.
+
## submodule.c ##
@@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *path)
char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
4: 33a582ef13 ! 4: 63e736e69d submodule: record superproject gitdir during 'update'
@@ Commit message

+ ## Documentation/config/submodule.txt ##
+@@ Documentation/config/submodule.txt: submodule.superprojectGitDir::
+ reference to determine whether the current repo is a submodule to
+ another repo; if this reference is absent, Git will treat the current
+ repo as though it is not a submodule (this does not make a difference to
+- most Git commands). It is set automatically during submodule creation
+- and 'git submodule absorbgitdir'.
++ most Git commands). It is set automatically during submodule creation,
++ update, and 'git submodule absorbgitdir'.
+
## git-submodule.sh ##
@@ git-submodule.sh: cmd_update()
;;
5: a8b5d40a77 < -: ---------- submodule: use config to find superproject worktree

Signature · 1 line
2.35.0.263.gb82422642f-goog
#15 lore raw

PATCH v7 1/4 t7400-submodule-basic: modernize inspect() helper

Since the inspect() helper in the submodule-basic test suite was written, 'git -C <dir>' was added. By using -C, we no longer need a reference to the base directory for the test. This simplifies callsites, and will make the addition of other arguments in later patches more readable.

---
 t/t7400-submodule-basic.sh | 40 +++++++++++++++-----------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
t/t7400-submodule-basic.sh +16 -24
index e7cec2e457..40cf8d89aa 100755
@@ -107,23 +107,15 @@ test_expect_success 'setup - repository to add submodules to' '
 # generates, which will expand symbolic links.
 submodurl=$(pwd -P)
 
-listbranches() {
-	git for-each-ref --format='%(refname)' 'refs/heads/*'
-}
-
 inspect() {
-	dir=$1 &&
-	dotdot="${2:-..}" &&
-
-	(
-		cd "$dir" &&
-		listbranches >"$dotdot/heads" &&
-		{ git symbolic-ref HEAD || :; } >"$dotdot/head" &&
-		git rev-parse HEAD >"$dotdot/head-sha1" &&
-		git update-index --refresh &&
-		git diff-files --exit-code &&
-		git clean -n -d -x >"$dotdot/untracked"
-	)
+	sub_dir=$1 &&
+
+	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
+	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
+	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
+	git -C "$sub_dir" update-index --refresh &&
+	git -C "$sub_dir" diff-files --exit-code &&
+	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
 test_expect_success 'submodule add' '
@@ -146,7 +138,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod ../.. &&
+	inspect addtest/submod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -248,7 +240,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch ../.. &&
+	inspect addtest/submod-branch &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -264,7 +256,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz ../../.. &&
+	inspect addtest/dotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -280,7 +272,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+	inspect addtest/dotslashdotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -296,7 +288,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz ../../.. &&
+	inspect addtest/slashslashsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -312,7 +304,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod ../.. &&
+	inspect addtest/realsubmod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -328,7 +320,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 ../.. &&
+	inspect addtest/realsubmod2 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -359,7 +351,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 ../.. &&
+	inspect addtest/realsubmod3 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
Signature · 1 line
2.35.0.263.gb82422642f-goog
#16 lore raw

PATCH v7 2/4 introduce submodule.superprojectGitDir record

In reply to Emily Shaffer

Teach submodules a config variable referencing to their superproject's gitdir. Git commands can rely on this reference to determine whether the current repo is a submodule to another repo. If this reference is absent, Git may assume the current repo is not a submodule.

In practice, some commands which specifically reference the submodule relationship, like 'git rev-parse --show-superproject-working-tree', will still search the parent directory. Other newly added or implicit behavior, such as "git status" showing the submodule's status in relation to the superproject, or a config which is shared between the superproject and the submodule, should not search the parent directory and instead rely on this "submodule.superprojectGitDir" config.

By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the submodule's pointer. And by using the path from gitdir to gitdir, we can move the submodule within the superproject's tree structure without breaking the submodule's pointer, too. Finally, by pointing at the superproject's worktree gitdir (if it exists), we ensure that we can tell which worktree contains our submodule.

This commit teaches "git submodule add" to add the aformentioned config variable. Subsequent commits will teach other commands to do so.

---
 Documentation/config/submodule.txt |  8 ++++++++
 builtin/submodule--helper.c        | 11 ++++++++++
 t/t7400-submodule-basic.sh         | 32 ++++++++++++++++++++----------
 3 files changed, 41 insertions(+), 10 deletions(-)
Documentation/config/submodule.txt +8 -0
index ee454f8126..bebd25684e 100644
@@ -91,3 +91,11 @@ submodule.alternateErrorStrategy::
 	`ignore`, `info`, `die`. Default is `die`. Note that if set to `ignore`
 	or `info`, and if there is an error with the computed alternate, the
 	clone proceeds as if no alternate was specified.
+
+submodule.superprojectGitDir::
+	If this repository is a submodule, the relative path from this repo's
+	gitdir to its superproject's gitdir. Git commands may rely on this
+	reference to determine whether the current repo is a submodule to
+	another repo; if this reference is absent, Git will treat the current
+	repo as though it is not a submodule (this does not make a difference to
+	most Git commands). It is set automatically during submodule creation.
builtin/submodule--helper.c +11 -0
index c5d3fc3817..0485b384a1 100644
@@ -1839,6 +1839,17 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Set the path from submodule's new gitdir to superproject's gitdir.
+	 * The latter may be a worktree gitdir. However, it is not possible for
+	 * the submodule to have a worktree-specific gitdir or config at clone
+	 * time, because "extensions.worktreeConfig" is only valid when set in
+	 * the local gitconfig, which the brand new submodule does not have yet.
+	 */
+	git_config_set_in_file(p, "submodule.superprojectGitDir",
+			       relative_path(absolute_path(get_git_dir()),
+					     sm_gitdir, &sb));
+
 	free(sm_alternate);
 	free(error_strategy);
 
t/t7400-submodule-basic.sh +22 -10
index 40cf8d89aa..7e4cc89bf5 100755
@@ -109,12 +109,24 @@ submodurl=$(pwd -P)
 
 inspect() {
 	sub_dir=$1 &&
+	super_dir=$2 &&
 
 	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
 	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.superprojectGitDir contains the path from the
+	# submodule's gitdir to the superproject's gitdir.
+
+	super_abs_gitdir=$(git -C "$super_dir" rev-parse --absolute-git-dir) &&
+	sub_abs_gitdir=$(git -C "$sub_dir" rev-parse --absolute-git-dir) &&
+
+	[ "$(git -C "$sub_dir" config --get submodule.superprojectGitDir)" = \
+	  "$(test-tool path-utils relative_path "$super_abs_gitdir" \
+						"$sub_abs_gitdir")" ] &&
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
@@ -138,7 +150,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod &&
+	inspect addtest/submod addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -240,7 +252,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch &&
+	inspect addtest/submod-branch addtest &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -256,7 +268,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz &&
+	inspect addtest/dotsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -272,7 +284,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz &&
+	inspect addtest/dotslashdotsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -288,7 +300,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz &&
+	inspect addtest/slashslashsubmod/frotz addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -304,7 +316,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod &&
+	inspect addtest/realsubmod addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -320,7 +332,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 &&
+	inspect addtest/realsubmod2 addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -351,7 +363,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 &&
+	inspect addtest/realsubmod3 addtest &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -492,7 +504,7 @@ test_expect_success 'update should work when path is an empty dir' '
 	git submodule update -q >update.out &&
 	test_must_be_empty update.out &&
 
-	inspect init &&
+	inspect init . &&
 	test_cmp expect head-sha1
 '
 
@@ -551,7 +563,7 @@ test_expect_success 'update should checkout rev1' '
 	echo "$rev1" >expect &&
 
 	git submodule update init &&
-	inspect init &&
+	inspect init . &&
 
 	test_cmp expect head-sha1
 '
Signature · 1 line
2.35.0.263.gb82422642f-goog
#17 lore raw

PATCH v7 3/4 submodule: record superproject gitdir during absorbgitdirs

In reply to Emily Shaffer

Already during 'git submodule add' we record a pointer to the superproject's gitdir. However, this doesn't help brand-new submodules created with 'git init' and later absorbed with 'git submodule absorbgitdirs'. Let's start adding that pointer during 'git submodule absorbgitdirs' too.

---
 Documentation/config/submodule.txt |  3 +-
 submodule.c                        | 23 +++++++++
 t/t7412-submodule-absorbgitdirs.sh | 82 +++++++++++++++++++++++++++++-
 3 files changed, 105 insertions(+), 3 deletions(-)
Documentation/config/submodule.txt +2 -1
index bebd25684e..f801f49ea1 100644
@@ -98,4 +98,5 @@ submodule.superprojectGitDir::
 	reference to determine whether the current repo is a submodule to
 	another repo; if this reference is absent, Git will treat the current
 	repo as though it is not a submodule (this does not make a difference to
-	most Git commands). It is set automatically during submodule creation.
+	most Git commands). It is set automatically during submodule creation
+	and 'git submodule absorbgitdir'.
submodule.c +23 -0
index c689070524..d7395c7551 100644
@@ -2097,6 +2097,9 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
 	struct strbuf new_gitdir = STRBUF_INIT;
 	const struct submodule *sub;
+	struct config_set sub_cs;
+	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
+	int tmp;
 
 	if (submodule_uses_worktrees(path))
 		die(_("relocate_gitdir for submodule '%s' with "
@@ -2127,6 +2130,26 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 
 	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
 
+	/*
+	 * Note location of superproject's gitdir. Because the submodule already
+	 * has a gitdir and local config, we can store this pointer from
+	 * worktree config to worktree config, if the submodule has
+	 * extensions.worktreeConfig set.
+	 */
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+	/* return 0 indicates config was found - we have a worktree config */
+	if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp))
+		strbuf_addstr(&config_path, ".worktree");
+
+	git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
+			       relative_path(absolute_path(get_git_dir()),
+					     real_new_git_dir, &sb));
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7412-submodule-absorbgitdirs.sh +80 -2

98 lines not on this page · raw message

Signature · 1 line
2.35.0.263.gb82422642f-goog
#18 lore raw

PATCH v7 4/4 submodule: record superproject gitdir during 'update'

In reply to Emily Shaffer

A recorded path to the superproject's gitdir might be added during 'git submodule add', but in some cases - like submodules which were created before 'git submodule add' learned to record that info - it might be useful to update the pointer. Let's do it during 'git submodule update', when we already have a handle to the superproject while calling operations on the submodules.

---
 Documentation/config/submodule.txt |  4 ++--
 git-submodule.sh                   | 15 +++++++++++++++
 t/t7406-submodule-update.sh        | 27 +++++++++++++++++++++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
Documentation/config/submodule.txt +2 -2
index f801f49ea1..ab37800954 100644
@@ -98,5 +98,5 @@ submodule.superprojectGitDir::
 	reference to determine whether the current repo is a submodule to
 	another repo; if this reference is absent, Git will treat the current
 	repo as though it is not a submodule (this does not make a difference to
-	most Git commands). It is set automatically during submodule creation
-	and 'git submodule absorbgitdir'.
+	most Git commands). It is set automatically during submodule creation,
+	update, and 'git submodule absorbgitdir'.
git-submodule.sh +15 -0
index 652861aa66..7c247bee7f 100755
@@ -449,6 +449,21 @@ cmd_update()
 			;;
 		esac
 
+		# Store a poitner to the superproject's gitdir. This may have
+		# changed, unless it's a fresh clone. Write to worktree if
+		# applicable, and point to superproject's worktree gitdir if
+		# applicable.
+		if test -z "$just_cloned"
+		then
+			sm_gitdir="$(git -C "$sm_path" rev-parse --absolute-git-dir)"
+			relative_gitdir="$(git rev-parse --path-format=relative \
+							 --prefix "${sm_gitdir}" \
+							 --git-dir)"
+
+			git -C "$sm_path" config --worktree \
+				submodule.superprojectgitdir "$relative_gitdir"
+		fi
+
 		if test -n "$recursive"
 		then
 			(
t/t7406-submodule-update.sh +27 -0
index 11cccbb333..b42a339982 100755
@@ -1061,4 +1061,31 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds superproject gitdir to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.superprojectGitdir &&
+	 git submodule update &&
+	 test-tool path-utils relative_path \
+		"$(git rev-parse --absolute-git-dir)" \
+		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
+	 git -C submodule config submodule.superprojectGitdir >actual &&
+	 test_cmp expect actual
+	)
+'
+
+test_expect_success 'submodule update uses config.worktree if applicable' '
+	(cd super &&
+	 git -C submodule config --unset submodule.superprojectGitDir &&
+	 git -C submodule config extensions.worktreeConfig true &&
+	 git submodule update &&
+	 test-tool path-utils relative_path \
+		"$(git rev-parse --absolute-git-dir)" \
+		"$(git -C submodule rev-parse --absolute-git-dir)" >expect &&
+	 git -C submodule config submodule.superprojectGitdir >actual &&
+	 test_cmp expect actual &&
+
+	 test_file_not_empty "$(git -C submodule rev-parse --absolute-git-dir)/config.worktree"
+	)
+'
+
 test_done
Signature · 1 line
2.35.0.263.gb82422642f-goog
#19 lore raw
In reply to Emily Shaffer
Quoted 6 lines · Emily Shaffer

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Sounds sensible.

Quoted 3 lines

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules.

I'd expect that there is a way (e.g. "git fsck") that helps the users notice when the actual filesystem layout contradicts with what the gitdir-to-gitdir link says, and repair the repositories when they go out of sync if possible.

It would be similar to "git worktree", where a link between the ".git" file that records "gitdir:" in a secondary worktree and the repository's $GIT_DIR/worktrees/*/gitdir, and the "repair" command can be used to bring them back in sync after moving the real repository without telling the secondary worktree about the move.

Quoted 7 lines

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong.

The other side of the coin is that, even when a configuration variable says that you are a submodule of the superproject at location X, if such a submodule gets moved out of the superproject (perhaps because the end-user wanted to concentrate on that submodule project alone as an independent project) and the superproject that used to be at location X got archived away, trusting and relying on what the configuration variable says would not help us access the now-gone superproject. And that would not change no matter how strongly we declare that it is the source of truth.

Unless we have a very good way to detect inconsistency and stop spreading the damage (e.g. the setting thought our superproject sits at directory X, but that location is now occupied by a different repository that is not related), I am still skeptical about the "setting is the sole truth" design.

#20 lore raw
In reply to Emily Shaffer
Quoted 1 line · Emily Shaffer

Since v6:

Thanks for the re-roll!

Quoted 9 lines

I've dropped the fifth commit to use this new config for git rev-parse --show-superproject-working-tree. I think it did more harm than good - that tool uses an odd way to determine whether the superproject is actually the superproject, anyways.

I poked a little bit at trying to find some benchmark to demonstrate that "submodule.superprojectGitDir" is actually faster - but I didn't end up able to write one without writing a ton of new code to traverse the filesystem.

I'm assuming that's tested against some variant of the submodule-in-C[1] conversion. I.e. at least when I tested it [2][3] it seemed easy to come up with (probably overly artificial) benchmarks where it would matter for the shell-out in this series.

The question performance-wise was rather whether we'd just be introducing the config mechanism as a transitory performance workaround, and the need for it would evaporate once that C migration happened (re original CL quoted in [3]).

Quoted 2 lines

To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

And I'm honestly still at the point of not even being against this whole thing, although it probably sounds like that. I'm really not.

I just genuinely don't get where this is headed. I.e. for the last iteration I did a demo patch on top that showed that there was no case added by the series where the on-the-fly discovery wasn't equivalent to the set-in-config value[4].

That change showed that after this series in a state where the config *is* redundant to on the fly discovery (or maybe not, and we're just missing test coverage).

But since you're citing correctness do you have some repo->sub relationship in mind that would be ambiguous in a way where the configuration would resolve the ambiguity?

I can imagine how such a thing might work, e.g. if we gave submodules some git-worktree-like method of being completely detached from the parent. I.e. being able to place a /usr/me/repo.git whose submodule entry for a "test" dir lives in /opt/tests or something. So when you "cd /opt/tests" you wouldn't be able to detect you're within a submodule.

(I'm assuming the case where the submodule has its own "in-tree" .git, is that even supported anymore...?)

But I can't think of one where such an ambiguity would arise within our current featureset.

What I really can't see is how if the need for such "config [...] for correctness" would arise how that doesn't also invalidate the assumptions you're making in 3/4 and 4/4.

I.e. surely if we need the config for correctness it's also true that we can't after-the-fact add the config on the fly to (such) existing submodules without user intervention. Or maybe the ambiguity would only arise from the POV of the submodule, but not for commands executed within the parent?

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/
3. https://lore.kernel.org/git/[email protected]/
4. https://lore.kernel.org/git/[email protected]/

#21 lore raw
Quoted 5 lines · Ævar Arnfjörð Bjarmason

I just genuinely don't get where this is headed. I.e. for the last iteration I did a demo patch on top that showed that there was no case added by the series where the on-the-fly discovery wasn't equivalent to the set-in-config value[4].

Quoted 7 lines

That change showed that after this series in a state where the config *is* redundant to on the fly discovery (or maybe not, and we're just missing test coverage).

But since you're citing correctness do you have some repo->sub relationship in mind that would be ambiguous in a way where the configuration would resolve the ambiguity?

This is an excellent question, which I wish I could have raised in my earlier response. A clear explanation why this setting is not a redundant copy but adds real information on top of what we should be able to learn from the filesystem structure would really help in justifying the new thing.

Thanks.

#22 lore raw
In reply to Ævar Arnfjörð Bjarmason

Hi,

Quoted 5 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Emily Shaffer

To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

Quoted 7 lines

And I'm honestly still at the point of not even being against this whole thing, although it probably sounds like that. I'm really not.

I just genuinely don't get where this is headed. I.e. for the last iteration I did a demo patch on top that showed that there was no case added by the series where the on-the-fly discovery wasn't equivalent to the set-in-config value[4].

Here's a few examples:

1. Suppose I track my $HOME directory as a git repository. Within my home directory, I have a src/git/ subdirectory with a clone of git.git, but I never intended to treat this as a submodule.

If I run "git rev-parse --show-superproject-working-tree", then it will discover my home directory repository, run ls-files in there to see if it has GITLINK entries, and either see one for src/git if I had "git add"ed it by mistake or not see one. In either case, it would it would view my src/git/ directory as being a submodule of my home directory even though I hadn't intended it to be so.

2. Suppose I have a copy of a repository such as
https://gerrit.googlesource.com/gerrit/, with all its submodules. I am in the plugins/replication/ directory.

If I run "git rev-parse --show-superproject-working-tree", then it will discover my gerrit repository, run ls-files in there to see if it has GITLINK entries, and use the result to decide whether the cwd is a submodule. So for example, if I had run "git rm --cached plugins/replication" to _prepare to_ remove the plugins/replication submodule, then "git rev-parse --show-superproject-working-tree" will produce the wrong result.

3. Suppose I am not using submodules at all. I have a clone of mawk.git and I am working there.

If I run "git rev-parse --show-superproject-working-tree", then I'm presumably interested in doing something submodule-specific; nothing wrong with that. But the series we're responding to is meant to support a wider variety of operations --- for example, suppose I am running a plain "git status" operation.

If "git status" runs "git rev-parse --show-superproject-working-tree", then git would walk up the filesystem above my mawk/ directory, looking for another .git dir. We can reach an NFS automounter directory and just hang. Even without an NFS automounter, we'd expect this to take a while because, unlike normal repository discovery, we have no reason to believe that the walk is going to quickly discover a .git directory and terminate. So this would violate user expectations.

Thanks and hope that helps,
Jonathan

Quoted 1 line

4. https://lore.kernel.org/git/[email protected]/

#23 lore raw
Quoted 6 lines · Jonathan Nieder

Here's a few examples:

1. Suppose I track my $HOME directory as a git repository. Within my home directory, I have a src/git/ subdirectory with a clone of git.git, but I never intended to treat this as a submodule.

Quoted 6 lines

If I run "git rev-parse --show-superproject-working-tree", then it will discover my home directory repository, run ls-files in there to see if it has GITLINK entries, and either see one for src/git if I had "git add"ed it by mistake or not see one. In either case, it would it would view my src/git/ directory as being a submodule of my home directory even though I hadn't intended it to be so.

I am not sure about this one. If you added an unrelated one with "git add" by mistake, you'd want to know about the mistake sooner rather than later, no?

Quoted 11 lines

2. Suppose I have a copy of a repository such as
https://gerrit.googlesource.com/gerrit/, with all its submodules. I am in the plugins/replication/ directory.

If I run "git rev-parse --show-superproject-working-tree", then it will discover my gerrit repository, run ls-files in there to see if it has GITLINK entries, and use the result to decide whether the cwd is a submodule. So for example, if I had run "git rm --cached plugins/replication" to _prepare to_ remove the plugins/replication submodule, then "git rev-parse --show-superproject-working-tree" will produce the wrong result.

Yes, looking only at the index of the superproject will have that problem, but don't other things in the superproject point at the submodule, too, e.g. submodule.<name>.* configuration variables?

And then, after removing them to truly dissociate the submodule from the superproject, "git rev-parse --show-superproject-working-tree" may stop saying that it is a submodule, but this series wants to make it irrelevant what the command says. Until you unset the configuration variable in the submodule, it will stay to be a submodule of the superproject, but the superproject no longer thinks it is responsible for the submodule. You'll have to deal with an inconsistent state during the transition either way, so I am not sure it is the best solution to introduce an extra setting that can easily go out of sync.

Quoted 9 lines

3. Suppose I am not using submodules at all. I have a clone of mawk.git and I am working there.

If I run "git rev-parse --show-superproject-working-tree", then I'm presumably interested in doing something submodule-specific; nothing wrong with that. But the series we're responding to is meant to support a wider variety of operations --- for example, suppose I am running a plain "git status" operation.

Quoted 8 lines

If "git status" runs "git rev-parse --show-superproject-working-tree", then git would walk up the filesystem above my mawk/ directory, looking for another .git dir. We can reach an NFS automounter directory and just hang. Even without an NFS automounter, we'd expect this to take a while because, unlike normal repository discovery, we have no reason to believe that the walk is going to quickly discover a .git directory and terminate. So this would violate user expectations.

It would be a problem, but I do not know if "this is a submodule of that superproject" link is the only solution, let alone the most effective one. It seems to me that you are looking more for something like GIT_CEILING_DIRECTORIES.

#24 lore raw
Quoted 15 lines · Junio C Hamano
Quoted 6 lines · Jonathan Nieder

Here's a few examples:

1. Suppose I track my $HOME directory as a git repository. Within my home directory, I have a src/git/ subdirectory with a clone of git.git, but I never intended to treat this as a submodule.

Quoted 6 lines

If I run "git rev-parse --show-superproject-working-tree", then it will discover my home directory repository, run ls-files in there to see if it has GITLINK entries, and either see one for src/git if I had "git add"ed it by mistake or not see one. In either case, it would it would view my src/git/ directory as being a submodule of my home directory even though I hadn't intended it to be so.

Quoted 3 lines

I am not sure about this one. If you added an unrelated one with "git add" by mistake, you'd want to know about the mistake sooner rather than later, no?

My point with this example is that it's useful to have a definition of what is a submodule repository, to make it unambiguous whether this repository is a submodule or whether it's just a repository that happens to have been cloned inside of a git-managed worktree.

For the specific example of having run "git add", I don't have any very strong opinions.

[...]

Quoted 3 lines
Quoted 3 lines

2. Suppose I have a copy of a repository such as
https://gerrit.googlesource.com/gerrit/, with all its submodules. I am in the plugins/replication/ directory.

[...]

Quoted 8 lines
Quoted 4 lines

So for example, if I had run "git rm --cached
plugins/replication" to _prepare to_ remove the plugins/replication submodule, then "git rev-parse --show-superproject-working-tree" will produce the wrong result.

Yes, looking only at the index of the superproject will have that problem, but don't other things in the superproject point at the submodule, too, e.g. submodule.<name>.* configuration variables?

What all of those suggested alternatives have in common is that they are pointers from another repository to the submodule.

This would be the first time in git history that we are saying a property of a repository depends on having to examine files outside of it. I guess the main question I'd have is, why _wouldn't_ I want a submodule to be able to point to the superproject containing it? I can think of many advantages to having that linkage, and the main disadvantage I can think of is that it is a change.

I don't think that submodule.<name>.* is an adequate substitute for having this setting, because it requires
- finding the superproject
- mapping the <name> to a path, using .gitmodules
- comparing the path to the submodule location

which would be complex, slow, and error-prone.

The one thing that I think could approach being an adequate substitute is examining the path to the current repository and stripping off path components until we find modules/; then the parent is the containing superproject. That would only work for absorbed submodules, though, and it would be less explicit than having a config item.

Quoted 10 lines

And then, after removing them to truly dissociate the submodule from the superproject, "git rev-parse --show-superproject-working-tree" may stop saying that it is a submodule, but this series wants to make it irrelevant what the command says. Until you unset the configuration variable in the submodule, it will stay to be a submodule of the superproject, but the superproject no longer thinks it is responsible for the submodule. You'll have to deal with an inconsistent state during the transition either way, so I am not sure it is the best solution to introduce an extra setting that can easily go out of sync.

This hints at a reason why one wouldn't want the linkage back --- dealing with the ambiguity of inconsistencies (what if a submodule declares a superproject but the superproject does not declare the submodule?).

I would not expect that ambiguity to be much of a problem, because the typical way to use superproject linkage would be to print output from commands like "git status": for example,

This is a submodule of ../../gerrit; you can run

git -C ../../gerrit status

to get the status of the superproject.

An inconsistency could occur due to the user using "mv" (instead of "git mv") to move a submodule to a path a different number of path components from its superproject. One way to handle that would be to make submodules record a boolean setting reflecting whether they are a submodule, instead of the path to the superproject. (This would be similar to settings like core.bare.) Alternatively, if the path to the superproject is recorded and if "git fsck" is able to notice such an inconsistency, then the user should be able to have an okay experience repairing it.

[...]

Quoted 9 lines
Quoted 8 lines

If "git status" runs "git rev-parse --show-superproject-working-tree", then git would walk up the filesystem above my mawk/ directory, looking for another .git dir. We can reach an NFS automounter directory and just hang. Even without an NFS automounter, we'd expect this to take a while because, unlike normal repository discovery, we have no reason to believe that the walk is going to quickly discover a .git directory and terminate. So this would violate user expectations.

Quoted 4 lines

It would be a problem, but I do not know if "this is a submodule of that superproject" link is the only solution, let alone the most effective one. It seems to me that you are looking more for something like GIT_CEILING_DIRECTORIES.

Who is the "you" addressed here? The end user can use GIT_CEILING_DIRECTORIES if they are expecting to run git commands within an NFS automounter directory and outside of any git repository, but they'd be right to be surprised if that suddenly became required when inside git repositories. I don't think we should assume that running an extra .git discovery walk is cost-free to users who are not using submodules and an acceptable burden to impose on them for the sake of submodule users.

Thanks and hope that helps,
Jonathan

#25 lore raw
Quoted 4 lines · Jonathan Nieder

My point with this example is that it's useful to have a definition of what is a submodule repository, to make it unambiguous whether this repository is a submodule or whether it's just a repository that happens to have been cloned inside of a git-managed worktree.

OK, together with the other "no need to let NFS automounter worry about parent directories", it makes a very successful argument for a single bit (i.e. this is a free-standing repository and is not a submodule, so no need to auto-discover if it is one). I think the "Alternatively" you later mention to solve ambiguity with just a single bit, without "this is a submodule of that superproject" linkage, is essentially the same?

But I do not think it argues for the need to say "a config, not filesystem layout, must be the single source of truth to say which superproject this repository belongs as its submodule".

Quoted 3 lines

This would be the first time in git history that we are saying a property of a repository depends on having to examine files outside of it.

Well, path-based configuration inclusion, with configuration driven hooks, I do not think the distinction matters much anymore in these days.

Quoted 2 lines

I guess the main question I'd have is, why _wouldn't_ I want a submodule to be able to point to the superproject containing it?

Because with (the absense of) a single "this is freestanding" bit, by default the filesystem layout can already "point" at it?

#26 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Jonathan Nieder

My point with this example is that it's useful to have a definition of what is a submodule repository, to make it unambiguous whether this repository is a submodule or whether it's just a repository that happens to have been cloned inside of a git-managed worktree.

Quoted 7 lines

OK, together with the other "no need to let NFS automounter worry about parent directories", it makes a very successful argument for a single bit (i.e. this is a free-standing repository and is not a submodule, so no need to auto-discover if it is one). I think the "Alternatively" you later mention to solve ambiguity with just a single bit, without "this is a submodule of that superproject" linkage, is essentially the same?

That resolution - "teach submodules to know they're submodules, but not whose submodule they are" - would still count as a success to me. The reason I proposed a path instead of a boolean here was simply because storing a path is a boolean (by whether it's present or not) *and* additional information (the path to the superproject), and it seemed silly to me to opt for less information. Or, to put it another way - "am I a submodule?" seems pretty vital, and "yes, and I belong to xyz" is an optimization on top of that. So I don't terribly mind sending this as just a boolean, if we feel that the effort to keep it up outweighs the benefit of saving us a filesystem walk.

I'm not completely convinced that it does, though - would the addition of a 'git fsck' check for this config be satisfactory? In other words, is the problem that the execution of this series wasn't thorough enough and it should be refined, or that the concept itself is beyond saving?

- Emily

Quoted 18 lines

But I do not think it argues for the need to say "a config, not filesystem layout, must be the single source of truth to say which superproject this repository belongs as its submodule".

Quoted 3 lines

This would be the first time in git history that we are saying a property of a repository depends on having to examine files outside of it.

Well, path-based configuration inclusion, with configuration driven hooks, I do not think the distinction matters much anymore in these days.

Quoted 2 lines

I guess the main question I'd have is, why _wouldn't_ I want a submodule to be able to point to the superproject containing it?

Because with (the absense of) a single "this is freestanding" bit, by default the filesystem layout can already "point" at it?

#27 lore raw
Quoted 16 lines · Emily Shaffer
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Jonathan Nieder

My point with this example is that it's useful to have a definition of what is a submodule repository, to make it unambiguous whether this repository is a submodule or whether it's just a repository that happens to have been cloned inside of a git-managed worktree.

Quoted 7 lines

OK, together with the other "no need to let NFS automounter worry about parent directories", it makes a very successful argument for a single bit (i.e. this is a free-standing repository and is not a submodule, so no need to auto-discover if it is one). I think the "Alternatively" you later mention to solve ambiguity with just a single bit, without "this is a submodule of that superproject" linkage, is essentially the same?

Quoted 2 lines

That resolution - "teach submodules to know they're submodules, but not whose submodule they are" - would still count as a success to me.

Thanks, both. Sounds like a good path forward.

[...]

Quoted 5 lines

So I don't terribly mind sending this as
just a boolean, if we feel that the effort to keep it up outweighs the benefit of saving us a filesystem walk.

I'm not completely convinced that it does, though

Personally, I'm convinced --- e.g., life gets painful enough when core.worktree ends up pointing to the wrong path, so being able to avoid that complexity seems like a nice outcome.

Quoted 4 lines

- would the addition
of a 'git fsck' check for this config be satisfactory? In other words, is the problem that the execution of this series wasn't thorough enough and it should be refined, or that the concept itself is beyond saving?

For the absorbed case, the path to the superproject should be pretty stable, and in that case it's probably possible to make this robust enough. (That said, the path to the superproject gitdir would typically just be "../..", at least as long as we have the other patch to escape slashes in the submodule name.) In the non-absorbed case, it seems likely to get messy fast because a user can "mv" the submodule around.

Another kind of case that gets interesting is when there are multiple superproject worktrees and multiple submodule worktrees. Does the relative path become a per-worktree variable? Using a boolean saves us from having to think through it.

Thanks for the clear explanations,
Jonathan

#28 lore raw
In reply to Jonathan Nieder
Quoted 11 lines · Jonathan Nieder

Hi,

Quoted 8 lines · Ævar Arnfjörð Bjarmason
Quoted 2 lines · Emily Shaffer

To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

And I'm honestly still at the point of not even being against this whole thing, although it probably sounds like that. I'm really not.

Quoted 6 lines
Quoted 4 lines

I just genuinely don't get where this is headed. I.e. for the last iteration I did a demo patch on top that showed that there was no case added by the series where the on-the-fly discovery wasn't equivalent to the set-in-config value[4].

Here's a few examples:

I've read the downthread, but it's probably best to reply to this...

Quoted 15 lines

1. Suppose I track my $HOME directory as a git repository. Within my home directory, I have a src/git/ subdirectory with a clone of git.git, but I never intended to treat this as a submodule.

If I run "git rev-parse --show-superproject-working-tree", then it will discover my home directory repository, run ls-files in there to see if it has GITLINK entries, and either see one for src/git if I had "git add"ed it by mistake or not see one. In either case, it would it would view my src/git/ directory as being a submodule of my home directory even though I hadn't intended it to be so.

2. Suppose I have a copy of a repository such as
https://gerrit.googlesource.com/gerrit/, with all its submodules. I am in the plugins/replication/ directory.

Quoted 7 lines

If I run "git rev-parse --show-superproject-working-tree", then it will discover my gerrit repository, run ls-files in there to see if it has GITLINK entries, and use the result to decide whether the cwd is a submodule. So for example, if I had run "git rm --cached plugins/replication" to _prepare to_ remove the plugins/replication submodule, then "git rev-parse --show-superproject-working-tree" will produce the wrong result.

These both seem like valid edge cases, but they're still going to be the same edge case on the "parent" side even with a proposed cache (whether it's a boolean or a path).

I.e. the question here is really not one of caching, but of what it means for Y to be a submodule of X.

I assumed that we'd prefer a 1=1 relationship between the parent reporting that Y is a submodule of it, and Y reporting that it is a submodule (of the parent at some <path>).

If that's the case we can walk up and ask parent .git's whether they think the <path> is their submodule.

If it's not the case perhaps a config is needed, but then that surely has wider implications. I.e. won't it be the case that we can't add the config after-the-fact as this series proposes in those some ambiguous cases?xo

Quoted 9 lines

3. Suppose I am not using submodules at all. I have a clone of mawk.git and I am working there.

If I run "git rev-parse --show-superproject-working-tree", then I'm presumably interested in doing something submodule-specific; nothing wrong with that. But the series we're responding to is meant to support a wider variety of operations --- for example, suppose I am running a plain "git status" operation.

Quoted 8 lines

If "git status" runs "git rev-parse --show-superproject-working-tree", then git would walk up the filesystem above my mawk/ directory, looking for another .git dir. We can reach an NFS automounter directory and just hang. Even without an NFS automounter, we'd expect this to take a while because, unlike normal repository discovery, we have no reason to believe that the walk is going to quickly discover a .git directory and terminate. So this would violate user expectations.

We have a /a/b/c/d.git mounted, but not a parent /a/b/, and walking upwards causes it to be mounted?

#29 lore raw
Quoted 2 lines · Ævar Arnfjörð Bjarmason

We have a /a/b/c/d.git mounted, but not a parent /a/b/, and walking upwards causes it to be mounted?

;-)

#30 lore raw

v8 cover teach submodules to know they're submodules

In reply to Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

CI run: https://github.com/nasamuffin/git/actions/runs/1866957146

Since v7:

Actually a fairly large rework. Rather than keeping the path from gitdir to gitdir, just keep a boolean under 'submodule.hasSuperproject'. The idea is that from this boolean, we can decide whether to traverse the filesystem looking for a superproject.

Because this simplifies the implementation, I compressed the three middle commits into one. As proof-of-concept, I added a patch at the end to check for this boolean when running git rev-parse --show-superproject-working-tree.

One thing I'm not sure about: in the tests, I check whether the config is set, but not what the boolean value of it is. Is there a better way to do that? For example, I could imagine someone deciding to set submodule.hasSuperproject = false and the tests would not function correctly in that case. I think we don't really normalize the value on a boolean config like that, so I didn't want to write a lot of comparison to check if the value is 1 or true or True or TRUE or Yes or .... Am I overthinking it?

The other thing I'm not sure about: since it's just a bool, we're not restricted to setting this config only when we have both gitdir paths available. That makes me want to set the config any time we are doing something with submodules anyway, like any time 'git-submodule--helper' is used. But that helper seems to be called in the context of the superproject, not of the submodules, so adding this config for each submodule we touch would be a second child process. Is there some other common entry point for submodules that we can use?

- Emily

Since v6:

I've dropped the fifth commit to use this new config for git rev-parse --show-superproject-working-tree. I think it did more harm than good - that tool uses an odd way to determine whether the superproject is actually the superproject, anyways.

I poked a little bit at trying to find some benchmark to demonstrate that "submodule.superprojectGitDir" is actually faster - but I didn't end up able to write one without writing a ton of new code to traverse the filesystem. To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

So, the only real changes between v6 and v7 are some documentation changes suggested by Jonathan Tan
(https://lore.kernel.org/git/20211117234300.2598132-1-jonathantanmy%40google.com).

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Since v4:

The only real change here is a slight semantics change to map from <submodule gitdir> to <superproject common git dir>. In every case *except* for when the superproject has a worktree, this changes nothing. For the case when the superproject has a worktree, this means that now submodules will refer to the general superproject common dir (e.g. no worktree-specific refs or configs or whatnot).

I *think* that because a submodule should exist in the context of the common dir, not the worktree gitdir, that is ok. However, it does mean it would be difficult to do something like sharing a config specific to the worktree (the initial goal of this series).

$ROOT/.git
$ROOT/.git/config.superproject <- shared by $ROOT/.git/modules/sub $ROOT/.git/modules/sub <- points to $ROOT/.git
$ROOT/.git/worktrees/wt
$ROOT/.git/worktrees/wt/config.superproject <- contains a certain config-based pre-commit hook

If the submodule only knows about the common dir, that is tough, because the submodule would basically have to guess which worktree it's in from its own path. There would be no way for '$WT/sub' to inherit '$ROOT/.git/worktrees/wt/config.superproject'.

That said... right now, we don't support submodules in worktrees very well at all. A submodule in a worktree will get a brand new gitdir in $ROOT/.git/worktrees/modules/ (and that brand new gitdir would point to the super's common dir). So I think we can punt on this entire question until we teach submodules and worktrees to play more gracefully together (it's on my long list...), and at that time we can probably introduce a pointer from $ROOT/.git/modules/sub/worktrees/wt/ to $ROOT/.git/worktrees/wt/....

Or, to summarize the long ramble above: "this is still kind of weird with worktrees, but let's fix it later when we fix worktrees more thoroughly".

(More rambling about worktree weirdness here:
https://lore.kernel.org/git/YYRaII8YWVxlBqsF%40google.com )

Since v3, a pretty major change: the semantics of submodule.superprojectGitDir has changed, to point from the submodule's gitdir to the superproject's gitdir (in v3 and earlier, we kept a path from the submodule's *worktree* to the superproject's gitdir instead). This cleans up some of the confusions about the behavior when a submodule worktree moves around in the superproject's tree, or in a future when we support submodules having multiple worktrees.

I also tried to simplify the tests to use 'test-tool path-utils relative_path' everywhere - I think that makes them much more clear for a test reader, but if you're reviewing and it isn't obvious what we're testing for, please speak up.

I think this is pretty mature and there was a lot of general agreement that the gitdir->gitdir association was the way to go, so please be brutal and look for nits, leaks, etc. this round ;)
[/v4 cover letter]

Emily Shaffer (3):
t7400-submodule-basic: modernize inspect() helper introduce submodule.hasSuperproject record
rev-parse: short-circuit superproject worktree when config unset

 Documentation/config/submodule.txt |  6 ++++
 builtin/submodule--helper.c        |  5 +++
 git-submodule.sh                   |  3 ++
 submodule.c                        | 30 ++++++++++++++++++
 t/t7400-submodule-basic.sh         | 42 ++++++++++++-------------
 t/t7406-submodule-update.sh        |  8 +++++
 t/t7412-submodule-absorbgitdirs.sh | 50 ++++++++++++++++++++++++++++--
 7 files changed, 119 insertions(+), 25 deletions(-)

Range-diff against v7:

1:  1a85deb1c5 < -:  ---------- introduce submodule.superprojectGitDir record
-:  ---------- > 1:  251510c687 t7400-submodule-basic: modernize inspect() helper
2:  7a44b0edf9 ! 2:  34cbfd81ee submodule: record superproject gitdir during absorbgitdirs

@@ Metadata
Author: Emily Shaffer <[email protected]>

## Commit message ##
- submodule: record superproject gitdir during absorbgitdirs
+ introduce submodule.hasSuperproject record

- Already during 'git submodule add' we record a pointer to the
- superproject's gitdir. However, this doesn't help brand-new
- submodules created with 'git init' and later absorbed with 'git
- submodule absorbgitdirs'. Let's start adding that pointer during 'git
- submodule absorbgitdirs' too.
+ Teach submodules a config variable indicating the fact that they are a
+ submodule. If this config is set to false or unset, Git may assume the
+ current repo is not a submodule.
+
+ Git commands can use this variable to decide whether to traverse the
+ filesystem and look for a superproject at all. 'git rev-parse
+ --show-superproject-working-tree' can learn to exit early if this config
+ is unset or false. Other newly added or implicit behavior - like "git
+ status" showing the submodule's status in relation to the superproject,
+ or a config shared between the superproject and submodule - can use this
+ config to decide whether to search the parent directory to find a
+ superproject.
+
+ Introduce this config everywhere we add a new submodule, or touch one
+ that already exists, so that we can proliferate it in repos which are
+ already out in the world using submodules.

+ Helped-by: Junio C Hamano <[email protected]>

## Documentation/config/submodule.txt ##
-@@ Documentation/config/submodule.txt: submodule.superprojectGitDir::
- reference to determine whether the current repo is a submodule to
- another repo; if this reference is absent, Git will treat the current
- repo as though it is not a submodule (this does not make a difference to
-- most Git commands). It is set automatically during submodule creation.
-+ most Git commands). It is set automatically during submodule creation
-+ and 'git submodule absorbgitdir'.
+@@ Documentation/config/submodule.txt: submodule.alternateErrorStrategy::
+ ignore, info, die. Default is die. Note that if set to ignore
+ or info, and if there is an error with the computed alternate, the
+ clone proceeds as if no alternate was specified.
++
++submodule.hasSuperproject::
++ Indicates whether this repository is a submodule. If this config is set
++ to 'true', Git may traverse the filesystem above this submodule in order
++ to identify the superproject. It is set automatically during submodule
++ creation, update, and 'git submodule absorbgitdir'.
+
+ ## builtin/submodule--helper.c ##
+@@ builtin/submodule--helper.c: static int clone_submodule(struct module_clone_data *clone_data)
+ git_config_set_in_file(p, "submodule.alternateErrorStrategy",
+ error_strategy);
+
++ /*
++ * Teach the submodule that it's a submodule.
++ */
++ git_config_set_in_file(p, "submodule.hasSuperproject", "true");
++
+ free(sm_alternate);
+ free(error_strategy);
+
+
+ ## git-submodule.sh ##
+@@ git-submodule.sh: cmd_update()
+ ;;
+ esac
+
++ # Note that the submodule is a submodule.
++ git -C "$sm_path" config submodule.hasSuperproject "true"
++
+ if test -n "$recursive"
+ then
+ (

## submodule.c ##
@@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *path)
@@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
const struct submodule *sub;
+ struct config_set sub_cs;
+ struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
-+ int tmp;

if (submodule_uses_worktrees(path))
die(_("relocate_gitdir for submodule '%s' with "
@@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
+ strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+ git_configset_init(&sub_cs);
+ git_configset_add_file(&sub_cs, config_path.buf);
-+ /* return 0 indicates config was found - we have a worktree config */
-+ if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp))
-+ strbuf_addstr(&config_path, ".worktree");
+
-+ git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir",
-+ relative_path(absolute_path(get_git_dir()),
-+ real_new_git_dir, &sb));
++ git_config_set_in_file(config_path.buf, "submodule.hasSuperproject",
++ "true");
+
+ git_configset_clear(&sub_cs);
+ strbuf_release(&config_path);
@@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
free(real_old_git_dir);
free(real_new_git_dir);

+ ## t/t7400-submodule-basic.sh ##
+@@ t/t7400-submodule-basic.sh: inspect() {
+ git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
+ git -C "$sub_dir" update-index --refresh &&
+ git -C "$sub_dir" diff-files --exit-code &&
++
++ # Ensure that submodule.hasSuperproject is set.
++ git -C "$sub_dir" config "submodule.hasSuperproject"
++
+ git -C "$sub_dir" clean -n -d -x >untracked
+ }
+
+
+ ## t/t7406-submodule-update.sh ##
+@@ t/t7406-submodule-update.sh: test_expect_success 'submodule update --quiet passes quietness to fetch with a s
+ )
+ '
+
++test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
++ (cd super &&
++ git -C submodule config --unset submodule.hasSuperproject &&
++ git submodule update &&
++ git -C submodule config submodule.hasSuperproject
++ )
++'
++
+ test_done
+
## t/t7412-submodule-absorbgitdirs.sh ##
@@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorb the git dir' '
git status >actual.1 &&
@@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorb the git dir' '
- test_cmp expect.2 actual.2
+ test_cmp expect.2 actual.2 &&
+
-+ # make sure the submodule cached the superproject gitdir correctly
-+ submodule_gitdir="$(git -C sub1 rev-parse --path-format=absolute --git-common-dir)" &&
-+ superproject_gitdir="$(git rev-parse --path-format=absolute --git-common-dir)" &&
-+
-+ test-tool path-utils relative_path "$superproject_gitdir" \
-+ "$submodule_gitdir" >expect &&
-+ git -C sub1 config submodule.superprojectGitDir >actual &&
-+
-+ test_cmp expect actual
++ git -C sub1 config submodule.hasSuperproject
'

test_expect_success 'absorbing does not fail for deinitialized submodules' '
@@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorb the git dir in a
- test_cmp expect.2 actual.2
+ test_cmp expect.2 actual.2 &&
+
-+ sub1_gitdir="$(git -C sub1 rev-parse --path-format=absolute --git-common-dir)" &&
-+ sub1_nested_gitdir="$(git -C sub1/nested rev-parse --path-format=absolute --git-common-dir)" &&
-+
-+ test-tool path-utils relative_path "$sub1_gitdir" "$sub1_nested_gitdir" \
-+ >expect &&
-+ git -C sub1/nested config submodule.superprojectGitDir >actual &&
-+
-+ test_cmp expect actual
++ git -C sub1/nested config submodule.hasSuperproject
'

test_expect_success 're-setup nested submodule' '
@@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
+ # absorb the git dir
+ git submodule absorbgitdirs sub4 &&
+
-+ # make sure the submodule noted the superproject gitdir correctly
-+ submodule_gitdir="$(git -C sub4 rev-parse --absolute-git-dir)" &&
-+ superproject_gitdir="$(git rev-parse --absolute-git-dir)" &&
-+
-+ test-tool path-utils relative_path "$superproject_gitdir" \
-+ "$submodule_gitdir" >expect &&
-+ git -C sub4 config submodule.superprojectGitDir >actual &&
-+
-+ test_cmp expect actual
++ # make sure the submodule noted the superproject
++ git -C sub4 config submodule.hasSuperproject
+ )
+'
+
@@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
+ # absorb the git dir
+ git submodule absorbgitdirs sub5 &&
+
-+ # make sure the submodule noted the superproject gitdir correctly
-+ submodule_gitdir="$(git -C sub5 rev-parse --absolute-git-dir)" &&
-+ superproject_gitdir="$(git rev-parse --absolute-git-dir)" &&
-+
-+ test-tool path-utils relative_path "$superproject_gitdir" \
-+ "$submodule_gitdir" >expect &&
-+ git -C sub5 config submodule.superprojectGitDir >actual &&
-+
-+ test_cmp expect actual &&
-+
-+ # make sure the config went into the submodule config.worktree
-+ test_file_not_empty "$submodule_gitdir/config.worktree"
++ # make sure the submodule noted the superproject
++ git -C sub5 config submodule.hasSuperproject
+ )
+'
+

3:  63e736e69d < -:  ---------- submodule: record superproject gitdir during 'update'
-:  ---------- > 3:  c14ee8760f rev-parse: short-circuit superproject worktree when config unset
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#31 lore raw

PATCH v8 1/3 t7400-submodule-basic: modernize inspect() helper

Since the inspect() helper in the submodule-basic test suite was written, 'git -C <dir>' was added. By using -C, we no longer need a reference to the base directory for the test. This simplifies callsites, and will make the addition of other arguments in later patches more readable.

---
 t/t7400-submodule-basic.sh | 40 +++++++++++++++-----------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
t/t7400-submodule-basic.sh +16 -24
index e7cec2e457..40cf8d89aa 100755
@@ -107,23 +107,15 @@ test_expect_success 'setup - repository to add submodules to' '
 # generates, which will expand symbolic links.
 submodurl=$(pwd -P)
 
-listbranches() {
-	git for-each-ref --format='%(refname)' 'refs/heads/*'
-}
-
 inspect() {
-	dir=$1 &&
-	dotdot="${2:-..}" &&
-
-	(
-		cd "$dir" &&
-		listbranches >"$dotdot/heads" &&
-		{ git symbolic-ref HEAD || :; } >"$dotdot/head" &&
-		git rev-parse HEAD >"$dotdot/head-sha1" &&
-		git update-index --refresh &&
-		git diff-files --exit-code &&
-		git clean -n -d -x >"$dotdot/untracked"
-	)
+	sub_dir=$1 &&
+
+	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
+	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
+	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
+	git -C "$sub_dir" update-index --refresh &&
+	git -C "$sub_dir" diff-files --exit-code &&
+	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
 test_expect_success 'submodule add' '
@@ -146,7 +138,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod ../.. &&
+	inspect addtest/submod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -248,7 +240,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch ../.. &&
+	inspect addtest/submod-branch &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -264,7 +256,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz ../../.. &&
+	inspect addtest/dotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -280,7 +272,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+	inspect addtest/dotslashdotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -296,7 +288,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz ../../.. &&
+	inspect addtest/slashslashsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -312,7 +304,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod ../.. &&
+	inspect addtest/realsubmod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -328,7 +320,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 ../.. &&
+	inspect addtest/realsubmod2 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -359,7 +351,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 ../.. &&
+	inspect addtest/realsubmod3 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#32 lore raw

PATCH v8 2/3 introduce submodule.hasSuperproject record

In reply to Emily Shaffer

Teach submodules a config variable indicating the fact that they are a submodule. If this config is set to false or unset, Git may assume the current repo is not a submodule.

Git commands can use this variable to decide whether to traverse the filesystem and look for a superproject at all. 'git rev-parse --show-superproject-working-tree' can learn to exit early if this config is unset or false. Other newly added or implicit behavior - like "git status" showing the submodule's status in relation to the superproject, or a config shared between the superproject and submodule - can use this config to decide whether to search the parent directory to find a superproject.

Introduce this config everywhere we add a new submodule, or touch one that already exists, so that we can proliferate it in repos which are already out in the world using submodules.

---
 Documentation/config/submodule.txt |  6 ++++
 builtin/submodule--helper.c        |  5 +++
 git-submodule.sh                   |  3 ++
 submodule.c                        | 18 +++++++++++
 t/t7400-submodule-basic.sh         |  4 +++
 t/t7406-submodule-update.sh        |  8 +++++
 t/t7412-submodule-absorbgitdirs.sh | 50 ++++++++++++++++++++++++++++--
 7 files changed, 92 insertions(+), 2 deletions(-)
Documentation/config/submodule.txt +6 -0
index ee454f8126..99d5260b8e 100644
@@ -91,3 +91,9 @@ submodule.alternateErrorStrategy::
 	`ignore`, `info`, `die`. Default is `die`. Note that if set to `ignore`
 	or `info`, and if there is an error with the computed alternate, the
 	clone proceeds as if no alternate was specified.
+
+submodule.hasSuperproject::
+	Indicates whether this repository is a submodule. If this config is set
+	to 'true', Git may traverse the filesystem above this submodule in order
+	to identify the superproject. It is set automatically during submodule
+	creation, update, and 'git submodule absorbgitdir'.
builtin/submodule--helper.c +5 -0
index c5d3fc3817..92986646bc 100644
@@ -1839,6 +1839,11 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Teach the submodule that it's a submodule.
+	 */
+	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
+
 	free(sm_alternate);
 	free(error_strategy);
 
git-submodule.sh +3 -0
index 652861aa66..59dffda775 100755
@@ -449,6 +449,9 @@ cmd_update()
 			;;
 		esac
 
+		# Note that the submodule is a submodule.
+		git -C "$sm_path" config submodule.hasSuperproject "true"
+
 		if test -n "$recursive"
 		then
 			(
submodule.c +18 -0
index c689070524..741104af8a 100644
@@ -2097,6 +2097,8 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
 	struct strbuf new_gitdir = STRBUF_INIT;
 	const struct submodule *sub;
+	struct config_set sub_cs;
+	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
 
 	if (submodule_uses_worktrees(path))
 		die(_("relocate_gitdir for submodule '%s' with "
@@ -2127,6 +2129,22 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 
 	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
 
+	/*
+	 * Note location of superproject's gitdir. Because the submodule already
+	 * has a gitdir and local config, we can store this pointer from
+	 * worktree config to worktree config, if the submodule has
+	 * extensions.worktreeConfig set.
+	 */
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+
+	git_config_set_in_file(config_path.buf, "submodule.hasSuperproject",
+			       "true");
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7400-submodule-basic.sh +4 -0
index 40cf8d89aa..833fa01961 100755
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	git -C "$sub_dir" config "submodule.hasSuperproject"
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
t/t7406-submodule-update.sh +8 -0
index 11cccbb333..422c3cc343 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.hasSuperproject &&
+	 git submodule update &&
+	 git -C submodule config submodule.hasSuperproject
+	)
+'
+
 test_done
t/t7412-submodule-absorbgitdirs.sh +48 -2

66 lines not on this page · raw message

Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#37 lore raw

PATCH v8 3/3 rev-parse: short-circuit superproject worktree when config unset

In reply to Emily Shaffer

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'.

---

Maybe it's actually better to warn instead of error here? Or maybe it's best not to say anything, but to set 'submodule.hasSuperproject' after we successfully find the superproject?

Either way - I ran the test suite with this early exit added and everything still passed. I made this change hoping to get a little signal on whether the series achieved its goal, and in that regard I'm satisfied.

---
 submodule.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
submodule.c +12 -0
index 741104af8a..463e7f0c48 100644
@@ -2237,6 +2237,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;
@@ -2250,6 +2251,17 @@ int get_superproject_working_tree(struct strbuf *buf)
 		 */
 		return 0;
 
+	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 never set.
+		 */
+		error(_("Asked to find a superproject, but submodule.hasSuperproject != true"));
+		return 0;
+	}
+
 	if (!strbuf_realpath(&one_up, "../", 0))
 		return 0;
 
Signature · 1 line
2.35.1.574.g5d30c73bfb-goog
#40 lore raw
In reply to Emily Shaffer
Quoted 3 lines · Emily Shaffer

One thing I'm not sure about: in the tests, I check whether the config is set, but not what the boolean value of it is. Is there a better way to do that?

Are you looking for value normalization during both setting and retrieving, i.e.

	$ git config vari.able 0 ;# or "no" or "off"
	$ git config --type=bool vari.abble
	false
	$ git config vari.able 1 ;# or "yes" or "on"
	$ git config --type=bool vari.abble
	true
	$ git config --type=bool vari.able yes ;# or "1" or "on"
	$ git config vari.able
	true
#33 lore raw

Re: [PATCH v8 2/3] introduce submodule.hasSuperproject record

In reply to Emily Shaffer

submodule.c static void relocate_single_git_dir_into_superproject(const char *path) · line 2137

Quoted 6 lines · Emily Shaffer
submodule.c +6 -0
+	/*
+	 * Note location of superproject's gitdir. Because the submodule already
+	 * has a gitdir and local config, we can store this pointer from
+	 * worktree config to worktree config, if the submodule has
+	 * extensions.worktreeConfig set.
+	 */

Probably the comment is a bit stale. There is no longer a pointer or location of superproject's gitdir recorded anywhere.

Quoted 18 lines
submodule.c +10 -0
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+
+	git_config_set_in_file(config_path.buf, "submodule.hasSuperproject",
+			       "true");
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7400-submodule-basic.sh +0 -0
index 40cf8d89aa..833fa01961 100755

t/t7400-submodule-basic.sh inspect() { · line 120

Quoted 6 lines
t/t7400-submodule-basic.sh +3 -0
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	git -C "$sub_dir" config "submodule.hasSuperproject"

Are we sufficiently happy to see the variable is set to anything, or do we require it to be set to boolean true?

If the former, the above is fine, with trailing && added.

If the latter, then something like

	val=$(git config --type=bool "submodule.hasSuperproject") &&
	test "$val" = true &&

would be more appropriate, but I wonder something like

test_config_is () {
	local var expect val
	var="$1" expect="$2"
	shift 2
        val=$(git "$@" config --type=bool "$var") &&
	test "$val" = "$expect"
}

would be in order. That would allow us to write

	test_config_is submodule.hasSuperproject true -C "$sub_dir" &&
Quoted 8 lines
t/t7400-submodule-basic.sh +0 -0
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
t/t7406-submodule-update.sh +0 -0
index 11cccbb333..422c3cc343 100755

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1066

Quoted 6 lines
t/t7406-submodule-update.sh +3 -0
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.hasSuperproject &&

Are we testing that submodule.hasSuperproject is set, and that it can successfully be unset? "config --unset no.such.var" will exit with non-zero status.

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1068

Quoted 2 lines
t/t7406-submodule-update.sh +2 -0
+	 git submodule update &&
+	 git -C submodule config submodule.hasSuperproject

Ditto.

#38 lore raw

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

In reply to Emily Shaffer
Quoted 13 lines · Emily Shaffer
submodule.c +1 -0
index 741104af8a..463e7f0c48 100644
@@ -2237,6 +2237,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;

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2255

Quoted 5 lines
submodule.c +2 -0
@@ -2250,6 +2251,17 @@ int get_superproject_working_tree(struct strbuf *buf)
 		 */
 		return 0;
 
+	if (git_config_get_bool("submodule.hassuperproject", &has_superproject_cfg)
+	    || !has_superproject_cfg) {

git_config_get_bool() returns 0 when it successfully finds the variable, so the above says "If submodule.hasSuperproject is not set at all, or if it is set to false, then..."

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2262

Quoted 7 lines
submodule.c +7 -0
+		/*
+		 * 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 never set.
+		 */
+		error(_("Asked to find a superproject, but submodule.hasSuperproject != true"));
+		return 0;

But given that this thing is new, I am not sure if that is a sensible guard to use here. Shouldn't we say

- If submodule.hasSuperproject is EXPLICITLY set to false then ...

instead? I.e.

	if (!git_config_get_bool("submodule.hassuperproject", &value) &&
	    !value) {
		error(_("asked to ..."));
		return 0;
	}
#41 lore raw
In reply to Junio C Hamano
Quoted 17 lines · Junio C Hamano
Quoted 3 lines · Emily Shaffer

One thing I'm not sure about: in the tests, I check whether the config is set, but not what the boolean value of it is. Is there a better way to do that?

Are you looking for value normalization during both setting and retrieving, i.e.

	$ git config vari.able 0 ;# or "no" or "off"
	$ git config --type=bool vari.abble
	false
	$ git config vari.able 1 ;# or "yes" or "on"
	$ git config --type=bool vari.abble
	true
Quoted 4 lines
	$ git config --type=bool vari.able yes ;# or "1" or "on"
	$ git config vari.able
	true

Ah, thanks! This helps. But that means I still need to check the return value, and associate "didn't find anything" (1) with the default as documented in Docs/config/submodule.txt, right?

Either way, this is useful. Thanks!

- Emily

#34 lore raw

Re: [PATCH v8 2/3] introduce submodule.hasSuperproject record

In reply to Junio C Hamano
Quoted 12 lines · Junio C Hamano

submodule.c static void relocate_single_git_dir_into_superproject(const char *path) · line 2137

Quoted 6 lines · Emily Shaffer
submodule.c +6 -0
+	/*
+	 * Note location of superproject's gitdir. Because the submodule already
+	 * has a gitdir and local config, we can store this pointer from
+	 * worktree config to worktree config, if the submodule has
+	 * extensions.worktreeConfig set.
+	 */

Probably the comment is a bit stale. There is no longer a pointer or location of superproject's gitdir recorded anywhere.

Thanks. I considered replacing it with a new comment about "now we'll note that it's got a superproject", but I think that's clear enough from the config set line, so I deleted the comment entirely.

Quoted 46 lines
Quoted 19 lines
submodule.c +10 -0
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+
+	git_config_set_in_file(config_path.buf, "submodule.hasSuperproject",
+			       "true");
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7400-submodule-basic.sh +0 -0
index 40cf8d89aa..833fa01961 100755

t/t7400-submodule-basic.sh inspect() { · line 120

Quoted 6 lines
t/t7400-submodule-basic.sh +3 -0
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	git -C "$sub_dir" config "submodule.hasSuperproject"

Are we sufficiently happy to see the variable is set to anything, or do we require it to be set to boolean true?

If the former, the above is fine, with trailing && added.

If the latter, then something like

	val=$(git config --type=bool "submodule.hasSuperproject") &&
	test "$val" = true &&

would be more appropriate, but I wonder something like

test_config_is () {
	local var expect val
	var="$1" expect="$2"
	shift 2
        val=$(git "$@" config --type=bool "$var") &&
	test "$val" = "$expect"
}
Quoted 4 lines

would be in order. That would allow us to write

	test_config_is submodule.hasSuperproject true -C "$sub_dir" &&

This seemed neat, so I started to look into implementing it, and found test_cmp_config() which takes additional args to pass to git config
- so I should be able to achieve this same thing with test_config_is -C "$sub_dir" --type=bool true submodule.hasSuperproject.

Quoted 19 lines
Quoted 8 lines
t/t7400-submodule-basic.sh +0 -0
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
t/t7406-submodule-update.sh +0 -0
index 11cccbb333..422c3cc343 100755

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1066

Quoted 6 lines
t/t7406-submodule-update.sh +3 -0
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.hasSuperproject &&

Are we testing that submodule.hasSuperproject is set, and that it can successfully be unset? "config --unset no.such.var" will exit with non-zero status.

Quoted 4 lines

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1068

Quoted 2 lines
t/t7406-submodule-update.sh +2 -0
+	 git submodule update &&
+	 git -C submodule config submodule.hasSuperproject

Ditto.

Ah, thanks.

#35 lore raw

Re: [PATCH v8 2/3] introduce submodule.hasSuperproject record

In reply to Emily Shaffer

Reviewing this series lightly because I will need to base 'ar/submodule-update reroll pt 2' on this (pt.1 is at
https://lore.kernel.org/git/[email protected]).

Quoted 5 lines · Emily Shaffer
git-submodule.sh +0 -0
index 652861aa66..59dffda775 100755

git-submodule.sh cmd_update() · line 457

Quoted 9 lines
git-submodule.sh +3 -0
@@ -449,6 +449,9 @@ cmd_update()
 			;;
 		esac
 
+		# Note that the submodule is a submodule.
+		git -C "$sm_path" config submodule.hasSuperproject "true"
+
 		if test -n "$recursive"
 		then
 			(

This hunk has a textual conflict with 'ar/submodule-update reroll pt 2', but the fix is easy - just teach "git submodule--helper update" to set the config in C.

Quoted 7 lines
t/t7406-submodule-update.sh +0 -0
index 11cccbb333..422c3cc343 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1072

Quoted 10 lines
t/t7406-submodule-update.sh +8 -0
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.hasSuperproject &&
+	 git submodule update &&
+	 git -C submodule config submodule.hasSuperproject
+	)
+'
+
 test_done

I think there is a gap in the test coverage. I notice that this doesn't test that we set submodule.hasSuperproject when the submodule is cloned for the first time with 'git submodule update'. I thought that maybe the test for this was here...

Quoted 5 lines
t/t7400-submodule-basic.sh +0 -0
index 40cf8d89aa..833fa01961 100755

t/t7400-submodule-basic.sh inspect() { · line 124

Quoted 10 lines
t/t7400-submodule-basic.sh +4 -0
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	git -C "$sub_dir" config "submodule.hasSuperproject"
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 

But when I removed the "set submodule.hasSuperproject in submodule" line, i.e.

git -C "$sm_path" config submodule.hasSuperproject "true"

t7400 still passes.

#36 lore raw

Re: [PATCH v8 2/3] introduce submodule.hasSuperproject record

Quoted 10 lines · Glen Choo
Quoted 8 lines · Emily Shaffer
git-submodule.sh +0 -0
index 652861aa66..59dffda775 100755
@@ -449,6 +449,9 @@ cmd_update()
 			;;
 		esac
 
Quoted 10 lines

git-submodule.sh cmd_update() · line 457

Quoted 6 lines
git-submodule.sh +3 -0
+		# Note that the submodule is a submodule.
+		git -C "$sm_path" config submodule.hasSuperproject "true"
+
 		if test -n "$recursive"
 		then
 			(

This hunk has a textual conflict with 'ar/submodule-update reroll pt 2', but the fix is easy - just teach "git submodule--helper update" to set the config in C.

From our dicussion (offline), it turns out this statement isn't really correct because we do set the config in C, but we do it in clone_submodule():

   diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
   index c5d3fc3817..92986646bc 100644
   --- a/builtin/submodule--helper.c
   +++ b/builtin/submodule--helper.c
   @@ -1839,6 +1839,11 @@ static int clone_submodule(struct module_clone_data *clone_data)
       git_config_set_in_file(p, "submodule.alternateErrorStrategy",
                  error_strategy);

   +	/*
   +	 * Teach the submodule that it's a submodule.
   +	 */
   +	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
   +
     free(sm_alternate);
     free(error_strategy);
Quoted 43 lines
Quoted 7 lines
t/t7406-submodule-update.sh +0 -0
index 11cccbb333..422c3cc343 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1072

Quoted 10 lines
t/t7406-submodule-update.sh +8 -0
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 git -C submodule config --unset submodule.hasSuperproject &&
+	 git submodule update &&
+	 git -C submodule config submodule.hasSuperproject
+	)
+'
+
 test_done

I think there is a gap in the test coverage. I notice that this doesn't test that we set submodule.hasSuperproject when the submodule is cloned for the first time with 'git submodule update'. I thought that maybe the test for this was here...

Quoted 5 lines
t/t7400-submodule-basic.sh +0 -0
index 40cf8d89aa..833fa01961 100755

t/t7400-submodule-basic.sh inspect() { · line 124

Quoted 10 lines
t/t7400-submodule-basic.sh +4 -0
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	git -C "$sub_dir" config "submodule.hasSuperproject"
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 

But when I removed the "set submodule.hasSuperproject in submodule" line, i.e.

Quoted 3 lines

git -C "$sm_path" config submodule.hasSuperproject "true"

t7400 still passes.

So we would expect that newly cloned submodules would pass even without this .sh line.

I don't think we need to do this twice in C and in shell. We can move this line:

   +	git_config_set_in_file(p, "submodule.hasSuperproject", "true");

into run-update-procedure (and out of clone_submodule()). This way it's guaranteed to touch every submodule (newly cloned or not).

#39 lore raw

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

In reply to Junio C Hamano
Quoted 37 lines · Junio C Hamano
Quoted 13 lines · Emily Shaffer
submodule.c +1 -0
index 741104af8a..463e7f0c48 100644
@@ -2237,6 +2237,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;

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2255

Quoted 5 lines
submodule.c +2 -0
@@ -2250,6 +2251,17 @@ int get_superproject_working_tree(struct strbuf *buf)
 		 */
 		return 0;
 
+	if (git_config_get_bool("submodule.hassuperproject", &has_superproject_cfg)
+	    || !has_superproject_cfg) {

git_config_get_bool() returns 0 when it successfully finds the variable, so the above says "If submodule.hasSuperproject is not set at all, or if it is set to false, then..."

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2262

Quoted 7 lines
submodule.c +7 -0
+		/*
+		 * 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 never set.
+		 */
+		error(_("Asked to find a superproject, but submodule.hasSuperproject != true"));
+		return 0;

But given that this thing is new, I am not sure if that is a sensible guard to use here. Shouldn't we say

Quoted 1 line

- If submodule.hasSuperproject is EXPLICITLY set to false then ...

Ah, interesting. I think that makes sense. I wrote this patch hoping to get an additional check for completeness of the previous patch (that is
- if I can rely on that value for this other operation, and all the tests still pass without me touching them, then I seem to have wired the new config through everywhere) and I think it's served that purpose; for the real world, that's a little more dangerous, so I think relying on explicit false makes sense. Will do.

Quoted 9 lines

instead? I.e.

	if (!git_config_get_bool("submodule.hassuperproject", &value) &&
	    !value) {
		error(_("asked to ..."));
		return 0;
	}
#42 lore raw

v9 cover teach submodules to know they're submodules

In reply to Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

CI run: https://github.com/nasamuffin/git/actions/runs/1954710601

Since v8:

Only a couple of minor fixes.

Junio pointed out that I could write the tests better using --type=bool and 'test_cmp_config', and that we could be a little more careful about when to give up on 'git rev-parse --show-superproject-working-dir'.

Glen mentioned that builtin/submodule--helper.c:run_update_procedure() is called unconditionally earlier in the same function where I had added the config in git-submodule.sh. So, I moved the config set into submodule--helper.c to reduce possible edge cases where the config might not be set.

Otherwise, this series is pretty much unchanged.

Since v7:

Actually a fairly large rework. Rather than keeping the path from gitdir to gitdir, just keep a boolean under 'submodule.hasSuperproject'. The idea is that from this boolean, we can decide whether to traverse the filesystem looking for a superproject.

Because this simplifies the implementation, I compressed the three middle commits into one. As proof-of-concept, I added a patch at the end to check for this boolean when running git rev-parse --show-superproject-working-tree.

One thing I'm not sure about: in the tests, I check whether the config is set, but not what the boolean value of it is. Is there a better way to do that? For example, I could imagine someone deciding to set submodule.hasSuperproject = false and the tests would not function correctly in that case. I think we don't really normalize the value on a boolean config like that, so I didn't want to write a lot of comparison to check if the value is 1 or true or True or TRUE or Yes or .... Am I overthinking it?

The other thing I'm not sure about: since it's just a bool, we're not restricted to setting this config only when we have both gitdir paths available. That makes me want to set the config any time we are doing something with submodules anyway, like any time 'git-submodule--helper' is used. But that helper seems to be called in the context of the superproject, not of the submodules, so adding this config for each submodule we touch would be a second child process. Is there some other common entry point for submodules that we can use?

- Emily

Since v6:

I've dropped the fifth commit to use this new config for git rev-parse --show-superproject-working-tree. I think it did more harm than good - that tool uses an odd way to determine whether the superproject is actually the superproject, anyways.

I poked a little bit at trying to find some benchmark to demonstrate that "submodule.superprojectGitDir" is actually faster - but I didn't end up able to write one without writing a ton of new code to traverse the filesystem. To be honest, I'm not all that interested in performance
- I want the config added for correctness, instead.

So, the only real changes between v6 and v7 are some documentation changes suggested by Jonathan Tan
(https://lore.kernel.org/git/20211117234300.2598132-1-jonathantanmy%40google.com).

Since v5:

A couple things. Firstly, a semantics change *back* to the semantics of v3 - we map from gitdir to gitdir, *not* from common dir to common dir, so that theoretically a submodule with multiple worktrees in multiple superproject worktrees will be able to figure out which worktree of the superproject it's in. (Realistically, that's not really possible right now, but I'd like to change that soon.)

Secondly, a rewording of comments and commit messages to indicate that this isn't a cache of some expensive operation, but rather intended to be the source of truth for all submodules. I also added a fifth commit rewriting git rev-parse --show-superproject-working-tree to demonstrate what that means in practice - but from a practical standpoint, I'm a little worried about that fifth patch. More details in the patch 5 description.

I did discuss Ævar's idea of relying on in-process filesystem digging to find the superproject's gitdir with the rest of the Google team, but in the end decided that there are some worries about filesystem digging in this way (namely, some ugly interactions with network drives that are actually already an issue for Googler Linux machines). Plus, the allure of being able to definitively know that we're a submodule is pretty strong. ;) But overall, this is the direction I'd prefer to keep going in, rather than trying to guess from the filesystem going forward.

Since v4:

The only real change here is a slight semantics change to map from <submodule gitdir> to <superproject common git dir>. In every case *except* for when the superproject has a worktree, this changes nothing. For the case when the superproject has a worktree, this means that now submodules will refer to the general superproject common dir (e.g. no worktree-specific refs or configs or whatnot).

I *think* that because a submodule should exist in the context of the common dir, not the worktree gitdir, that is ok. However, it does mean it would be difficult to do something like sharing a config specific to the worktree (the initial goal of this series).

$ROOT/.git
$ROOT/.git/config.superproject <- shared by $ROOT/.git/modules/sub $ROOT/.git/modules/sub <- points to $ROOT/.git
$ROOT/.git/worktrees/wt
$ROOT/.git/worktrees/wt/config.superproject <- contains a certain config-based pre-commit hook

If the submodule only knows about the common dir, that is tough, because the submodule would basically have to guess which worktree it's in from its own path. There would be no way for '$WT/sub' to inherit '$ROOT/.git/worktrees/wt/config.superproject'.

That said... right now, we don't support submodules in worktrees very well at all. A submodule in a worktree will get a brand new gitdir in $ROOT/.git/worktrees/modules/ (and that brand new gitdir would point to the super's common dir). So I think we can punt on this entire question until we teach submodules and worktrees to play more gracefully together (it's on my long list...), and at that time we can probably introduce a pointer from $ROOT/.git/modules/sub/worktrees/wt/ to $ROOT/.git/worktrees/wt/....

Or, to summarize the long ramble above: "this is still kind of weird with worktrees, but let's fix it later when we fix worktrees more thoroughly".

(More rambling about worktree weirdness here:
https://lore.kernel.org/git/YYRaII8YWVxlBqsF%40google.com )

Since v3, a pretty major change: the semantics of submodule.superprojectGitDir has changed, to point from the submodule's gitdir to the superproject's gitdir (in v3 and earlier, we kept a path from the submodule's *worktree* to the superproject's gitdir instead). This cleans up some of the confusions about the behavior when a submodule worktree moves around in the superproject's tree, or in a future when we support submodules having multiple worktrees.

I also tried to simplify the tests to use 'test-tool path-utils relative_path' everywhere - I think that makes them much more clear for a test reader, but if you're reviewing and it isn't obvious what we're testing for, please speak up.

I think this is pretty mature and there was a lot of general agreement that the gitdir->gitdir association was the way to go, so please be brutal and look for nits, leaks, etc. this round ;)
[/v4 cover letter]

Emily Shaffer (3):
t7400-submodule-basic: modernize inspect() helper introduce submodule.hasSuperproject record
rev-parse: short-circuit superproject worktree when config unset

 Documentation/config/submodule.txt |  6 ++++
 builtin/submodule--helper.c        | 11 +++++++
 submodule.c                        | 30 ++++++++++++++++++
 t/t1500-rev-parse.sh               | 10 +++++-
 t/t7400-submodule-basic.sh         | 42 ++++++++++++-------------
 t/t7406-submodule-update.sh        |  8 +++++
 t/t7412-submodule-absorbgitdirs.sh | 50 ++++++++++++++++++++++++++++--
 7 files changed, 131 insertions(+), 26 deletions(-)

Range-diff against v8:

-:  ---------- > 1:  251510c687 t7400-submodule-basic: modernize inspect() helper
1:  34cbfd81ee ! 2:  da01dc7c10 introduce submodule.hasSuperproject record
    @@ builtin/submodule--helper.c: static int clone_submodule(struct module_clone_data
      	free(sm_alternate);
      	free(error_strategy);
      
    -
    - ## git-submodule.sh ##
    -@@ git-submodule.sh: cmd_update()
    - 			;;
    - 		esac
    +@@ builtin/submodule--helper.c: static int run_update_procedure(int argc, const char **argv, const char *prefix)
    + 
    + 	free(prefixed_path);
      
    -+		# Note that the submodule is a submodule.
    -+		git -C "$sm_path" config submodule.hasSuperproject "true"
    ++	/*
    ++	 * This entry point is always called from a submodule, so this is a
    ++	 * good place to set a hint that this repo is a submodule.
    ++	 */
    ++	git_config_set("submodule.hasSuperproject", "true");
     +
    - 		if test -n "$recursive"
    - 		then
    - 			(
    + 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
    + 		return do_run_update_procedure(&update_data);
    + 
     
      ## submodule.c ##
     @@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *path)
    @@ submodule.c: static void relocate_single_git_dir_into_superproject(const char *p
      
      	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
      
    -+	/*
    -+	 * Note location of superproject's gitdir. Because the submodule already
    -+	 * has a gitdir and local config, we can store this pointer from
    -+	 * worktree config to worktree config, if the submodule has
    -+	 * extensions.worktreeConfig set.
    -+	 */
     +	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
     +	git_configset_init(&sub_cs);
     +	git_configset_add_file(&sub_cs, config_path.buf);
    @@ t/t7400-submodule-basic.sh: inspect() {
      	git -C "$sub_dir" diff-files --exit-code &&
     +
     +	# Ensure that submodule.hasSuperproject is set.
    -+	git -C "$sub_dir" config "submodule.hasSuperproject"
    ++	test_cmp_config -C "$sub_dir" true --type=bool "submodule.hasSuperproject"
     +
      	git -C "$sub_dir" clean -n -d -x >untracked
      }
    @@ t/t7406-submodule-update.sh: test_expect_success 'submodule update --quiet passe
      
     +test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
     +	(cd super &&
    -+	 git -C submodule config --unset submodule.hasSuperproject &&
    ++	 test_unconfig submodule.hasSuperproject &&
     +	 git submodule update &&
    -+	 git -C submodule config submodule.hasSuperproject
    ++	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
     +	)
     +'
     +
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorb the git dir' '
     -	test_cmp expect.2 actual.2
     +	test_cmp expect.2 actual.2 &&
     +
    -+	git -C sub1 config submodule.hasSuperproject
    ++	test_cmp_config -C sub1 true --type=bool submodule.hasSuperproject
      '
      
      test_expect_success 'absorbing does not fail for deinitialized submodules' '
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorb the git dir in a
     -	test_cmp expect.2 actual.2
     +	test_cmp expect.2 actual.2 &&
     +
    -+	git -C sub1/nested config submodule.hasSuperproject
    ++	test_cmp_config -C sub1/nested true --type=bool submodule.hasSuperproject
      '
      
      test_expect_success 're-setup nested submodule' '
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
     +	git submodule absorbgitdirs sub4 &&
     +
     +	# make sure the submodule noted the superproject
    -+	git -C sub4 config submodule.hasSuperproject
    ++	test_cmp_config -C sub4 true --type=bool submodule.hasSuperproject
     +	)
     +'
     +
    @@ t/t7412-submodule-absorbgitdirs.sh: test_expect_success 'absorbing fails for a s
     +	git submodule absorbgitdirs sub5 &&
     +
     +	# make sure the submodule noted the superproject
    -+	git -C sub5 config submodule.hasSuperproject
    ++	test_cmp_config -C sub5 true --type=bool submodule.hasSuperproject
     +	)
     +'
     +
2:  c14ee8760f < -:  ---------- rev-parse: short-circuit superproject worktree when config unset
-:  ---------- > 3:  1893a84fdc rev-parse: short-circuit superproject worktree when config unset
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#43 lore raw

PATCH v9 1/3 t7400-submodule-basic: modernize inspect() helper

Since the inspect() helper in the submodule-basic test suite was written, 'git -C <dir>' was added. By using -C, we no longer need a reference to the base directory for the test. This simplifies callsites, and will make the addition of other arguments in later patches more readable.

---
 t/t7400-submodule-basic.sh | 40 +++++++++++++++-----------------------
 1 file changed, 16 insertions(+), 24 deletions(-)
t/t7400-submodule-basic.sh +16 -24
index e7cec2e457..40cf8d89aa 100755
@@ -107,23 +107,15 @@ test_expect_success 'setup - repository to add submodules to' '
 # generates, which will expand symbolic links.
 submodurl=$(pwd -P)
 
-listbranches() {
-	git for-each-ref --format='%(refname)' 'refs/heads/*'
-}
-
 inspect() {
-	dir=$1 &&
-	dotdot="${2:-..}" &&
-
-	(
-		cd "$dir" &&
-		listbranches >"$dotdot/heads" &&
-		{ git symbolic-ref HEAD || :; } >"$dotdot/head" &&
-		git rev-parse HEAD >"$dotdot/head-sha1" &&
-		git update-index --refresh &&
-		git diff-files --exit-code &&
-		git clean -n -d -x >"$dotdot/untracked"
-	)
+	sub_dir=$1 &&
+
+	git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads &&
+	{ git -C "$sub_dir" symbolic-ref HEAD || :; } >head &&
+	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
+	git -C "$sub_dir" update-index --refresh &&
+	git -C "$sub_dir" diff-files --exit-code &&
+	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
 test_expect_success 'submodule add' '
@@ -146,7 +138,7 @@ test_expect_success 'submodule add' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod ../.. &&
+	inspect addtest/submod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -248,7 +240,7 @@ test_expect_success 'submodule add --branch' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/submod-branch ../.. &&
+	inspect addtest/submod-branch &&
 	test_cmp expect-heads heads &&
 	test_cmp expect-head head &&
 	test_must_be_empty untracked
@@ -264,7 +256,7 @@ test_expect_success 'submodule add with ./ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotsubmod/frotz ../../.. &&
+	inspect addtest/dotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -280,7 +272,7 @@ test_expect_success 'submodule add with /././ in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+	inspect addtest/dotslashdotsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -296,7 +288,7 @@ test_expect_success 'submodule add with // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/slashslashsubmod/frotz ../../.. &&
+	inspect addtest/slashslashsubmod/frotz &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -312,7 +304,7 @@ test_expect_success 'submodule add with /.. in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod ../.. &&
+	inspect addtest/realsubmod &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -328,7 +320,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod2 ../.. &&
+	inspect addtest/realsubmod2 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
@@ -359,7 +351,7 @@ test_expect_success 'submodule add in subdirectory' '
 	) &&
 
 	rm -f heads head untracked &&
-	inspect addtest/realsubmod3 ../.. &&
+	inspect addtest/realsubmod3 &&
 	test_cmp expect heads &&
 	test_cmp expect head &&
 	test_must_be_empty untracked
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#44 lore raw

PATCH v9 2/3 introduce submodule.hasSuperproject record

In reply to Emily Shaffer

Teach submodules a config variable indicating the fact that they are a submodule. If this config is set to false or unset, Git may assume the current repo is not a submodule.

Git commands can use this variable to decide whether to traverse the filesystem and look for a superproject at all. 'git rev-parse --show-superproject-working-tree' can learn to exit early if this config is unset or false. Other newly added or implicit behavior - like "git status" showing the submodule's status in relation to the superproject, or a config shared between the superproject and submodule - can use this config to decide whether to search the parent directory to find a superproject.

Introduce this config everywhere we add a new submodule, or touch one that already exists, so that we can proliferate it in repos which are already out in the world using submodules.

---
 Documentation/config/submodule.txt |  6 ++++
 builtin/submodule--helper.c        | 11 +++++++
 submodule.c                        | 12 +++++++
 t/t7400-submodule-basic.sh         |  4 +++
 t/t7406-submodule-update.sh        |  8 +++++
 t/t7412-submodule-absorbgitdirs.sh | 50 ++++++++++++++++++++++++++++--
 6 files changed, 89 insertions(+), 2 deletions(-)
Documentation/config/submodule.txt +6 -0
index ee454f8126..99d5260b8e 100644
@@ -91,3 +91,9 @@ submodule.alternateErrorStrategy::
 	`ignore`, `info`, `die`. Default is `die`. Note that if set to `ignore`
 	or `info`, and if there is an error with the computed alternate, the
 	clone proceeds as if no alternate was specified.
+
+submodule.hasSuperproject::
+	Indicates whether this repository is a submodule. If this config is set
+	to 'true', Git may traverse the filesystem above this submodule in order
+	to identify the superproject. It is set automatically during submodule
+	creation, update, and 'git submodule absorbgitdir'.
builtin/submodule--helper.c +11 -0
index c5d3fc3817..eda9ed550e 100644
@@ -1839,6 +1839,11 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Teach the submodule that it's a submodule.
+	 */
+	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
+
 	free(sm_alternate);
 	free(error_strategy);
 
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);
 
submodule.c +12 -0
index c689070524..aafbd628ad 100644
@@ -2097,6 +2097,8 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
 	struct strbuf new_gitdir = STRBUF_INIT;
 	const struct submodule *sub;
+	struct config_set sub_cs;
+	struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT;
 
 	if (submodule_uses_worktrees(path))
 		die(_("relocate_gitdir for submodule '%s' with "
@@ -2127,6 +2129,16 @@ static void relocate_single_git_dir_into_superproject(const char *path)
 
 	relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
 
+	strbuf_addf(&config_path, "%s/config", real_new_git_dir);
+	git_configset_init(&sub_cs);
+	git_configset_add_file(&sub_cs, config_path.buf);
+
+	git_config_set_in_file(config_path.buf, "submodule.hasSuperproject",
+			       "true");
+
+	git_configset_clear(&sub_cs);
+	strbuf_release(&config_path);
+	strbuf_release(&sb);
 	free(old_git_dir);
 	free(real_old_git_dir);
 	free(real_new_git_dir);
t/t7400-submodule-basic.sh +4 -0
index 40cf8d89aa..53c8bf699d 100755
@@ -115,6 +115,10 @@ inspect() {
 	git -C "$sub_dir" rev-parse HEAD >head-sha1 &&
 	git -C "$sub_dir" update-index --refresh &&
 	git -C "$sub_dir" diff-files --exit-code &&
+
+	# Ensure that submodule.hasSuperproject is set.
+	test_cmp_config -C "$sub_dir" true --type=bool "submodule.hasSuperproject"
+
 	git -C "$sub_dir" clean -n -d -x >untracked
 }
 
t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done
t/t7412-submodule-absorbgitdirs.sh +48 -2

66 lines not on this page · raw message

Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#59 lore raw

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

In reply to Emily Shaffer

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.

---
 submodule.c          | 18 ++++++++++++++++++
 t/t1500-rev-parse.sh | 10 +++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
submodule.c +18 -0
index aafbd628ad..64760f1e3a 100644
@@ -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;
 
t/t1500-rev-parse.sh +9 -1
index 1c2df08333..dd35036bd6 100755
@@ -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:
Signature · 1 line
2.35.1.616.g0bdcbb4464-goog
#60 lore raw

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

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2253

Quoted 6 lines · Emily Shaffer
submodule.c +6 -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.
+	 */

That's a round-about way to say that submodule.hassuperproject defaults to true, isn't it ;-)?

submodule.c int get_superproject_working_tree(struct strbuf *buf) · line 2261

Quoted 8 lines
submodule.c +8 -0
+	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"));

s/Asked to/asked to/, probably.

Quoted 27 lines
submodule.c +3 -0
+		return 0;
+	}
+
 	if (!strbuf_realpath(&one_up, "../", 0))
 		return 0;
t/t1500-rev-parse.sh +9 -1
index 1c2df08333..dd35036bd6 100755
@@ -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:
#45 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Emily Shaffer

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2632

Quoted 12 lines · Emily Shaffer
builtin/submodule--helper.c +6 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);

In Glen's update to rewrite "submodule update" in C, this part is replaced with a call to update_submodule2(). I am not sure what the current repository is at this point of the code with and without Glen's topic, but are we sure we are in a submodule we discovered?

builtin/submodule--helper.c::run_update_procedure() takes sm_path to the path to the submodule, presumably from superproject's point of view, and the callchain leads to a call to run_update_command() eventually, which uses run_command_v_opt_cd_env() to go in to the submodule repository and run an external git command (like "checkout"), so it looks like what git_config_set() updates is the superprojects' configuration, not the configuration of a particular submodule being updated.

The other one, where cmd_clone() sets the variable in submodule's configuration file, looks good, but I am not sure about this one.

#56 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Emily Shaffer

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1066

Quoted 3 lines · Emily Shaffer
t/t7406-submodule-update.sh +3 -0
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&

So, before we run the test, we unconfig the variable in the SUPER repository, and then

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1067

Quoted 1 line
t/t7406-submodule-update.sh +1 -0
+	 git submodule update &&

update the submodule, and then

t/t7406-submodule-update.sh test_expect_success 'submodule update --quiet passes quietness to fetch with a s · line 1068

Quoted 1 line
t/t7406-submodule-update.sh +1 -0
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject

go in to the submodule and check the value of the variable?

Shouldn't the first part be more like

	(cd super &&
	 test_unconfig -C submodule submodule.hasSuperproject &&

if we want to make sure "submodule update" sets it there?

Quoted 2 lines
(quoted diff) +2 -0
+	)
+'
#61 lore raw

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

In reply to Junio C Hamano
Quoted 4 lines · Junio C Hamano
Quoted 1 line · Emily Shaffer

+ error(_("Asked to find a superproject, but submodule.hasSuperproject == false"));

s/Asked to/asked to/, probably.

This is a user-facing error message, not just a programmer-facing message, correct? If so, then perhaps: s/==/is/

#46 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Emily Shaffer
builtin/submodule--helper.c +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
Quoted 13 lines

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2632

Quoted 8 lines
builtin/submodule--helper.c +6 -0
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);

In Glen's update to rewrite "submodule update" in C, this part is replaced with a call to update_submodule2(). I am not sure what the current repository is at this point of the code with and without Glen's topic, but are we sure we are in a submodule we discovered?

With my topic, this call would be moved into update_submodule2(). The repository at that point is the superproject.

Quoted 11 lines

builtin/submodule--helper.c::run_update_procedure() takes sm_path to the path to the submodule, presumably from superproject's point of view, and the callchain leads to a call to run_update_command() eventually, which uses run_command_v_opt_cd_env() to go in to the submodule repository and run an external git command (like "checkout"), so it looks like what git_config_set() updates is the superprojects' configuration, not the configuration of a particular submodule being updated.

The other one, where cmd_clone() sets the variable in submodule's configuration file, looks good, but I am not sure about this one.

But in this series, the current repository is the submodule because this part happens in a "run-update-procedure" child process.

So there is a slight conflict here, but the conflict existed even before this change (we used to do this twice in git-submodule.sh and module_clone()).

Because of that conflict, I was planning to base "part2" on this series anyway, and if anything, this change makes the conflict better because we now set "submodule.hasSuperproject" in only one place (run_update_procedure()) instead of two.

So I think this change improves this series and improves the interaction with mine.

#47 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Emily Shaffer
(quoted diff) +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
Quoted 13 lines

Review static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2632

Quoted 8 lines
(quoted diff) +6 -0
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);

In Glen's update to rewrite "submodule update" in C, this part is replaced with a call to update_submodule2(). I am not sure what the current repository is at this point of the code with and without Glen's topic, but are we sure we are in a submodule we discovered?

Rereading this, I realize you probably meant that this conflicts with part1, not part2...

At the end of part1, update_submodule2() is called from inside the submodule (specifically from run_update_procedure()). So a good merge conflict resolution would be to set the config _before_ calling update_submodule2(). e.g.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 ----

builtin/submodule--helper.c +5 -0
index bef9ab22d4..f53808d995 100644
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }
#57 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Emily Shaffer
Quoted 7 lines · Emily Shaffer
Documentation/config/submodule.txt +0 -0
index ee454f8126..99d5260b8e 100644
builtin/submodule--helper.c +0 -0
index c5d3fc3817..eda9ed550e 100644

builtin/submodule--helper.c static int clone_submodule(struct module_clone_data *clone_data) · line 1848

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -1839,6 +1839,11 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Teach the submodule that it's a submodule.
+	 */
+	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
+
 	free(sm_alternate);
 	free(error_strategy);

This git_config_set_* is superfluous - it sets the config in newly cloned submodules..

Quoted 4 lines
builtin/submodule--helper.c +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2633

Quoted 10 lines
builtin/submodule--helper.c +6 -0
 
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);
 

but this is called over *all* submodules, so we're guaranteed to always set the config if "git submodule update" isn't interrupted halfway.

I don't think we guarantee correctness if it is interrupted halfway e.g. core.worktree can be unset if it is interrupted halfway (because ensure-core-worktree is called adjacent to run-update-procedure, not inside of update-clone).

So I think it's better to just drop the previous hunk - it will disappear anyway in gc/submodule-update-part2.

#48 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Glen Choo
Quoted 36 lines · Glen Choo
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Emily Shaffer
(quoted diff) +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
Quoted 13 lines

Review static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2632

Quoted 8 lines
(quoted diff) +6 -0
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);

In Glen's update to rewrite "submodule update" in C, this part is replaced with a call to update_submodule2(). I am not sure what the current repository is at this point of the code with and without Glen's topic, but are we sure we are in a submodule we discovered?

Rereading this, I realize you probably meant that this conflicts with part1, not part2...

At the end of part1, update_submodule2() is called from inside the submodule (specifically from run_update_procedure()). So a good merge conflict resolution would be to set the config _before_ calling update_submodule2(). e.g.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 ----

builtin/submodule--helper.c +0 -0
index bef9ab22d4..f53808d995 100644

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2686

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }

That matched my tentative resolution I made last night, but what do you think about this part of the test added by the patch?

t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done

We go to "super", make sure that superproject does not have submodule.hasSuperproject set, run "git submodule update", and see if the configuration file in "submodule" subdirectory has the variable set. It does not clear the variable from the submodule before starting, so the variable given to the submodule when it was cloned would be there, even if "git submodule update" failed to set it.

I am wondering if it should do something like the attached instead.

We

* clear the variable from "super" and "super/submodule" repositories;

* run "git submodule update";

* ensure that "git submodule update" did not touch "super/.git/config";

* ensure that "git submodule update" added the variable to "super/submodule/.git/config".

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

With the conflict resolution above, this "corrected" test fails and shows that superproject's configuration file is updated after "git submodule update".

This series alone, without your topic, this "corrected" test fails, and that is where my "are we sure we are mucking with the configuration file in the submodule"? comes from.

t/t7406-submodule-update.sh +12 -0
index 000e055811..c9912bb242 100755
@@ -1083,4 +1083,16 @@ test_expect_success 'submodule update --filter sets partial clone settings' '
 	test_cmp_config -C super-filter/submodule blob:none remote.origin.partialclonefilter
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 test_unconfig -C submodule submodule.hasSuperproject &&
+	 git submodule update &&
+	 echo in super &&
+	 test_cmp_config false --type=bool submodule.hasSuperproject &&
+	 echo in submodule &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done
#49 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

Quoted 56 lines · Junio C Hamano
Quoted 5 lines
builtin/submodule--helper.c +0 -0
index bef9ab22d4..f53808d995 100644

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2686

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }

That matched my tentative resolution I made last night, but what do you think about this part of the test added by the patch?

t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done

We go to "super", make sure that superproject does not have submodule.hasSuperproject set, run "git submodule update", and see if the configuration file in "submodule" subdirectory has the variable set. It does not clear the variable from the submodule before starting, so the variable given to the submodule when it was cloned would be there, even if "git submodule update" failed to set it.

I am wondering if it should do something like the attached instead.

We

* clear the variable from "super" and "super/submodule" repositories;

* run "git submodule update";

* ensure that "git submodule update" did not touch "super/.git/config";

Quoted 8 lines

* ensure that "git submodule update" added the variable to "super/submodule/.git/config".

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

Yes, the test you've described is closer to what I thought the original test was trying to do. Seeing this test pass gave me a false sense of confidence hm..

Quoted 7 lines

With the conflict resolution above, this "corrected" test fails and shows that superproject's configuration file is updated after "git submodule update".

This series alone, without your topic, this "corrected" test fails, and that is where my "are we sure we are mucking with the configuration file in the submodule"? comes from.

Yeah looks like we aren't in the submodule after all:

		out=$(git submodule--helper run-update-procedure \
			  ${wt_prefix:+--prefix "$wt_prefix"} \
			  ${GIT_QUIET:+--quiet} \
			  ${force:+--force} \
			  ${just_cloned:+--just-cloned} \
			  ${nofetch:+--no-fetch} \
			  ${depth:+"$depth"} \
			  ${update:+--update "$update"} \
			  ${prefix:+--recursive-prefix "$prefix"} \
			  ${sha1:+--oid "$sha1"} \
			  ${subsha1:+--suboid "$subsha1"} \
			  "--" \
			  "$sm_path")

This says "do the update at this submodule path", but this is being run from the superproject.

So I suppose the way forward is one of the following:

- Revert my original suggestion
- Revert my original suggestion AND remove the git_config_set from "module_clone()" (before this, we unconditionally set this value in git-submodule.sh anyway)
- Set the config in the submodule even though we are running from the superproject (this is possible, ensure_core_worktree() does this).

In any case, sorry for the faulty suggestion :(

#50 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

Quoted 61 lines · Glen Choo
Quoted 59 lines · Junio C Hamano
Quoted 5 lines
builtin/submodule--helper.c +0 -0
index bef9ab22d4..f53808d995 100644

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2686

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }

That matched my tentative resolution I made last night, but what do you think about this part of the test added by the patch?

t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done

We go to "super", make sure that superproject does not have submodule.hasSuperproject set, run "git submodule update", and see if the configuration file in "submodule" subdirectory has the variable set. It does not clear the variable from the submodule before starting, so the variable given to the submodule when it was cloned would be there, even if "git submodule update" failed to set it.

I am wondering if it should do something like the attached instead.

We

* clear the variable from "super" and "super/submodule" repositories;

* run "git submodule update";

* ensure that "git submodule update" did not touch "super/.git/config";

* ensure that "git submodule update" added the variable to "super/submodule/.git/config".

Quoted 9 lines
Quoted 5 lines

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

Yes, the test you've described is closer to what I thought the original test was trying to do. Seeing this test pass gave me a false sense of confidence hm..

Correction, seeing the _original_ test pass gave me false sense of confidence.

Quoted 9 lines
Quoted 7 lines

With the conflict resolution above, this "corrected" test fails and shows that superproject's configuration file is updated after "git submodule update".

This series alone, without your topic, this "corrected" test fails, and that is where my "are we sure we are mucking with the configuration file in the submodule"? comes from.

- Set the config in the submodule even though we are running from the superproject (this is possible, ensure_core_worktree() does this).

If it helps, I was able to do this up by copying ensure_core_worktree(), and this passes the amended test.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 ----

builtin/submodule--helper.c +0 -5
index 4d02dd05ca..3bb7a65762 100644
@@ -1838,11 +1838,6 @@ static int clone_submodule(struct module_clone_data *clone_data)
    git_config_set_in_file(p, "submodule.alternateErrorStrategy",
              error_strategy);
 
-	/*
-	 * Teach the submodule that it's a submodule.
-	 */
-	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
-
  free(sm_alternate);
  free(error_strategy);
 
@@ -2560,6 +2555,20 @@ static int update_clone(int argc, const char **argv, const char *prefix)
  return update_submodules(&suc);

}

+static void set_hassuperproject(const char *sm_path)
+{
+ struct repository subrepo;
+ char *cfg_file;
+
+ if (repo_submodule_init(&subrepo, the_repository, sm_path, null_oid()))
+ die(_("could not get a repository handle for submodule '%s'"), sm_path);
+
+ cfg_file = repo_git_path(&subrepo, "config");
+ git_config_set_in_file(cfg_file, "submodule.hasSuperproject", "true");
+
+ free(cfg_file);
+}
+
static int run_update_procedure(int argc, const char **argv, const char *prefix)
{
int force = 0, quiet = 0, nofetch = 0, just_cloned = 0;
@@ -2622,10 +2631,9 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
free(prefixed_path);

/*
- * This entry point is always called from a submodule, so this is a
- * good place to set a hint that this repo is a submodule.
+ * Teach the submodule that it's a submodule.
*/
- git_config_set("submodule.hasSuperproject", "true");
+ set_hassuperproject(update_data.sm_path);

  if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
    return do_run_update_procedure(&update_data);
#62 lore raw
In reply to Emily Shaffer
Quoted 42 lines · Emily Shaffer

For the original cover letter, see
https://lore.kernel.org/git/20210611225428.1208973-1-emilyshaffer%40google.com.

CI run: https://github.com/nasamuffin/git/actions/runs/1954710601

Since v8:

Only a couple of minor fixes.

Junio pointed out that I could write the tests better using --type=bool and 'test_cmp_config', and that we could be a little more careful about when to give up on 'git rev-parse --show-superproject-working-dir'.

Glen mentioned that builtin/submodule--helper.c:run_update_procedure() is called unconditionally earlier in the same function where I had added the config in git-submodule.sh. So, I moved the config set into submodule--helper.c to reduce possible edge cases where the config might not be set.

Otherwise, this series is pretty much unchanged.

Since v7:

Actually a fairly large rework. Rather than keeping the path from gitdir to gitdir, just keep a boolean under 'submodule.hasSuperproject'. The idea is that from this boolean, we can decide whether to traverse the filesystem looking for a superproject.

Because this simplifies the implementation, I compressed the three middle commits into one. As proof-of-concept, I added a patch at the end to check for this boolean when running git rev-parse --show-superproject-working-tree.

One thing I'm not sure about: in the tests, I check whether the config is set, but not what the boolean value of it is. Is there a better way to do that? For example, I could imagine someone deciding to set submodule.hasSuperproject = false and the tests would not function correctly in that case. I think we don't really normalize the value on a boolean config like that, so I didn't want to write a lot of comparison to check if the value is 1 or true or True or TRUE or Yes or .... Am I overthinking it?

Quoted 8 lines

The other thing I'm not sure about: since it's just a bool, we're not restricted to setting this config only when we have both gitdir paths available. That makes me want to set the config any time we are doing something with submodules anyway, like any time 'git-submodule--helper' is used. But that helper seems to be called in the context of the superproject, not of the submodules, so adding this config for each submodule we touch would be a second child process. Is there some other common entry point for submodules that we can use?

I really don't mean to bring up the same points again, but I'm still genuinely unsure what this is intended to solve in the end.

I.e. from the original RFC we went from it being for optimizations for the shellscript "git rev-parse", to suggestions that the configured path would be "canonical" in a way we couldn't discover on-the-fly (i.e. some of Jonathan's noted edge cases [1]).

But now it's a boolean indicating "it's there, discover it", and the implied (but not really explicitly stated) reason in 2/3 is that it's purely for optimization purposes at this point.

But it's an optimization without a benchmark.

In [1] Jonathan (if I understood it correctly, see [2]) might have suggested this is important to deal with some Google in-house NFS-a-like auto-mounting software, i.e. the "walking up" is truly expensive in some scenarios.

I do worry a bit that we'll be creating behavior edge cases related to this, and if the problem being solved is for a relatively obscure setup is it worth it, and in that case perhaps there should be a "I need this optimization" setting guarding it?

But I don't know, a concrete case where this series makes a difference would really help.

I tried to come up with one before[3] and all I could find was fleeting cases we'd see go away with the migration of the remaining parts of git-submodule.sh to C, which we already have in-flight patches for (or rather, Glen is AFAIK at series 1/2 of submitting those, with 1/2 in-flight).

In any case I think lifting the bits of [3] where we assert that this doesn't introduce any behavior change with a GIT_TEST_* knob would be valuable.

I.e. as long a the intent isn't a behavior change let's test that get_superproject_working_tree() doesn't need this across the entire test suite, with specific tests that opt-in to the behavior (or do a whole test suite run in that mode), rather than the default being opt-out.

An opt-out is just a recipe for growing accidental implicit dependencies, which explicitly isn't what we want for a "just an optimization" knob. We do the same sort of opt-in/out-out testing for e.g. split index, untracked cache etc (see the GIT_TEST_* bits in ci/run-build-and-tests.sh). AFAICT a fix-up of just adding the git_env_bool() here to this code in your 3/3 would do it:

	if (!git_env_bool("GIT_TEST_NO_SUBMODULE_HAS_SUPERPROJECT", 0) &&
	    !git_config_get_bool("submodule.hassuperproject", &has_superproject_cfg)
	    && !has_superproject_cfg)

And then adding GIT_TEST_NO_SUBMODULE_HAS_SUPERPROJECT=true to linux-TEST-vars in ci/run-build-and-tests.sh. The tests that do rely on submodule.hassuperproject would need to set GIT_TEST_NO_SUBMODULE_HAS_SUPERPROJECT=false of course...

1. https://lore.kernel.org/git/[email protected]/
2. https://lore.kernel.org/git/[email protected]/
3. https://lore.kernel.org/git/[email protected]/

#63 lore raw
Quoted 3 lines · Ævar Arnfjörð Bjarmason

But now it's a boolean indicating "it's there, discover it", and the implied (but not really explicitly stated) reason in 2/3 is that it's purely for optimization purposes at this point.

You may know that I have a separate checkout of the 'todo' branch at path "Meta" in my working tree.

I could use the hasSuperproject=false setting there, to say "this is *NOT* a submodule, even the parent directory is a working tree of a different repository, it is not our superproject, so do *NOT* bother to go up to discover anything".

If that configuration weren't there in the "Meta/.git/config", the parent directory of "Meta" (which has its own ".git") cannot tell if that "Meta" thing is a submodule being prepared that hasn't been added yet, or it will never intended to be a submodule. I would imagine that "git add X" can later be taught to refuse to add X if there is X/.git and X/.git/config says it explicitly says that it does not have a superproject.

So, I am not sure if it is a good characterization that it is for optimization at all.

#58 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Glen Choo
Quoted 42 lines · Glen Choo
Quoted 7 lines · Emily Shaffer
Documentation/config/submodule.txt +0 -0
index ee454f8126..99d5260b8e 100644
builtin/submodule--helper.c +0 -0
index c5d3fc3817..eda9ed550e 100644

builtin/submodule--helper.c static int clone_submodule(struct module_clone_data *clone_data) · line 1848

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -1839,6 +1839,11 @@ static int clone_submodule(struct module_clone_data *clone_data)
 		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
 				       error_strategy);
 
+	/*
+	 * Teach the submodule that it's a submodule.
+	 */
+	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
+
 	free(sm_alternate);
 	free(error_strategy);

This git_config_set_* is superfluous - it sets the config in newly cloned submodules..

Quoted 4 lines
builtin/submodule--helper.c +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2633

Quoted 10 lines
builtin/submodule--helper.c +6 -0
 
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);
 

but this is called over *all* submodules, so we're guaranteed to always set the config if "git submodule update" isn't interrupted halfway.

Quoted 7 lines

I don't think we guarantee correctness if it is interrupted halfway e.g. core.worktree can be unset if it is interrupted halfway (because ensure-core-worktree is called adjacent to run-update-procedure, not inside of update-clone).

So I think it's better to just drop the previous hunk - it will disappear anyway in gc/submodule-update-part2.

Ah, this makes sense. Sure, will do.

- Emily

#54 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Junio C Hamano
Quoted 81 lines · Junio C Hamano
Quoted 36 lines · Glen Choo
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Emily Shaffer
(quoted diff) +0 -0
@@ -2617,6 +2622,12 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
 
 	free(prefixed_path);
 
Quoted 13 lines

Review static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2632

Quoted 8 lines
(quoted diff) +6 -0
+	/*
+	 * This entry point is always called from a submodule, so this is a
+	 * good place to set a hint that this repo is a submodule.
+	 */
+	git_config_set("submodule.hasSuperproject", "true");
+
 	if (!oideq(&update_data.oid, &update_data.suboid) || update_data.force)
 		return do_run_update_procedure(&update_data);

In Glen's update to rewrite "submodule update" in C, this part is replaced with a call to update_submodule2(). I am not sure what the current repository is at this point of the code with and without Glen's topic, but are we sure we are in a submodule we discovered?

Rereading this, I realize you probably meant that this conflicts with part1, not part2...

At the end of part1, update_submodule2() is called from inside the submodule (specifically from run_update_procedure()). So a good merge conflict resolution would be to set the config _before_ calling update_submodule2(). e.g.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 ----

builtin/submodule--helper.c +0 -0
index bef9ab22d4..f53808d995 100644

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2686

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }

That matched my tentative resolution I made last night, but what do you think about this part of the test added by the patch?

t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done

We go to "super", make sure that superproject does not have submodule.hasSuperproject set, run "git submodule update", and see if the configuration file in "submodule" subdirectory has the variable set. It does not clear the variable from the submodule before starting, so the variable given to the submodule when it was cloned would be there, even if "git submodule update" failed to set it.

I am wondering if it should do something like the attached instead.

Quoted 8 lines

We

* clear the variable from "super" and "super/submodule" repositories;

* run "git submodule update";

* ensure that "git submodule update" did not touch "super/.git/config";

Yeah, this is a good idea, and indeed when I add this step the bug pointed out downthread becomes clear. Thanks.

Quoted 39 lines

* ensure that "git submodule update" added the variable to "super/submodule/.git/config".

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

With the conflict resolution above, this "corrected" test fails and shows that superproject's configuration file is updated after "git submodule update".

This series alone, without your topic, this "corrected" test fails, and that is where my "are we sure we are mucking with the configuration file in the submodule"? comes from.

t/t7406-submodule-update.sh +12 -0
index 000e055811..c9912bb242 100755
@@ -1083,4 +1083,16 @@ test_expect_success 'submodule update --filter sets partial clone settings' '
 	test_cmp_config -C super-filter/submodule blob:none remote.origin.partialclonefilter
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 test_unconfig -C submodule submodule.hasSuperproject &&
+	 git submodule update &&
+	 echo in super &&
+	 test_cmp_config false --type=bool submodule.hasSuperproject &&
+	 echo in submodule &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done
#55 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

Quoted 5 lines · Emily Shaffer
Quoted 5 lines

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

If we wanted to be kosher about this, we could start the test with

git config submodule.hassuperproject 1

in the "super" repository, clear the variable in the "submodule" repository, before running the "git submodule update" step, which
(1) should not touch the "super" configuration and (2) should touch the "submodule" configuration.

If we inspect in the "super" repository after "submodule update"

    value=$(git config submodule.hassuperproject) &&
    test "$value" = 1

I think we can tell if a buggy "submodule update" overwrites the "super" configuration from "1" to "true". And downstream tests will take "1" as true just fine.

And of course, in "submodule", the variable after "submodule update" must be set to true, which can be checked with

value=$(git -C submodule config --type=bool submodule.hassuperproject) && test "$value" = true

The trick depends on the hardcoded value to represent "true" in the code this patch adds, but that is the canonical way to spell true in the config, according to "git config --type=bool", so the dependency may not be too bad.

Just a thought.

#51 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

In reply to Glen Choo
Quoted 109 lines · Glen Choo
Quoted 61 lines · Glen Choo
Quoted 59 lines · Junio C Hamano
Quoted 5 lines
builtin/submodule--helper.c +0 -0
index bef9ab22d4..f53808d995 100644

builtin/submodule--helper.c static int run_update_procedure(int argc, const char **argv, const char *prefix) · line 2686

Quoted 10 lines
builtin/submodule--helper.c +5 -0
@@ -2672,6 +2677,11 @@ static int run_update_procedure(int argc, const char **argv, const char *prefix)
                                            &update_data.update_strategy);
 
        free(prefixed_path);
+       /*
+        * This entry point is always called from a submodule, so this is a
+        * good place to set a hint that this repo is a submodule.
+        */
+       git_config_set("submodule.hasSuperproject", "true");
        return update_submodule2(&update_data);
 }

That matched my tentative resolution I made last night, but what do you think about this part of the test added by the patch?

t/t7406-submodule-update.sh +8 -0
index 11cccbb333..ec2397fc69 100755
@@ -1061,4 +1061,12 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s
 	)
 '
 
+test_expect_success 'submodule update adds submodule.hasSuperproject to older repos' '
+	(cd super &&
+	 test_unconfig submodule.hasSuperproject &&
+	 git submodule update &&
+	 test_cmp_config -C submodule true --type=bool submodule.hasSuperproject
+	)
+'
+
 test_done

We go to "super", make sure that superproject does not have submodule.hasSuperproject set, run "git submodule update", and see if the configuration file in "submodule" subdirectory has the variable set. It does not clear the variable from the submodule before starting, so the variable given to the submodule when it was cloned would be there, even if "git submodule update" failed to set it.

I am wondering if it should do something like the attached instead.

We

* clear the variable from "super" and "super/submodule" repositories;

* run "git submodule update";

* ensure that "git submodule update" did not touch "super/.git/config";

* ensure that "git submodule update" added the variable to "super/submodule/.git/config".

Quoted 9 lines
Quoted 5 lines

Clearing the variable from "super" is technically wrong because the repository is set up as a submodule of "recursivesuper" and if we had further tests, we should restore it in "super", but the point is that we are makng sure "git submodule update" sets the variable in the configuration file of the submodule, and not in the superproject's.

Yes, the test you've described is closer to what I thought the original test was trying to do. Seeing this test pass gave me a false sense of confidence hm..

Correction, seeing the _original_ test pass gave me false sense of confidence.

Quoted 9 lines
Quoted 7 lines

With the conflict resolution above, this "corrected" test fails and shows that superproject's configuration file is updated after "git submodule update".

This series alone, without your topic, this "corrected" test fails, and that is where my "are we sure we are mucking with the configuration file in the submodule"? comes from.

- Set the config in the submodule even though we are running from the superproject (this is possible, ensure_core_worktree() does this).

If it helps, I was able to do this up by copying ensure_core_worktree(), and this passes the amended test.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 ----

builtin/submodule--helper.c +0 -5
index 4d02dd05ca..3bb7a65762 100644
@@ -1838,11 +1838,6 @@ static int clone_submodule(struct module_clone_data *clone_data)
    git_config_set_in_file(p, "submodule.alternateErrorStrategy",
              error_strategy);
 
-	/*
-	 * Teach the submodule that it's a submodule.
-	 */
-	git_config_set_in_file(p, "submodule.hasSuperproject", "true");
-
  free(sm_alternate);
  free(error_strategy);
 

builtin/submodule--helper.c static int update_clone(int argc, const char **argv, const char *prefix) · line 2564

Quoted 10 lines
builtin/submodule--helper.c +7 -0
@@ -2560,6 +2555,20 @@ static int update_clone(int argc, const char **argv, const char *prefix)
  return update_submodules(&suc);
 }
 
+static void set_hassuperproject(const char *sm_path)
+{
+	struct repository subrepo;
+	char *cfg_file;
+
+	if (repo_submodule_init(&subrepo, the_repository, sm_path, null_oid()))
+		die(_("could not get a repository handle for submodule '%s'"), sm_path);

Isn't the repo_submodule_init() fairly expensive? I think this is doing a whole repo_init() call we would not otherwise be doing.... Is it good enough to generate the config from sm_path, by using strbuf_repo_worktree_path(), and simply be tolerant of the failure if <sm-gitdir>/config doesn't exist?

Otherwise, this is a good workaround I think. Thanks.

- Emily

#52 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

Quoted 5 lines · Emily Shaffer
Quoted 5 lines
(quoted diff) +5 -0
+static void set_hassuperproject(const char *sm_path)
+{
+	struct repository subrepo;
+	char *cfg_file;
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (repo_submodule_init(&subrepo, the_repository, sm_path, null_oid()))
+		die(_("could not get a repository handle for submodule '%s'"), sm_path);

Isn't the repo_submodule_init() fairly expensive? I think this is doing a whole repo_init() call we would not otherwise be doing.... Is it good enough to generate the config from sm_path, by using strbuf_repo_worktree_path(), and simply be tolerant of the failure if <sm-gitdir>/config doesn't exist?

Ah, I was misreading the implementation of repo_submodule_init() and I see now that won't work. I guess it is fine to just invoke repo_submodule_init() then, unless someone has another idea.

#53 lore raw

Re: [PATCH v9 2/3] introduce submodule.hasSuperproject record

Quoted 15 lines · Emily Shaffer
Quoted 5 lines · Emily Shaffer
Quoted 5 lines
(quoted diff) +5 -0
+static void set_hassuperproject(const char *sm_path)
+{
+	struct repository subrepo;
+	char *cfg_file;
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (repo_submodule_init(&subrepo, the_repository, sm_path, null_oid()))
+		die(_("could not get a repository handle for submodule '%s'"), sm_path);

Isn't the repo_submodule_init() fairly expensive? I think this is doing a whole repo_init() call we would not otherwise be doing.... Is it good enough to generate the config from sm_path, by using strbuf_repo_worktree_path(), and simply be tolerant of the failure if <sm-gitdir>/config doesn't exist?

Quoted 3 lines

Ah, I was misreading the implementation of repo_submodule_init() and I see now that won't work. I guess it is fine to just invoke repo_submodule_init() then, unless someone has another idea.

Yes, it's difficult to avoid calling repo_submodule_init() because it's hard to get the gitdir using just the path to the submodule in the working tree (sm_path).

Are we particular about avoiding calls to repo_submodule_init()? I don't recall hearing this as an objection before. If so, I'll keep this in mind as I work on more submodule things.

As an aside, ensure_core_worktree() already calls repo_submodule_init(), so this wouldn't be the first time "submodule update" calls repo_submodule_init(), and a potential optimization might be to cache the result in between invocations of repo_submodule_init().