-
Notifications
You must be signed in to change notification settings - Fork 3
fix: integrate git hooks with pre-commit #17
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # SPDX-FileCopyrightText: Canonical Ltd. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| default_install_hook_types: [commit-msg, pre-push] | ||
|
|
||
| repos: | ||
| - repo: local | ||
| hooks: | ||
| - id: gitlance-commit-msg | ||
| name: Validate commit message with gitlance | ||
| entry: bash githooks/commit-msg | ||
| language: system | ||
| stages: [commit-msg] | ||
| - id: gitlance-pre-push | ||
| name: Validate commits with gitlance | ||
| entry: python githooks/pre_commit_pre_push.py | ||
| language: python | ||
| stages: [pre-push] | ||
| always_run: true | ||
| pass_filenames: false | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain why you added this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The adapter gets the pushed ref from pre-commit's environment. Changed filenames are not inputs to Gitlance, so they should not be passed. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/bin/bash | ||
|
|
||
| # SPDX-FileCopyrightText: Canonical Ltd. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| set -e | ||
|
|
||
| # Check that gitlance is available | ||
| if ! command -v gitlance >/dev/null 2>&1; then | ||
| echo "Error: gitlance is not installed or not in PATH" | ||
| echo "To bypass: git commit --no-verify" | ||
| exit 1 | ||
| fi | ||
|
|
||
| message_file="$1" | ||
|
|
||
| if ! gitlance --message-file "$message_file"; then | ||
| echo | ||
| echo "Commit rejected: Message validation failed" | ||
| echo "To bypass: git commit --no-verify" | ||
| exit 1 | ||
| fi | ||
|
|
||
| exit 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # SPDX-FileCopyrightText: Canonical Ltd. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| def main() -> int: | ||
| local_branch = os.environ.get("PRE_COMMIT_LOCAL_BRANCH", "HEAD") | ||
| if local_branch != "HEAD" and not local_branch.startswith("refs/heads/"): | ||
| return 0 | ||
|
|
||
| head = os.environ.get("PRE_COMMIT_TO_REF") or local_branch | ||
| return subprocess.call( | ||
| ["gitlance", "--head", head, "--not-on-remotes"], | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why you added this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gitlance validates commits rather than files, so this keeps the hook running for pushes with no changed files, such as an empty commit.