mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-08-08 00:04:41 +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.
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright 2026 alibaba/open-code-review Contributors
|
|
|
|
// Package suggestdiff provides line-level diff computation between code snippets,
|
|
// used for CLI rendering of review suggestions with ANSI color codes.
|
|
package suggestdiff
|
|
|
|
import "strings"
|
|
|
|
// DiffLineType marks a line as context, added, or deleted.
|
|
type DiffLineType int
|
|
|
|
const (
|
|
DiffContext DiffLineType = iota
|
|
DiffAdded
|
|
DiffDeleted
|
|
)
|
|
|
|
// DiffLine is a single line in the diff result.
|
|
type DiffLine struct {
|
|
Type DiffLineType
|
|
Content string
|
|
}
|
|
|
|
// ComputeLineDiff returns a line-level diff between oldLines and newLines.
|
|
// Uses Myers-style LCS to find common subsequences, then emits context/added/deleted lines.
|
|
func ComputeLineDiff(oldLines, newLines []string) []DiffLine {
|
|
m, n := len(oldLines), len(newLines)
|
|
if m == 0 && n == 0 {
|
|
return nil
|
|
}
|
|
|
|
// LCS DP table
|
|
lcs := make([][]int, m+1)
|
|
for i := range lcs {
|
|
lcs[i] = make([]int, n+1)
|
|
}
|
|
for i := 1; i <= m; i++ {
|
|
for j := 1; j <= n; j++ {
|
|
if strings.EqualFold(strings.TrimSpace(oldLines[i-1]), strings.TrimSpace(newLines[j-1])) {
|
|
lcs[i][j] = lcs[i-1][j-1] + 1
|
|
} else {
|
|
lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1])
|
|
}
|
|
}
|
|
}
|
|
|
|
// Backtrack to produce diff
|
|
var result []DiffLine
|
|
i, j := m, n
|
|
back := make([]DiffLine, 0, max(m, n)*2)
|
|
for i > 0 || j > 0 {
|
|
if i > 0 && j > 0 && strings.EqualFold(strings.TrimSpace(oldLines[i-1]), strings.TrimSpace(newLines[j-1])) {
|
|
back = append(back, DiffLine{Type: DiffContext, Content: oldLines[i-1]})
|
|
i--
|
|
j--
|
|
} else if j > 0 && (i == 0 || lcs[i][j-1] >= lcs[i-1][j]) {
|
|
back = append(back, DiffLine{Type: DiffAdded, Content: newLines[j-1]})
|
|
j--
|
|
} else {
|
|
back = append(back, DiffLine{Type: DiffDeleted, Content: oldLines[i-1]})
|
|
i--
|
|
}
|
|
}
|
|
|
|
// Reverse
|
|
for idx := len(back) - 1; idx >= 0; idx-- {
|
|
result = append(result, back[idx])
|
|
}
|
|
return result
|
|
}
|
|
|
|
func max(a, b int) int {
|
|
if a > b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|