Flutter conventional commits—with git hooks and Lefthook 👑

Conventional Commits, Git Hooks, and Lefthook, a trifecta that can transform, maintaining code quality and consistency in your development.

Flutter conventional commits—with git hooks and Lefthook 👑
Conventional Commits in Flutter with git hooks and lefthook

In the ever-evolving world of Flutter app development, maintaining code quality and consistency is paramount. Enter Conventional Commits, Git Hooks, and Lefthook, a trifecta that can transform your development process. Join in this blog post as we explore how these powerful tools can help you standardize your commit messages, automate code quality checks, and supercharge your Flutter projects. Say goodbye to messy commits and hello to a more organized and efficient development experience!”.

Step 1: Installing Lefthook

To get started with Lefthook and enhance your Git workflow, follow simple installation instructions:

lefthook/docs/install.md at master · evilmartians/lefthook
Fast and powerful Git hooks manager for any type of projects. - evilmartians/lefthook

Step 2: Check commit message

Now that you have Lefthook installed and ready to go, let's set up a commit message file written in Dart to ensure consistency in your commit messages. This is especially useful when using Conventional Commits.

  1. In your Flutter project's root directory, create a new file for your Dart commit message script. You can name it something like commit_message.dart.
  2. Open the commit_message.dart file in your preferred code editor.
  3. Write your Dart script to generate commit messages. You can customize this script to suit your project's needs and coding conventions. For example:
import 'dart:io';

void main() {
  final commitMessageFile = File('.git/COMMIT_EDITMSG');

  if (!commitMessageFile.existsSync()) {
    print('Commit message file does not exist.');
    exit(1);
  }

  final commitMessage = commitMessageFile.readAsStringSync();

  if (!isValidCommitMessage(commitMessage)) {
    print('👎 Invalid commit message format.');
    print('Commit message should follow the Conventional Commits format.');
    exit(1);
  }

  print('👍 Valid commit message!');
  exit(0);
}

bool isValidCommitMessage(String commitMessage) {
  final RegExp pattern = RegExp(r'^(feat|fix|docs|chore)(\(.+\))?: .+');
  return pattern.hasMatch(commitMessage);
}

Validation commit message code

Step 3: Adding the Commit Message Script to Your Lefthook Configuration

Now that you have your Dart commit message script ready, it's time to integrate it into your Lefthook configuration so that Lefthook can automatically generate commit messages using your script.

  1. In your project's root directory, locate or create the lefthook.yml file. If it doesn't exist, you can initialize Lefthook by running npx lefthook install in your project directory.
  2. Open this file using your preferred code editor.
  3. In the pre-commit section of your lefthook.yml file, add a call to your Dart commit message script. For example:
commit-msg:
  commands:
  - name: Dart Commit Message Script
    validate:
      run: flutter pub run ./commit_message.dart
💡
Make sure to replace ./commit_message.dart with the correct path to your Dart commit message script if it's located in a different directory.

Save the lefthook.yml file.

Step 4: Test it

If everything was done correctly, you should see the following message during the correct commit message:

Valid commit message

And if the commit message does not follow the conventional commit standard, you should see the following message:

Invalid commit message

Summary

With this setup, Lefthook will execute your Dart commit message script before each commit, allowing you to generate and validate commit messages automatically.

Source code

In case of any problems, take a look at the sample repository with the above configuration:

GitHub - KrzysztofLen/flutter-conventional-commits: Flutter conventional commits—with git hooks and Lefthook 👑
Flutter conventional commits—with git hooks and Lefthook 👑 - GitHub - KrzysztofLen/flutter-conventional-commits: Flutter conventional commits—with git hooks and Lefthook 👑


Thanks for reading ♥️♥️