Repository navigation
Inspecting the index during a pre-commit is empty #1388
Description
Activity
Thanks for the clear issue description, very informative!
It's not the first time I hear that git hooks aren't working as they should depending on how executes them. Most often they fail in cron jobs though.
Since the
indexappears to be empty, I can imagine it get the wrong path. Now it's mentioned that the 'right path' was validated already, so is it correct that that repo.git_dir is what it should be?If it is, you might try to re-throw this OSError as it's the only reason it would yield an empty index. Unfortunately it doesn't check the actual cause of the error, it should only yield an empty index if the file isn't present yet, which should barely ever be happening.
So adding the code:
lfd = LockedFD(index._file_path) lfd.open(write=False, stream=False)
led to a stack trace (below), which mentions that there is already an
index.lock, which would actually make sense as we're in a "pre-commit" hook so in the middle of a Git operation 🤦. Is it possible to run in a "readonly" state that wouldn't require the index lock?Otherwise I'll have to reconsider my approach to checking the index
Stack trace:
Traceback (most recent call last): File "/tmp/tmpgwk46x0q/repo_aps1mss/py_env-python3.9/lib/python3.9/site-packages/gitdb/util.py", line 323, in open fd = os.open(self._lockfilepath(), lockmode, int("600", 8)) FileExistsError: [Errno 17] File exists: '/home/robin.moss/repo/.git/index.lock' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/tmp/tmpgwk46x0q/repo_aps1mss/py_env-python3.9/bin/meta_check", line 8, in <module> sys.exit(run()) File "/tmp/tmpgwk46x0q/repo_aps1mss/py_env-python3.9/lib/python3.9/site-packages/unity_pre_commit/meta_check.py", line 41, in run lfd.open(write=False, stream=False) File "/tmp/tmpgwk46x0q/repo_aps1mss/py_env-python3.9/lib/python3.9/site-packages/gitdb/util.py", line 330, in open raise IOError("Lock at %r could not be obtained" % self._lockfilepath()) from e OSError: Lock at '/home/robin.moss/repo/.git/index.lock' could not be obtainedI have created a new release which simply won't take a read lock anymore - it's not required at all, to solve this particular problem.
Thank you, that fixed my issue :)
I've written a small script to work with pre-commit, which when run locally seems to work but when I run it through pre-commit the git index is empty and I get errors.
The issue I'm seeing is when I run the script via PyCharm the checks pass, but when it's run automatically by pre-commit the
meta_file_in_indexfails because theindexappears to be empty.Any advice on what I'm doing wrong or suggestions for digging deeper into what's going on would be greatly appreciated.
What I've done so far:
The issue raised with pre-commit: pre-commit/pre-commit#2184
Source Code: