{"thread":{"id":"52878","subject":"Bug: 'git ls-files' doesn't work on submodule in post-commit hook","startedAt":"2020-02-24T07:12:10Z","lastAt":"2020-02-25T07:10:55Z","messageCount":3,"participants":["Stefan","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"392394","messageId":"ede4b653-35bf-d163-c27d-d23602dbd726@green-sparklet.de","threadId":"52878","inReplyTo":null,"subject":"Bug: 'git ls-files' doesn't work on submodule in post-commit hook","fromName":"Stefan","fromEmail":"git.kernel.org_20@green-sparklet.de","sentAt":"2020-02-24T07:03:15Z","receivedAt":"2020-02-24T07:12:10Z","isPatch":false,"sender":{"key":"git.kernel.org_20@green-sparklet.de","avatar":null},"body":"Hey there,\n\nI 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.\nThe same call in a post-checkout hook works perfectly.\n\nExample setup to try it out, use a empty (temporary) folder:\n--- snipp ---\nmkdir git-submodulerepo\ncd git-submodulerepo\ngit init\ngit config user.email \"you@example.com\"\ngit config user.name \"Your Name\"\necho \"nothing\" > submodule_file.txt\ngit add submodule_file.txt\ngit commit submodule_file.txt -m \"Add my file\"\ncd ..\n \nmkdir git-repo\ncd git-repo\ngit init\ngit config user.email \"you@example.com\"\ngit config user.name \"Your Name\"\necho \"nothing\" > rubbish.txt\ngit add rubbish.txt\ngit commit rubbish.txt -m \"Add my rubbish\"\n \ngit submodule add ../git-submodulerepo\n \nprintf '#!/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\nchmod +x .git/hooks/post-commit\ncp .git/hooks/post-commit .git/hooks/post-checkout\nchmod +x .git/hooks/post-checkout\n--- snapp ---\n\n\nSo 'git ls-files' call in post-checkout hook works:\n$ git checkout HEAD\nA       .gitmodules\nA       git-submodulerepo\nGit says my files are:\nsubmodule_file.txt\n---------------\nFilesystem says my files are:\nsubmodule_file.txt\n---------------\n \nA 'git ls-files' in post-commit doesn’t work:\n$  echo nothing >> rubbish.txt && git commit rubbish.txt -m \"Add just rubbish\"\nGit says my files are:\n---------------\nFilesystem says my files are:\nsubmodule_file.txt\n---------------\n[master e05efb1] just rubbish\n1 file changed, 1 insertion(+)\n \n\nThis was tested using git version 2.25.0.windows.1\nWith git version 2.21.0 (cygwin) git ls-files outputs \"fatal: .git/index: index file open failed: Not a directory\"\n\nI didn’t test other hooks.\n\nCalling \"git ls-files\" in hook for your main repository (and not a submodule) works for both cases.\nI checked for basic things (I'm in submodule directory in both hooks), so I guess this is a bug in git.\n\nAny questions?\nThanks in advance!\n\nStefan\n"},{"id":"392461","messageId":"20200225011320.GL6462@camp.crustytoothpaste.net","threadId":"52878","inReplyTo":"ede4b653-35bf-d163-c27d-d23602dbd726@green-sparklet.de","subject":"Re: Bug: 'git ls-files' doesn't work on submodule in post-commit hook","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-02-25T01:13:20Z","receivedAt":"2020-02-25T01:13:29Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2020-02-24 at 07:03:15, Stefan wrote:\n> So 'git ls-files' call in post-checkout hook works:\n> $ git checkout HEAD\n> A       .gitmodules\n> A       git-submodulerepo\n> Git says my files are:\n> submodule_file.txt\n> ---------------\n> Filesystem says my files are:\n> submodule_file.txt\n> ---------------\n>  \n> A 'git ls-files' in post-commit doesn’t work:\n> $  echo nothing >> rubbish.txt && git commit rubbish.txt -m \"Add just rubbish\"\n> Git says my files are:\n> ---------------\n> Filesystem says my files are:\n> submodule_file.txt\n> ---------------\n> [master e05efb1] just rubbish\n> 1 file changed, 1 insertion(+)\n\nCan you check to see if the GIT_DIR environment variable is set in your\nhook?  In some cases, Git sets various GIT_* environment variables when\ncalling a hook or command, and if you want another repository, you'd\nneed to unset those values.\n-- \nbrian m. carlson: Houston, Texas, US\nOpenPGP: https://keybase.io/bk2204\n"},{"id":"392462","messageId":"539bac71-a4af-f96c-ae08-e8fa659c5dd1@green-sparklet.de","threadId":"52878","inReplyTo":"20200225011320.GL6462@camp.crustytoothpaste.net","subject":"Re: Bug: 'git ls-files' doesn't work on submodule in post-commit hook","fromName":"Stefan","fromEmail":"git.kernel.org_20@green-sparklet.de","sentAt":"2020-02-25T07:10:52Z","receivedAt":"2020-02-25T07:10:55Z","isPatch":false,"sender":{"key":"git.kernel.org_20@green-sparklet.de","avatar":null},"body":"Hi brian,\n\nI checked it by replacing the printf line in my example with this one:\nprintf '#!/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\n\nIn both hooks, GIT_DIR is empty, so this shouldn't be the problem.\nThanks anyways.\n\nOn 2020-02-25 02:13, brian m. carlson wrote:\n> Can you check to see if the GIT_DIR environment variable is set in your\n> hook?  In some cases, Git sets various GIT_* environment variables when\n> calling a hook or command, and if you want another repository, you'd\n> need to unset those values.\n> \n"}]}