crook

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2021 License: MIT Imports: 12 Imported by: 0

README

Crook

A lightweight, single binary git hook management tool.

Logo

Installation

Build the binary
go install gitlab.com/JohnAnthony/crook/cmd/crook@latest
Add to your project
crook init

Once this command is run, .crook.yaml and .crook/ will be created. These are your config file and hooks directory. You will also be informed of any further setup required by the output of this command - usually that you need to run git config core.hooksPath .crook to switch to using Crook hooks.

Config

The config file is in YAML, and allows you to define tasks to run for different hooks. By default, only the pre-commit hook is installed - see the next section for important information on how to sync installed hooks with those defined in the config file.

tasks:
- name: example task                                # Required
  exec: echo this is not a real command && return 1 # Required
  hook: pre-commit                                  # Optional
  glob: '*'                                         # Optional
  dir: '.'                                          # Optional
  replace: '{}'                                     # Optional

name is shown when the task is run
exec is the command to run when the hook is triggered
hook is the git hook that should trigger the command; see the section on syncing below for adding or removing hooks to match the config file
glob has different behaviour depending upon the hook; see the Hooks section below
dir is the directory in which to run the command
replace is the pattern to replace in the exec command, and injects different values depending upon the hook; see the Hooks section below

Sync / using hooks other than pre-commit

By default, only the pre-commit hook is installed in .crook/. If .crook.yaml is changed to include new hooks then running the following command will cause Crook to match up installed hooks with those used in crook.yaml

crook sync
Examples

Run golangci-lint on commits that contain .go files, fixing any issues that can be automatically fixed

tasks:
- name: lint
  exec: golangci-lint run --fix
  glob: '*.go'

Run eslint and prettier on all changed files of a suitable type, fixing any issues that can be automatically fixed

tasks:
- name: eslint
  exec: ./node_modules/.bin/eslint --fix {}
  glob: '*.{ts,tsx}'
- name: prettier
  exec: ./node_modules/.bin/prettier --write {}
  glob: '*.{ts,tsx,js,jsx,md,json}'

Hooks

pre-commit

In this hook's script, {} will be replaced with a list of files that have been modified.

Using glob in a pre-commit task will cause the list of files to be filtered to only those which match the glob. If all are filtered, exec is not run.

commit-msg

In this task's exec, {} will be replaced with the path to the commit message file.

Using glob in a commit-msg task will cause exec to only be run if the commit message matches the glob provided.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTaskName    = errors.New("Invalid task name")
	ErrInvalidTaskExec    = errors.New("Invalid task exec")
	ErrInvalidTaskHook    = errors.New("Invalid task hook")
	ErrInvalidTaskReplace = errors.New("Invalid task replace")
)

Functions

func GitRoot

func GitRoot() (string, error)

Types

type Config

type Config struct {
	HooksPath string
	Tasks     map[GitHook][]Task
}

func DefaultConfig

func DefaultConfig() Config

func LoadConfigYAML

func LoadConfigYAML(path string) (Config, error)

type Crook

type Crook struct {
	// contains filtered or unexported fields
}

func New

func New(cfg *Config) (Crook, error)

func (*Crook) Exec

func (c *Crook) Exec(hook GitHook, allFiles []string) error

func (*Crook) Sync

func (c *Crook) Sync() error

type GitHook

type GitHook string
const (
	GitHookApplyPatchMsg    GitHook = "applypatch-msg"
	GitHookCommitMsg        GitHook = "commit-msg"
	GitHookPostApplyPatch   GitHook = "post-applypatch"
	GitHookPostCheckout     GitHook = "post-checkout"
	GitHookPostCommit       GitHook = "post-commit"
	GitHookPostMerge        GitHook = "post-merge"
	GitHookPostReceive      GitHook = "post-receive"
	GitHookPostRewrite      GitHook = "post-rewrite"
	GitHookPostUpdate       GitHook = "post-update"
	GitHookPreApplyPatch    GitHook = "pre-applypatch"
	GitHookPreAutoGC        GitHook = "pre-auto-gc"
	GitHookPreCommit        GitHook = "pre-commit"
	GitHookPrePush          GitHook = "pre-push"
	GitHookPreRebase        GitHook = "pre-rebase"
	GitHookPreReceive       GitHook = "pre-receive"
	GitHookPrepareCommitMsg GitHook = "prepare-commit-msg"
	GitHookUpdate           GitHook = "update"
)

func (GitHook) IsValid

func (gs GitHook) IsValid() bool

type Task

type Task struct {
	Name    string
	Dir     string
	Exec    string
	Glob    glob.Glob
	Hook    GitHook
	Replace string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL