revgrep

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: Apache-2.0 Imports: 12 Imported by: 19

README

Overview

revgrep is a CLI tool used to filter static analysis tools to only lines changed based on a commit reference.

Install

go install github.com/golangci/revgrep/cmd/revgrep@latest

Usage

In the scenario below, a change was made causing a warning in go vet on line 5, but go vet will show all warnings. Using revgrep, you can show only warnings for lines of code that have been changed (in this case, hiding line 6).

[user@host dir (master)]$ go vet
main.go:5: missing argument for Sprintf("%s"): format reads arg 1, have only 0 args
main.go:6: missing argument for Sprintf("%s"): format reads arg 1, have only 0 args
[user@host dir (master)]$ go vet |& revgrep
main.go:5: missing argument for Sprintf("%s"): format reads arg 1, have only 0 args

|& is shown above as many static analysis programs write to stderr, not stdout, |& combines both stderr and stdout. It could also be achieved with go vet 2>&1 | revgrep.

revgrep CLI tool will return an exit status of 1 if any issues match, else it will return 0. Consider using ${PIPESTATUS[0]} for the exit status of the go vet command in the above example.

Usage: revgrep [options] [from-rev] [to-rev]

from-rev filters issues to lines changed since (and including) this revision
  to-rev filters issues to lines changed since (and including) this revision, requires <from-rev>

  If no revisions are given, and there are unstaged changes or untracked files, only those changes are shown
  If no revisions are given, and there are no unstaged changes or untracked files, only changes in HEAD~ are shown
  If from-rev is given and to-rev is not, only changes between from-rev and HEAD are shown.

    -d    Show debug output
      -regexp string
              Regexp to match path, line number, optional column number, and message

Other Examples

Issues between branches:

[user@host dir (feature/branch)]$ go vet |& revgrep master

Issues since last push:

[user@host dir (master)]$ go vet |& revgrep origin/master

Documentation

Overview

Package revgrep filter static analysis tools to only lines changed based on a commit reference.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GitPatch

func GitPatch(ctx context.Context, revisionFrom, revisionTo string) (io.Reader, []string, error)

GitPatch returns a patch from a git repository. If no git repository was found and no errors occurred, nil is returned, else an error is returned revisionFrom and revisionTo defines the git diff parameters, if left blank and there are unstaged changes or untracked files, only those will be returned else only check changes since HEAD~. If revisionFrom is set but revisionTo is not, untracked files will be included, to exclude untracked files set revisionTo to HEAD~. It's incorrect to specify revisionTo without a revisionFrom.

Types

type Checker

type Checker struct {
	// Patch file (unified) to read to detect lines being changed,
	// if nil revgrep will attempt to detect the VCS and generate an appropriate patch.
	// Auto-detection will search for uncommitted changes first,
	// if none found, will generate a patch from last committed change.
	// File paths within patches must be relative to current working directory.
	Patch io.Reader
	// NewFiles is a list of file names (with absolute paths) where the entire contents of the file is new.
	NewFiles []string
	// Debug sets the debug writer for additional output.
	Debug io.Writer
	// RevisionFrom check revision starting at, leave blank for auto-detection ignored if patch is set.
	RevisionFrom string
	// WholeFiles indicates that the user wishes to see all issues that comes up anywhere in any file that has been changed in this revision or patch.
	WholeFiles bool
	// RevisionTo checks revision finishing at, leave blank for auto-detection ignored if patch is set.
	RevisionTo string
	// Regexp to match path, line number, optional column number, and message.
	Regexp string
	// AbsPath is used to make an absolute path of an issue's filename to be relative in order to match patch file.
	// If not set, current working directory is used.
	AbsPath string
	// contains filtered or unexported fields
}

Checker provides APIs to filter static analysis tools to specific commits, such as showing only issues since last commit.

func (*Checker) Check

func (c *Checker) Check(ctx context.Context, reader io.Reader, writer io.Writer) (issues []Issue, err error)

Check scans reader and writes any lines to writer that have been added in [Checker.Patch].

Returns the issues written to writer when no error occurs.

If no VCS could be found or other VCS errors occur, all issues are written to writer and an error is returned.

File paths in reader must be relative to current working directory or absolute.

func (*Checker) IsNew added in v0.7.0

func (c *Checker) IsNew(filePath string, line int) (hunkPos int, isNew bool)

IsNew checks whether issue found by linter is new: it was found in changed lines.

WARNING: it requires to call Checker.Prepare before call this method to load the changes from patch.

func (*Checker) IsNewIssue

func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool)

IsNewIssue checks whether issue found by linter is new: it was found in changed lines.

WARNING: it requires to call Checker.Prepare before call this method to load the changes from patch.

func (*Checker) Prepare

func (c *Checker) Prepare(ctx context.Context) error

Prepare extracts a patch and changed lines.

WARNING: it should only be used before an explicit call to Checker.IsNewIssue/Checker.IsNew.

WARNING: only [Checker.Patch], [Checker.RevisionFrom], [Checker.RevisionTo], [Checker.WholeFiles] options are used, the other options ([Checker.Regexp], [Checker.AbsPath]) are only used by Checker.Check.

type InputIssue

type InputIssue interface {
	FilePath() string
	Line() int
}

InputIssue represents issue found by some linter.

type Issue

type Issue struct {
	// File is the name of the file as it appeared from the patch.
	File string
	// LineNo is the line number of the file.
	LineNo int
	// ColNo is the column number or 0 if none could be parsed.
	ColNo int
	// HunkPos is position from file's first @@, for new files this will be the line number.
	// See also: https://developer.github.com/v3/pulls/comments/#create-a-comment
	HunkPos int
	// Issue text as it appeared from the tool.
	Issue string
	// Message is the issue without file name, line number and column number.
	Message string
}

Issue contains metadata about an issue found.

Directories

Path Synopsis
cmd
revgrep
Package main a CLI tool used to filter static analysis tools to only lines changed based on a commit reference.
Package main a CLI tool used to filter static analysis tools to only lines changed based on a commit reference.

Jump to

Keyboard shortcuts

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