-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
190 lines (175 loc) · 5.58 KB
/
diff.go
File metadata and controls
190 lines (175 loc) · 5.58 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
package main
import (
"bufio"
"fmt"
"os/exec"
"strconv"
"strings"
)
type PrecedingLineMatch struct {
Content string
Filename string
LineNumber int
ContentSimilarity float32
}
const LINE_ADD = 0
const LINE_UPDATE = 5
const LINE_MOVE = 10
const LINE_MOVE_UPDATE = 15
const MINIMUM_RAW_SIMILARITY = 0.74
const MINIMUM_SIMILARITY_FIRST_TERM_ONLY = 0.9
const MINIMUM_SIMILARITY_SUBSEQUENT_TERMS = 0.8
const STATE_FREE_FORM = 0
const STATE_IN_DIFF = 10
func FindPredecessorLineFromDiff(directory string, sha string, targetFilename string, targetLineNumber int, lineContents string) LineChange {
cmd := exec.Command("git", "rev-list", "--parents", sha)
cmd.Dir = directory
stdout, err := cmd.StdoutPipe()
if err != nil {
FailAndExit("Error getting parent list for %s: %v", sha, err)
}
defer stdout.Close()
err = cmd.Start()
if err != nil {
FailAndExit("Error starting git rev-list command: %v", err)
}
scanner := bufio.NewScanner(stdout)
scanner.Scan()
line := scanner.Text()
revListParents := strings.Split(line, " ")
if len(revListParents) > 1 {
// We can use the tilde some_sha~ syntax to mean "parent of some_sha"
shaWithTilde := fmt.Sprintf("%s~", sha)
cmd = exec.Command("git", "diff", shaWithTilde, sha)
} else {
// no parents = we need to diff vs an explicit "blank repo" sha
// From https://stackoverflow.com/questions/40883798/how-to-get-git-diff-of-the-first-commit
BlankRepoSha := "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
cmd = exec.Command("git", "diff", BlankRepoSha, sha)
}
cmd.Dir = directory
stdout, err = cmd.StdoutPipe()
if err != nil {
FailAndExit("Error creating diff for %s: %v", sha, err)
}
defer stdout.Close()
err = cmd.Start()
if err != nil {
FailAndExit("Error starting git diff command for %s: %v", sha, err)
}
scanner = bufio.NewScanner(stdout)
currentFilename := ""
currentLineNumber := 0
previousLineNumber := 0
maxLineNumber := 0
state := STATE_FREE_FORM
var precedingLineMatch *PrecedingLineMatch = nil
overallLineNumber := 0
for scanner.Scan() {
overallLineNumber++
line := scanner.Text()
if state == STATE_FREE_FORM && strings.HasPrefix(line, "diff --git a/") {
splitLine := strings.Split(line, " ")
fileBit := splitLine[2]
currentFilename = fileBit[2:]
currentLineNumber = 0
} else if state == STATE_FREE_FORM && strings.HasPrefix(line, "@@ ") {
splitLine := strings.Split(line, " ")
fromLines := strings.Split(splitLine[1], ",")
toLines := strings.Split(splitLine[2], ",")
previousLineNumber, err = strconv.Atoi(fromLines[0])
previousLineNumber = absint(previousLineNumber)
if err != nil {
FailAndExit("Error converting old offset: %v", err)
}
var currentLineNumber int
var changeCount int
if len(toLines) > 1 {
currentLineNumber, err = strconv.Atoi(toLines[0])
if err != nil {
FailAndExit("Error converting old offset: %v", err)
}
changeCount, err = strconv.Atoi(toLines[1])
if err != nil {
FailAndExit("Error converting diff line count: %v", err)
}
} else {
currentLineNumber = 0
changeCount, err = strconv.Atoi(toLines[0])
if err != nil {
FailAndExit("Error converting diff line count: %v", err)
}
}
maxLineNumber = currentLineNumber + changeCount - 1
state = STATE_IN_DIFF
} else if state == STATE_IN_DIFF {
if strings.HasPrefix(line, "-") {
contentSimilarity := LineContentSimilarity(lineContents, line[1:])
if contentSimilarity > MINIMUM_RAW_SIMILARITY {
if precedingLineMatch == nil || precedingLineMatch.ContentSimilarity >= contentSimilarity {
precedingLineMatch = &PrecedingLineMatch{
Content: line[1:],
Filename: currentFilename,
LineNumber: previousLineNumber,
ContentSimilarity: contentSimilarity,
}
}
}
previousLineNumber++
} else if strings.HasPrefix(line, "+") {
currentLineNumber++
} else {
if strings.HasPrefix(line, " ") && line[1:] == lineContents && (precedingLineMatch == nil || precedingLineMatch.ContentSimilarity < 1) {
// we found a context line that matches, so treat it as overriding *unless we also found a previous line that exactly matches*
precedingLineMatch = &PrecedingLineMatch{
Content: line[1:],
Filename: currentFilename,
LineNumber: previousLineNumber,
ContentSimilarity: 1.0,
}
}
currentLineNumber++
previousLineNumber++
}
if currentLineNumber >= maxLineNumber {
state = STATE_FREE_FORM
}
}
}
if err := scanner.Err(); err != nil {
FailAndExit("Error reading stdout: %v", err)
}
err = cmd.Wait()
if err != nil {
FailAndExit("Got error waiting for diff %s to finish: %v", sha, err)
}
if precedingLineMatch == nil {
return LineChange{
Sha: sha,
ChangeType: LINE_ADD,
Filename: targetFilename,
LineNumber: targetLineNumber,
Content: lineContents,
Previous: nil,
Commit: DetailsForCommit(directory, sha),
}
} else {
var changeType int
if precedingLineMatch.LineNumber != targetLineNumber && precedingLineMatch.Content != lineContents {
changeType = LINE_MOVE_UPDATE
} else if precedingLineMatch.Content != lineContents {
changeType = LINE_UPDATE
} else {
changeType = LINE_MOVE
}
return LineChange{
Sha: sha,
ChangeType: changeType,
Filename: targetFilename,
LineNumber: targetLineNumber,
Content: lineContents,
Previous: BlameAndDeriveChange(directory, &sha, &precedingLineMatch.Content, precedingLineMatch.Filename, precedingLineMatch.LineNumber),
Commit: DetailsForCommit(directory, sha),
}
}
}