Skip to content

Commit 3dc2b52

Browse files
sguiheuxMathieu Fenniak
authored andcommitted
fix: multiline comment invalidation (#12950)
Found issues during the process of invalidation of a multiline comment (link to #12582): * Update a line in the middle of the comment * Update/Delete the last line of the comment No problem with: * Deleting a line in the middle of the comment * Update/Delete the first line of the comment I added all these cases in the pull_review_test.go ### Tests for Go changes - I added test coverage for Go changes... - [X] in their respective `*_test.go` for unit tests. - I ran... - [X] `make pr-go` before pushing Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12950 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
1 parent c2dcdc9 commit 3dc2b52

4 files changed

Lines changed: 242 additions & 5 deletions

File tree

models/issues/comment.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,43 @@ func (c *Comment) CheckLineRangeValid(ctx context.Context, repo *repo_model.Repo
897897
}
898898
anchorResolvedLine := anchorBlame.LineNumber
899899

900-
// A line that no longer resolves is treated as "modified but present" and tolerated; only a
901-
// resolved line landing at an unexpected offset (lines inserted/removed inside the range) is a break.
900+
// Catch a line changed by a later commit by comparing each range line's current content
901+
// to the comment's Patch (its content at creation; lines the PR itself changed already match it).
902+
// The Patch uses the comment's line coordinates, so trust it only
903+
// when the anchor's recorded content equals its current content — otherwise fall back below.
904+
if expected := git.PatchRightSideContent(c.Patch); len(expected) > 0 {
905+
if headLines, ok := c.headFileLines(gitRepo, currentHead, anchorBlame.FilePath); ok {
906+
lineAt := func(n uint64) (string, bool) {
907+
if n >= 1 && n <= uint64(len(headLines)) {
908+
return headLines[n-1], true
909+
}
910+
return "", false
911+
}
912+
anchorExpected, hasAnchor := expected[int64(c.UnsignedLine())]
913+
anchorCurrent, hasCurrent := lineAt(anchorResolvedLine)
914+
if hasAnchor && hasCurrent && anchorExpected == anchorCurrent {
915+
trusted := true
916+
for i := int64(1); i <= c.ExtraLinesCount; i++ {
917+
exp, okExp := expected[int64(c.UnsignedLine())+i]
918+
cur, okCur := lineAt(anchorResolvedLine + uint64(i))
919+
if !okExp || !okCur {
920+
trusted = false // mapping incomplete -> fall back to the offset-only check
921+
break
922+
}
923+
if exp != cur {
924+
return "invalid", nil // a range line was changed after the comment was made
925+
}
926+
}
927+
if trusted {
928+
return "valid", nil
929+
}
930+
}
931+
}
932+
}
933+
934+
// Fallback (offset-only): a line that no longer resolves is treated as "modified but present"
935+
// and tolerated; only a resolved line landing at an unexpected offset (lines inserted/removed
936+
// inside the range) is a break.
902937
startLine := c.UnsignedLine()
903938
for i := int64(1); i <= c.ExtraLinesCount; i++ {
904939
blame, err := c.resolveLineAtHead(gitRepo, startLine+uint64(i), currentHead)
@@ -919,6 +954,25 @@ func (c *Comment) CheckLineRangeValid(ctx context.Context, repo *repo_model.Repo
919954
return resultJSON == "valid", nil
920955
}
921956

957+
// headFileLines reads the content of treePath at the given commit and returns its lines
958+
// (dropping the trailing empty element produced by a final newline). ok is false when the
959+
// file can't be read at head.
960+
func (c *Comment) headFileLines(gitRepo *git.Repository, head, treePath string) (lines []string, ok bool) {
961+
commit, err := gitRepo.GetCommit(head)
962+
if err != nil {
963+
return nil, false
964+
}
965+
content, err := commit.GetFileContent(treePath, -1)
966+
if err != nil {
967+
return nil, false
968+
}
969+
lines = strings.Split(content, "\n")
970+
if n := len(lines); n > 0 && lines[n-1] == "" {
971+
lines = lines[:n-1]
972+
}
973+
return lines, true
974+
}
975+
922976
// CodeCommentLink returns the url to a comment in code
923977
func (c *Comment) CodeCommentLink(ctx context.Context) string {
924978
err := c.LoadIssue(ctx)

modules/git/diff.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,53 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
278278
return strings.Join(newHunk, "\n"), nil
279279
}
280280

281+
// PatchRightSideContent parses a unified diff patch (such as a stored code-comment
282+
// Patch) and returns the content of each line on the right (new) side, keyed by its
283+
// new line number. Added ('+') and context (' ') lines are included; removed ('-')
284+
// lines and the "\ No newline at end of file" marker are skipped. It lets callers
285+
// recover the file content captured when a comment was created, to detect whether
286+
// the commented lines were changed afterwards.
287+
func PatchRightSideContent(patch string) map[int64]string {
288+
result := make(map[int64]string)
289+
scanner := bufio.NewScanner(strings.NewReader(patch))
290+
scanner.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
291+
292+
var rightLine int64
293+
inHunk := false
294+
for scanner.Scan() {
295+
line := scanner.Text()
296+
if strings.HasPrefix(line, "@@") {
297+
submatches := hunkRegex.FindStringSubmatch(line)
298+
if submatches == nil {
299+
inHunk = false
300+
continue
301+
}
302+
for i, name := range hunkRegex.SubexpNames() {
303+
if name == "beginNew" {
304+
rightLine, _ = strconv.ParseInt(submatches[i], 10, 64)
305+
}
306+
}
307+
inHunk = rightLine > 0
308+
continue
309+
}
310+
if !inHunk || len(line) == 0 {
311+
continue
312+
}
313+
switch line[0] {
314+
case '+', ' ':
315+
result[rightLine] = line[1:]
316+
rightLine++
317+
case '-', '\\':
318+
// '-' is left-only; '\' is the "no newline at end of file" marker
319+
break
320+
default:
321+
// a non-hunk line (e.g. the next "diff --git" header); stop this hunk
322+
inHunk = false
323+
}
324+
}
325+
return result
326+
}
327+
281328
var ErrLineNotFound = errors.New("line not found in diff")
282329

283330
type LinePlacement struct {

modules/git/diff_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,47 @@ index 2d203fb..d0cb63f 100644
436436
require.ErrorIs(t, err, ErrLineNotFound)
437437
})
438438
}
439+
440+
func TestPatchRightSideContent(t *testing.T) {
441+
t.Run("empty", func(t *testing.T) {
442+
assert.Empty(t, PatchRightSideContent(""))
443+
})
444+
445+
t.Run("single hunk with a replacement", func(t *testing.T) {
446+
patch := "diff --git a/file1.md b/file1.md\n" +
447+
"--- a/file1.md\n" +
448+
"+++ b/file1.md\n" +
449+
"@@ -48,3 +48,3 @@\n" +
450+
" Line 48\n" +
451+
" Line 49\n" +
452+
"-Line 50\n" +
453+
"+Line 50--modified"
454+
assert.Equal(t, map[int64]string{
455+
48: "Line 48",
456+
49: "Line 49",
457+
50: "Line 50--modified",
458+
}, PatchRightSideContent(patch))
459+
})
460+
461+
t.Run("added lines shift the right side", func(t *testing.T) {
462+
patch := "@@ -10,2 +10,4 @@\n" +
463+
" ctx\n" +
464+
"+added a\n" +
465+
"+added b\n" +
466+
" after"
467+
assert.Equal(t, map[int64]string{
468+
10: "ctx",
469+
11: "added a",
470+
12: "added b",
471+
13: "after",
472+
}, PatchRightSideContent(patch))
473+
})
474+
475+
t.Run("removed lines do not consume right-side numbers", func(t *testing.T) {
476+
patch := "@@ -5,3 +5,1 @@\n" +
477+
"-gone 1\n" +
478+
"-gone 2\n" +
479+
" kept"
480+
assert.Equal(t, map[int64]string{5: "kept"}, PatchRightSideContent(patch))
481+
})
482+
}

tests/integration/pull_review_test.go

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"forgejo.org/modules/test"
3636
issue_service "forgejo.org/services/issue"
3737
"forgejo.org/services/mailer"
38+
pull_service "forgejo.org/services/pull"
3839
repo_service "forgejo.org/services/repository"
3940
files_service "forgejo.org/services/repository/files"
4041
"forgejo.org/tests"
@@ -2016,10 +2017,15 @@ func TestPullRequestCommentPlacement(t *testing.T) {
20162017
// Push a second commit that changes lines BEFORE the range (removing lines 1-10),
20172018
// which shifts the range but keeps it contiguous.
20182019
content = strings.Replace(content, "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\n", "", 1)
2019-
tester.changeFile("file1.md", content)
2020+
newSHA := tester.changeFile("file1.md", content)
2021+
2022+
// Run the invalidation pass synchronously instead of waiting for the async
2023+
// goroutine, then check the comment is still valid.
2024+
pr, err := issues_model.GetPullRequestByIndex(t.Context(), tester.repo.ID, tester.pr.Index)
2025+
require.NoError(t, err)
2026+
require.NoError(t, pull_service.InvalidateCodeComments(t.Context(),
2027+
issues_model.PullRequestList{pr}, tester.user, tester.repo, newSHA))
20202028

2021-
// Wait a bit for async invalidation to run, then check the comment is still valid.
2022-
time.Sleep(2 * time.Second)
20232029
commentReloaded := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID})
20242030
assert.False(t, commentReloaded.Invalidated)
20252031

@@ -2038,6 +2044,92 @@ func TestPullRequestCommentPlacement(t *testing.T) {
20382044
}
20392045
tester.assertFilesChangedDiff(diff)
20402046
})
2047+
2048+
// Helper: comment on lines 48-50 (anchor 48, middle 49, last 50, all modified by the PR), then a
2049+
// later commit applies `secondCommit` to the file and we check the resulting invalidation state.
2050+
runRangeInvalidation := func(t *testing.T, secondCommit func(content string) string, wantInvalidated bool) {
2051+
tester := newPullRequestCommentPlacementTester(t)
2052+
2053+
content := tester.fileContent
2054+
content = strings.Replace(content, "Line 48\n", "Line 48--modified\n", 1)
2055+
content = strings.Replace(content, "Line 49\n", "Line 49--modified\n", 1)
2056+
content = strings.Replace(content, "Line 50\n", "Line 50--modified\n", 1)
2057+
tester.changeFile("file1.md", content)
2058+
tester.createPR()
2059+
2060+
comment := tester.multiLineCommentFromFilesChanged("file1.md", 48, 2)
2061+
assert.EqualValues(t, 48, comment.Line)
2062+
assert.EqualValues(t, 2, comment.ExtraLinesCount)
2063+
assert.False(t, comment.Invalidated)
2064+
2065+
newSHA := tester.changeFile("file1.md", secondCommit(content))
2066+
2067+
if wantInvalidated {
2068+
assert.EventuallyWithT(t, func(t *assert.CollectT) {
2069+
commentReloaded := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID})
2070+
assert.True(t, commentReloaded.Invalidated)
2071+
}, 5*time.Second, 50*time.Millisecond)
2072+
} else {
2073+
// Run the invalidation pass synchronously instead of waiting for the async
2074+
// goroutine, then assert the comment stayed valid.
2075+
pr, err := issues_model.GetPullRequestByIndex(t.Context(), tester.repo.ID, tester.pr.Index)
2076+
require.NoError(t, err)
2077+
require.NoError(t, pull_service.InvalidateCodeComments(t.Context(),
2078+
issues_model.PullRequestList{pr}, tester.user, tester.repo, newSHA))
2079+
2080+
commentReloaded := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: comment.ID})
2081+
assert.False(t, commentReloaded.Invalidated)
2082+
}
2083+
}
2084+
2085+
t.Run("multi-line comment invalidated when a middle line is deleted by a later commit", func(t *testing.T) {
2086+
defer tests.PrintCurrentTest(t)()
2087+
runRangeInvalidation(t, func(content string) string {
2088+
return strings.Replace(content, "Line 49--modified\n", "", 1)
2089+
}, true)
2090+
})
2091+
2092+
t.Run("multi-line comment invalidated when a middle line content changes in a later commit", func(t *testing.T) {
2093+
defer tests.PrintCurrentTest(t)()
2094+
runRangeInvalidation(t, func(content string) string {
2095+
return strings.Replace(content, "Line 49--modified\n", "Line 49--changed-again\n", 1)
2096+
}, true)
2097+
})
2098+
2099+
t.Run("multi-line comment invalidated when the last line content changes in a later commit", func(t *testing.T) {
2100+
defer tests.PrintCurrentTest(t)()
2101+
runRangeInvalidation(t, func(content string) string {
2102+
return strings.Replace(content, "Line 50--modified\n", "Line 50--changed-again\n", 1)
2103+
}, true)
2104+
})
2105+
2106+
t.Run("multi-line comment not invalidated when a line outside the range changes", func(t *testing.T) {
2107+
defer tests.PrintCurrentTest(t)()
2108+
runRangeInvalidation(t, func(content string) string {
2109+
return strings.Replace(content, "Line 60\n", "Line 60--modified\n", 1)
2110+
}, false)
2111+
})
2112+
2113+
t.Run("multi-line comment invalidated when the last line is deleted by a later commit", func(t *testing.T) {
2114+
defer tests.PrintCurrentTest(t)()
2115+
runRangeInvalidation(t, func(content string) string {
2116+
return strings.Replace(content, "Line 50--modified\n", "", 1)
2117+
}, true)
2118+
})
2119+
2120+
t.Run("multi-line comment invalidated when the first (anchor) line content changes in a later commit", func(t *testing.T) {
2121+
defer tests.PrintCurrentTest(t)()
2122+
runRangeInvalidation(t, func(content string) string {
2123+
return strings.Replace(content, "Line 48--modified\n", "Line 48--changed-again\n", 1)
2124+
}, true)
2125+
})
2126+
2127+
t.Run("multi-line comment invalidated when the first (anchor) line is deleted by a later commit", func(t *testing.T) {
2128+
defer tests.PrintCurrentTest(t)()
2129+
runRangeInvalidation(t, func(content string) string {
2130+
return strings.Replace(content, "Line 48--modified\n", "", 1)
2131+
}, true)
2132+
})
20412133
})
20422134
}
20432135

0 commit comments

Comments
 (0)