lefthook

command module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2019 License: MIT Imports: 1 Imported by: 0

README

Build Status

Hookah

Hookah it`s a simple manager of git hooks.

Sponsored by Evil Martians

asciicast

Installation

Add Hookah to your system or build it from sources.

go
go get github.com/Arkweid/hookah
npm and yarn
npm i @arkweid/hookah-js --save-dev
# or yarn:
yarn add -D @arkweid/hookah-js

# Now you can call it:
npx hookah -h

NOTE: if you install it this way you should call it with npx for all listed examples below.

snap
sudo snap install --devmode hookah
brew
brew install Arkweid/hookah/hookah

Or take it from binaries and install manualy

Scenarios

First time user

Go to your project directory and run:

hookah install

It add for you configuration file hookah.yml with default directories for hooks sources. Now we ready to add hooks! For example we want to add pre commit hooks. Lets do that:

hookah add pre-commit

It will add a hook .git/hooks/pre-commit. So every time when you run git commit this file will be executed. That directories also will be created .hookah and .hookah-local. Use first one for project/team hooks. Second one for you personal hooks. Add it to .gitignore

Next fill the directory .hookah/pre-commit with executables you like

├───.hookah
│   └───pre-commit
│       ├─── fail_script
│       └─── ok_script

Example:

cat > .hookah/pre-commit/fail_script

#!/bin/sh
exit 1

cat > .hookah/pre-commit/ok_script

#!/bin/sh
exit 0

# Now we can commit:
git commit -am "It fail"

Done! Pretty simple, huh?

Complete example

hookah.yml

source_dir: ".hookah"
source_dir_local: ".hookah-local"

pre-commit:
  # Specify additional parameters for script files
  # If your scripts have shebang notation you can skip
  # this section
  scripts:
    "hello.js":
      runner: node
    "any.go":
      runner: go run

  # Not enough speed? Run all of them in parallel!
  # Default: false
  parallel: true

  commands:
    eslint:
      glob: "*.{js,ts}"
      runner: yarn eslint {staged_files}
    rubocop:
      tags: backend style
      glob: "*.{rb}"
      exclude: "application.rb|routes.rb" # simple regexp for more flexibility
      runner: bundle exec rubocop {all_files}
    govet:
      tags: backend style
      files: git ls-files -m # we can explicity define scope of files
      glob: "*.{go}"
      runner: go vet {files} # {files} will be replaced by matched files as arguments

If your team have backend and frontend developers, you can skip unnsecesary hooks this way: hookah-local.yml

pre-commit:
  # I am fronted developer. Skip all this backend stuff!
  exclude_tags:
    - backend

  scripts:
    "any.go":
      runner: docker exec -it --rm <container_id_or_name> {cmd} # Wrap command from hookah.yml in docker
  commands:
    govet:
      skip: true # You can also skip command with this option
I want to run hook groups directly!

No problem, hookah have command for that:

hookah run pre-commit

# You will see the summary:
[ FAIL ] fail_script
[ OK ] ok_script
I want to use my own runner! And I dont want to change team/repository scripts.

Ok! For example you have any.go script. We can run it in this way:

Add hookah-local.yml

Add it to .gitignore. It your personal settings.

Next customize the any.go script:

pre-commit:
  "any.go":
    runner: "go run"

Done! Now our script will be executed like this:

go run any.go
I clone the existed repo which use hookah. How can I setup hooks?

We suppose repo already have the hookah structure. So all of you need it run install:

hookah install

Hookah wiil read existed hook groups and reproduce hooks in .git/hooks directory.

How can I run my linter against only modified files?

No problem. Lets take rubocop linter for ruby as example:

#!/bin/sh

git ls-files -m | xargs rubocop
I dont like bash. Give me working example for golang

Ok-ok! This is how any.go may looks like:

package main

import (
  "fmt"
  "os"
  "os/exec"
  "strings"
  
  "github.com/Arkweid/hookah/context"
)

func main() {
  files, _ := context.StagedFiles()
  files = context.FilterByExt(files, ".rb")

  cmd := exec.Command("rubocop", strings.Join(files, " "))

  outputBytes, err := cmd.CombinedOutput()

  fmt.Println(string(outputBytes))

  if err != nil {
    os.Exit(1)
  }
}

We include context package only for convenience. It`s just few useful functions.

Uninstall
hookah uninstall

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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