Skip to content

Commit 4a3e67d

Browse files
authored
Merge pull request #1498 from cloudflare/blame
Check content for changes when running git blame
2 parents 7a33d66 + 66d2068 commit 4a3e67d

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

cmd/pint/tests/0069_bitbucket_unmodified.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,22 @@ annotations:
147147
link: https://cloudflare.github.io/pint/checks/alerts/for.html
148148
line: 3
149149
- path: rules.yml
150-
message: "alerts/comparison: always firing alert"
150+
message: "Problem reported on unmodified line 5, annotation moved here: alerts/comparison: always firing alert"
151151
severity: LOW
152152
type: CODE_SMELL
153153
link: https://cloudflare.github.io/pint/checks/alerts/comparison.html
154-
line: 5
154+
line: 4
155155
- path: rules.yml
156-
message: "promql/regexp: redundant regexp"
156+
message: "Problem reported on unmodified line 5, annotation moved here: promql/regexp: redundant regexp"
157157
severity: LOW
158158
type: CODE_SMELL
159159
link: https://cloudflare.github.io/pint/checks/promql/regexp.html
160-
line: 5
160+
line: 4
161161
- path: rules.yml
162-
message: "alerts/for: redundant field with default value"
162+
message: "Problem reported on unmodified line 6, annotation moved here: alerts/for: redundant field with default value"
163163
severity: LOW
164164
type: CODE_SMELL
165165
link: https://cloudflare.github.io/pint/checks/alerts/for.html
166-
line: 6
166+
line: 4
167167
--- END ---
168168

cmd/pint/tests/0123_ci_owner_allowed.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,6 @@ annotations:
148148
severity: MEDIUM
149149
type: BUG
150150
link: https://cloudflare.github.io/pint/checks/rule/owner.html
151-
line: 8
151+
line: 7
152152
--- END ---
153153

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fixed incorrect incorrect suggestions generated by [query/cost](checks/query/cost.md) check.
8+
- Fixed unmodified lines incorrectly marked as modified when running `pint ci`.
89

910
## v0.74.4
1011

internal/discovery/git_branch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ groups:
867867
Name: "rules.yml",
868868
SymlinkTarget: "rules.yml",
869869
},
870-
ModifiedLines: []int{8, 9},
870+
ModifiedLines: []int{8},
871871
Rule: mustParse(7, "- alert: rule2\n expr: sum(foo) by(job)\n"),
872872
},
873873
},
@@ -1080,7 +1080,7 @@ groups:
10801080
Name: "rules.yml",
10811081
SymlinkTarget: "rules.yml",
10821082
},
1083-
ModifiedLines: []int{3, 11, 12, 13, 14, 15, 16},
1083+
ModifiedLines: []int{3, 11, 12, 15, 16},
10841084
PathError: parser.ParseError{
10851085
Line: 11,
10861086
Err: errors.New("could not find expected ':'"),

internal/git/changes.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func Changes(cmd CommandRunner, baseBranch string, filter PathFilter) ([]*FileCh
204204
slog.Debug("File was turned into a symlink", slog.String("path", change.Path.After.Name))
205205
change.Body.ModifiedLines = CountLines(change.Body.After)
206206
case change.Path.Before.Type != Missing && change.Path.After.Type != Missing && change.Path.After.Type != Symlink:
207-
change.Body.ModifiedLines, err = getModifiedLines(cmd, change.Commits, change.Path.After.EffectivePath(), lastCommit)
207+
change.Body.ModifiedLines, err = getModifiedLines(cmd, change.Commits, change.Path.After.EffectivePath(), lastCommit, change.Body.Before, change.Body.After)
208208
if err != nil {
209209
return nil, fmt.Errorf("failed to run git blame for %s: %w", change.Path.After.EffectivePath(), err)
210210
}
@@ -261,7 +261,7 @@ func getChangeByPath(changes []*FileChange, fpath string) *FileChange {
261261
return nil
262262
}
263263

264-
func getModifiedLines(cmd CommandRunner, commits []string, fpath, atCommit string) ([]int, error) {
264+
func getModifiedLines(cmd CommandRunner, commits []string, fpath, atCommit string, bodyBefore, bodyAfter []byte) ([]int, error) {
265265
slog.Debug("Getting list of modified lines",
266266
slog.Any("commits", commits),
267267
slog.String("path", fpath),
@@ -271,11 +271,21 @@ func getModifiedLines(cmd CommandRunner, commits []string, fpath, atCommit strin
271271
return nil, err
272272
}
273273

274+
linesBefore := bytes.Split(bodyBefore, []byte("\n"))
275+
linesAfter := bytes.Split(bodyAfter, []byte("\n"))
276+
274277
modLines := make([]int, 0, len(lines))
275278
for _, line := range lines {
276279
if !slices.Contains(commits, line.Commit) && line.Line == line.PrevLine {
277280
continue
278281
}
282+
283+
if line.PrevLine <= len(linesBefore) && line.Line <= len(linesAfter) {
284+
if bytes.Equal(linesBefore[line.PrevLine-1], linesAfter[line.Line-1]) {
285+
continue
286+
}
287+
}
288+
279289
modLines = append(modLines, line.Line)
280290
}
281291
slog.Debug("List of modified lines",

internal/git/changes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestChanges(t *testing.T) {
188188
Body: git.BodyDiff{
189189
Before: []byte("foo"),
190190
After: []byte("foo"),
191-
ModifiedLines: []int{1},
191+
ModifiedLines: []int{},
192192
},
193193
},
194194
},
@@ -583,7 +583,7 @@ func TestChanges(t *testing.T) {
583583
Body: git.BodyDiff{
584584
Before: []byte("l1\nl2\nl3\n"),
585585
After: []byte("l1\nl3\n"),
586-
ModifiedLines: []int{2},
586+
ModifiedLines: []int{1, 2},
587587
},
588588
},
589589
},

0 commit comments

Comments
 (0)