diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d76c408..4a510b0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,4 +1,7 @@ -default_install_hook_types: [commit-msg, prepare-commit-msg] +minimum_pre_commit_version: "3.2.0" +default_install_hook_types: [pre-commit, commit-msg, prepare-commit-msg] +default_stages: + - pre-commit repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 diff --git a/README.md b/README.md index 7db4864..227d4eb 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,16 @@ # git-hooks -This repo contains custom git hooks we use at Two. To add a hook to a repo you have to add it to the repo's `.pre-commit-config.yaml` file +This repo contains custom git hooks we use at Two. To add a hook to a repo you +have to add it to the repo's `.pre-commit-config.yaml` file ## Add Hook To Repo -First make sure you have installed pre-commit in your repo with `brew install pre-commit` and installed the relevant hooks with `pre-commit install --hook-type `. See `stages` in [.pre-commit-hooks.yaml](.pre-commit-hooks.yaml) for relevant hook types. For more information on pre-commit usage, refer to [docs](https://pre-commit.com/#developing-hooks-interactively). +First make sure you have installed pre-commit in your repo with `brew install +pre-commit` and installed the relevant hooks with `pre-commit install +--hook-type `. See `stages` in +[.pre-commit-hooks.yaml](.pre-commit-hooks.yaml) for relevant hook types. For +more information on pre-commit usage, refer to +[docs](https://pre-commit.com/#developing-hooks-interactively). ### Example Config @@ -18,7 +24,9 @@ To only check for a reference to a Linear issue in your commit message, add this - id: linear-ref ``` -To check for both a reference to a Linear issue as well as a conventional commit type (`/[!]: `), add this: +To check for both a reference to a Linear issue as well as a conventional +commit type (`/[!]: `), add +this: ```yaml # .pre-commit-config.yaml @@ -38,22 +46,28 @@ Alternatively, you can use ssh - id: commit-type-with-linear-ref ``` +## Developmment -## Setup -1. Create virtual environment: +### 1. Create virtual environment - python3 -m venv venv +```bash +python3 -m venv venv +``` -2. Install requirements: +### 2. Install requirements - pip3 install '.[dev]' +```bash +pip3 install -e '.[dev]' +``` -## Release Process (git-hooks repo) +### 3. Release -1. If you have permission to push to main directly, skip to step 2. Otherwise create a new Linear ticket with a title "git-hooks release X.Y.Z" and a new branch based on that Linear ticket branching off of main. +1. If you have permission to push to main directly, skip to step 2. Otherwise + create a new Linear ticket with a title "git-hooks release X.Y.Z" and a new + branch based on that Linear ticket branching off of main. 2. Run `bumpver update --dry` to check version update will be as expected. -5. Run `bumpver update` to update git-hooks version. This creates a bump commit. -6. Push directly to `main` if you have permission to do so or via a PR if not. -8. Check out `main` branch and set tag with `git tag X.Y.Z` based on [CalVer](https://calver.org/) convention. -9. Push tags with `git push --tags`. -10. Go to GH and draft a new release - choose tag and select "generate release notes". +3. Run `bumpver update` to update git-hooks version. This creates a bump commit. +4. Push directly to `main` if you have permission to do so or via a PR if not. +5. Check out `main` branch and set tag with `git tag X.Y.Z` based on [CalVer](https://calver.org/) convention. +6. Push tags with `git push --tags`. +7. Go to GH and draft a new release - choose tag and select "generate release notes". diff --git a/git_hooks/commit_msg.py b/git_hooks/commit_msg.py index 239f29a..301ebf0 100644 --- a/git_hooks/commit_msg.py +++ b/git_hooks/commit_msg.py @@ -23,7 +23,7 @@ See https://github.com/two-inc/git-hooks/blob/24.06.24/README.md for more info. {ENDC} -{common.commit_type_doc} +{common.commit_types_doc} """ diff --git a/git_hooks/common.py b/git_hooks/common.py index 095ecaa..bd702bf 100644 --- a/git_hooks/common.py +++ b/git_hooks/common.py @@ -1,39 +1,59 @@ -commit_types = [ - "build updating build configuration, development tools", - "chore updating grunt tasks etc.", - "ci updating deployment configuration", - "docs changes to documentation", - "fix patching a bug in the codebase", - "feat adding a new feature to the code", - "feat! adding a new feature that introduces breaking API change", - "hotfix updating a bug in production", - "perf updating code to make performance enhancements", - "refactor updating code without any functional change", - "revert updating code to earlier change", - "style formatting changes, missing semicolons, etc.", - "test for adding missing tests, refactoring tests; no production code change", -] - -commit_type_doc = ( - """Valid conventional commit types are: +commit_types: dict[str, str] = { + "build": "updating build configuration, development tools", + "chore": "updating grunt tasks etc.", + "ci": "updating deployment configuration", + "docs": "changes to documentation", + "fix": "patching a bug in the codebase", + "feat": "adding a new feature to the code", + "feat!": "adding a new feature that introduces breaking API change", + "hotfix": "updating a bug in production", + "perf": "updating code to make performance enhancements", + "refactor": "updating code without any functional change", + "revert": "updating code to earlier change", + "style": "formatting changes, missing semicolons, etc.", + "test": "for adding missing tests, refactoring tests; no production code change", +} -\t""" - + "\n\t".join(commit_types) - + "\n" +commit_types_title: str = "Valid conventional commit types are:" +commit_types_block: str = "\t" + "\n\t".join( + [f"{k.ljust(10)}{v}" for k, v in commit_types.items()] ) +commit_types_doc: str = f""" +{commit_types_title} -commented_commit_type_doc = "# " + commit_type_doc.replace("\n", "\n#") +{commit_types_block} +""" +commit_types_doc_commented: str = "\n".join( + [ + f"# {commit_types_title}", + "#" + commit_types_block.replace("\n", "\n#"), + ] +) -commit_type_regex = ( - "(?:build|chore|ci|docs|feat|fix|hotfix|perf|refactor|revert|style|test)" +commit_type_regex: str = f"(?:{'|'.join(commit_types.keys())})" +teams: list[str] = [ + "ATL", + "CET", + "DEL", + "INF", + "KNA", + "L2", + "NOR", + "T", +] +linear_ref: str = ( + "(?:" + f"{'|'.join(t for t in teams)}" + "|" + f"{'|'.join(t.lower() for t in teams)}" + ")-[0-9]{1,5}" ) -linear_ref = "(?:t|T|kna|KNA|cet|CET|nor|NOR|l2|L2|del|DEL)-[0-9]{1,5}" -valid_commit_regex = ( +valid_commit_regex: str = ( f"^{linear_ref}/{commit_type_regex}!?: |Merge .+|Revert .+|Bump version .+" ) -partial_branch_regex = f"({linear_ref})[-|_](.*)" -branch_regex = f"^(.*)/{partial_branch_regex}" -commit_msg_title_regex = f"^({commit_type_regex}!?):? (.*)" -commit_msg_issue_regex = f"^({linear_ref})/(.*)" -issue_regex = f"^{linear_ref}$" -prefix_regex = f"^({commit_type_regex})" +partial_branch_regex: str = f"({linear_ref})[-|_](.*)" +branch_regex: str = f"^(.*)/{partial_branch_regex}" +commit_msg_title_regex: str = f"^({commit_type_regex}!?):? (.*)" +commit_msg_issue_regex: str = f"^({linear_ref})/(.*)" +issue_regex: str = f"^{linear_ref}$" +prefix_regex: str = f"^({commit_type_regex})" diff --git a/git_hooks/prepare_commit_msg.py b/git_hooks/prepare_commit_msg.py index 3f72f71..f0645ee 100644 --- a/git_hooks/prepare_commit_msg.py +++ b/git_hooks/prepare_commit_msg.py @@ -170,7 +170,7 @@ def prepare_commit_msg(raw_commit_msg: str, branch: str) -> str: commit_msg_title = ( commit_msg_title_data["commit_msg_title"] or branch_data["commit_msg_title"] ) - commit_type_lines = [common.commented_commit_type_doc] if edit_mode else [] + commit_type_lines = [common.commit_types_doc_commented] if edit_mode else [] linear_commit_msg_lines = ( [linear_data["commit_msg_body"]] if linear_data.get("commit_msg_body") else [] ) diff --git a/pyproject.toml b/pyproject.toml index 9e352d4..ff33fe7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,8 @@ dependencies = [ dev = [ "bumpver==2022.1119", "pytest==6.2.5", - "coverage==7.5.3" + "coverage==7.5.3", + "pre-commit==3.7.1", ] [project.scripts]