-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_input.go
More file actions
107 lines (93 loc) · 2.66 KB
/
diff_input.go
File metadata and controls
107 lines (93 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package tokendiff
import (
"bufio"
"fmt"
"io"
"strings"
)
// isGitExtendedHeader returns true if the line is a git extended header.
func isGitExtendedHeader(line string) bool {
prefixes := []string{"diff ", "index ", "new file", "deleted file", "similarity", "rename", "Binary"}
for _, p := range prefixes {
if strings.HasPrefix(line, p) {
return true
}
}
return false
}
// isDiffHeader returns true if the line is a file header.
func isDiffHeader(line string) bool {
return strings.HasPrefix(line, "---") || strings.HasPrefix(line, "+++")
}
// isHunkHeader returns true if the line is a hunk header.
func isHunkHeader(line string) bool {
return strings.HasPrefix(line, "@@")
}
// diffProcessor handles processing unified diff input.
type diffProcessor struct {
output io.Writer
opts Options
fmtOpts FormatOptions
oldLines []string
newLines []string
inHunk bool
}
// flushHunk outputs accumulated changes as word-level diff.
func (p *diffProcessor) flushHunk() {
if len(p.oldLines) == 0 && len(p.newLines) == 0 {
return
}
oldText := strings.Join(p.oldLines, "\n")
newText := strings.Join(p.newLines, "\n")
result := DiffWholeFiles(oldText, newText, p.opts, p.fmtOpts)
fmt.Fprintln(p.output, result.Formatted)
p.oldLines = nil
p.newLines = nil
}
// processHunkLine handles a line inside a hunk.
func (p *diffProcessor) processHunkLine(line string) {
switch {
case strings.HasPrefix(line, "-"):
p.oldLines = append(p.oldLines, line[1:])
case strings.HasPrefix(line, "+"):
p.newLines = append(p.newLines, line[1:])
case strings.HasPrefix(line, " "):
p.flushHunk()
fmt.Fprintln(p.output, line[1:])
case line == "":
p.flushHunk()
fmt.Fprintln(p.output, line)
default:
p.flushHunk()
fmt.Fprintln(p.output, line)
}
}
// processLine handles a single line of diff input.
func (p *diffProcessor) processLine(line string) {
switch {
case isDiffHeader(line), isGitExtendedHeader(line):
p.flushHunk()
p.inHunk = false
fmt.Fprintln(p.output, line)
case isHunkHeader(line):
p.flushHunk()
p.inHunk = true
fmt.Fprintln(p.output, line)
case p.inHunk:
p.processHunkLine(line)
default:
fmt.Fprintln(p.output, line)
}
}
// ProcessUnifiedDiff reads a unified diff from input and applies word-level
// diffing to each hunk. The result is written to output with diff headers
// preserved and hunk content replaced with word-level diff output.
func ProcessUnifiedDiff(input io.Reader, output io.Writer, opts Options, fmtOpts FormatOptions) error {
scanner := bufio.NewScanner(input)
p := &diffProcessor{output: output, opts: opts, fmtOpts: fmtOpts}
for scanner.Scan() {
p.processLine(scanner.Text())
}
p.flushHunk()
return scanner.Err()
}