Coder Social home page Coder Social logo

git_hooks's Introduction

git_hooks

https://img.shields.io/github/license/xuzhongpeng/git_hooks Pub Star travis

git_hooks can prevent bad git commit,git push and more easy in dart and flutter! It is similar to husky.

Install

dev_dependencies:
  git_hooks: ^1.0.0

then

pub get

or

flutter pub get

create files in .git/hooks

Here has two ways

  1. Using git_hooks command

activate git_hooks in shell

pub global activate git_hooks

Now,we can let the git hooks bring into effect

git_hooks create bin/git_hooks.dart
  1. Using dart code:

create main.dart file in /bin/

void main() async{
  GitHooks.init(targetPath: "bin/flutter_git_hooks.dart");
}

then dart bin/main.dart in shell.

It will create some hooks files in .git/hooks. You can check whether the installation is correct by judging whether the file(".git/hooks/commit-msg" and other fils) exists.

It will create a file git_hooks.dart in your project root directory.

Delete files in .git/hooks

  1. using git_hooks shell command
git_hooks uninstall
  1. using dart codes
void main() async{
  GitHooks.unInstall();
}

Notion

Target File: The file that the git hooks points to. It is /git_hooks.dart as default.

Git hook command file: The Git hooks file. Such as /.git/hooks/commit-msg.

Using

You can change git_hooks.dart

import "package:git_hooks/flutter_git_hooks.dart";
import "dart:io";

void main(List arguments) {
  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;
}

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;
}

If you want interrupt your commit or push,you can return false.Then you can return true if only nothing to do.

add file to git

git add .
git commit -m 'some messages'

If it output following.Congratulations on your successful use of it.

start pre-commit hook...               /
-this is pre-commit

1.1s
start prepare-commit-msg hook...       /
1.1s
start commit-msg hook...               /
\commit message is 'some messages'
this is commitMsg

1.2s

Define our own hook functions

You can use enum Git to Define more hooks functions.

There is all hooks provide

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

You can click here to learn more about git hooks.

Debugging hooks code with VSCode

If you debugging pre-commit hooks.

You can execute dart {{targetFile}} pre-commit.

or add Configuration in VSCode

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "debugger git hooks",
      "program": "flutter_git_hooks.dart",//your targetFile
      "request": "launch",
      "type": "dart",
      "args": ["pre-commit"] // hooks argument
    }
  ]
}

git_hooks's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.