Skip to content

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

Closed
@robin-moss

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 python3
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)]
    print(os.getcwd())
    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()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions