Skip to content

Inspecting the index during a pre-commit is empty #1388

Description

@robin-moss

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_index fails because the index appears 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:

  • Validated it's the right path
  • Validating it's on the same branch

The issue raised with pre-commit: pre-commit/pre-commit#2184

Source Code:

#!/usr/bin/env python3importosimportsysfromgitimportRepo, IndexFile, DiffIndexdeffilter_assets(path: str):
lower_case=path.lower()
returnlower_case.startswith("assets")
defpath_to_meta_file(path: str):
returnpath+".meta"defmeta_file_exists(meta_file: str):
returnos.path.isfile(meta_file)
defmeta_file_in_index(meta_file: str, index: IndexFile):
returnany(xforxinindex.entries.keys() ifx[0] ==meta_file)
defmeta_file_in_diff(meta_file: str, diff: DiffIndex):
returnany(xforxindiffifx.a_path==meta_file)
defrun():
files= [pathforpathinsys.argv[1:] iffilter_assets(path)]
print(os.getcwd())
repo=Repo(os.getcwd())
index=repo.indexmodified=index.diff(None)
errors= []
forfileinfiles:
meta_file=path_to_meta_file(file)
ifnotmeta_file_exists(meta_file):
errors.append(f"{file} does not have a matching meta file {meta_file}")
continueifnotmeta_file_in_index(meta_file, index):
errors.append(f"{meta_file} is not in the git index")
continueifmeta_file_in_diff(meta_file, modified):
errors.append(f"{meta_file} has been changed but is not staged")
iflen(errors) >0:
print("At least one file has an issue with it's meta file:")
print(" * "+"\n * ".join(errors))
sys.exit(1)
if__name__=="__main__":
run()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions