Make sql pre-commit check work when pre-commit hooks are in non-standard locations (#14955)

* make sql pre-commit check work with git worktrees

* Comment reasoning in script file

Co-authored-by: Kyle Altendorf <sda@fstab.net>

---------

Co-authored-by: Kyle Altendorf <sda@fstab.net>
This commit is contained in:
Matt Hauff 2023-03-31 13:42:17 -07:00 committed by GitHub
parent 20bfba0fa6
commit bdd6eb8bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -9,7 +9,13 @@ from typing import Dict, Set, Tuple
def check_create(sql_type: str, cwd: str, exemptions: Set[Tuple[str, str]] = set()) -> int:
lines = check_output(["git", "grep", f"CREATE {sql_type}"], cwd=cwd).decode("ascii").split("\n")
# the need for this change seems to come from the git precommit plus the python pre-commit environment
# having GIT_DIR specified but not GIT_WORK_TREE. this is an issue in some less common git setups
# such as with worktrees, at least in particular uses of them. i think that we could switch to letting
# pre-commit provide the file list instead of reaching out to git to build that list ourselves. until we
# make time to handle that, this is an alternative to alleviate the issue.
exemptions = set((cwd + "/" + file, name) for file, name in exemptions)
lines = check_output(["git", "grep", f"CREATE {sql_type}"]).decode("ascii").split("\n")
ret = 0
@ -21,6 +27,8 @@ def check_create(sql_type: str, cwd: str, exemptions: Set[Tuple[str, str]] = set
continue
if "db_upgrade_func.py" in line:
continue
if not line.startswith(cwd):
continue
name = line.split(f"CREATE {sql_type}")[1]
if name.startswith(" IF NOT EXISTS"):