LinuxCommandLibrary

grepdiff

Search patches for matching lines

SYNOPSIS

grepdiff [options] <EXPRESSION> [DIFF_FILE]

PARAMETERS

-i, --ignore-case
    Ignore case distinctions in the EXPRESSION.

-v, --invert-match
    Select non-matching lines instead of matching lines.

-n, --line-number
    Prefix each output line with the 1-based line number within its respective file.

-c, --count
    Suppress normal output; instead print a count of matching lines for each input file.

-l, --files-with-matches
    Suppress normal output; instead print the name of each input file from which output would normally have been produced.

-E, --extended-regexp
    Interpret EXPRESSION as an extended regular expression (ERE).

-F, --fixed-strings
    Interpret EXPRESSION as a list of fixed strings, separated by newlines, any of which is to be matched.

-p NUM, --strip=NUM
    Strip NUM leading path components from file names mentioned in the diff, similar to patch -p.

--color[=WHEN]
    Highlight matches. WHEN can be 'always', 'never', or 'auto'.

-h, --no-filename
    Suppress the prefixing of file names on output when there is more than one file matched.

DESCRIPTION

grepdiff is a specialized command-line utility designed to search for a specified regular expression pattern exclusively within the lines and files affected by a diff (patch) output. Instead of performing a general search across an entire filesystem or directory, grepdiff intelligently parses a diff stream (from standard input or a file) to identify the modified or new content.

It then applies grep only to these targeted sections, effectively allowing users to find specific patterns within recent changes or proposed modifications. This command is exceptionally valuable for code reviews, debugging, and analyzing patches, as it helps narrow down searches to relevant code. It processes diff outputs typically generated by tools like diff -u or git diff, providing a powerful way to inspect the evolution of codebases.

CAVEATS

grepdiff processes the output of a diff tool, not the files directly. It operates on the *new* state of files as represented by the diff, not the original or intermediate states. Its effectiveness relies on the diff format being correctly parsed (typically unified or context diffs).

INPUT HANDLING

grepdiff expects its input to be a diff (patch) file. This input can be provided as an argument to the command or, more commonly, piped directly from the standard output of a diff-generating command (e.g., diff -u file1 file2 | grepdiff 'pattern' or git diff | grepdiff 'pattern'). It then intelligently parses this diff content to identify the files and specific lines that have been changed, added, or deleted.

TARGETED SEARCH EFFICIENCY

The primary advantage of grepdiff over a recursive grep (e.g., grep -r 'pattern' .) is its highly targeted approach. Instead of searching entire directories, it focuses its search solely on the content that has been modified or newly introduced within a given diff. This significantly improves search efficiency, especially in large projects or when reviewing extensive changes, as it avoids unnecessary processing of unchanged files or irrelevant parts of code.

HISTORY

grepdiff is part of the patchutils collection, a suite of tools designed to facilitate working with patches and diffs in Unix-like environments. It was developed to extend the functionality of standard utilities like grep and diff, providing more sophisticated methods for analyzing and manipulating code changes. Its existence addresses the need for targeted searching specifically within the context of source code modifications, complementing version control workflows.

SEE ALSO

grep(1), diff(1), patch(1), git-diff(1)

Copied to clipboard