-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel.go
More file actions
183 lines (161 loc) · 5.62 KB
/
model.go
File metadata and controls
183 lines (161 loc) · 5.62 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
package git_diff_parser
import (
"fmt"
"strings"
)
type contentChangeType string
const (
contentChangeTypeAdd contentChangeType = "add"
contentChangeTypeDelete contentChangeType = "delete"
contentChangeTypeModify contentChangeType = "modify"
contentChangeTypeNOOP contentChangeType = ""
)
// contentChange is a part of the line that starts with ` `, `-`, `+`.
// Consecutive contentChange build a line.
// A `~` is a special case of contentChange that is used to indicate a new line.
type contentChange struct {
Type contentChangeType `json:"type"`
From string `json:"from"`
To string `json:"to"`
}
type changeList []contentChange
// hunkLine keeps a normalized, apply-friendly view of a hunk line.
type hunkLine struct {
Kind byte `json:"kind"`
Text string `json:"text"`
HasNewline bool `json:"has_newline"`
OldEOF bool `json:"old_eof,omitempty"`
NewEOF bool `json:"new_eof,omitempty"`
}
// hunk is a line that starts with @@.
// Each hunk shows one area where the files differ.
// Unified format hunks look like this:
// @@ from-file-line-numbers to-file-line-numbers @@
//
// line-from-either-file
// line-from-either-file…
//
// If a hunk contains just one line, only its start line number appears. Otherwise its line numbers look like 'start,count'. An empty hunk is considered to start at the line that follows the hunk.
type hunk struct {
ChangeList changeList `json:"change_list"`
Lines []hunkLine `json:"lines,omitempty"`
StartLineNumberOld int `json:"start_line_number_old"`
CountOld int `json:"count_old"`
StartLineNumberNew int `json:"start_line_number_new"`
CountNew int `json:"count_new"`
}
func (l *hunkLine) markNoNewline() {
l.HasNewline = false
}
func (h *hunk) markEOFMarkers() {
oldSeen := 0
newSeen := 0
for i := range h.Lines {
line := &h.Lines[i]
if line.Kind == ' ' || line.Kind == '-' {
oldSeen++
}
if line.Kind == ' ' || line.Kind == '+' {
newSeen++
}
if !line.HasNewline || strings.TrimSuffix(line.Text, "\r") != "" {
continue
}
line.OldEOF = (line.Kind == ' ' || line.Kind == '-') && oldSeen == h.CountOld
line.NewEOF = (line.Kind == ' ' || line.Kind == '+') && newSeen == h.CountNew
}
}
func (changes *changeList) isSignificant() bool {
for _, change := range *changes {
if change.Type != contentChangeTypeNOOP {
return true
}
}
return false
}
func (h *hunk) GoString() string {
return fmt.Sprintf(
"git_diff_parser.Hunk{ChangeList:%#v, StartLineNumberOld:%d, CountOld:%d, StartLineNumberNew:%d, CountNew:%d}",
h.ChangeList,
h.StartLineNumberOld,
h.CountOld,
h.StartLineNumberNew,
h.CountNew,
)
}
type fileDiffType string
const (
fileDiffTypeAdded fileDiffType = "add"
fileDiffTypeDeleted fileDiffType = "delete"
fileDiffTypeModified fileDiffType = "modify"
)
type binaryDeltaType string
const (
binaryDeltaTypeLiteral binaryDeltaType = "literal"
binaryDeltaTypeDelta binaryDeltaType = "delta"
)
type binaryPatch struct {
Type binaryDeltaType `json:"type"`
Count int
Content string
}
// fileDiff models the subset of Git patch metadata exposed by this parser.
//
// Git represents generated diff pairs as diff_filepair:
// https://github.com/git/git/blob/aec3f587505a472db67e9462d0702e7d463a449d/diffcore.h#L107-L130
//
// Git emits unified patch file headers in builtin_diff:
// https://github.com/git/git/blob/aec3f587505a472db67e9462d0702e7d463a449d/diff.c#L3838-L3930
type fileDiff struct {
FromFile string `json:"from_file"`
ToFile string `json:"to_file"`
Type fileDiffType `json:"type"`
IsBinary bool `json:"is_binary"`
OldMode string `json:"old_mode,omitempty"`
NewMode string `json:"new_mode,omitempty"`
IndexOld string `json:"index_old,omitempty"`
IndexNew string `json:"index_new,omitempty"`
IndexMode string `json:"index_mode,omitempty"`
SimilarityIndex int `json:"similarity_index,omitempty"`
DissimilarityIndex int `json:"dissimilarity_index,omitempty"`
RenameFrom string `json:"rename_from,omitempty"`
RenameTo string `json:"rename_to,omitempty"`
CopyFrom string `json:"copy_from,omitempty"`
CopyTo string `json:"copy_to,omitempty"`
Hunks []hunk `json:"hunks"`
BinaryPatch []binaryPatch `json:"binary_patch"`
// Parser-only paths from the "---" and "+++" file header lines. Git apply
// validates these against the diff --git/copy/rename names in apply.c:
// https://github.com/git/git/blob/aec3f587505a472db67e9462d0702e7d463a449d/apply.c#L929-L966
// https://github.com/git/git/blob/aec3f587505a472db67e9462d0702e7d463a449d/apply.c#L1330-L1453
//
// They are intentionally unexported and omitted from JSON because they are
// input syntax used for validation, not semantic diff metadata.
oldFileHeaderPath string
newFileHeaderPath string
}
func (fd *fileDiff) GoString() string {
var hunksStr string
if fd.Hunks == nil {
hunksStr = "[]git_diff_parser.Hunk(nil)"
} else {
hunks := make([]string, len(fd.Hunks))
for i := range fd.Hunks {
hunks[i] = fd.Hunks[i].GoString()
}
hunksStr = "[]git_diff_parser.Hunk{" + strings.Join(hunks, ", ") + "}"
}
return fmt.Sprintf(
"&git_diff_parser.FileDiff{FromFile:%#v, ToFile:%#v, Type:%#v, IsBinary:%t, NewMode:%#v, Hunks:%s, BinaryPatch:%#v}",
fd.FromFile,
fd.ToFile,
fd.Type,
fd.IsBinary,
fd.NewMode,
hunksStr,
fd.BinaryPatch,
)
}
type diff struct {
FileDiff []fileDiff `json:"file_diff"`
}