mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-08-08 16:24:27 +00:00
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
* chore: add SPDX license headers to all source files
Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.
Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).
This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.
* fix: restore execute permissions on scripts
* docs: add license header instructions to CONTRIBUTING guides
* docs: add license header instructions to pages contributing guides
* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL
* fix: apply code review suggestions for license scripts
- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u
* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)
* fix(pages): use split/join instead of replace to avoid CodeQL false positive
CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
240 lines
6.2 KiB
Go
240 lines
6.2 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright 2026 alibaba/open-code-review Contributors
|
|
|
|
package diff
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/alibaba/open-code-review/internal/model"
|
|
)
|
|
|
|
// ResolveLineNumbers populates StartLine/EndLine on each comment by matching
|
|
// the ExistingCode against the corresponding file's diff hunks (primary), or
|
|
// falling back to scanning the full new-file content line-by-line.
|
|
func ResolveLineNumbers(comments []model.LlmComment, diffs []model.Diff) []model.LlmComment {
|
|
if len(comments) == 0 || len(diffs) == 0 {
|
|
return comments
|
|
}
|
|
|
|
// Build lookup: newPath -> *Diff
|
|
diffByPath := make(map[string]*model.Diff, len(diffs))
|
|
for i := range diffs {
|
|
d := &diffs[i]
|
|
if d.NewPath != "/dev/null" && d.NewPath != "" {
|
|
diffByPath[d.NewPath] = d
|
|
}
|
|
if d.OldPath != "/dev/null" && d.OldPath != "" {
|
|
diffByPath[d.OldPath] = d
|
|
}
|
|
}
|
|
|
|
result := make([]model.LlmComment, len(comments))
|
|
copy(result, comments)
|
|
|
|
for i := range result {
|
|
cm := &result[i]
|
|
if cm.StartLine > 0 || cm.EndLine > 0 {
|
|
continue
|
|
}
|
|
if cm.ExistingCode == "" {
|
|
continue
|
|
}
|
|
d, ok := diffByPath[cm.Path]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
// Primary: try matching from deleted/context lines in diff hunks
|
|
if resolveFromHunk(d, cm) {
|
|
continue
|
|
}
|
|
|
|
// Fallback: scan the new file content for consecutive matches
|
|
resolveFromFileContent(d, cm)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// ResolveComment attempts to resolve StartLine/EndLine for a single comment
|
|
// by matching ExistingCode against the diff. Returns true on success.
|
|
func ResolveComment(cm *model.LlmComment, d *model.Diff) bool {
|
|
if cm.StartLine > 0 || cm.EndLine > 0 {
|
|
return true
|
|
}
|
|
if cm.ExistingCode == "" {
|
|
return false
|
|
}
|
|
if resolveFromHunk(d, cm) {
|
|
return true
|
|
}
|
|
return resolveFromFileContent(d, cm)
|
|
}
|
|
|
|
// indexedLine pairs a normalized line with its absolute file line number.
|
|
type indexedLine struct {
|
|
lineNum int
|
|
content string
|
|
}
|
|
|
|
// resolveFromHunk tries to find startLine/endLine by matching ExistingCode
|
|
// against hunk lines. It tries the new-side first (context + added lines →
|
|
// new-file line numbers), then falls back to old-side (context + deleted →
|
|
// old-file line numbers).
|
|
func resolveFromHunk(d *model.Diff, cm *model.LlmComment) bool {
|
|
hunks := ParseHunks(d.Diff)
|
|
if len(hunks) == 0 {
|
|
return false
|
|
}
|
|
|
|
targetLines := splitAndNormalize(cm.ExistingCode)
|
|
if len(targetLines) == 0 {
|
|
return false
|
|
}
|
|
|
|
for i := range hunks {
|
|
newSide := extractSideLines(&hunks[i], true)
|
|
if start, end, ok := matchConsecutive(newSide, targetLines); ok {
|
|
cm.StartLine = start
|
|
cm.EndLine = end
|
|
return true
|
|
}
|
|
}
|
|
|
|
for i := range hunks {
|
|
oldSide := extractSideLines(&hunks[i], false)
|
|
if start, end, ok := matchConsecutive(oldSide, targetLines); ok {
|
|
cm.StartLine = start
|
|
cm.EndLine = end
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// extractSideLines extracts one side of the diff from a hunk.
|
|
// When newSide is true, returns context+added lines with new-file line numbers.
|
|
// When newSide is false, returns context+deleted lines with old-file line numbers.
|
|
func extractSideLines(hunk *Hunk, newSide bool) []indexedLine {
|
|
var result []indexedLine
|
|
oldLine := hunk.OldStart
|
|
newLine := hunk.NewStart
|
|
|
|
for _, l := range hunk.Lines {
|
|
switch l.Type {
|
|
case HunkContext:
|
|
if newSide {
|
|
result = append(result, indexedLine{newLine, normalizeLine(l.Content)})
|
|
} else {
|
|
result = append(result, indexedLine{oldLine, normalizeLine(l.Content)})
|
|
}
|
|
oldLine++
|
|
newLine++
|
|
case HunkAdded:
|
|
if newSide {
|
|
result = append(result, indexedLine{newLine, normalizeLine(l.Content)})
|
|
}
|
|
newLine++
|
|
case HunkDeleted:
|
|
if !newSide {
|
|
result = append(result, indexedLine{oldLine, normalizeLine(l.Content)})
|
|
}
|
|
oldLine++
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// matchConsecutive scans sideLines for a consecutive run matching all targetLines.
|
|
func matchConsecutive(sideLines []indexedLine, targetLines []string) (startLine, endLine int, found bool) {
|
|
if len(targetLines) == 0 || len(sideLines) < len(targetLines) {
|
|
return 0, 0, false
|
|
}
|
|
for i := 0; i <= len(sideLines)-len(targetLines); i++ {
|
|
matched := true
|
|
for j, target := range targetLines {
|
|
if sideLines[i+j].content != target {
|
|
matched = false
|
|
break
|
|
}
|
|
}
|
|
if matched {
|
|
return sideLines[i].lineNum, sideLines[i+len(targetLines)-1].lineNum, true
|
|
}
|
|
}
|
|
return 0, 0, false
|
|
}
|
|
|
|
// resolveFromFileContent scans the new file content line-by-line for consecutive
|
|
// matches of the normalized existing_code.
|
|
func resolveFromFileContent(d *model.Diff, cm *model.LlmComment) bool {
|
|
if d.NewFileContent == "" {
|
|
return false
|
|
}
|
|
|
|
fileLines := strings.Split(d.NewFileContent, "\n")
|
|
targetLines := splitAndNormalize(cm.ExistingCode)
|
|
if len(targetLines) == 0 {
|
|
return false
|
|
}
|
|
|
|
// Normalize file lines the same way as target: skip blanks so that
|
|
// blank lines in the source don't break the sliding-window match.
|
|
// "Consecutive" here means adjacent non-blank lines.
|
|
normalizedFileLines := make([]string, 0, len(fileLines))
|
|
fileLineNums := make([]int, 0, len(fileLines))
|
|
for i, line := range fileLines {
|
|
n := normalizeLine(strings.TrimRight(line, "\r"))
|
|
if n == "" {
|
|
continue
|
|
}
|
|
normalizedFileLines = append(normalizedFileLines, n)
|
|
fileLineNums = append(fileLineNums, i+1)
|
|
}
|
|
|
|
if len(normalizedFileLines) < len(targetLines) {
|
|
return false
|
|
}
|
|
|
|
for i := 0; i <= len(normalizedFileLines)-len(targetLines); i++ {
|
|
matched := true
|
|
for j, target := range targetLines {
|
|
if normalizedFileLines[i+j] != target {
|
|
matched = false
|
|
break
|
|
}
|
|
}
|
|
if matched {
|
|
cm.StartLine = fileLineNums[i]
|
|
cm.EndLine = fileLineNums[i+len(targetLines)-1]
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// splitAndNormalize splits code text into lines and normalizes each one.
|
|
func splitAndNormalize(code string) []string {
|
|
raw := strings.Split(code, "\n")
|
|
result := make([]string, 0, len(raw))
|
|
for _, line := range raw {
|
|
n := normalizeLine(line)
|
|
if n == "" {
|
|
continue
|
|
}
|
|
result = append(result, n)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// normalizeLine removes leading/trailing whitespace and strips any leading
|
|
// '+' or '-' diff marker.
|
|
func normalizeLine(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
s = strings.TrimPrefix(s, "+")
|
|
s = strings.TrimPrefix(s, "-")
|
|
return strings.TrimSpace(s)
|
|
}
|