feat(parser/diff): support max diff size
All checks were successful
submodules sync / sync (push) Successful in 40s
build / build (push) Successful in 1m26s
build / trigger-build-image (push) Successful in 7s

This commit is contained in:
张泊明518370910136 2024-11-04 08:26:53 -05:00
parent 59bbd98a58
commit cc435f9156
GPG Key ID: D47306D7062CDA9D

View File

@ -30,7 +30,7 @@ type Conf struct {
CompareSpace bool CompareSpace bool
AlwaysHide bool AlwaysHide bool
ForceQuitOnDiff bool ForceQuitOnDiff bool
MaxDiffLines int MaxDiffSize int
} }
} }
} }
@ -86,7 +86,7 @@ func (*Diff) Run(results []stage.ExecutorResult, confAny any) (
// Generate diff block with surrounding context // Generate diff block with surrounding context
diffOutput := generateDiffWithContext( diffOutput := generateDiffWithContext(
stdoutLines, resultLines, diffOps, output.MaxDiffLines) stdoutLines, resultLines, diffOps, output.MaxDiffSize)
diffOutput = strings.TrimSuffix(diffOutput, "\n \n") diffOutput = strings.TrimSuffix(diffOutput, "\n \n")
comment += fmt.Sprintf( comment += fmt.Sprintf(
"```diff\n%s\n```\n", "```diff\n%s\n```\n",
@ -228,37 +228,42 @@ func reverse(s []operation) []operation {
// generateDiffWithContext creates a diff block with surrounding context from stdout and result. // generateDiffWithContext creates a diff block with surrounding context from stdout and result.
func generateDiffWithContext( func generateDiffWithContext(
stdoutLines, resultLines []string, ops []operation, maxDiffLines int, stdoutLines, resultLines []string, ops []operation, maxSize int,
) string { ) string {
var diffBuilder strings.Builder var diffBuilder strings.Builder
srcIndex, dstIndex, lineCount := 0, 0, 0 srcIndex, dstIndex, lineCount := 0, 0, 0
for _, op := range ops { for _, op := range ops {
s := ""
switch op { switch op {
case INSERT: case INSERT:
if dstIndex < len(resultLines) { if dstIndex < len(resultLines) {
diffBuilder.WriteString(fmt.Sprintf("+ %s\n", resultLines[dstIndex])) s = fmt.Sprintf("+ %s\n", resultLines[dstIndex])
dstIndex += 1 dstIndex += 1
lineCount += 1
} }
case MOVE: case MOVE:
if srcIndex < len(stdoutLines) { if srcIndex < len(stdoutLines) {
diffBuilder.WriteString(fmt.Sprintf(" %s\n", stdoutLines[srcIndex])) s = fmt.Sprintf(" %s\n", stdoutLines[srcIndex])
srcIndex += 1 srcIndex += 1
dstIndex += 1 dstIndex += 1
lineCount += 1
} }
case DELETE: case DELETE:
if srcIndex < len(stdoutLines) { if srcIndex < len(stdoutLines) {
diffBuilder.WriteString(fmt.Sprintf("- %s\n", stdoutLines[srcIndex])) s = fmt.Sprintf("- %s\n", stdoutLines[srcIndex])
srcIndex += 1 srcIndex += 1
lineCount += 1 lineCount += 1
} }
} }
if maxDiffLines > 0 && lineCount >= maxDiffLines { if maxSize > 0 && diffBuilder.Len()+len(s) > maxSize {
remaining := maxSize - diffBuilder.Len()
if remaining > 0 {
diffBuilder.WriteString(s[:remaining])
}
diffBuilder.WriteString("\n\n(truncated)")
break break
} }
diffBuilder.WriteString(s)
} }
return diffBuilder.String() return diffBuilder.String()