Skip to content

Commit a1f5896

Browse files
committed
Don't mark lines from other commits as modified
1 parent 599d47e commit a1f5896

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

internal/discovery/git_branch_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,56 @@ groups:
12701270
},
12711271
},
12721272
},
1273+
{
1274+
title: "rule partially replaced",
1275+
setup: func(t *testing.T) {
1276+
commitFile(t, "rules.yml", `
1277+
groups:
1278+
- name: v1
1279+
rules:
1280+
- record: up:sum
1281+
expr: |
1282+
sum(
1283+
up
1284+
)
1285+
`, "v1")
1286+
1287+
_, err := git.RunGit("checkout", "-b", "v2")
1288+
require.NoError(t, err, "git checkout v2")
1289+
1290+
commitFile(t, "rules.yml", `
1291+
groups:
1292+
- name: v1
1293+
rules:
1294+
- record: up:count
1295+
expr: |
1296+
count(
1297+
up
1298+
)
1299+
`, "v2")
1300+
},
1301+
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, nil), "main", 4, parser.PrometheusSchema, model.UTF8Validation, nil),
1302+
entries: []discovery.Entry{
1303+
{
1304+
State: discovery.Added,
1305+
Path: discovery.Path{
1306+
Name: "rules.yml",
1307+
SymlinkTarget: "rules.yml",
1308+
},
1309+
ModifiedLines: []int{5, 7},
1310+
Rule: mustParse(4, " - record: up:count\n expr: |\n count(\n up\n )\n"),
1311+
},
1312+
{
1313+
State: discovery.Removed,
1314+
Path: discovery.Path{
1315+
Name: "rules.yml",
1316+
SymlinkTarget: "rules.yml",
1317+
},
1318+
ModifiedLines: []int{5, 7},
1319+
Rule: mustParse(4, " - record: up:sum\n expr: |\n sum(\n up\n )\n"),
1320+
},
1321+
},
1322+
},
12731323
}
12741324

12751325
for _, tc := range testCases {

internal/git/changes.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,17 @@ func getModifiedLines(cmd CommandRunner, commits []string, fpath, atCommit strin
273273

274274
linesBefore := bytes.Split(bodyBefore, []byte("\n"))
275275
linesAfter := bytes.Split(bodyAfter, []byte("\n"))
276+
slog.Debug("Number of lines", slog.Int("before", len(linesBefore)), slog.Int("after", len(linesAfter)))
276277

277278
modLines := make([]int, 0, len(lines))
278279
for _, line := range lines {
279-
if !slices.Contains(commits, line.Commit) && line.Line == line.PrevLine {
280+
slog.Debug("Checking line", slog.String("commit", line.Commit), slog.Int("prev", line.PrevLine), slog.Int("line", line.Line))
281+
if !slices.Contains(commits, line.Commit) {
280282
continue
281283
}
282284

283285
if line.PrevLine <= len(linesBefore) && line.Line <= len(linesAfter) {
286+
slog.Debug("Checking line content", slog.String("before", string(linesBefore[line.PrevLine-1])), slog.String("after", string(linesAfter[line.Line-1])))
284287
if bytes.Equal(linesBefore[line.PrevLine-1], linesAfter[line.Line-1]) {
285288
continue
286289
}

internal/git/changes_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,46 @@ func TestChanges(t *testing.T) {
646646
},
647647
err: "",
648648
},
649+
{
650+
title: "rule partially replaced",
651+
setup: func(t *testing.T) git.CommandRunner {
652+
mustRun(t, "init", "--initial-branch=main", ".")
653+
require.NoError(t, os.WriteFile("main.txt", []byte("l1\nl2\nl3\n"), 0o644))
654+
mustRun(t, "add", "main.txt")
655+
gitCommit(t, "init")
656+
657+
mustRun(t, "checkout", "-b", "v2")
658+
require.NoError(t, os.WriteFile("main.txt", []byte("l1\nl3\n"), 0o644))
659+
mustRun(t, "add", "main.txt")
660+
gitCommit(t, "edit")
661+
662+
mustRun(t, "mv", "main.txt", "pr.txt")
663+
gitCommit(t, "rename")
664+
665+
return debugGitRun(t)
666+
},
667+
changes: []*git.FileChange{
668+
{
669+
Commits: []string{"1", "2"},
670+
Path: git.PathDiff{
671+
Before: git.Path{
672+
Name: "main.txt",
673+
Type: git.File,
674+
},
675+
After: git.Path{
676+
Name: "pr.txt",
677+
Type: git.File,
678+
},
679+
},
680+
Body: git.BodyDiff{
681+
Before: []byte("l1\nl2\nl3\n"),
682+
After: []byte("l1\nl3\n"),
683+
ModifiedLines: []int{1, 2},
684+
},
685+
},
686+
},
687+
err: "",
688+
},
649689
}
650690

651691
for _, tc := range testCases {

0 commit comments

Comments
 (0)