Git.Commands.Clean (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git clean.

Removes untracked files from the working tree. Supports force, dry-run, directory cleaning, ignored file cleaning, exclusion patterns, and quiet mode.

Interactive mode (-i) is intentionally not supported because it requires interactive terminal input which cannot be driven programmatically.

Summary

Functions

Returns the argument list for git clean.

Parses the output of git clean.

Types

t()

@type t() :: %Git.Commands.Clean{
  directories: boolean(),
  dry_run: boolean(),
  exclude: String.t() | nil,
  force: boolean(),
  ignored: boolean(),
  only_ignored: boolean(),
  paths: [String.t()],
  quiet: boolean()
}

Functions

args(command)

@spec args(t()) :: [String.t()]

Returns the argument list for git clean.

Builds the argument list from the struct fields. At least one of :force or :dry_run should be set, as git requires -f for actual cleaning unless clean.requireForce is set to false.

Examples

iex> Git.Commands.Clean.args(%Git.Commands.Clean{dry_run: true})
["clean", "-n"]

iex> Git.Commands.Clean.args(%Git.Commands.Clean{force: true, directories: true})
["clean", "-f", "-d"]

iex> Git.Commands.Clean.args(%Git.Commands.Clean{force: true, ignored: true})
["clean", "-f", "-x"]

iex> Git.Commands.Clean.args(%Git.Commands.Clean{force: true, exclude: "*.log"})
["clean", "-f", "-e", "*.log"]

iex> Git.Commands.Clean.args(%Git.Commands.Clean{force: true, paths: ["src/", "tmp/"]})
["clean", "-f", "--", "src/", "tmp/"]

parse_output(stdout, exit_code)

@spec parse_output(String.t(), non_neg_integer()) ::
  {:ok, [String.t()]} | {:error, {String.t(), non_neg_integer()}}

Parses the output of git clean.

On success (exit code 0), parses the output into a list of file paths that were removed or would be removed. Lines from git clean look like "Removing file.txt" or "Would remove file.txt".

Returns {:ok, [String.t()]} on success or {:error, {stdout, exit_code}} on failure.