diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd9e1937..c6868b73 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,9 +6,18 @@ exclude: | )$ repos: + - repo: local + hooks: + - id: commit-message-prefix + name: Validate Commit Message for src/plus Changes + entry: python ./.precommit/validate_commit_message.py + language: python + stages: [commit-msg] + - repo: local hooks: - id: lint + stages: [pre-commit] name: lint language: system entry: bash -c '$HOME/.nvm/nvm-exec npm run lint' @@ -22,6 +31,7 @@ repos: rev: 'v3.1.0' hooks: - id: prettier + stages: [pre-commit] exclude: | (?x)^( docs/.*| @@ -31,11 +41,13 @@ repos: rev: v1.1.2 hooks: - id: markdown-toc + stages: [pre-commit] name: README.md files: ^README.md$ - repo: local hooks: - id: no-plus-in-core + stages: [pre-commit] name: No "plus" in core language: pygrep entry: 'plus' @@ -50,6 +62,7 @@ repos: - repo: local hooks: - id: no-console-log + stages: [pre-commit] name: No console.log() calls language: pygrep entry: 'console\.log' diff --git a/.precommit/validate_commit_message.py b/.precommit/validate_commit_message.py new file mode 100644 index 00000000..8e5944ec --- /dev/null +++ b/.precommit/validate_commit_message.py @@ -0,0 +1,38 @@ +import subprocess +import sys + + +def get_staged_files(): + result = subprocess.run(["git", "diff", "--cached", "--name-only"], stdout=subprocess.PIPE, text=True) + files = result.stdout.splitlines() + return files + + +def get_commit_message(commit_msg_filepath): + with open(commit_msg_filepath, 'r') as f: + return f.readline().strip() + + +def main(): + commit_msg_filepath = sys.argv[1] + commit_message = get_commit_message(commit_msg_filepath) + staged_files = get_staged_files() + + has_plus_changes = any(f.startswith('src/plus') for f in staged_files) + print(commit_message) + starts_with_plus = commit_message.startswith('[PLUS]') + + if has_plus_changes and not starts_with_plus: + print("'[PLUS]' not found in commit message, but there's changes are from 'src/plus'.") + return 1 + + if starts_with_plus and not all(f.startswith('src/plus') for f in staged_files): + print("'[PLUS]' found in commit message, but there's changes from other directories. \n" + "Changes MUST be only from 'src/plus'.") + return 1 + + return 0 + + +if __name__ == "__main__": + sys.exit(main())