gitgrep

package
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gitgrep provides fast, reusable search over a git repository's working tree, index, a single revision, or history. It shells out to the native git CLI (git grep, the pickaxe, and streamed patches), which is the fastest and most portable approach and matches the rest of gogit.

gitgrep is deliberately generic and free of any domain policy: callers pass patterns in and receive matches out. It defines no term lists, no severities, and no notion of what a match means. Callers own that, and callers are responsible for redacting Match.Text / Patch.Hunk, which are returned verbatim and may contain the very content being searched for.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamPatches

func StreamPatches(ctx context.Context, repoPath, rng string, fn func(Patch) error) error

StreamPatches walks `git log -p` over rng and invokes fn once per file diff, with commit context. rng may be a range ("base..head"), a single rev, "--all", or "" (the default log from HEAD). If fn returns a non-nil error the walk stops and that error is returned.

Exhaustive history walks (e.g. "--all" on a large repo) can produce large output; prefer a bounded range where possible.

Types

type HistoryMatch

type HistoryMatch struct {
	Commit  string `json:"commit"`
	Author  string `json:"author"`
	Date    string `json:"date"`
	Path    string `json:"path"`
	Pattern string `json:"pattern"`
}

HistoryMatch is a commit whose diff added or removed a pattern, attributed to the specific file whose diff contained it.

func HistoryPickaxe

func HistoryPickaxe(ctx context.Context, repoPath string, opts Options) ([]HistoryMatch, error)

HistoryPickaxe finds commits whose diff adds or removes any pattern (git log -S for fixed strings, -G for regex), and attributes each to the file whose diff contained the pattern. One git log runs per pattern.

type Match

type Match struct {
	// Rev is the commit/tree-ish searched, or "" for the working tree/index.
	Rev  string `json:"rev,omitempty"`
	Path string `json:"path"`
	Line int    `json:"line"`
	// Text is the raw matched line. Callers must redact it if needed.
	Text string `json:"text"`
}

Match is one matching line from GrepTree.

func GrepTree

func GrepTree(ctx context.Context, repoPath string, opts Options) ([]Match, error)

GrepTree searches the working tree, the index (Staged), or a single revision (Rev) using git grep. It reports one Match per matching line. Only tracked content at the chosen tree is searched; brand-new untracked files are not covered (a caller needing those must add a filesystem pass).

Because git grep applies -i/-E/-F globally, patterns are grouped by those flags and one git grep is run per group; results are concatenated in group order.

type Options

type Options struct {
	Patterns []Pattern
	// Pathspecs optionally limits the search to matching paths.
	Pathspecs []string
	// SkipBinary skips binary files (git grep -I). Recommended true.
	SkipBinary bool

	// Rev selects what GrepTree searches: "" searches the working tree,
	// otherwise a commit/tree-ish is searched. Ignored by history calls.
	Rev string
	// Staged searches the index (git grep --cached) instead of the working
	// tree. Mutually exclusive with Rev. Ignored by history calls.
	Staged bool
}

Options configures a search. Patterns are OR-combined: a line/commit matches if any pattern matches.

type Patch

type Patch struct {
	Commit string `json:"commit"`
	Author string `json:"author"`
	Date   string `json:"date"`
	Path   string `json:"path"`
	// Hunk is the raw diff body for this file (from the first @@ onward, or
	// the whole file section when no @@ is present). It is returned verbatim
	// and may contain searched-for content; callers must redact if needed.
	Hunk string `json:"hunk"`
}

Patch is one file's diff within one commit.

type Pattern

type Pattern struct {
	// Value is a literal string when Regex is false, or a POSIX extended
	// regular expression (git grep -E / git log -G) when Regex is true.
	Value string
	// Regex selects extended-regex matching (-E) over fixed-string (-F).
	Regex bool
	// IgnoreCase performs case-insensitive matching (-i).
	IgnoreCase bool
}

Pattern is a single search term.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL