All Projects → qgadrian → elixir_git_hooks

qgadrian / elixir_git_hooks

Licence: MIT License
🪝 Add git hooks to Elixir projects

Programming Languages

elixir
2628 projects
shell
77523 projects

Projects that are alternatives of or similar to elixir git hooks

modular-git-hooks
A tool for organizing git hooks into directories of modular files.
Stars: ✭ 19 (-80.61%)
Mutual labels:  git-hook, git-hooks
hex-color-regex
Regular expression for matching hex color values from string.
Stars: ✭ 29 (-70.41%)
Mutual labels:  hex
graphmath
An Elixir library for performing 2D and 3D mathematics.
Stars: ✭ 72 (-26.53%)
Mutual labels:  hex
HexPatternView
Create Beautiful hex design View.
Stars: ✭ 44 (-55.1%)
Mutual labels:  hex
color
A library of well-tested helper methods for working with colors.
Stars: ✭ 13 (-86.73%)
Mutual labels:  hex
php-git-hooks
Git hooks for the local repository of the PHP project
Stars: ✭ 20 (-79.59%)
Mutual labels:  git-hooks
hexagonTab
Hexagon bookmarks accented with a chosen colour. Customise the layout, style, background and bookmarks with hexagonTab.
Stars: ✭ 65 (-33.67%)
Mutual labels:  hex
enforce-git-message
Enforces conventional git commit messages for git repositories
Stars: ✭ 30 (-69.39%)
Mutual labels:  git-hooks
colors-convert
🦚 A simple colors library
Stars: ✭ 15 (-84.69%)
Mutual labels:  hex
detect-secrets
A developer-friendly secrets detection tool for CI and pre-commit hooks based on Yelp's detect-secrets
Stars: ✭ 43 (-56.12%)
Mutual labels:  git-hooks
gleam compile
Tiny hex package to make the development experience of using gleam in elixir (and especially phoenix projects) better.
Stars: ✭ 29 (-70.41%)
Mutual labels:  hex
agala
Full featured messaging bot framework.
Stars: ✭ 70 (-28.57%)
Mutual labels:  hex
hex.vim
nifty functions for your Elixir Hex dependencies
Stars: ✭ 24 (-75.51%)
Mutual labels:  hex
modern-office-git-diff
An experiment in tracking and diffing versions of modern Microsoft Office files in Git.
Stars: ✭ 51 (-47.96%)
Mutual labels:  git-hooks
rusty-hook
git hook manager, geared toward Rust projects
Stars: ✭ 93 (-5.1%)
Mutual labels:  git-hooks
colorsys
🎨 Minimalistic color converter for RGB, HSV, HSL, CMYK, HEX and CSS strings
Stars: ✭ 53 (-45.92%)
Mutual labels:  hex
husky-elixir
🐶 Git hooks made easy
Stars: ✭ 47 (-52.04%)
Mutual labels:  git-hooks
simple graphql client
SimpleGraphqlClient is a graphql client, focused on simplicity and ease of use.
Stars: ✭ 17 (-82.65%)
Mutual labels:  hex
hexlightning
台灣人自己的黑名單機器人,更多的創意同時打造一個可以被信任的服務。
Stars: ✭ 34 (-65.31%)
Mutual labels:  hex
colorsys.rs
Lib for modifying colors and converting to other spaces
Stars: ✭ 28 (-71.43%)
Mutual labels:  hex

Coverage Status Hex version Hex Docs Build Status Inline docs

GitHooks 🪝

Configure git hooks in your Elixir projects.

Main features:

  • Simplicity: Automatic or manually install the configured git hook actions.
  • Flexibility: You choose what to use to define the git hooks actions:
    • Bash commands
    • Executable files
    • Elixir modules
  • No limits: Any git hook is and will be supported out of the box, you can check here the git hooks list available.

Table of Contents

Installation

Add to dependencies:

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

Then install and compile the dependencies:

mix deps.get && mix deps.compile

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.

Automatic installation

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

See configuration to disable the automatic install.

Manual installation

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

mix git_hooks.install

Configuration

Auto install

To disable the automatic install of the git hooks set the configuration key auto_install to false.

Hook 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.
  • branches: Allow or forbid the hook configuration to run (or not) in certain branches using whitelist or blacklist configuration (see example below). You can use regular expressions to match a branch name.

Git submodules

This library supports git submodules, just add your git_hooks configuration to any of the submodules projects.

Setting a custom git hooks config path is also supported:

git config core.hooksPath .myCustomGithooks/

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

Task types

For more information, check the module documentation for each of the different supported tasks.

Mix task

This is the preferred option to run mix tasks, as it will provide the best execution feedback.

Just add in your config the mix tasks you want to run. You can also set the args to be used by the mix task:

config :git_hooks,
  verbose: true,
  hooks: [
    commit_msg: [
      tasks: [
        {:mix_task, :test},
        {:mix_task, :format, ["--dry-run"]}
      ]
    ]
  ]

By default this library expects by default the following return values from mix tasks:

0
:ok
nil

If you want to support additional success return values from your mix tasks, you can add them by adding the following configuration:

config :git_hooks,
  extra_success_returns: [
    {:noop, []},
    {:ok, []}
  ]

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

Copyright and License

Copyright © 2022 Adrián Quintás

Source code is released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].