Git.Command behaviour (git v0.6.1)

Copy Markdown View Source

Behaviour and runner for git commands.

Modules implementing this behaviour define how to build argument lists for a specific git subcommand and how to parse the resulting output.

Summary

Callbacks

Returns the argument list for this command.

Optional. Returns data to write to the command's stdin, or nil for none.

Parses the stdout and exit code from the git process into a result.

Functions

Runs a git command.

Callbacks

args(command)

@callback args(command :: struct()) :: [String.t()]

Returns the argument list for this command.

input(command)

(optional)
@callback input(command :: struct()) :: iodata() | nil

Optional. Returns data to write to the command's stdin, or nil for none.

Implemented by commands that read stdin (for example git mktree and git update-index --index-info). Stdin is only supported by the default Git.Runner.Forcola runner; under Git.Runner.SystemCmd a command that supplies input gets {:error, :stdin_unsupported}.

parse_output(stdout, exit_code)

@callback parse_output(stdout :: String.t(), exit_code :: non_neg_integer()) ::
  {:ok, term()} | {:error, term()}

Parses the stdout and exit code from the git process into a result.

Functions

run(mod, command, config)

@spec run(module(), struct(), Git.Config.t()) :: {:ok, term()} | {:error, term()}

Runs a git command.

Takes a module implementing the Git.Command behaviour, a command struct, and a Git.Config. Builds the full argument list, executes git through the configured Git.Runner, and delegates parsing to the command module.

Execution is routed through config.runner (see Git.Config and Git.Runner). The default Git.Runner.Forcola runs git in its own process group and kills the group on timeout (leak-free); Git.Runner.SystemCmd is the fallback when forcola is unavailable.

If the command module implements the optional input/1 callback and returns non-nil, that data is written to the command's stdin.

If the command exceeds the configured timeout, returns {:error, :timeout}.

Examples

Git.Command.run(Git.Commands.Status, %Git.Commands.Status{}, config)