MediumUse pre-commit Git hook in Flutter to enforce quality checks | by Rahul Rastogi | Medium
Sitemap
Rahul Rastogi

Rahul Rastogi

Mobile Apps @EX2 solutions, India. Technology enthusiast, Flutter, Android and more.

Use pre-commit Git hook in Flutter to enforce quality checks

6 min readAug 13, 2024
Press enter or click to view image in full size
Photo by Jakub Żerdzicki on Unsplash

Git hooks allow you to execute a script when a particular event take place in the Git lifecycle (git commit , git push etc).

You can use pre-commitGit hook to perform some checks before the execution of each git commit command.

So whenever someone wants to commit changes in the source code of repository, then shell script of pre-commit hook will get executed. If the shell script execution gives any error then the git commit command will fail.

This gives you a good opportunity to perform certain quality checks. Not just that, even you can perform some changes in the source code using some commands.

For instance,

  1. You can check if the source code is formatted or not.
  2. You can run dart format . command to format the unformatted source code files before each commit.
  3. You can run flutter analyze command to check Lint errors, and if needed, restrict developer from making any commit in source code.
  4. You can run dart fix command to fix auto-fixable minor issues in your source code before each commit.
  5. You can run flutter test command before each commit to verify that new changes in the source code aren’t breaking any existing unit test. Also, you can even check the percentage of unit test coverage.
  6. …rest is in your hands now. You can customise the behaviour of git hooks as per the needs of your project.

By default, git hooks reside in the repository_root/.git/hooks directory of the repository. This .git folder is hidden by default in each repository.

In MacOS, you can navigate to repository’s root folder in the terminal and then run open .git/hooks command. This will display the contents of hooks folder like this:

Press enter or click to view image in full size
Files in the .git/hooks folder by default

Now, navigate to the pre-commit.sample file from the hooks folder, and rename this to just pre-commit. Don’t give any name to the file extension.

Remove all the content of this file and write these simple two lines in the pre-commit file:

#!/bin/sh
echo "Executing before commit"

Make pre-commit file executable by running:

chmod +x pre-commit

Now, in your repository make any source code changes and attempt to commit the changes.

Press enter or click to view image in full size
Showing pre-commit git hook file content, and execution of git commit command in the terminal

In above screen, what we notice is that the pre-commit git hook is configured to print “Executing before commit” message on the terminal window. And when we ran git commit -m "test commit" command on the repository path, then:

  1. First the script of pre-commit hook got executed.
  2. Then, the changes got committed.

Now, let’s analyze the flutter source code using flutter analyze and restrict the commit if the flutter Lint reports any issue of error severity.

Get Rahul Rastogi’s stories in your inbox

Join Medium for free to get updates from this writer.

Replace the content of pre-commitfile with the below script:

#!/bin/sh

result=$(flutter analyze --no-fatal-infos --no-fatal-warnings)
exitCode=$? # If the exitCode is zero, then command executed successfully.

if [ $exitCode -ne 0 ]; then
echo "$result"
printf "Flutter analyze found issues, please fix them."
exit 1
fi
printf "Finished running flutter analyze command."

For verifying this above script, let’s force an error in our source code. You can make any change your source code to show red lines of error like this:

Press enter or click to view image in full size

Let’s execute the git commit -m "committing a file with error" command in the terminal at the root folder of project:

Press enter or click to view image in full size

As the user attempted to commit changes having error, the changes are refused to be committed as the pre-commit git hook caught the error just before the commit.

Here, the pre-commit git hook failed because the flutter analyze command couldn’t be executed successfully.

Let’s add one more script in the pre-commit hook. Let’s use the dart fix command to fix the auto-fixable issues.

In Flutter, there are minor code issues which can be fixed by just running the dart fix command in the project.

You must have seen the warning in code of “unused import” or “use const with constructor to improve performance” and so on.

For such basic issues, dart suggest a auto-fix and we fix them in the Android Studio/Visual Studio Code IDE itself quickly. IDE does that by applying specific type of lint fix for that particular file only.

Let’s add some Lint warnings in the source code by adding an unused import and ignoring the const constructor:

Press enter or click to view image in full size
Source code having two auto-fixable lint issues

Append the below script to the pre-commit hook file.

result=$(dart fix --apply)
exitCode=$?

# Adding the files changed by `dart fix` command to git.
git add .

echo "$result"

if [ $exitCode -ne 0 ]; then
echo "dart fix command failed to execute."
exit 1
fi
echo "Finished running dart fix command."

Now, commit your changes git commit -m "test commit" on the git.

When the git commit command completes, you’ll see that the changes are committed on Git and both the warnings won’t be there in the committed source code.

Press enter or click to view image in full size
Issues fixed by pre-commit hook

Both the auto-fixable issues left by the developer in the source code, fixed automatically while committing the changes on Git.

For having both of the features explained in this blog, your pre-commit will be having these scripts:

#!/bin/sh

result=$(flutter analyze --no-fatal-infos --no-fatal-warnings)
exitCode=$? # If the exitCode is zero, then command executed successfully.

if [ $exitCode -ne 0 ]; then
echo "$result"
echo "Flutter analyze found issues, please fix them."
exit 1
fi
echo "Finished running flutter analyze command."

################################################################

# 2. Run `dart dry --apply` to auto-fix basic issues.
result=$(dart fix --apply)
exitCode=$?

# Adding the files updated by `dart fix` command to git.
git add .

echo "$result"

if [ $exitCode -ne 0 ]; then
echo "dart fix command failed to execute."
exit 1
fi
echo "Finished running dart fix command."

You can extend the capability of pre-commit git hook as per your needs. In fact, you can explore and add more type of git hooks which suits best in your project.

Saving git hooks to repository

Note that the “pre-commit” hook is local to the development machine. If you want the other developers working on the same project to also have the “pre-commit” hook execution before each commit then you’ll have to push your pre-commit hook in the source code repository.

You can also change the path of git hooks for the repository to somewhere in your project.

Let’s create a git_hooks named folder in the project’s root directory and move the pre-commit git hook file from the .git/hooks folder to git_hooks folder.

Run this command in your project’s root path to set the git hooks path to git_hooks folder.

git config core.hooksPath git_hooks

This way we changed the lookup for the git hooks to a place within our project .You can push the git_hooks folder in the repository now.

Note that the other developers will have to run the above command on their machine once they take the pull having git_hooks folder.

https://www.linkedin.com/in/rahul-rastogi-tech/

--

--

Rahul Rastogi
Rahul Rastogi

Mobile Apps @EX2 solutions, India. Technology enthusiast, Flutter, Android and more.