The original link

preface

With the release of Flutter 2.0, more and more companies and individuals are trying to use Flutter to write high-performance, cross-platform, and consistently rendered apps. Of course, while pursuing efficiency, don’t forget to add more stringent restrictions to your code to ensure quality. Today I recommend a plugin for submitting code to Github or GitLab that uses the DART language locally to limit the quality of the submitted code.

Git Hooks

Git Hooks are Hooks that are used in the system. What it does in Git is to run a script that completes an event when you run a Git command to manipulate a repository or add a project file. If it fails, the event is aborted. Here are all the hooks officially defined for those interested. For example, we create a new pre-commit file under.git/hooks/ and type the following

#! /bin/shEcho 'file is submitting' exit(-1)Copy the code

Then we execute it on the command line

Git add test git commitCopy the code

Then we will see on the command line that the file is being committed, and the file is not finally committed to the local repository because I exited above.

This way, we can write some validation in this file, such as using dartanalyzer to see if the local project syntax has syntax problems.

git_hooks

This is a Dart command line plug-in and a Dart plug-in. We see that the shell script used above to write validation has some problems:

  • Each person’s.gitFolders are not versioned, so a person pulls the code for the first time in.git/hooksThere are no hooks files running under this folder
  • Shell scripts are difficult to write and difficult to get started

The git_hooks library addresses this problem. It does this by generating hooks from the command, and then checking with DART before committing or committing as we commit with git. It allows us programmers to ignore the hooks file and operate on all hooks in dart code.

Let’s make a term here that we can understand

Dart hook file: Dart file generated in your project that uses the DART language to operate on the hooks file

You can install it by using the following command. Make sure your DART is installed and environment variables are configured before you install it. Make sure your DART version is greater than 2.1.0

Run the dart –version command to view the DART version

Then run pub global activate git_hooks to install git_hooks globally

Create the hooks project file

When we open a project with git version management (see the.git folder in the project)

Add to the pubspec.yaml file in your project

dev_dependencies:
  git_hooks: ^ 0.1.5
Copy the code

The commands to create the hooks file and dart hook file are:

git_hooks create {{targetFileName}}
Copy the code
  • TargetFileName refers toDart hook fileIt can be anywhere in the project, such as in the root directorygit_hooks.dartFile (which is also the file created by default if this parameter is not passed)

So let’s do:

git_hooks create git_hooks.dart
Copy the code

If the output

create files... All files wrote successful! 0.2 sCopy the code

That means we’ve created it. Check to see if there are multiple files in the.git/hooks file such as pre-commit,pre-push, etc. Check to see if the git_links.dart file has been generated in the root directory

Open the git_links.dart file and you’ll see the following

import 'package:git_hooks/git_hooks.dart';
// import 'dart:io';

void main(List arguments) {
  // ignore: omit_local_variable_types
  Map<Git, UserBackFun> params = {
    Git.commitMsg: commitMsg,
    Git.preCommit: preCommit
  };
  GitHooks.call(arguments, params);
}

Future<bool> commitMsg() async {
  // String rootDir = Directory.current.path;
  // String commitMsg = Utils.getCommitEditMsg();
  // if (commitMsg.startsWith('fix:')) {
  // return true; // you can return true let commit go
  // } else
  // return false;
  return true;
}

Future<bool> preCommit() async {
  // try {
  // ProcessResult result = await Process.run('dartanalyzer', ['bin']);
  // print(result.stdout);
  // if (result.exitCode ! = 0) return false;
  // } catch (e) {
  // return false;
  // }
  return true;
}
Copy the code

Explanation:

  • Params under main is a Map of the set of hooks we want to customize, each corresponding to the specific function below
  • Githooks. call is written in a fixed way, and the library internally executes the hook function and validates it
  • The commitMsg function is an example that is executed when the Git commit-msg script is executed, and if false is returned, the git operation is terminated

Let’s try to open comments

Future<bool> commitMsg() async {
  var commitMsg = Utils.getCommitEditMsg();
  if (commitMsg.startsWith('fix:')) {
    return true; // you can return true let commit go
  } else  {
    print('you should add `fix` in the commit message');
    return false; }}Copy the code

Then execute from the command line of the project path:

Git add git_hooks.Copy the code

The script output is as follows

you should add `fix` in the commit message
Copy the code

And the file is not submitted to the local repository.

And you’re done!

Delete allHooks file

When we get stuck with a hook we wrote and don’t want to use this validation, we have two ways to delete the hook

  1. delete.git/hooksFolder, which is the hooks folder instead of.gitfolder
  2. Through the commandgit_hooks uninstallTo delete

Enumerations of other hooks

You can add more hook operations through the enumeration below

enum Git {
  applypatchMsg,
  preApplypatch,
  postApplypatch,
  preCommit,
  prepareCommitMsg,
  commitMsg,
  postCommit,
  preRebase,
  postCheckout,
  postMerge,
  prePush,
  preReceive,
  update,
  postReceive,
  postUpdate,
  pushToCheckout,
  preAutoGc,
  postRewrite,
  sendemailValidate
}
Copy the code

For details, see the official Git documentation

conclusion

Above is my example, of course we can do a lot more with this tool, such as using dartanalyzer to check code quality, using dartfmt to check code file format alignment, and so on.

Pub Warehouse address

Making the address

Git hooks official documentation