# Bug: 'git ls-files' doesn't work on submodule in post-commit hook

3 messages from 2020-02-24 to 2020-02-25. Participants: Stefan, brian m. carlson.
Thread: https://gitlist.dev/t/52878

## Stefan, 2020-02-24 07:03

Subject: Bug: 'git ls-files' doesn't work on submodule in post-commit hook
Message-ID: <ede4b653-35bf-d163-c27d-d23602dbd726@green-sparklet.de>
URL: https://gitlist.dev/e/ede4b653-35bf-d163-c27d-d23602dbd726%40green-sparklet.de

```
Hey there,

I think I found a bug in git: I want to use 'git ls-files' onto a git submodule, called in a post-commit hook of main repository, which doesn’t work.
The same call in a post-checkout hook works perfectly.

Example setup to try it out, use a empty (temporary) folder:
--- snipp ---
mkdir git-submodulerepo
cd git-submodulerepo
git init
git config user.email "you@example.com"
git config user.name "Your Name"
echo "nothing" > submodule_file.txt
git add submodule_file.txt
git commit submodule_file.txt -m "Add my file"
cd ..
 
mkdir git-repo
cd git-repo
git init
git config user.email "you@example.com"
git config user.name "Your Name"
echo "nothing" > rubbish.txt
git add rubbish.txt
git commit rubbish.txt -m "Add my rubbish"
 
git submodule add ../git-submodulerepo
 
printf '#!/bin/bash\ncd git-submodulerepo\necho Git says my files are:\ngit ls-files\necho ---------------\necho Filesystem says my files are:\nls -w1\necho ---------------\n' > .git/hooks/post-commit
chmod +x .git/hooks/post-commit
cp .git/hooks/post-commit .git/hooks/post-checkout
chmod +x .git/hooks/post-checkout
--- snapp ---


So 'git ls-files' call in post-checkout hook works:
$ git checkout HEAD
A       .gitmodules
A       git-submodulerepo
Git says my files are:
submodule_file.txt
---------------
Filesystem says my files are:
submodule_file.txt
---------------
 
A 'git ls-files' in post-commit doesn’t work:
$  echo nothing >> rubbish.txt && git commit rubbish.txt -m "Add just rubbish"
Git says my files are:
---------------
Filesystem says my files are:
submodule_file.txt
---------------
[master e05efb1] just rubbish
1 file changed, 1 insertion(+)
 

This was tested using git version 2.25.0.windows.1
With git version 2.21.0 (cygwin) git ls-files outputs "fatal: .git/index: index file open failed: Not a directory"

I didn’t test other hooks.

Calling "git ls-files" in hook for your main repository (and not a submodule) works for both cases.
I checked for basic things (I'm in submodule directory in both hooks), so I guess this is a bug in git.

Any questions?
Thanks in advance!

Stefan

```

## brian m. carlson, 2020-02-25 01:13

Subject: Re: Bug: 'git ls-files' doesn't work on submodule in post-commit hook
Message-ID: <20200225011320.GL6462@camp.crustytoothpaste.net>
URL: https://gitlist.dev/e/20200225011320.GL6462%40camp.crustytoothpaste.net
In-Reply-To: <ede4b653-35bf-d163-c27d-d23602dbd726@green-sparklet.de>

```
On 2020-02-24 at 07:03:15, Stefan wrote:
> So 'git ls-files' call in post-checkout hook works:
> $ git checkout HEAD
> A       .gitmodules
> A       git-submodulerepo
> Git says my files are:
> submodule_file.txt
> ---------------
> Filesystem says my files are:
> submodule_file.txt
> ---------------
>  
> A 'git ls-files' in post-commit doesn’t work:
> $  echo nothing >> rubbish.txt && git commit rubbish.txt -m "Add just rubbish"
> Git says my files are:
> ---------------
> Filesystem says my files are:
> submodule_file.txt
> ---------------
> [master e05efb1] just rubbish
> 1 file changed, 1 insertion(+)

Can you check to see if the GIT_DIR environment variable is set in your
hook?  In some cases, Git sets various GIT_* environment variables when
calling a hook or command, and if you want another repository, you'd
need to unset those values.
-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204

```

## Stefan, 2020-02-25 07:10

Subject: Re: Bug: 'git ls-files' doesn't work on submodule in post-commit hook
Message-ID: <539bac71-a4af-f96c-ae08-e8fa659c5dd1@green-sparklet.de>
URL: https://gitlist.dev/e/539bac71-a4af-f96c-ae08-e8fa659c5dd1%40green-sparklet.de
In-Reply-To: <20200225011320.GL6462@camp.crustytoothpaste.net>

```
Hi brian,

I checked it by replacing the printf line in my example with this one:
printf '#!/bin/bash\necho GIT_DIR is: $GIT_DIR\ncd git-submodulerepo\necho Git says my files are:\ngit ls-files\necho ---------------\necho Filesystem says my files are:\nls -w1\necho ---------------\n' > .git/hooks/post-commit

In both hooks, GIT_DIR is empty, so this shouldn't be the problem.
Thanks anyways.

On 2020-02-25 02:13, brian m. carlson wrote:
> Can you check to see if the GIT_DIR environment variable is set in your
> hook?  In some cases, Git sets various GIT_* environment variables when
> calling a hook or command, and if you want another repository, you'd
> need to unset those values.
> 

```
