Easy Git Hooks for Flutter

Automate your workflow with git hooks for various activities like linting, formatting, testing, etc. within Flutter projects using Lefthook

Ryan Dsilva
Dev Genius

--

Lefthook + Flutter = ❤

As projects grow larger and the codebase is continuously edited by multiple developers at the same time, the need for maintaining code standards and code quality becomes really essential. Of course, that responsibility would lie with the individual developers but this process can be made really efficient and painless using the power of git hooks.

Git hooks are a way of firing off custom scripts/actions before executing a git command — commit, push, etc. There are a lot of git hooks available to use as documented in the official git website, but in this article, we are going to focus on primarily two: pre-push and pre-commit along with how to easily implement them in a Flutter project.

There is a great article¹ by Roberta, that explains how to set up custom git hooks in Flutter projects by modifying the content in the .git folder. The .git folder is a hidden folder at the root of your repository. But there is also another way to implement this, using Lefthook² — a polyglot git hooks manager.

Firstly, we need to install lefthook globally. Based on your environment, there are a few options —

If you have Node.js installed: npm install -g @arkweid/lefthook

If you’re in a Ruby-based environment: gem install lefthook

You may need to run these commands as sudo

That’s it, lefthook is now available for use. Navigate to your project root and run — lefthook install

The lefthook install command has to be run once per project / repository

Lefthook works by using a YAML file that is placed in the project root. So let’s create that file — lefthook.yaml

This file defines which git hooks should be used and which scripts/commands should be run during those hooks. With Flutter, it is very easy to have code that is formatted differently based on the individual developers' preference. To resolve this, we can use the commands available in the Flutter SDK to standardize our code before committing/pushing to version control.

Before committing, we’re going to configure two commands. The first one to lint the dart code using default rules from the Flutter SDK and the second to format the code. The file starts with the name of the hook followed by an array of commands on the next indentation level. I have defined two commands —

pre-commit:
commands:
lint_code:
glob: '*.dart'
run: dart fix lib && git add .
format_code:
glob: '*.dart'
run: flutter format {staged_files} && git add .

Here, lint_code and format_code are the names I have given to the commands. These can be anything. The glob parameter defines which files to run the commands on and there are also special git variables available like {staged_files}. For a detailed reference to this, refer to the lefthook documentation.

The second set of commands are for before we push anything to version control. There’s a general belief in the developer community that you should always test your code before committing anything to version control. Along with that, running a code analysis to find any potential problems is the second task in the queue. Both of these are independent tasks and can be run in parallel and we will do exactly that.

pre-push:
parallel: true
commands:
tests:
run: flutter test
static_code_analysis:
run: flutter analyze

The parallel parameter specifies whether the commands should run in parallel, by default it is set to false which means that the commands will run sequentially.

The whole point of using git hooks is that if any of these commands defined the hooks fails, an error will be thrown exiting the current git operation meaning that the git command will not execute. This is really helpful and ensures that the developers produce good code quality even if they forget at first.

Here is the entire lefthook.yaml file —

Lefthook YAML for Flutter projects

This is a basic setup and can be extended to suit your specific needs. Now whenever you run the git commands — commit/push, lefthook will run your commands and show an output summary if the commands successfully passed or failed. Moreover, lefthook can be used with a variety of other languages and frameworks as it is not developed for one specific language or framework — hence polyglot.

Thank you for spending time reading this article and do share it if you found it useful!

References

[1] Git Hooks Explained using Flutter, by Roberta

[2] Lefthook — The polyglot git hooks manager

--

--

Full Stack TypeScript/JavaScript Developer | Flutter | Deep Learning | Grad Student — Purdue University