Blog Infos
Author
Published
Topics
Published

Let’s explore pre-commit a more structured way to commit history and how to make it easier for people to contribute to your projects

 

Photo by Yancy Min on Unsplash

 

Commit
Pull Request
<type>[optional scope]: [REFERENCE-1234]
<description>
[optional body]
[optional footer(s)]

Real-world examples can look like this:

chore: [PRJ1-123] run tests on jenkins ci
fix(server): [PRJ2-1234] send cors headers
send CORS headers by reflecting the origin header in the request
feat(blog): [PRJ2-1234] add comment section
Examples
// without scope
feat: allow provided config object to extend other configs

BREAKING CHANGE: `extends` key in config file is now used for extending other config files

// with scope
feat(lang): add Polish language

Commit message with multi-paragraph body and multiple footers:

fix: prevent racing of requests

Introduce a request id and a reference to latest request. Dismiss
incoming responses other than from latest request.

Remove timeouts which were used to mitigate the racing issue but are
obsolete now.

Reviewed-by: Z
Refs: #123

One recommendation is to use the revert type, and a footer that references the commit SHAs that are being reverted:

revert: let us never again speak of the noodle incident

Refs: 676104e, a215868
Commit Types

Credit: Android Academics

How to setup for Android
brew install pre-commit

//check version after installatio complete 
pre-commit --version
pre-commit install -t commit-msg -t pre-commit -t pre-push - allow-missing-config

3. Add pre-commit plugins to your project. Create the config file .pre-commit-config.yaml in your project directory:

default_stages: [commit, push]
repos:
  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: v2.1.0
    hooks:
      - id: commitlint
        stages: [commit-msg]
        additional_dependencies:
          - "@commitlint/config-conventional"
          - conventional-changelog-conventionalcommits

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.4.0
    hooks:
      - id: check-json
      - id: check-merge-conflict
      - id: detect-private-key
      - id: no-commit-to-branch

  - repo: https://github.com/jguttman94/pre-commit-gradle
    rev: v0.3.0
    hooks:
      - id: gradle-spotless
        args: ['-w', --wrapper]

4. Add lint to your project. Create the config file commitlint.config.js in your project directory:

module.exports = {
    extends: ["@commitlint/config-conventional"],
    // Validate for issue/ticket numbers
    parserPreset: {
        parserOpts: {
            // these are samples, add possible prefixes based on your project requirement
            issuePrefixes: ['ANDR-', 'TEST-','DSC-', 'ABC-', 'CO-'] 
        }
    },
    rules: {
        "body-leading-blank": [ 1, "always" ],
        "footer-leading-blank": [ 1, "always" ],
        "header-max-length": [ 2, "always", 72 ],
        "scope-case": [ 2, "always", "lower-case" ],
        "subject-case": [
            2,
            "never",
            [ "sentence-case", "start-case", "pascal-case", "upper-case" ]
        ],
        "subject-empty": [ 2, "never" ],
        "subject-full-stop": [ 2, "never", "." ],
        "type-case": [ 2, "always", "lower-case" ],
        "type-empty": [ 2, "never" ],
        "type-enum": [
            2,
            "always",
            [
                "build",
                "chore",
                "ci",
                "docs",
                "feat",
                "feature",
                "fix",
                "perf",
                "refactor",
                "revert",
                "style",
                "test"
            ]
        ]
    }
};

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

> git commit -m 'fix: [TOL-6911] test lint'
[INFO] Initializing environment for https://github.com/alessandrojcm
/commitlint-pre-commit-hook:@commitlint/config-conventional,
conventional-changelog-conventionalcommits.
Fix End of
Files.........................................................Passed
Check JSON...........................................(no files to check)
Skipped
Check for merge
conflicts................................................Passed
Detect Private
Key.......................................................Passed
Don't commit to
branch...................................................Passed
[INFO] Installing environment for https://github.com/alessandrojcm
/commitlint-pre-commit-hook.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
commitlint..............................................................
.Passed
- hook id: commitlint
- duration: 0.43s
[branchify-devops-changes fde84e2] fix: [TOL-6911] test lint
 1 file changed, 3 insertions(+), 3 deletions(-)
git commit --allow-empty -m ""
More from the Author

Code Commit Guidelines using Conventional Commits

Resources

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu