Git.Search (git v0.4.0)

Copy Markdown View Source

Higher-level search helpers that compose lower-level Git functions.

Provides a unified interface for searching repository content, commit messages, change history, and tracked file names.

All functions accept an optional keyword list. Use :config to specify the repository via a Git.Config struct; when omitted a default config is built from the environment.

Summary

Functions

Searches commit messages for a query string.

Finds tracked files matching a glob pattern.

Searches tracked files for lines matching a pattern.

Finds commits that introduced or removed a string (pickaxe search).

Functions

commits(query, opts \\ [])

@spec commits(
  String.t(),
  keyword()
) :: {:ok, [Git.Commit.t()]} | {:error, term()}

Searches commit messages for a query string.

Uses git log --grep=<query> under the hood.

Options

  • :config - a Git.Config struct
  • :author - filter by author
  • :since - commits after this date
  • :until_date - commits before this date
  • :max_count - limit number of results
  • :all_match - require all patterns to match (--all-match)

Examples

{:ok, commits} = Git.Search.commits("fix bug")
{:ok, commits} = Git.Search.commits("feat", author: "Alice")

files(pattern, opts \\ [])

@spec files(
  String.t(),
  keyword()
) :: {:ok, [String.t()]} | {:error, term()}

Finds tracked files matching a glob pattern.

Uses Git.ls_files/1 with the pattern passed as a path filter.

Options

Examples

{:ok, files} = Git.Search.files("*.ex")
{:ok, files} = Git.Search.files("lib/**/*.ex")

grep(pattern, opts \\ [])

@spec grep(
  String.t(),
  keyword()
) :: {:ok, [Git.GrepResult.t()]} | {:error, term()}

Searches tracked files for lines matching a pattern.

Wraps Git.grep/2 with a consistent interface.

Options

Accepts all options supported by Git.grep/2.

Examples

{:ok, results} = Git.Search.grep("defmodule")
{:ok, results} = Git.Search.grep("TODO", ignore_case: true)

pickaxe(pattern, opts \\ [])

@spec pickaxe(
  String.t(),
  keyword()
) :: {:ok, [Git.Commit.t()]} | {:error, term()}

Finds commits that introduced or removed a string (pickaxe search).

Uses git log -S<pattern> by default, or git log -G<pattern> when :regex is true.

Options

  • :config - a Git.Config struct
  • :regex - use -G (regex) instead of -S (string match)
  • :author - filter by author
  • :since - commits after this date
  • :until_date - commits before this date
  • :max_count - limit number of results

Examples

{:ok, commits} = Git.Search.pickaxe("my_function")
{:ok, commits} = Git.Search.pickaxe("def \w+", regex: true)