Skip to content
Add git hooks to Elixir projects
Elixir Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
lib
priv
test
.credo.exs
.formatter.exs
.gitignore
.tool-versions
.travis.yml
CODE_OF_CONDUCT.md
LICENSE
README.md
coveralls.json
mix.exs
mix.lock

README.md

Coverage Status Hex version Hex Docs Build Status Inline docs

GitHooks

Installs git hooks that will run in your Elixir project.

Any git hook type is supported, check here the hooks list.

Table of Contents

Installation

Add to dependencies:

def deps do
  [{:git_hooks, "~> 0.3.2", only: [:test, :dev], runtime: false}]
end

Then install and compile the dependencies:

mix deps.get && mix deps.compile

Backup current hooks

This project will backup automatically your the hook files that are going to be overwrite.

The backup files will have the file extension .pre_git_hooks_backup.

Automatic installation

This library will install automatically the configured git hooks in your config.exs file.

Manual installation

You can manually install the configured git hooks at any time by running:

mix git_hooks.install

Configuration

One or more git hooks can be configured, those hooks will be the ones installed in your git project.

Currently there are supported two configuration options:

  • tasks: A list of the commands that will be executed when running a git hook. See types of tasks for more info.
  • verbose: If true, the output of the mix tasks will be visible. This can be configured globally or per git hook.

Example config

In config/config.exs

if Mix.env() != :prod do
  config :git_hooks,
    verbose: true,
    hooks: [
      pre_commit: [
        tasks: [
          "mix format"
        ]
      ],
      pre_push: [
        verbose: false,
        tasks: [
          "mix dialyzer",
          "mix test",
          "echo 'success!'"
        ]
      ]
    ]
end

Type of tasks

Command

To run a simple command you can either declare a string or a tuple with the command you want to run. For example, having "mix test" and {:cmd, "mix test"} in the hook tasks will be equivalent.

If you want to forward the git hook arguments, add the option include_hook_args: true.

config :git_hooks,
  verbose: true,
  hooks: [
    commit_msg: [
      tasks: [
        {:cmd, "echo 'test'"},
        {:cmd, "elixir ./priv/test_task.ex", include_hook_args: true},
      ]
    ]
  ]

Executable file

The following configuration uses a script file to be run with a git hook. If you want to forward the git hook arguments, add the option include_hook_args: true.

config :git_hooks,
  verbose: true,
  hooks: [
    commit_msg: [
      tasks: [
        {:file, "./priv/test_script"},
        {:file, "./priv/test_script_with_args", include_hook_args: true},
      ]
    ]
  ]

The script file executed will receive the arguments from git, so you can use them as you please.

Removing a hook

When a git hook configuration is removed, the installed hook will automatically delete it.

Any backup done at the moment will still be kept.

Execution

Automatic execution

The configured mix tasks will run automatically for each git hook.

Manual execution

You can also run manually any configured git hook as well.

The following example will run the pre_commit configuration:

mix git_hooks.run pre_commit

It is also possible to run all the configured hooks:

mix git_hooks.run all
You can’t perform that action at this time.