Git.Commands.Grep (git v0.4.0)

Copy Markdown View Source

Implements the Git.Command behaviour for git grep.

Searches tracked files in a git repository for lines matching a pattern. Supports various matching modes, output formats, and context options.

Summary

Functions

Returns the argument list for git grep.

Parses the output of git grep.

Types

t()

@type t() :: %Git.Commands.Grep{
  after_context: non_neg_integer() | nil,
  all_match: boolean(),
  before_context: non_neg_integer() | nil,
  break: boolean(),
  context: non_neg_integer() | nil,
  count: boolean(),
  extended_regexp: boolean(),
  files_with_matches: boolean(),
  files_without_match: boolean(),
  fixed_strings: boolean(),
  heading: boolean(),
  ignore_case: boolean(),
  invert_match: boolean(),
  line_number: boolean(),
  max_count: non_neg_integer() | nil,
  no_index: boolean(),
  paths: [String.t()],
  pattern: String.t(),
  perl_regexp: boolean(),
  quiet: boolean(),
  recurse_submodules: boolean(),
  ref: String.t() | nil,
  show_function: boolean(),
  untracked: boolean(),
  word_regexp: boolean()
}

Functions

args(command)

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

Returns the argument list for git grep.

Examples

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello"})
["grep", "-n", "hello"]

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello", ignore_case: true})
["grep", "-n", "-i", "hello"]

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello", files_with_matches: true})
["grep", "-l", "hello"]

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello", count: true})
["grep", "-c", "hello"]

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello", ref: "HEAD"})
["grep", "-n", "hello", "HEAD"]

iex> Git.Commands.Grep.args(%Git.Commands.Grep{pattern: "hello", paths: ["lib/"]})
["grep", "-n", "hello", "--", "lib/"]

parse_output(stdout, exit_code)

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

Parses the output of git grep.

On success (exit code 0), parses results based on the command mode. Exit code 1 means no matches were found and returns {:ok, []}. Other exit codes return {:error, {stdout, exit_code}}.