I'm having a wired problem with git ls-files. I have a python script that check mandatory files are added to git using
git ls-files --error-unmatch
The python script is running on an environment I have created, and uses subprocess.run to execute the git command. I execute the git ls-files command in the same folder the file is:
file_dir = os.path.dirname(file_path)
file_name = os.path.basename(file_path)
current_dir = os.getcwd()
os.chdir(file_dir)
process = sp.run(
["git", "ls-files", "--error-unmatch", file_name],
stdout=sp.PIPE, stderr=sp.STDOUT,
universal_newlines=True,
timeout=10,
)
os.chdir(current_dir)
That python script is called from cmake custom target on_push I have created. The cmake target on_push loads the python environment and then runs the python script.
Then, I call cmake --build build --target on_push on my pre_push hook to check mandatory files are not forgotten.
when I do a git push most of the times, the underlying execution of git ls-files --error-unmatch returns on error, but not when I execute it using cmake --build build --target on_push (outside git)
Additionally, I was creating a git helper bash script for something else. And this script runs all hooks in all submodules I have. I use git submodule foreach to do this. The command is:
git submodule foreach --recursive "if [[ \"\$name\" =~ \"$1\" ]]; then if [[ -f \".githooks/pre-push\" ]]; then ./.githooks/pre-push; else echo \"hooks not configured!\"; fi; fi
When I run this script, I have the same error as when doing a git push. All the files are reported not to be in git by git ls-files --error-unmatch.
I tried to run failing script directly or through cmake and it works, but does not work when running under git push or git submodule foreach. It seems to fail when the pre_push script is executed under the git process or sub-shell.
Does someone have a clue on where the problem is? Could it be some environment that is different when I run on a sub-shell (which I suppose is what also git push is doing) or git runs it?