PreCommitHook class

The Hook class represents a Git hook configuration that defines a set of tasks to be executed during specific Git hook events. This class allows you to automate checks, validations, or any custom scripts to ensure code quality and consistency across your repository.

Usage

To create a hook, instantiate the Hook class with the desired tasks and optional parameters:

import 'package:hooksman/hooksman.dart';

Hook main() {
  return Hook(
    tasks: [
      ReRegisterHooks(),
      ShellTask(
        name: 'Lint & Format',
        include: [Glob('**.dart')],
        exclude: [Glob('**.g.dart')],
        commands: (filePaths) => [
          'dart analyze --fatal-infos ${filePaths.join(' ')}',
          'dart format ${filePaths.join(' ')}',
        ],
      ),
      ShellTask(
        name: 'Build Runner',
        include: [Glob('lib/models/**.dart')],
        exclude: [Glob('**.g.dart')],
        commands: (filePaths) => [
          'sip run build_runner build',
        ],
      ),
      ShellTask(
        name: 'Tests',
        include: [Glob('**.dart')],
        exclude: [Glob('hooks/**')],
        commands: (filePaths) => [
          'sip test --concurrent --bail',
        ],
      ),
    ],
  );
}

The tasks parameter is a list of tasks to be executed by the hook. Each task can specify file patterns to include or exclude, and the commands or Dart code to run.

The diffArgs parameter allows you to specify how files are compared with the working directory, index, or commit.

The diffFilters parameter allows you to specify the statuses of files to include or exclude, such as added, modified, or deleted.

The backupFiles parameter specifies whether the original files should be backed up before running the hook.

The allowEmpty parameter determines whether the hook should allow empty commits.

Inheritance

Constructors

PreCommitHook.new({required List<HookTask> tasks, String diffFilters = _defaultDiffFilters, List<String> diffArgs = _defaultDiffArgs, bool allowEmpty = false})
const
PreCommitHook.verbose({required List<HookTask> tasks, String diffFilters = _defaultDiffFilters, List<String> diffArgs = _defaultDiffArgs, bool allowEmpty = false})
const

Properties

allowEmpty bool
If true, the hook will exit successfully even if there are no files after the tasks have run
final
diffArgs List<String>
Defaults to '--staged'
finalinherited
diffFilters String
Defaults to 'ACMR'
finalinherited
hashCode int
The hash code for this object.
no setterinherited
props List<Object?>
The list of properties that will be used to determine whether two instances are equal.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shouldRunOnEmpty bool
no setterinherited
stringify bool?
If set to true, the toString method will be overridden to output this instance's props.
no setterinherited
tasks List<HookTask>
finalinherited
verbose bool
finalinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
resolve(List<String> filePaths) ResolvedHook
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited