git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Nested submodule checkout
@ 2020-02-14 22:42 Damien Robert
  2020-02-17  4:51 ` Philippe Blain
  0 siblings, 1 reply; 13+ messages in thread
From: Damien Robert @ 2020-02-14 22:42 UTC (permalink / raw)
  To: git

Dear git developers,

I stumbled into this situation from which it was a bit painful to recover:

-> test script:

    mkdir ploum
    cd ploum
    git init
    echo 'foo' > foo
    git add foo
    git commit -m foo
    git branch nosubmodules

    mkdir plam
    cd plam
    git init
    echo 'bar' > bar
    git add bar
    git commit -m bar

    mkdir plim
    cd plim
    git init
    echo 'baz' > baz
    git add baz
    git commit -m baz

    cd ..
    git submodule add ./plim
    git commit -am 'Add submodule plim'

    cd ..
    git submodule add ./plam
    git commit -am 'Add submodule plam'

    git checkout nosubmodules
    git checkout --recurse-submodules master

-> The result is as follow:

Initialized empty Git repository in /data/dams/var/tmp/ploum/.git/
[master (root-commit) ec7c09a] foo
 1 file changed, 1 insertion(+)
 create mode 100644 foo
Branch 'nosubmodules' set up to track local branch 'master'.
Initialized empty Git repository in /data/dams/var/tmp/ploum/plam/.git/
[master (root-commit) 35e6696] bar
 1 file changed, 1 insertion(+)
 create mode 100644 bar
Initialized empty Git repository in /data/dams/var/tmp/ploum/plam/plim/.git/
[master (root-commit) b4712c1] baz
 1 file changed, 1 insertion(+)
 create mode 100644 baz
Adding existing repo at 'plim' to the index
[master 989c11d] Add submodule plim
 2 files changed, 4 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 plim
Adding existing repo at 'plam' to the index
[master 5b34041] Add submodule plam
 2 files changed, 4 insertions(+)
 create mode 100644 .gitmodules
 create mode 160000 plam
Migrating git directory of 'plam' from
'/data/dams/var/tmp/ploum/plam/.git' to
'/data/dams/var/tmp/ploum/.git/modules/plam'
Migrating git directory of 'plam/plim' from
'/data/dams/var/tmp/ploum/plam/plim/.git' to
'/data/dams/var/tmp/ploum/.git/modules/plam/modules/plim'
Switched to branch 'nosubmodules'
Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
fatal: exec '--super-prefix=plam/plim/': cd to 'plim' failed: No such file or directory
error: Submodule 'plim' could not be updated.
error: Submodule 'plam/plim' cannot checkout new HEAD.
error: Submodule 'plam' could not be updated.
M	plam
Switched to branch 'master'

As you can see, the nested plim submodules could not be recreated since the
folder does not exists yet in the 'nosubmodules' branch. This makes the
'plam' submodule update fails, and in the following state

Unstaged changes after reset:
D	.gitmodules
D	bar
D	plim

-> To recover

In the folder plam, do a `git reset` followed by a `git reset --hard`
(`git reset --hard` directly does not work:
fatal: exec '--super-prefix=plim/': cd to 'plim' failed: No such file or directory)

Indeed the first reset, which puts .gitmodules back in the index, is what
allows to do the `git submodule update` implied by `git reset --hard`.
Since (from what I understand) the path is only read from .gitmodules and
not from the .git/config (where there are only the submodules name which
are distinct from the path), this explain the failures observed.

Note that I wasn't able to reproduce in this small examples, but when
trying to repair I also add some strange errors of the form
'.git is not a git directory' (where .git was a pseudo symlink
gitdir: ../.git/modules/plam).

-> Question

My usage is probably non standard (I have quite a lot of nested
submodules), so I had a hard time to recover from this checkout. Is there a
better way? Would it be possible to make nested submodules checkout of this
form work out of the box?

Thanks!
Damien Robert

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Nested submodule checkout
  2020-02-14 22:42 Nested submodule checkout Damien Robert
@ 2020-02-17  4:51 ` Philippe Blain
  2020-02-18 17:08   ` Damien Robert
  2020-02-26 17:23   ` Nested submodule status bug Damien Robert
  0 siblings, 2 replies; 13+ messages in thread
From: Philippe Blain @ 2020-02-17  4:51 UTC (permalink / raw)
  To: Damien Robert; +Cc: git

Hi Damien,

I reported the same bug to the list back in September [1], and I’m glad to say I just finished (today!) a patch series [2] that fixes this bug.

> Le 14 févr. 2020 à 17:42, Damien Robert <damien.olivier.robert@gmail.com> a écrit :
> 
> Dear git developers,
> 
> I stumbled into this situation from which it was a bit painful to recover:
> 
> -> test script:
> 
>    mkdir ploum
>    cd ploum
>    git init
>    echo 'foo' > foo
>    git add foo
>    git commit -m foo
>    git branch nosubmodules
> 
>    mkdir plam
>    cd plam
>    git init
>    echo 'bar' > bar
>    git add bar
>    git commit -m bar
> 
>    mkdir plim
>    cd plim
>    git init
>    echo 'baz' > baz
>    git add baz
>    git commit -m baz
> 
>    cd ..
>    git submodule add ./plim
>    git commit -am 'Add submodule plim'
> 
>    cd ..
>    git submodule add ./plam
>    git commit -am 'Add submodule plam'
> 
>    git checkout nosubmodules
>    git checkout --recurse-submodules master
> 
> -> The result is as follow:
> 
> Initialized empty Git repository in /data/dams/var/tmp/ploum/.git/
> [master (root-commit) ec7c09a] foo
> 1 file changed, 1 insertion(+)
> create mode 100644 foo
> Branch 'nosubmodules' set up to track local branch 'master'.
> Initialized empty Git repository in /data/dams/var/tmp/ploum/plam/.git/
> [master (root-commit) 35e6696] bar
> 1 file changed, 1 insertion(+)
> create mode 100644 bar
> Initialized empty Git repository in /data/dams/var/tmp/ploum/plam/plim/.git/
> [master (root-commit) b4712c1] baz
> 1 file changed, 1 insertion(+)
> create mode 100644 baz
> Adding existing repo at 'plim' to the index
> [master 989c11d] Add submodule plim
> 2 files changed, 4 insertions(+)
> create mode 100644 .gitmodules
> create mode 160000 plim
> Adding existing repo at 'plam' to the index
> [master 5b34041] Add submodule plam
> 2 files changed, 4 insertions(+)
> create mode 100644 .gitmodules
> create mode 160000 plam

Here you just did ` git commit -am 'Add submodule plam’` so the next command according to your reproducer above would be `git checkout nosubmodules`

> Migrating git directory of 'plam' from
> '/data/dams/var/tmp/ploum/plam/.git' to
> '/data/dams/var/tmp/ploum/.git/modules/plam'
> Migrating git directory of 'plam/plim' from
> '/data/dams/var/tmp/ploum/plam/plim/.git' to
> '/data/dams/var/tmp/ploum/.git/modules/plam/modules/plim'
> Switched to branch 'nosubmodules'
> Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
>  (use "git pull" to update your local branch)

Here, git is migrating the git directories of both submodules to the git directory of the superproject (ploum). This tells me you probably have the `submodule.recurse` config set somewhere, as this is the behaviour I get I if I do `git checkout --recurse-submodules nosubmodules`.
If I just do `git checkout nosubmodules`, I get 
    
    $ git checkout nosubmodules 
    warning: unable to rmdir 'plam': Directory not empty
    Switched to branch 'nosubmodules'
    Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
      (use "git pull" to update your local branch)

and then doing `git checkout --recurse-submodules master` actually works. 

> fatal: exec '--super-prefix=plam/plim/': cd to 'plim' failed: No such file or directory
> error: Submodule 'plim' could not be updated.
> error: Submodule 'plam/plim' cannot checkout new HEAD.
> error: Submodule 'plam' could not be updated.
> M	plam
> Switched to branch 'master'
> 
> As you can see, the nested plim submodules could not be recreated since the
> folder does not exists yet in the 'nosubmodules' branch.

That’s the cause of the bug: in fact git tries to change directory into ‘plim’ before the ‘plim’ directory is created in the filesystem.

> This makes the
> 'plam' submodule update fails, and in the following state
> 
> Unstaged changes after reset:
> D	.gitmodules
> D	bar
> D	plim

At this point, if you go into ‘plam’ and do `git ls-files -s` (to list the content of the index), you will see that the index is empty (the checkout died before the index could be populated.) 

> -> To recover
> 
> In the folder plam, do a `git reset` followed by a `git reset --hard`
> (`git reset --hard` directly does not work:
> fatal: exec '--super-prefix=plim/': cd to 'plim' failed: No such file or directory)

That’s another hint that you have `submodule.recurse` set. I don’t get this error doing `git reset --hard`, but I get it doing `git reset --hard --recurse-submodules` (or `git reset --hard --r`, which works and is quicker to type!). `git reset` populates the index, so now `git ls-files -s` would now show the correct content of ‘plam’.

> Indeed the first reset, which puts .gitmodules back in the index, is what
> allows to do the `git submodule update` implied by `git reset --hard`.

In fact, `git reset --hard` does not spawn `git submodule update`, it calls functions in unpack-trees.c (that actually spawn `git read-tree —recurse-submodules`) to update the submodules recursively. 

> Note that I wasn't able to reproduce in this small examples, but when
> trying to repair I also add some strange errors of the form
> '.git is not a git directory' (where .git was a pseudo symlink
> gitdir: ../.git/modules/plam).
> 
> -> Question
> 
> My usage is probably non standard (I have quite a lot of nested
> submodules), so I had a hard time to recover from this checkout. Is there a
> better way? Would it be possible to make nested submodules checkout of this
> form work out of the box?

It is supposed to work out of the box when using `--recurse-submodules` or the `submodule.recurse` config. Although, it’s always possible to run into some bugs. 
One thing you can do to get out of tricky situations is to temporarily deactivate the config (`git -c submodule.recurse=0 <command>`). For example, after the failed `git checkout --recurse-submodules master` above, issuing

    git -c submodule.recurse=0 submodule update --recursive --force

would have correctly checked out the submodules. I have a git alias ‘no-rs’ (for no recurse-submodules) that I use in these situations:

    git config --global alias.no-rs ‘-c submodule.recurse=0’

Then the `submodule update` call above could be shortened to 

    git no-rs submodule update --recursive --force

Note that using the `submodule.recurse` config also applies to internal calls to git commands (issued by other git commands), so using adding `--no-recurse-submodules` to the command line might not be enough to completely turn off the effect of that config, hence this handy alias.

> 
> Thanks!
> Damien Robert
> 

Cheers,
Philippe.

[1] https://lore.kernel.org/git/7437BB59-4605-48EC-B05E-E2BDB2D9DABC@gmail.com/
[2] https://github.com/gitgitgadget/git/pull/555

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Nested submodule checkout
  2020-02-17  4:51 ` Philippe Blain
@ 2020-02-18 17:08   ` Damien Robert
  2020-02-19  4:19     ` Philippe Blain
  2020-02-26 17:23   ` Nested submodule status bug Damien Robert
  1 sibling, 1 reply; 13+ messages in thread
From: Damien Robert @ 2020-02-18 17:08 UTC (permalink / raw)
  To: Philippe Blain; +Cc: git

Hi Philippe,

From Philippe Blain, Sun 16 Feb 2020 at 23:51:43 (-0500) :
> I reported the same bug to the list back in September [1]

Meta question: is there an easy way I could have found your bug report?

> and I’m glad to say I just finished (today!) a patch series [2] that fixes this bug.

Great! Thanks for the patches!

> Here you just did ` git commit -am 'Add submodule plam’` so the next command according to your reproducer above would be `git checkout nosubmodules`
> 
> > Migrating git directory of 'plam' from
> > '/data/dams/var/tmp/ploum/plam/.git' to
> > '/data/dams/var/tmp/ploum/.git/modules/plam'
> > Migrating git directory of 'plam/plim' from
> > '/data/dams/var/tmp/ploum/plam/plim/.git' to
> > '/data/dams/var/tmp/ploum/.git/modules/plam/modules/plim'
> > Switched to branch 'nosubmodules'
> > Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
> >  (use "git pull" to update your local branch)
> 
> Here, git is migrating the git directories of both submodules to the git directory of the superproject (ploum). This tells me you probably have the `submodule.recurse` config set somewhere, as this is the behaviour I get I if I do `git checkout --recurse-submodules nosubmodules`.

Yes, you are of course right. I should have specified it in my bug report,
I tried to specify the setting explicitely in the last checkout but as you
noticed I forgot to specify it in all other commands, sorry about the
noise.

> That’s another hint that you have `submodule.recurse` set. I don’t get this error doing `git reset --hard`, but I get it doing `git reset --hard --recurse-submodules` (or `git reset --hard --r`, which works and is quicker to type!). `git reset` populates the index, so now `git ls-files -s` would now show the correct content of ‘plam’.

Oh, I did not know git expand unambiguous long options!

By the way the fact that `git reset` also support `--recurse-submodules` is not
specified in the man page. (It is in the help text thought).

And it would be nice if the documentation of submodule.recurse in
git-config specify the list of all affected commands, rather than just "all
commands that have a --recurse-submodules options".

(I could send a patch for this if there is interest)

> > Note that I wasn't able to reproduce in this small examples, but when
> > trying to repair I also add some strange errors of the form
> > '.git is not a git directory' (where .git was a pseudo symlink
> > gitdir: ../.git/modules/plam).

-> This is explained by your Patch 5/6

> would have correctly checked out the submodules. I have a git alias ‘no-rs’ (for no recurse-submodules) that I use in these situations:
>     git config --global alias.no-rs ‘-c submodule.recurse=0’

Can you use alias to define option settings (rather than commands followed
by options) without using the 'f() {}' trick?

Using your alias, I get
fatal: empty alias for no-rs

> Then the `submodule update` call above could be shortened to 
>     git no-rs submodule update --recursive --force

> Note that using the `submodule.recurse` config also applies to internal calls to git commands (issued by other git commands), so using adding `--no-recurse-submodules` to the command line might not be enough to completely turn off the effect of that config, hence this handy alias.

Ohh, good to know!

-- 
Damien Robert
http://www.normalesup.org/~robert/pro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Nested submodule checkout
  2020-02-18 17:08   ` Damien Robert
@ 2020-02-19  4:19     ` Philippe Blain
  0 siblings, 0 replies; 13+ messages in thread
From: Philippe Blain @ 2020-02-19  4:19 UTC (permalink / raw)
  To: Damien Robert; +Cc: git

Hi Damien,

> Le 18 févr. 2020 à 12:08, Damien Robert <damien.olivier.robert@gmail.com> a écrit :
> 
> Hi Philippe,
> 
> From Philippe Blain, Sun 16 Feb 2020 at 23:51:43 (-0500) :
>> I reported the same bug to the list back in September [1]
> 
> Meta question: is there an easy way I could have found your bug report?

That’s a good question. The search capabilities of public-inbox (the software running at lore.kernel.org) are explained at 
https://lore.kernel.org/git/_/text/help/

In this case, my bug report comes out in the following searches:
b:(nested submodule checkout)
s:(bug* submodule* checkout)

the first one can be refined with
b:(nested submodule checkout) NOT s:(cooking) NOT s:(ANNOUNCE) NOT s:(PATCH*)

> 
>> That’s another hint that you have `submodule.recurse` set. I don’t get this error doing `git reset --hard`, but I get it doing `git reset --hard --recurse-submodules` (or `git reset --hard --r`, which works and is quicker to type!). `git reset` populates the index, so now `git ls-files -s` would now show the correct content of ‘plam’.
> 
> Oh, I did not know git expand unambiguous long options!

Yes, it’s not widely known but can be useful! It’s documented in gitcli(7) (`man gitcli`) or online at
https://git-scm.com/docs/gitcli#_abbreviating_long_options

> By the way the fact that `git reset` also support `--recurse-submodules` is not
> specified in the man page. (It is in the help text thought).

Yes, this is on my radar, I’d like to get some time to clean this up sometimes soon. It’s also missing for `git restore`.

> And it would be nice if the documentation of submodule.recurse in
> git-config specify the list of all affected commands, rather than just "all
> commands that have a --recurse-submodules options".
> 
> (I could send a patch for this if there is interest)

That’s a very good idea in my opinion, especially since the search functionality on git-scm.com is presently limited to 10 hits from the manpages so it fails to find all occurrences of `--recurse-submodules` [1].

[1] https://github.com/git/git-scm.com/issues/1374

>> would have correctly checked out the submodules. I have a git alias ‘no-rs’ (for no recurse-submodules) that I use in these situations:
>>    git config --global alias.no-rs ‘-c submodule.recurse=0’
> 
> Can you use alias to define option settings (rather than commands followed
> by options) without using the 'f() {}' trick?
> 
> Using your alias, I get
> fatal: empty alias for no-rs

The alias specifies an option directly to the git "main" executable instead of a specific Git command (in this case setting a particular configuration variable for the duration of the command that will follow). This alias works for me, at least on Git 2.25. The git-config man page mentions aliases can start with an option to git [2]. If it does not work in an older version of Git then you can use 

    git config --global alias.no-rs ‘!git -c submodule.recurse=0'

which should do the trick.

[2] https://git-scm.com/docs/git-config#Documentation/git-config.txt-alias

>> Note that using the `submodule.recurse` config also applies to internal calls to git commands (issued by other git commands), so using adding `--no-recurse-submodules` to the command line might not be enough to completely turn off the effect of that config, hence this handy alias.
> 
> Ohh, good to know!

If you do go ahead with a patch listing the commands affected by submodule.recurse in git-config, then I think this should also be mentioned. 

Cheers,
Philippe.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Nested submodule status bug
  2020-02-17  4:51 ` Philippe Blain
  2020-02-18 17:08   ` Damien Robert
@ 2020-02-26 17:23   ` Damien Robert
  2020-02-27 10:05     ` Damien Robert
  1 sibling, 1 reply; 13+ messages in thread
From: Damien Robert @ 2020-02-26 17:23 UTC (permalink / raw)
  To: Philippe Blain; +Cc: git

Dear git developers,

still about nested submodules, I stumbled upon the following very strange
behaviour.

1) Make a baz submodule inside a bar submodule inside foo
  mkdir foo; cd foo; git init
  mkdir bar; cd bar; git init
  mkdir baz; cd baz; git init
  touch 1
  git add 1
  git commit -m 'first baz commit'
  cd ..
  git submodule add ./baz
  git commit -m "Add baz submodule"
  cd ..
  git submodule add ./bar
  git commit -m "Add bar submodule\n"
  git submodule absorbgitdirs
  cd bar
  touch 2

2) Now add the following alias:
  subs="! git -C baz status"

3) And run it
  git subs

To get:
~~~
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	../2

nothing added to commit but untracked files present (use "git add" to track)
~~~


While when not launched as an alias, `git -C baz status` give the correct
result:
On branch master
nothing to commit, working tree clean


Note: I get the same bug when I use
  subs="! sh -c 'cd baz; git status'"
The bug is only there if absorbgitdirs is used. Also I have to be in an
intermediate submodule.

Best regards,
Damien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Nested submodule status bug
  2020-02-26 17:23   ` Nested submodule status bug Damien Robert
@ 2020-02-27 10:05     ` Damien Robert
  2020-02-27 10:43       ` Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug] Damien Robert
  0 siblings, 1 reply; 13+ messages in thread
From: Damien Robert @ 2020-02-27 10:05 UTC (permalink / raw)
  To: Philippe Blain; +Cc: git

From Damien Robert, Wed 26 Feb 2020 at 18:23:41 (+0100) :
> 2) Now add the following alias:
>   subs="! git -C baz status"
> 
> 3) And run it
>   git subs
> 
> To get:
> ~~~
> On branch master
> Untracked files:
>   (use "git add <file>..." to include in what will be committed)
> 	../2
> 
> nothing added to commit but untracked files present (use "git add" to track)
> ~~~

Ok, so using GIT_TRACE and GIT_TRACE_STATUS and doing some debugging I
understand the cause of the bug. When using the alias,
the GIT_DIR environment is set to the bar module:
    GIT_DIR=/tmp/test/foo/.git/modules/bar
and is exported.

That's explain why in `baz` the status report the status of the `bar`
directory.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-27 10:05     ` Damien Robert
@ 2020-02-27 10:43       ` Damien Robert
  2020-02-27 15:50         ` GIT_DIR in aliases [Re: " Damien Robert
  0 siblings, 1 reply; 13+ messages in thread
From: Damien Robert @ 2020-02-27 10:43 UTC (permalink / raw)
  To: Philippe Blain, Nguyễn Thái Ngọc Duy; +Cc: git

> Ok, so using GIT_TRACE and GIT_TRACE_STATUS and doing some debugging I
> understand the cause of the bug. When using the alias,
> the GIT_DIR environment is set to the bar module:
>     GIT_DIR=/tmp/test/foo/.git/modules/bar
> and is exported.
> 
> That's explain why in `baz` the status report the status of the `bar`
> directory.

And looking at setup.c:760 it appears that `set_git_dir` (which set up among
other things the GIT_DIR env) is called because we are in a worktree.

More precisely: we have discovered a git dir (a git file), so we call
setup_discovered_git_dir, which itself calls setup_explicit_git_dir because
we are in a work tree environment. And set_up_explicit_git_dir calls
set_git_dir.


So I don't know what's the proper way to fix this. Keep track when we add
ourselves the GIT_DIR rather than when it is already provided and remove it
before executing a shell alias? Don't run the full configuration setup when
we are executing a shell alias anyway?

I am adding Nguyễn Thái Ngọc Duy to the discussion (I hope this is the
proper etiquette).

Best regards,
Damien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-27 10:43       ` Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug] Damien Robert
@ 2020-02-27 15:50         ` Damien Robert
  2020-02-28 19:02           ` Jeff King
  2020-02-29  1:42           ` Philippe Blain
  0 siblings, 2 replies; 13+ messages in thread
From: Damien Robert @ 2020-02-27 15:50 UTC (permalink / raw)
  To: Philippe Blain, Nguyễn Thái Ngọc Duy; +Cc: git

From Damien Robert, Thu 27 Feb 2020 at 11:43:30 (+0100) :
> And looking at setup.c:760 it appears that `set_git_dir` (which set up among
> other things the GIT_DIR env) is called because we are in a worktree.
> 
> More precisely: we have discovered a git dir (a git file), so we call
> setup_discovered_git_dir, which itself calls setup_explicit_git_dir because
> we are in a work tree environment. And set_up_explicit_git_dir calls
> set_git_dir.
> 
> So I don't know what's the proper way to fix this. Keep track when we add
> ourselves the GIT_DIR rather than when it is already provided and remove it
> before executing a shell alias? Don't run the full configuration setup when
> we are executing a shell alias anyway?

Sorry about the spam. Thinking about it some more, this has nothing to do
with submodules or worktrees, but about how we expect shell aliases to
work.

In Documentation/config/alias, it is documented that the alias will run a
the top level and GIT_PREFIX will be set.

In git.c#handle_alias, it is explicitly stated that shell alias call
setup_git_directory_gently because:
/* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */

So one might argue that the behaviour I observed is not a bug, but it is
still surprising for me (as a user), and maybe this could be stated more
clearly in the docs?

Furthermore there is a question of consistency. GIT_DIR will not always be set
before running a shell alias. Looking at `setup_discovered_git_dir`, it will
be set if we are in a bare dir, or core.worktree / WORK_TREE is set, or if
we have a gitfile.

The annoying side effect is that I cannot use as an alias a command that
iterate over submodules and run git commands inside them, because in this
alias GIT_DIR will be set sometimes, and sometimes not (a quick fix would be to
unset GIT_DIR in my alias).

So what would be the best way to handle this?
Thanks,
Damien

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-27 15:50         ` GIT_DIR in aliases [Re: " Damien Robert
@ 2020-02-28 19:02           ` Jeff King
  2020-02-28 20:22             ` Junio C Hamano
  2020-03-03 22:44             ` Damien Robert
  2020-02-29  1:42           ` Philippe Blain
  1 sibling, 2 replies; 13+ messages in thread
From: Jeff King @ 2020-02-28 19:02 UTC (permalink / raw)
  To: Damien Robert; +Cc: Philippe Blain, Nguyễn Thái Ngọc Duy, git

On Thu, Feb 27, 2020 at 04:50:57PM +0100, Damien Robert wrote:

> So one might argue that the behaviour I observed is not a bug, but it is
> still surprising for me (as a user), and maybe this could be stated more
> clearly in the docs?
> 
> Furthermore there is a question of consistency. GIT_DIR will not always be set
> before running a shell alias. Looking at `setup_discovered_git_dir`, it will
> be set if we are in a bare dir, or core.worktree / WORK_TREE is set, or if
> we have a gitfile.

We were discussing the same issue recently with regards to hooks. See:

  https://lore.kernel.org/git/20200130102933.GE840531@coredump.intra.peff.net/

and the responses. I think we could do better, but at the cost of
breaking a relatively obscure git-clone feature.

> The annoying side effect is that I cannot use as an alias a command that
> iterate over submodules and run git commands inside them, because in this
> alias GIT_DIR will be set sometimes, and sometimes not (a quick fix would be to
> unset GIT_DIR in my alias).

Yes, the recommended thing is to make sure GIT_DIR is unset if you're
going to chdir around and expect auto-discovery of the repository to
work.

Note there are other variables you might want to unset, too, if you're
switching repositories. Doing:

  unset $(git rev-parse --local-env-vars)

would cover the full list.

-Peff

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-28 19:02           ` Jeff King
@ 2020-02-28 20:22             ` Junio C Hamano
  2020-03-03 22:44             ` Damien Robert
  1 sibling, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2020-02-28 20:22 UTC (permalink / raw)
  To: Jeff King
  Cc: Damien Robert, Philippe Blain,
	Nguyễn Thái Ngọc Duy, git

Jeff King <peff@peff.net> writes:

> Yes, the recommended thing is to make sure GIT_DIR is unset if you're
> going to chdir around and expect auto-discovery of the repository to
> work.
>
> Note there are other variables you might want to unset, too, if you're
> switching repositories. Doing:
>
>   unset $(git rev-parse --local-env-vars)
>
> would cover the full list.

Thanks for mentioning --local-env-vars; I was going to ask you to
mention GIT_WORK_TREE anytime you mention unsetting GIT_DIR, but you
did it much better ;-).


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-27 15:50         ` GIT_DIR in aliases [Re: " Damien Robert
  2020-02-28 19:02           ` Jeff King
@ 2020-02-29  1:42           ` Philippe Blain
  2020-03-03 21:56             ` Damien Robert
  1 sibling, 1 reply; 13+ messages in thread
From: Philippe Blain @ 2020-02-29  1:42 UTC (permalink / raw)
  To: Damien Robert
  Cc: Nguyễn Thái Ngọc Duy, git, Junio C Hamano,
	Jeff King

Hi Damien,

> Le 27 févr. 2020 à 10:50, Damien Robert <damien.olivier.robert@gmail.com> a écrit :
> 
> So one might argue that the behaviour I observed is not a bug, but it is
> still surprising for me (as a user), and maybe this could be stated more
> clearly in the docs?

It sure is surprising, and not super intuitive.

> 
> Furthermore there is a question of consistency. GIT_DIR will not always be set
> before running a shell alias. Looking at `setup_discovered_git_dir`, it will
> be set if we are in a bare dir, or core.worktree / WORK_TREE is set, or if
> we have a gitfile.
> 
> The annoying side effect is that I cannot use as an alias a command that
> iterate over submodules and run git commands inside them, because in this
> alias GIT_DIR will be set sometimes, and sometimes not (a quick fix would be to
> unset GIT_DIR in my alias).

If that’s what your are trying to do, ‘git submodule foreach’ [1] seems like a better way to go. 
For example I have an alias ‘st-sub’ that calls git status in the superproject and then recursively in the submodules:

    $ cat ~/.gitconfig | grep st-sub
          st-sub = !git status "$@" && git sub foreach --recursive git status "$@"

[1] https://git-scm.com/docs/git-submodule#Documentation/git-submodule.txt-foreach--recursiveltcommandgt

> 
> So what would be the best way to handle this?
> Thanks,
> Damien

Cheers,
Philippe.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-29  1:42           ` Philippe Blain
@ 2020-03-03 21:56             ` Damien Robert
  0 siblings, 0 replies; 13+ messages in thread
From: Damien Robert @ 2020-03-03 21:56 UTC (permalink / raw)
  To: Philippe Blain
  Cc: Nguyễn Thái Ngọc Duy, git, Junio C Hamano,
	Jeff King

> If that’s what your are trying to do, ‘git submodule foreach’ [1] seems like a better way to go. 
> For example I have an alias ‘st-sub’ that calls git status in the superproject and then recursively in the submodules:
> 
>     $ cat ~/.gitconfig | grep st-sub
>           st-sub = !git status "$@" && git sub foreach --recursive git status "$@"

I have almost the same aliases :)
	s = status --short --branch
	subfor="submodule foreach --recursive"
	subs="!f() { git subfor git s \"$@\";}; f"
	recs="!f() { git s \"$@\"; git subs \"$@\";}; f"

But in practice I often want to run commands only in *changed* submodules.

For instance with the above 'git subs' yields a lot of non informative
messages like:
  Entering 'non/changed/submodule'
  ## master...origin/master
(and git foreach --quiet) removes the first line but not the second.

This is also useful for an alias like 'recursive_commit' where I only want
to commit changed submodules, to not get a lot of messages like
     nothing to commit, working tree clean.

So I wrote a script that parses the output of 'git status --porcelain=v2'
and run a command only in changed submodules (where I can specify if
changed means 'C', 'M' or 'U'). Except that I did not think about unseting
GIT_DIR and stumbled into this bug (I am sorry I went a bit on a wild chase
in this mailing list but I had already spent quite some time in trying to
find a minimal exemple, I first thought that my program had a parsing bug).

Now that I think about it, I probably also could do that with 'git
submodule foreach', by testing 'git diff --no-ext-diff [--cached] --quiet'
first before running the subcommand. This would give me the 'M' submodules,
but it is not clear to me how I could test for 'C' changed submodules with
this method.

-- 
Damien Robert
http://www.normalesup.org/~robert/pro

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]
  2020-02-28 19:02           ` Jeff King
  2020-02-28 20:22             ` Junio C Hamano
@ 2020-03-03 22:44             ` Damien Robert
  1 sibling, 0 replies; 13+ messages in thread
From: Damien Robert @ 2020-03-03 22:44 UTC (permalink / raw)
  To: Jeff King; +Cc: Philippe Blain, Nguyễn Thái Ngọc Duy, git

From Jeff King, Fri 28 Feb 2020 at 14:02:18 (-0500) :
> > Furthermore there is a question of consistency. GIT_DIR will not always be set
> > before running a shell alias. Looking at `setup_discovered_git_dir`, it will
> > be set if we are in a bare dir, or core.worktree / WORK_TREE is set, or if
> > we have a gitfile.

> We were discussing the same issue recently with regards to hooks. See:
>   https://lore.kernel.org/git/20200130102933.GE840531@coredump.intra.peff.net/
> and the responses. I think we could do better, but at the cost of
> breaking a relatively obscure git-clone feature.

Yes I found this discussion afterwards. This is related but a bit
different, the problem you discuss is that GIT_WORK_TREE is not always set
even when GIT_DIR is. The problem I have is that GIT_DIR is not always set; it
depends on the phase of the moon (are we in a worktree? ...), which makes
things that forget to unset it half broken and hard to debug.

I would argue that for scripts and hooks we should always set both. This is
easy to do (at least for scripts, I haven't looked at hooks) without
touching at setup_discovered_git_dir, so not affecting git clone.

In fact I would even argue that in setup_discovered_git_dir we should
always set both, and fix the breakages that appear, like the one you
mention for git clone. For instance I wonder if the recent report about
'git gui' setting GIT_DIR is not due to this.

> Note there are other variables you might want to unset, too, if you're
> switching repositories. Doing:
>   unset $(git rev-parse --local-env-vars)
> would cover the full list.

Thanks for the hint; I had forgotten about local-env-vars.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-03-03 22:44 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14 22:42 Nested submodule checkout Damien Robert
2020-02-17  4:51 ` Philippe Blain
2020-02-18 17:08   ` Damien Robert
2020-02-19  4:19     ` Philippe Blain
2020-02-26 17:23   ` Nested submodule status bug Damien Robert
2020-02-27 10:05     ` Damien Robert
2020-02-27 10:43       ` Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug] Damien Robert
2020-02-27 15:50         ` GIT_DIR in aliases [Re: " Damien Robert
2020-02-28 19:02           ` Jeff King
2020-02-28 20:22             ` Junio C Hamano
2020-03-03 22:44             ` Damien Robert
2020-02-29  1:42           ` Philippe Blain
2020-03-03 21:56             ` Damien Robert

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).