Backup current hooks
This library will backup automatically your current git hooks before overwriting them.
The backup files will have the file extension .pre_git_hooks_backup
.
Configuration
Auto install
To disable the automatic install of the git hooks set the configuration key auto_install
to
false
.
Custom project path
This library assumes a simple Elixir project architecture. This is, an Elixir project in the root of a git repository.
If you have a different project architecture, you can specify the absolute path
of your project using the project_path
configuration:
{project_path, 0} = System.cmd("pwd", [])
project_path = String.replace(project_path, ~r/\n/, "/")
config :git_hooks,
hooks: [
pre_commit: [
tasks: [
{:cmd, "mix format --check-formatted"}
]
]
],
project_path: project_path
Custom mix path
This library expects elixir
to be installed in your system and the mix
binary to be available. If you want to provide a specific path to run the mix
executable, it can be done using the mix_path
configuration.
The following example would run the hooks on a docker container:
config :git_hooks,
auto_install: false,
mix_path: "docker-compose exec mix",
Troubleshooting in docker containers
The mix_path
configuration can be used to run mix hooks on a Docker container.
If you have a TTY error running mix in a Docker container use docker exec --tty $(docker-compose ps -q web) mix
as the mix_path
. See this issue as reference.
Example config
In config/config.exs
use Mix.Config
# somewhere in your config file
if Mix.env() == :dev do
config :git_hooks,
auto_install: true,
verbose: true,
branches: [
whitelist: ["feature-.*"],
blacklist: ["master"]
],
hooks: [
pre_commit: [
tasks: [
{:cmd, "mix format --check-formatted"}
]
],
pre_push: [
verbose: false,
tasks: [
{:cmd, "mix dialyzer"},
{:cmd, "mix test --color"},
{:cmd, "echo 'success!'"}
]
]
]
end
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.
Elixir module
It is also possible to use Elixir modules to execute actions for a given git hook.
Just add in your config the
MFA ({module,
function, arity}
) definition:
config :git_hooks,
verbose: true,
hooks: [
commit_msg: [
tasks: [
{MyModule, :execute, 2}
]
]
]
To check how many args you function should expect check the git documentation to know which parameters are being sent on each hook.
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