Skip to content

Commit

Permalink
feat: add new issues.new-from-merge-base option (#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jan 31, 2025
1 parent dfd79c9 commit 192e98b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,10 @@ issues:
# Default: false
new: true

# Show only new issues created after the best common ancestor (merge-base against HEAD).
# Default: ""
new-from-merge-base: main

# Show only new issues created after git revision `REV`.
# Default: ""
new-from-rev: HEAD
Expand Down
15 changes: 9 additions & 6 deletions docs/src/docs/welcome/faq.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ Or you can create a [GitHub Issue](https://github.com/golangci/golangci-lint/iss

We are sure that every project can easily integrate `golangci-lint`, even the large one.

The idea is to not fix all existing issues. Fix only newly added issue: issues in new code.
To do this setup CI to run `golangci-lint` with option `--new-from-rev=HEAD~1`.
The idea is to not fix all existing issues.
Fix only newly added issue: issues in new code.

Also, take a look at option `--new`, but consider that CI scripts that generate unstaged files will make `--new` only point out issues in those files and not in the last commit.
In that regard `--new-from-rev=HEAD~1` is safer.
To do this setup CI to run `golangci-lint` with options `--new-from-merge-base=main` or `--new-from-rev=HEAD~1`.

Also, take a look at option `--new`,
but consider that CI scripts that generate unstaged files will make `--new` only point out issues in those files and not in the last commit.
In that regard `--new-from-merge-base=main` or `--new-from-rev=HEAD~1` are safer.

By doing this you won't create new issues in your code and can choose fix existing issues (or not).

## Why `--new-from-rev` or `--new-from-patch` don't seem to be working in some cases?
## Why `--new-from-xxx` don't seem to be working in some cases?

The options `--new-from-rev` and `--new-from-patch` work by comparing `git diff` output and issues.
The options `--new-from-merge-base`, `--new-from-rev`, and `--new-from-patch` work by comparing `git diff` output and issues.

If an issue is not reported as the same line as the changes then the issue will be skipped.
This is the line of the issue is not inside the lines changed.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ require (
github.com/golangci/gofmt v0.0.0-20250106114630-d62b90e6713d
github.com/golangci/misspell v0.6.0
github.com/golangci/plugin-module-register v0.1.1
github.com/golangci/revgrep v0.7.0
github.com/golangci/revgrep v0.8.0
github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed
github.com/gordonklaus/ineffassign v0.1.0
github.com/gostaticanalysis/forcetypeassert v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4081,6 +4081,10 @@
"type": "boolean",
"default": false
},
"new-from-merge-base": {
"description": "Show only new issues created after the best common ancestor (merge-base against HEAD).",
"type": "string"
},
"new-from-rev": {
"description": "Show only new issues created after this git revision.",
"type": "string"
Expand Down
1 change: 1 addition & 0 deletions pkg/config/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type Issues struct {
UniqByLine bool `mapstructure:"uniq-by-line"`

DiffFromRevision string `mapstructure:"new-from-rev"`
DiffFromMergeBase string `mapstructure:"new-from-merge-base"`
DiffPatchFilePath string `mapstructure:"new-from-patch"`
WholeFiles bool `mapstructure:"whole-files"`
Diff bool `mapstructure:"new"`
Expand Down
5 changes: 4 additions & 1 deletion pkg/result/processors/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ Processor = (*Diff)(nil)
type Diff struct {
onlyNew bool
fromRev string
fromMergeBase string
patchFilePath string
wholeFiles bool
patch string
Expand All @@ -36,6 +37,7 @@ func NewDiff(cfg *config.Issues) *Diff {
return &Diff{
onlyNew: cfg.Diff,
fromRev: cfg.DiffFromRevision,
fromMergeBase: cfg.DiffFromMergeBase,
patchFilePath: cfg.DiffPatchFilePath,
wholeFiles: cfg.WholeFiles,
patch: os.Getenv(envGolangciDiffProcessorPatch),
Expand All @@ -47,7 +49,7 @@ func (*Diff) Name() string {
}

func (p *Diff) Process(issues []result.Issue) ([]result.Issue, error) {
if !p.onlyNew && p.fromRev == "" && p.patchFilePath == "" && p.patch == "" {
if !p.onlyNew && p.fromRev == "" && p.fromMergeBase == "" && p.patchFilePath == "" && p.patch == "" {
return issues, nil
}

Expand All @@ -68,6 +70,7 @@ func (p *Diff) Process(issues []result.Issue) ([]result.Issue, error) {
checker := revgrep.Checker{
Patch: patchReader,
RevisionFrom: p.fromRev,
MergeBase: p.fromMergeBase,
WholeFiles: p.wholeFiles,
}

Expand Down

0 comments on commit 192e98b

Please sign in to comment.