GitHoox.Hook behaviour (GitHoox v0.1.0)

Copy Markdown View Source

Behaviour for git_hoox hooks.

Implement run/2 to execute logic. Optionally implement default_opts/0 to supply per-hook defaults (e.g. stage_fixed: true for formatters).

Example

defmodule MyApp.Hooks.Sobelow do
  @behaviour GitHoox.Hook

  @impl true
  def default_opts, do: [files: ~w(lib/**/*.ex)]

  @impl true
  def run([], _opts), do: :ok
  def run(files, _opts) do
    case System.cmd("mix", ["sobelow", "--exit" | files], stderr_to_stdout: true) do
      {_, 0} -> :ok
      {out, code} -> {:error, {code, out}}
    end
  end
end

Summary

Types

Files matched by hook's :files glob, after staging filter.

Hook options merged from default_opts/0 and user config.

Callbacks

Per-hook default options. Merged with user config; user wins on conflict.

Run hook against files.

Types

files()

@type files() :: [GitHoox.path()]

Files matched by hook's :files glob, after staging filter.

opts()

@type opts() :: keyword()

Hook options merged from default_opts/0 and user config.

Callbacks

default_opts()

(optional)
@callback default_opts() :: keyword()

Per-hook default options. Merged with user config; user wins on conflict.

run(files, opts)

@callback run(files(), opts()) :: GitHoox.hook_result()

Run hook against files.

Return :ok for read-only success. Return {:ok, modified} listing files the hook mutated — runner re-stages them if stage_fixed: true.