-
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.
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. I'm not sure I understand. Why is that different in the other hook? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,21 @@ Contributions are welcome! Please follow these guidelines. | |
| 5. Add `Signed-off-by` trailer to commits | ||
| 6. Open a pull request | ||
|
|
||
| ### Local commit validation | ||
|
|
||
| Gitlance can validate commit messages and pushed commits locally. Install | ||
| [pre-commit](https://pre-commit.com/#install), install the local Gitlance | ||
| binary, and register both hooks: | ||
|
|
||
| ```console | ||
| cargo install --path . --locked | ||
| task install-githooks | ||
| ``` | ||
|
|
||
| The installation is idempotent. If a `commit-msg` or `pre-push` hook already | ||
|
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. How does it behave when installing the gitlance hooks after they were updated locally? Does it update the hooks? Does it ignore the updates as it already has them? |
||
| exists, pre-commit preserves it as a legacy hook and runs it alongside | ||
| Gitlance. Running `pre-commit uninstall` restores the previous hooks. | ||
|
|
||
| ## Commit Requirements | ||
|
|
||
| - Use conventional format: `feat:`, `fix:`, `docs:`, etc. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #!/usr/bin/env python3 | ||
|
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. Gitlance supports taking advantage of Git's commit file, which is exposed to hooks before creating the commit. Does this script fill any gap in that implementation? |
||
|
|
||
| # 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"], | ||
|
Comment on lines
+17
to
+19
|
||
| ) | ||
|
|
||
|
|
||
| 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.
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 supports now validating files as well. If you mean a git log entry in a file. Did you find a limitation?