aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/golangci/revgrep
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2023-12-05 15:10:03 +0100
committerTaras Madan <tarasmadan@google.com>2023-12-06 11:31:44 +0000
commit2ab72b4feef2c97f22f90cfbf9e45a6cfcd08bda (patch)
treea6d19b94b6399fcc00a6cfa430885cd349dd1533 /vendor/github.com/golangci/revgrep
parente08e8f492d31d672cc245944c185f8aadf2ee695 (diff)
vendor: updates
Diffstat (limited to 'vendor/github.com/golangci/revgrep')
-rw-r--r--vendor/github.com/golangci/revgrep/.golangci.yml19
-rw-r--r--vendor/github.com/golangci/revgrep/revgrep.go121
2 files changed, 94 insertions, 46 deletions
diff --git a/vendor/github.com/golangci/revgrep/.golangci.yml b/vendor/github.com/golangci/revgrep/.golangci.yml
index b8ed6204f..02ed5ec84 100644
--- a/vendor/github.com/golangci/revgrep/.golangci.yml
+++ b/vendor/github.com/golangci/revgrep/.golangci.yml
@@ -28,12 +28,20 @@ linters-settings:
linters:
enable-all: true
disable:
- - maligned # Deprecated
- - scopelint # Deprecated
- - golint # Deprecated
- - interfacer # Deprecated
- - exhaustivestruct # Deprecated
+ - deadcode # deprecated
+ - exhaustivestruct # deprecated
+ - golint # deprecated
+ - ifshort # deprecated
+ - interfacer # deprecated
+ - maligned # deprecated
+ - nosnakecase # deprecated
+ - scopelint # deprecated
+ - structcheck # deprecated
+ - varcheck # deprecated
- cyclop # duplicate of gocyclo
+ - sqlclosecheck # not relevant (SQL)
+ - rowserrcheck # not relevant (SQL)
+ - execinquery # not relevant (SQL)
- dupl
- lll
- nestif
@@ -54,6 +62,7 @@ linters:
- nosnakecase
- nonamedreturns
- nilerr
+ - depguard
issues:
exclude-use-default: false
diff --git a/vendor/github.com/golangci/revgrep/revgrep.go b/vendor/github.com/golangci/revgrep/revgrep.go
index 4b990fa04..7796b1c01 100644
--- a/vendor/github.com/golangci/revgrep/revgrep.go
+++ b/vendor/github.com/golangci/revgrep/revgrep.go
@@ -1,3 +1,4 @@
+// Package revgrep filter static analysis tools to only lines changed based on a commit reference.
package revgrep
import (
@@ -17,31 +18,26 @@ import (
// Checker provides APIs to filter static analysis tools to specific commits,
// such as showing only issues since last commit.
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 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 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 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 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 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 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
// Calculated changes for next calls to IsNewIssue
@@ -56,9 +52,7 @@ type Issue struct {
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.
- //
+ // 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.
@@ -135,16 +129,14 @@ func (c *Checker) IsNewIssue(i InputIssue) (hunkPos int, isNew bool) {
return 0, false
}
-// Check scans reader and writes any lines to writer that have been added in
-// Checker.Patch.
+// 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.
+// 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.
+// File paths in reader must be relative to current working directory or absolute.
func (c *Checker) Check(reader io.Reader, writer io.Writer) (issues []Issue, err error) {
returnErr := c.Prepare()
writeAll := returnErr != nil
@@ -265,8 +257,7 @@ func (c *Checker) preparePatch() error {
}
// linesChanges returns a map of file names to line numbers being changed.
-// If key is nil, the file has been recently added, else it contains a slice
-// of positions that have been added.
+// If key is nil, the file has been recently added, else it contains a slice of positions that have been added.
func (c *Checker) linesChanged() map[string][]pos {
type state struct {
file string
@@ -343,17 +334,12 @@ func (c *Checker) linesChanged() map[string][]pos {
return changes
}
-// GitPatch returns a patch from a git repository, if no git repository was
-// 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.
+// 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.
func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) {
- var patch bytes.Buffer
-
// check if git repo exists
if err := exec.Command("git", "status", "--porcelain").Run(); err != nil {
// don't return an error, we assume the error is not repo exists
@@ -377,8 +363,9 @@ func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) {
newFiles = append(newFiles, string(file))
}
+ var patch bytes.Buffer
if revisionFrom != "" {
- cmd := exec.Command("git", "diff", "--color=never", "--relative", revisionFrom)
+ cmd := gitDiff(revisionFrom)
if revisionTo != "" {
cmd.Args = append(cmd.Args, revisionTo)
}
@@ -392,12 +379,12 @@ func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) {
if revisionTo == "" {
return &patch, newFiles, nil
}
+
return &patch, nil, nil
}
// make a patch for unstaged changes
- // use --no-prefix to remove b/ given: +++ b/main.go
- cmd := exec.Command("git", "diff", "--color=never", "--relative", "--")
+ cmd := gitDiff("--")
cmd.Stdout = &patch
if err := cmd.Run(); err != nil {
return nil, nil, fmt.Errorf("error executing git diff: %w", err)
@@ -412,7 +399,7 @@ func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) {
// check for changes in recent commit
- cmd = exec.Command("git", "diff", "--color=never", "--relative", "HEAD~", "--")
+ cmd = gitDiff("HEAD~", "--")
cmd.Stdout = &patch
if err := cmd.Run(); err != nil {
return nil, nil, fmt.Errorf("error executing git diff HEAD~: %w", err)
@@ -420,3 +407,55 @@ func GitPatch(revisionFrom, revisionTo string) (io.Reader, []string, error) {
return &patch, nil, nil
}
+
+func gitDiff(extraArgs ...string) *exec.Cmd {
+ cmd := exec.Command("git", "diff", "--color=never", "--no-ext-diff")
+
+ if isSupportedByGit(2, 41, 0) {
+ cmd.Args = append(cmd.Args, "--default-prefix")
+ }
+
+ cmd.Args = append(cmd.Args, "--relative")
+ cmd.Args = append(cmd.Args, extraArgs...)
+
+ return cmd
+}
+
+func isSupportedByGit(major, minor, patch int) bool {
+ output, err := exec.Command("git", "version").CombinedOutput()
+ if err != nil {
+ return false
+ }
+
+ parts := bytes.Split(bytes.TrimSpace(output), []byte(" "))
+ if len(parts) < 3 {
+ return false
+ }
+
+ v := string(parts[2])
+ if v == "" {
+ return false
+ }
+
+ vp := regexp.MustCompile(`^(\d+)\.(\d+)(?:\.(\d+))?.*$`).FindStringSubmatch(v)
+ if len(vp) < 4 {
+ return false
+ }
+
+ currentMajor, err := strconv.Atoi(vp[1])
+ if err != nil {
+ return false
+ }
+
+ currentMinor, err := strconv.Atoi(vp[2])
+ if err != nil {
+ return false
+ }
+
+ currentPatch, err := strconv.Atoi(vp[3])
+ if err != nil {
+ return false
+ }
+
+ return currentMajor*1_000_000_000+currentMinor*1_000_000+currentPatch*1_000 >= major*1_000_000_000+minor*1_000_000+patch*1_000
+}