-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdiff.v
More file actions
194 lines (178 loc) · 4.32 KB
/
diff.v
File metadata and controls
194 lines (178 loc) · 4.32 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import veb
import highlight
// render_diff_line is a template helper that returns the diff line's
// content with single-line syntax highlighting applied.
fn render_diff_line(content string, file_path string) veb.RawHtml {
return veb.RawHtml(highlight.highlight_line(content, file_path))
}
struct FileDiff {
mut:
path string
old_path string
is_new bool
is_deleted bool
is_renamed bool
is_binary bool
additions int
deletions int
hunks []DiffHunk
}
struct DiffHunk {
mut:
header string
old_start int
old_count int
new_start int
new_count int
lines []DiffLine
}
struct DiffLine {
mut:
kind string // 'context', 'add', 'del'
old_line int // 0 if not applicable
new_line int // 0 if not applicable
content string
}
// parse_unified_diff parses a `git diff` unified diff into FileDiff structs.
fn parse_unified_diff(raw string) []FileDiff {
mut files := []FileDiff{}
mut cur := FileDiff{}
mut cur_hunk := DiffHunk{}
mut in_file := false
mut in_hunk := false
mut old_l := 0
mut new_l := 0
for line in raw.split_into_lines() {
if line.starts_with('diff --git') {
if in_file {
if in_hunk {
cur.hunks << cur_hunk
}
files << cur
}
cur = FileDiff{}
cur_hunk = DiffHunk{}
in_file = true
in_hunk = false
parts := line.split(' ')
if parts.len >= 4 {
a_path := strip_diff_prefix(parts[2], 'a/')
b_path := strip_diff_prefix(parts[3], 'b/')
cur.old_path = a_path
cur.path = b_path
}
} else if line.starts_with('new file') {
cur.is_new = true
} else if line.starts_with('deleted file') {
cur.is_deleted = true
} else if line.starts_with('rename from') || line.starts_with('rename to') {
cur.is_renamed = true
} else if line.starts_with('Binary files') {
cur.is_binary = true
} else if line.starts_with('--- ') || line.starts_with('+++ ') {
// skip header lines
} else if line.starts_with('@@') {
if in_hunk {
cur.hunks << cur_hunk
}
cur_hunk = DiffHunk{
header: line
}
in_hunk = true
parse_hunk_header(line, mut cur_hunk)
old_l = cur_hunk.old_start
new_l = cur_hunk.new_start
} else if in_hunk && line.len > 0 {
first := line[0]
content := line[1..]
if first == ` ` {
cur_hunk.lines << DiffLine{
kind: 'context'
old_line: old_l
new_line: new_l
content: content
}
old_l++
new_l++
} else if first == `+` {
cur_hunk.lines << DiffLine{
kind: 'add'
new_line: new_l
content: content
}
new_l++
cur.additions++
} else if first == `-` {
cur_hunk.lines << DiffLine{
kind: 'del'
old_line: old_l
content: content
}
old_l++
cur.deletions++
} else if first == `\\` {
// "\ No newline at end of file" — ignore
}
}
}
if in_file {
if in_hunk {
cur.hunks << cur_hunk
}
files << cur
}
return files
}
fn (d &DiffLine) sign() string {
return match d.kind {
'add' { '+' }
'del' { '-' }
else { ' ' }
}
}
fn (d &DiffLine) side() string {
return if d.kind == 'add' { 'new' } else { 'old' }
}
fn (d &DiffLine) effective_line() int {
return if d.kind == 'add' { d.new_line } else { d.old_line }
}
fn (d &DiffLine) comment_field_name(file_path string) string {
return 'rc::${file_path}::${d.side()}::${d.effective_line()}'
}
fn (d &DiffLine) old_line_str() string {
return if d.old_line > 0 { d.old_line.str() } else { '' }
}
fn (d &DiffLine) new_line_str() string {
return if d.new_line > 0 { d.new_line.str() } else { '' }
}
fn strip_diff_prefix(s string, prefix string) string {
if s.starts_with(prefix) {
return s[prefix.len..]
}
return s
}
// parse_hunk_header parses lines like "@@ -1,3 +1,4 @@ optional context"
fn parse_hunk_header(line string, mut hunk DiffHunk) {
parts := line.split(' ')
for p in parts {
if p.len < 2 {
continue
}
if p[0] == `-` {
start, count := parse_range(p[1..])
hunk.old_start = start
hunk.old_count = count
} else if p[0] == `+` {
start, count := parse_range(p[1..])
hunk.new_start = start
hunk.new_count = count
}
}
}
fn parse_range(s string) (int, int) {
idx := s.index(',') or { return s.int(), 1 }
return s[..idx].int(), s[idx + 1..].int()
}