git_hooks 0.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

git_hooks #

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

git_hooks can prevent bad git commit,git push and more easy in dart and flutter!

Install #

dev_dependencies:
  git_hooks: ^0.0.1

then

pub get

or

flutter pub get

and then activate it in shell

pub global activate git_hooks

Uninstall #

dev_dependencies:
  - git_hooks: ^0.0.1
pub get

or

flutter pub get

and

pub global deactivate git_hooks

create files in .git/hooks #

We can create git hooks with

git_hooks create

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.

Using #

You can change git_hooks.dart

void main(List arguments) {
  Map<Git, UserBackFun> params = {
    Git.commitMsg: commitMsg,
    Git.preCommit: preCommit
  };
  change(arguments, params);
}

Future<bool> commitMsg() async {
  Directory rootDir = Directory.current;
  File myFile = new File(rootDir.path + "/.git/COMMIT_EDITMSG");
  print("commit message is '${myFile.readAsStringSync()}'");
  print('this is commitMsg');
  return false;
}
Future<bool> preCommit() async {
  print('this is pre-commit');
  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.

You can define own function.For example,I want define a hook what can verify commit message starts with 'fix:'.

git_hooks.dart

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

void main(List arguments) {
  Map<Git, UserBackFun> params = {Git.preCommit: commitMsg};
  change(arguments, params);
}

Future<bool> commitMsg() async {
  Directory rootDir = Directory.current;
  File myFile = new File(uri("${rootDir.path}/.git/COMMIT_EDITMSG"));
  String commitMsg = myFile.readAsStringSync();
  if (commitMsg.startsWith('fix:')) {
    return false;
  } else
    return false;
}

0.0.1 #

  • Initial version

0.0.2 #

  • fix some min-bugs and improve readme.md

0.0.3 #

  • improve readme.md

example/git_hooks.dart

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

void main(List arguments) {
  Map<Git, UserBackFun> params = {Git.commitMsg: commitMsg};
  change(arguments, params);
}

Future<bool> commitMsg() async {
  Directory rootDir = Directory.current;
  File myFile = new File(uri("${rootDir.path}/.git/COMMIT_EDITMSG"));
  String commitMsg = myFile.readAsStringSync();
  if (commitMsg.startsWith('fix:')) {
    return true;
  } else
    return false;
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate git_hooks

2. Use it

The package has the following executables:


$ git_hooks

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  git_hooks: ^0.0.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:git_hooks/git_hooks.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
87
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Feb 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/utils/logging.dart. (-8.63 points)

Analysis of lib/utils/logging.dart reported 18 hints, including:

line 61 col 43: Unnecessary new keyword.

line 66 col 50: Use = to separate a named parameter from its default value.

line 67 col 12: Unnecessary new keyword.

line 96 col 41: Unnecessary new keyword.

line 111 col 14: Unnecessary new keyword.

Fix bin/install/CreateHooks.dart. (-1.99 points)

Analysis of bin/install/CreateHooks.dart reported 4 hints:

line 8 col 21: Unnecessary new keyword.

line 17 col 15: Unnecessary new keyword.

line 22 col 24: Unnecessary new keyword.

line 32 col 23: Unnecessary new keyword.

Fix lib/runtime/run.dart. (-1.49 points)

Analysis of lib/runtime/run.dart reported 3 hints:

line 7 col 21: Unnecessary new keyword.

line 8 col 17: Unnecessary new keyword.

line 21 col 48: The for, if, and spread elements weren't supported until version 2.3.0, but this code is required to be able to run on earlier versions.

Fix bin/git_hooks.dart. (-0.50 points)

Analysis of bin/git_hooks.dart reported 1 hint:

line 18 col 18: Unnecessary new keyword.

Fix bin/install/hookTemplate.dart. (-0.50 points)

Analysis of bin/install/hookTemplate.dart reported 1 hint:

line 50 col 12: Unnecessary new keyword.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
path ^1.6.0 1.6.4
yaml ^2.1.0 2.2.0
Transitive dependencies
charcode 1.1.3
collection 1.14.12
meta 1.1.8
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0