Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
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

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown
Collaborator

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?

pass_filenames: false

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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?

15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.
Expand Down
17 changes: 8 additions & 9 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ tasks:

install-githooks:
desc: "Install git hooks for commit validation"
vars:
HOOKS_DIR:
sh: git rev-parse --git-path hooks
cmds:
- cp githooks/pre-push "{{.HOOKS_DIR}}/"
- chmod +x "{{.HOOKS_DIR}}/pre-push"
- cp githooks/commit-msg "{{.HOOKS_DIR}}/"
- chmod +x "{{.HOOKS_DIR}}/commit-msg"
- echo "✓ Git hooks installed to {{.HOOKS_DIR}}"
preconditions:
- sh: pre-commit --version
msg: "pre-commit is required: https://pre-commit.com/#install"
- sh: gitlance --help
msg: "gitlance must be installed and available in PATH: cargo install --path . --locked"
cmds:
- pre-commit install
- echo "✓ Git hooks installed with pre-commit"

setup:
desc: "Setup Rust toolchain and development environment"
Expand Down
24 changes: 24 additions & 0 deletions githooks/pre_commit_pre_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python3

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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())