/enforce-angular-commitmessages

Git Hooks to enforce Angular-style Git commit messages on client-side as welll as server-side

Primary LanguagePerlApache License 2.0Apache-2.0

Enforce Angular commit message style guide (client- & server-side)

This repository contains git hook scripts to enforce the Angular commit message style guide on the client-side (committer) as well as the server-side.

NOTE

Github does not support Asciidoc-include, so just click the links below :(

commit-msg – enforce Angular commit message style guide (client-side)

commit-msg is a git hook script to enforce the Angular commit message style guide for all client-side commits.

It will work on both Linux/Unix as well as on Windows platforms.

Installation

commit-msg implements a git client-side hook. Because hooks aren’t transferred with a clone of a project, you must distribute these scripts some other way and then have your users copy them to their .git/hooks directory and make them executable You can distribute these hooks within the project or in a separate project, but Git won’t set them up automatically.

Example installation for the repository root directory
cp commit-msg .git/hooks/
chmod +x .git/hooks/commit-msg

Note

Unless the server-side counterpart, this script only enforces commits. Messages i.e. generated during a git revert or a git merge are not passed to commit-msg and hence are not enforced.

Usage

If a commit does not comply to the Angular commit style guide it will fail and abort the commit with the following message.

Additionally to the cited style guide it also checks for at least 7 characters after ': '.

~/p/dashboard.git>  git commit -m "feat(): My first commit"
ERROR: Commit message does not following the commit styleguide.
       Please check the following guide:
           https://github.com/angular/angular.js/blob/v1.5.0/CONTRIBUTING.md#commit

SYNTAX:
    <type>(<scope>): <subject>

    <descriptive body>

TYPE:
    feat: A new feature
    fix: A bug fix
    docs: Documentation only changes
    style: Changes that do not affect the meaning of the code
    refactor: A code change that neither fixes a bug nor adds a feature
    perf: A code change that improves performance
    test: Adding missing tests
    chore: Changes to the build process or auxiliary tools and libraries

EXAMPLE:
    perf(ngAnimate): avoid jqLite/jQuery for upward DOM traversal

    The 'parentNode' property is well supported between all browsers. Since
    no other functionality was required here, we can use the DOM API directly.

pre-receive – enforce Angular commit message style guide (server-side)

pre-receive is a git hook script to enforce the Angular commit message style guide on the server-side.

Installation on plain Git server

Just install pre-receive as server-side hook on your git server.

Be aware, that git only supports one pre-receive hook script. So you must implement a delegate script if one is already present.

Installation on Gitlab

Gitlab support custom Git hooks. The setup is rather easy. Simply copy the script into repositories/<group>/<project>.git/.git/custom_hooks and mark it as executable. Make sure pre-receive matches the file name.

Usage

If a commit does not comply to the Angular commit style guide it will fail and abort the commit with the following message.

Additionally to the cited style guide it also checks for at least 7 characters after the colon (:)`.

~ git push ssh://git@localhost:10022/ben/qa.git test/commit-enforcement
Zähle Objekte: 6, Fertig.
Delta compression using up to 4 threads.
Komprimiere Objekte: 100% (5/5), Fertig.
Schreibe Objekte: 100% (6/6), 1.14 KiB | 0 bytes/s, Fertig.
Total 6 (delta 2), reused 0 (delta 0)
remote: Invalid commit message format for a881e515de5d0024c6c1141a296aab794fe82e53: blurb
remote:
remote: ================================================================================
remote: ERROR: 1 commit message(s) do not following the commit styleguide.
remote:        Please check the following guide or use '// @formatter:off':
remote:        https://github.com/angular/angular.js/blob/v1.5.0/CONTRIBUTING.md#commit
remote: ================================================================================
remote:
remote: SYNTAX:
remote:     <type>(<scope>): <subject>
remote:
remote:     <descriptive body>
remote:
remote: TYPE:
remote:      feat: A new feature              fix: A bug fix
remote:      docs: Documentation changes     test: Adding tests
remote:     style: Changes that do not affect the meaning of the code
remote:      perf: A code change that improves performance
remote:  refactor: A code change that neither fixes a bug nor adds a feature
remote:     chore: Changes to the build process or auxiliary tools and libraries
remote:
remote: EXAMPLE:
remote:     perf(ngAnimate): avoid jqLite/jQuery for upward DOM traversal
remote:
remote:     The 'parentNode' property is well supported between all browsers. Since
remote:     no other functionality was required here, we can use the DOM API directly.
To ssh://git@localhost:10022/ben/qa.git
 ! [remote rejected] test/commit-enforcement -> test/commit-enforcement (pre-receive hook declined)
error: Fehler beim Versenden einiger Referenzen nach 'ssh://git@localhost:10022/ben/qa.git'

Supressing check

You can stop the enforcement of all older commits if you add // @formatter:off to the message of a commit.

This is especially useful on starting / introducing the commit message style guide in a project, because creating a new branch will always contain all parent commits and potentially legacy commits not complying to the style guide.

Allowing default messages for merge and revert

For git revert and git merge operations, git applies and sometimes uses without confirmation default message, which do not comply with the Angular styleguide.

In case you do now want to impose unecessary obstacles you can configure pre-receive to accept these sort of messages by changing to configuration values inside the script.

$allowGitRevert

If 1, this allow default generated Git commit messages revert. Defaults to 0.

Revert "<original message>"

$allowGitMerge

If 1, this allow default generated Git commit messages for merge commits. Defaults to 0.

Merge branch '<sourcebranch>' into <target>