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

Re: [PATCH v2 1/1] submodule foreach: fix recursion of options

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jun 19, 2019, 18:19 UTC
Message-ID
<nycvar.QRO.7.76.6.1906191928420.44@tvgsbejvaqbjf.bet>
In-Reply-To
<201906181515.x5IFF8bg005587@pool-147-74-zam859.wlan.kfa-juelich.de>
Hi Morian,
On Tue, 18 Jun 2019, Morian Sonnet wrote:
Show 42 quoted lines
> "Morian Sonnet via GitGitGadget" <gitgitgadget@gmail.com> wrote:
>
> > Calling
> >
> >     git submodule foreach --recursive git reset --hard
> >
> > leads to an error stating that the option --hard is unknown to
> > submodule--helper.
> >
> > Reasons:
> >
> > . Above call is internally translated into
> >
> >     git submodule--helper foreach --recursive -- git reset --hard
> >
> > . After calling
> >
> >     git reset --hard
> >
> >   inside the first first level submodule,
> >
> >     git --super-prefix <submodulepath> submodule--helper \
> >       foreach --recursive git reset --hard
> >
> >   is called. Note the missing --.
> >
> > . Due to the removal of PARSE_OPT_KEEP_UNKNOWN in commit a282f5a906 the
> >   option --hard is not passed to
> >
> >     git reset
> >
> >   anymore, but leads to git submodule--helper complaining about an
> >   unknown option.
> >
> > Fix:
> >
> > . Add -- before the command to execute, such that now correctly
> >
> >     git --super-prefix <submodulepath> submodule--helper \
> >       foreach --recursive -- git reset --hard
> >
> >   is called.

This is a good explanation, even if the format is not exactly close to the existing commit messages ;-) If you look e.g. at https://github.com/git/git/commit/e5a329a279c7, you will see what I mean: there is much more "prose" (and less bullet points) going on.

Show 15 quoted lines
> > Signed-off-by: Morian Sonnet <moriansonnet@googlemail.com>
> > ---
> >  builtin/submodule--helper.c  | 1 +
> >  t/t7407-submodule-foreach.sh | 7 +++++++
> >  2 files changed, 8 insertions(+)
> >
> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> > index 0bf4aa088e..afaf0819c9 100644
> > --- a/builtin/submodule--helper.c
> > +++ b/builtin/submodule--helper.c
> > @@ -540,6 +540,7 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
> >  		if (info->quiet)
> >  			argv_array_push(&cpr.args, "--quiet");
> >
> > +		argv_array_push(&cpr.args, "--");
This is obviously correct.
Show 16 quoted lines
> >  		argv_array_pushv(&cpr.args, info->argv);
> >
> >  		if (run_command(&cpr))
> > diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
> > index 706ae762e0..c554589e6f 100755
> > --- a/t/t7407-submodule-foreach.sh
> > +++ b/t/t7407-submodule-foreach.sh
> > @@ -421,4 +421,11 @@ test_expect_success 'option-like arguments passed to foreach commands are not lo
> >  	test_cmp expected actual
> >  '
> >
> > +test_expect_success 'option-like arguments passed to foreach recurse correctly' '
> > +  (
> > +    cd super &&
> > +    git submodule foreach --recursive git reset --hard
> > +  )

I am terribly sorry that I did not catch this in the first round. I would find it even easier to read if it used the `-C` option, like so:

	git -C super submodule foreach --recursive git reset --hard

Then you do not need the subshell, and neither the `cd`. It would become a one-liner.

However, what is less obvious to me is that this would catch the regression, as I do not see from the context whether the current submodule structure is deep enough to trigger the reported problem. If I understand the commit message correctly, `super` would have to contain a submodule that itself contains a submodule.

If there was only one level of submodules (and from the context of this diff, it is not clear whether that is the case), the test case would pass even without the code change to `submodule.c`.

Of course, I can always dig deeper and find out myself (and of course I did exactly that). But in my mind, that points to something we can improve.

What I found is that the test case indeed fails without the fixed `builtin/submodule.c`, but that the nested submodule does not even need to be checked out, contrary to what I expected after reading the commit message.

And in fact, the nested submodule is not even checked out in the test script!

To make this test case more obvious, and at the same time to test a little more thoroughly, maybe it would make sense to initialize that "inner" submodule (in this test script, `super/nested1/nested2`), then make it dirty by changing a file before the `reset --hard`, and afterwards verify that the file in question was successfully reset.

That way, the test case would start to fail if anybody changed the script in a way where the submodule nesting was all of a sudden no longer deep enough to verify what the test case wants to verify.

Otherwise we would risk that this test case would start passing for the wrong reasons at some stage.

What I have in mind would look somewhat like this:
	: make sure that nested2 is initialized &&
	git -C super/nested1 submodule update --init nested2 &&
	: make sure that nested2 is dirty &&
	echo dirty >super/nested1/nested2/file &&
	test_must_fail git -C super/nested1/nested2 update-index --refresh &&
	git -C super submodule foreach --recursive git reset --hard &&
	: now nested2 is clean &&
	git -C super/nested1/nested2 update-index --refresh

I might be overthinking this, though. Maybe it would be enough to make sure that nested1/nested2 is a nested submodule, e.g. by something like this:

	is_submodule () {
		case "$(git -C "${1%/*}" ls-files --stage "${1##*/}")" in
		160000*) return 0;;
		*) return 1;;
		esac
	} &&
	is_submodule super/nested1 &&
	is_submodule super/nested1/nested2 &&
	git -C super submodule foreach --recursive git reset --hard

which has the advantage of looking shorter, but it does not really verify that `git reset --hard` *has* been working correctly in nested2: it did not, as that nested submodule was not even initialized at that point.

So I dunno... What do you think? How can we make this test case both more understandable and more robust against future edits at the same time?

Ciao, Johannes

Show 6 quoted lines
> > +'
> > +
> >  test_done
> > --
> > gitgitgadget
>
Previous: Morian SonnetNext: Morian Sonnet
Message 7 of 19 in “submodule foreach: fix recursion of options”
  1. 0/1 submodule foreach: fix recursion of optionsMorian Sonnet via GitGitGadget, Jun 12, 2019
  2. 1/1 submodule foreach: fix recursion of optionsMorian Sonnet via GitGitGadget, Jun 12, 2019
  3. Eric SunshineJun 12, 2019
  4. 0/1 submodule foreach: fix recursion of optionsMorian Sonnet via GitGitGadget, Jun 12, 2019
  5. 1/1 submodule foreach: fix recursion of optionsMorian Sonnet via GitGitGadget, Jun 12, 2019
  6. Morian SonnetJun 18, 2019
  7. Johannes SchindelinJun 19, 2019
  8. Morian SonnetJun 22, 2019
  9. Johannes SchindelinJun 24, 2019
  10. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  11. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  12. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  13. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  14. Johannes SchindelinJun 24, 2019
  15. Junio C HamanoJun 24, 2019
  16. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  17. submodule foreach: fix recursion of optionsMorian Sonnet, Jun 24, 2019
  18. Duy NguyenJun 25, 2019
  19. Junio C HamanoJun 25, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.