-
Notifications
You must be signed in to change notification settings - Fork 2
/
python.go
59 lines (47 loc) · 1.69 KB
/
python.go
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
package python
import (
"regexp"
"github.com/sergi/go-diff/diffmatchpatch"
"github.com/atlantistechnology/sdt/pkg/types"
"github.com/atlantistechnology/sdt/pkg/utils"
)
func simplifyParseTree(parseTree string) string {
reLineNo := regexp.MustCompile(`(?m)lineno=\d+`)
mod1 := reLineNo.ReplaceAllString(parseTree, "lineno=?")
reOffset := regexp.MustCompile(`(?m)offset=\d+`)
mod2 := reOffset.ReplaceAllString(mod1, "offset=?")
return mod2
}
func Diff(filename string, options types.Options, config types.Config) string {
var currentTree []byte
var headTree []byte
pythonCmd := config.Commands["python"].Executable
switches := config.Commands["python"].Switches
canonical := false // Generate AST, don't canonicalize
if filename == "" {
//-- Comparison of two local files
filename, headTree, currentTree = utils.LocalFileTrees(
pythonCmd, switches, options, "Python", canonical)
} else {
//-- Comparison of a branch/revision to a current file
headTree, currentTree = utils.RevisionToCurrentTree(
filename, pythonCmd, switches, options, "Python", canonical)
}
// Make the trees into slightly simpler string representation
headTreeString := simplifyParseTree(string(headTree))
currentTreeString := simplifyParseTree(string(currentTree))
// Perform the diff between the versions
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(headTreeString, currentTreeString, false)
if options.Parsetree {
return utils.ColorDiff(dmp, diffs,
types.Python, options.Dumbterm, options.Minimal)
}
if options.Semantic {
return utils.SemanticChanges(
dmp, diffs, filename,
headTree, headTreeString,
types.Python, options.Dumbterm, options.Minimal)
}
return "| No diff type specified"
}