DEV Community

Cover image for Automate Your Git Workflows with Git Hooks ๐Ÿš€
Syed Muhammad Yaseen
Syed Muhammad Yaseen

Posted on

1

Automate Your Git Workflows with Git Hooks ๐Ÿš€

Helloooooooo!

Hope you're doing great! This is SMY! ๐Ÿ‘‹ Let's Jump right in ๐Ÿš€

....

I've settled up an example commit-msg hook that on every commit

โ†ณ checks for branch name convention i.e [type]/ABC-[ticketnumber]-[objective]

โ†ณ checks for commit-msg convention i.e [ABC-ticketnumber] [type]: objective

โ†ณ checks for lints, prettier, and run unit tests

Gist Link: https://gist.github.com/smyaseen/17083d60d02a07b2a3122410e2d39b6f

.....

Contents:

  • โšก Wait What?

  • โšก But Why?

  • โšก But How?

1๏ธโƒฃ What -

โ†ณ Git hooks are scripts that run automatically whenever a particular event occurs in a Git repository.

โ†ณ They let you customize Gitโ€™s internal behaviour and trigger customizable actions at key points in the development life cycle.

โ†ณ There are local hooks and server-side hooks.
โ†ณ Local hooks run on your machine and server-side on the remote repository.

local hooks:

โ†ณ pre-commit
โ†ณ prepare-commit-msg
โ†ณ commit-msg
โ†ณ post-commit
โ†ณ post-checkout
โ†ณ pre-rebase

server hooks:

โ†ณ pre-receive
โ†ณ update
โ†ณ post-receive

2๏ธโƒฃ Why -

โ†ณ For example, you want to run Lint checks before files are committed, so we use the pre-commit hook.

โ†ณ another example you want to enforce the standard branch name and commit-msg convention, so we use the commit-msg hook.

โ†ณ and lots more...

3๏ธโƒฃ How -

โ†ณ For easy setup, in a JavaScript project, install a library called Husky and follow the documentation

https://www.npmjs.com/package/husky

โ†ณ An example is that I want to enforce branch names, commit message convention, and run lint, prettier, and tests.

โ†ณ We can use multiple hooks. In my case, I will add a commit-msg hook. The reason I haven't chosen pre-commit was that it doesn't check for commit-msg as it runs before commit. I don't want a developer to run checks and fail at commit message, fix, and run all again. I want to check for the commit message first.

โ†ณ Create a commit-msg hook, following husky's documentation, and paste the following example:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# Checks for branch name


currentBranch=$(git rev-parse --abbrev-ref HEAD)
requiredPattern="^(build|chore|feat|docs|refactor|perf|test)/ABC-\d+-.+$"

if ! echo "$currentBranch" | grep -qE $requiredPattern; then
    echo "\nInvalid branch name: $currentBranch"
    echo "-"
    echo "Should follow this pattern: build|chore|feat|docs|refactor|perf|test/ABC-ticketnumber-any-text"
    echo "-"
    echo "example: docs/ABC-123-update-readme.md"
    echo "-"
    echo "Refer to this for convention:"
    echo "-"
    echo "build : Changes related to building the code (e.g. adding npm dependencies or external libraries)."
    echo "-"
    echo "chore: Changes that do not affect the external user (e.g. updating the .gitignore file or .prettierrc file)."
    echo "-"
    echo "feat: A new feature."
    echo "-"
    echo "fix: A bug fix."
    echo "-"
    echo "docs: Documentation a related changes."
    echo "-"
    echo "refactor: A code that neither fix bug nor adds a feature."
    echo "-"
    echo "perf: A code that improves performance style: A code that is related to styling."
    echo "-"
    echo "test: Adding new test or making changes to existing test"
    echo "-\n"

    exit 1  # Branch name doesn't match the pattern, exit with error code
fi

# Checks for commit message

commit_message="$(cat "$1")"

pattern='^\[ABC-[0-9]+\] (build|chore|feat|docs|refactor|perf|test): .+$'

if [[ ! $commit_message =~ $pattern ]]; then
    echo "\nInvalid commit message: $commit_message"
    echo "-"
    echo "Should follow this pattern: [ABC-ticketnumber] build|chore|feat|docs|refactor|perf|test: objective"
    echo "-"
    echo "example: [ABC-15] chore: updated .gitignore"
    echo "-"
    echo "Refer to this for convention:"
    echo "-"
    echo "build : Changes related to building the code (e.g. adding npm dependencies or external libraries)."
    echo "-"
    echo "chore: Changes that do not affect the external user (e.g. updating the .gitignore file or .prettierrc file)."
    echo "-"
    echo "feat: A new feature."
    echo "-"
    echo "fix: A bug fix."
    echo "-"
    echo "docs: Documentation a related changes."
    echo "-"
    echo "refactor: A code that neither fix bug nor adds a feature."
    echo "-"
    echo "perf: A code that improves performance style: A code that is related to styling."
    echo "-"
    echo "test: Adding new test or making changes to existing test"
    echo "-\n"
  exit 1
fi


# npx lint-staged -- uncomment when have lint setted up
# npm run test -- uncomment when have test setted up
Enter fullscreen mode Exit fullscreen mode

โ†ณ Now, whenever you commit, it will check for branch and commit naming convention along with running lint checks. Finally, it will run tests in the end, after everything else is good to go.

4๏ธโƒฃ Result -

โœจ Consistency.
โœจ Standardization.
โœจ Mind Off the Trivial.
โœจ Automate & Focus on Impactful.

Wrapping Up:

We just Elevated Your Development Workflow with a Git Hooks. ๐Ÿš€

.....

Now you can supercharge your development workflow ๐Ÿš€

That's it, folks! I hope it was a good read for you. Thank you! โœจ

๐Ÿ‘‰ Follow me

GitHub

LinkedIn

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay