Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add githook script to alert on changes that require re-installs #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -31,3 +31,13 @@ repos:
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: local
hooks:
- id: check_deps_changes
name: Check for dependency changes
entry: python -m scripts.githook_deps_check
language: system
always_run: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider setting always_run: false to improve performance, as the script likely doesn't need to run on every commit.

stages: [post-checkout, post-merge]
verbose: true
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -274,9 +274,12 @@ format: ## Run black and isort code formatter
format-modified: ## Run black and isort code formatter on modified files
($(VENV_RUN); python -m isort `git ls-files -m | grep '\.py$$' | xargs`; python -m black `git ls-files -m | grep '\.py$$' | xargs` )

init-precommit: ## install te pre-commit hook into your local git repository
init-precommit: ## install the pre-commit hook into your local git repository
($(VENV_RUN); pre-commit install)
Comment on lines +277 to 278
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo in comment: 'te' should be 'the'


init-githooks: ## install all githooks into your local git repository
($(VENV_RUN); pre-commit install -t pre-commit -t post-checkout -t post-merge)

clean: ## Clean up (npm dependencies, downloaded infrastructure code, compiled Java classes)
rm -rf .filesystem
rm -rf localstack/dashboard/web/node_modules
49 changes: 49 additions & 0 deletions scripts/githook_deps_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Git hook that checks if there's been any updates to a set of predefined files

This is for example useful to be alerted if there might be a need to reinstall the project
"""

import os
import subprocess

from rich.console import Console

c = Console()

DISABLE_CHECKS_ENV_VAR = "GITHOOK_DISABLE_DEPS_CHECK"

# basically anything that might lead to issues with an existing local setup
files_to_watch = ["setup.py", "setup.cfg", "requirements.txt", "Makefile", "pyproject.toml"]

# TODO: alert on rebase
# TODO: alert on pull
# TODO: compare providers
# TODO: compare plugins


if __name__ == "__main__":
if os.environ.get(DISABLE_CHECKS_ENV_VAR, "0") == "1":
exit(0)
Comment on lines +26 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add a log message when checks are disabled to inform the user.


envs = {k: v for k, v in os.environ.items() if k.startswith("PRE_COMMIT")}

# checkout
from_commit = envs.get("PRE_COMMIT_FROM_REF")
to_commit = envs.get("PRE_COMMIT_TO_REF")

if from_commit and to_commit:
r = subprocess.run(
["git", "diff", "--name-only", from_commit, to_commit], capture_output=True
)
Comment on lines +35 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Use 'git diff --name-only' with '--no-renames' flag to avoid issues with renamed files.


files = r.stdout.decode().splitlines()

for file in files_to_watch:
if file in files:
c.print(
f"[red]ATTENTION![/red] Found a change in file [red]{file}[/red] during the checkout."
)
c.print(
f"\t>> show changes: [bold]git diff {from_commit[:8]}:{file} {to_commit[:8]}:{file}[/bold]\n"
)
Comment on lines +42 to +49
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a set for faster lookup of changed files.