GitHoox.Hook behaviour (GitHoox v0.4.2)

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.

Hook-specific NimbleOptions schema.

Run hook against files.

Functions

Merge a hook module's default_opts/0 with user-supplied opts.

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.

opts_schema()

(optional)
@callback opts_schema() :: keyword()

Hook-specific NimbleOptions schema.

Used by GitHoox.Config to validate any opts the user passes that are not part of the global schema (:files, :stage_fixed, :timeout, :env). Hooks that do not implement this callback accept arbitrary extra opts without validation.

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.

Functions

merge_defaults(mod, user_opts)

@spec merge_defaults(module(), opts()) :: opts()

Merge a hook module's default_opts/0 with user-supplied opts.

User opts win on conflict. Modules without default_opts/0 contribute an empty list.