-
-
Notifications
You must be signed in to change notification settings - Fork 910
Closed
Description
describe your issue
I am trying to write a new hook that checks that a .meta file exists for every file under the Assets folder (Unity specific check), I have working code that uses GitPython==3.1.24 to inspect the Git index. When I run this code directly it works as expected, however when I run it via pre-commit (as a hook or try repo) the index check fails (meta_file_in_index).
It looks like although it's finding the correct git repo, the index is empty, I've tried several things to figure out what is happening but I've not had any luck. Any advice would be apprecaited
Hook Code
import os
import sys
from git import Repo, IndexFile, DiffIndex
def filter_assets(path: str):
lower_case = path.lower()
return lower_case.startswith("assets")
def path_to_meta_file(path: str):
return path + ".meta"
def meta_file_exists(meta_file: str):
return os.path.isfile(meta_file)
def meta_file_in_index(meta_file: str, index: IndexFile):
return any(x for x in index.entries.keys() if x[0] == meta_file)
def meta_file_in_diff(meta_file: str, diff: DiffIndex):
return any(x for x in diff if x.a_path == meta_file)
def run():
files = [path for path in sys.argv[1:] if filter_assets(path)]
repo = Repo(os.getcwd())
index = repo.index
modified = index.diff(None)
errors = []
for file in files:
meta_file = path_to_meta_file(file)
if not meta_file_exists(meta_file):
errors.append(f"{file} does not have a matching meta file {meta_file}")
continue
if not meta_file_in_index(meta_file, index):
errors.append(f"{meta_file} is not in the git index")
continue
if meta_file_in_diff(meta_file, modified):
errors.append(f"{meta_file} has been changed but is not staged")
if len(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()pre-commit --version
pre-commit 2.13.0
.pre-commit-config.yaml
---
- id: meta_check
name: Check meta files are in the Git index
entry: meta_check
exclude: ".meta$"
language: python~/.cache/pre-commit/pre-commit.log (if present)
No response
Metadata
Metadata
Assignees
Labels
No labels