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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
/*
delta is a command-line diff utility.
Usage:
`delta <file1> <file2> <merged>`
file1 is set to the name of the temporary file containing the contents of the diff pre-image.
file2 is set to the name of the temporary file containing the contents of the diff post-image.
merged is the name of the file which is being compared.
*/
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/exec"
"runtime/pprof"
"strings"
"time"
"github.com/octavore/delta"
"github.com/octavore/delta/delta/static"
"github.com/octavore/delta/delta/vendor/browser"
"github.com/octavore/delta/formatter"
)
const VERSION = "0.3.0"
func main() {
// only one of the following should be provided
open := flag.Bool("open", false, "open the file in the gui")
html := flag.Bool("html", false, "print out html")
install := flag.Bool("install", false, "install to gitconfig")
uninstall := flag.Bool("uninstall", false, "remove from gitconfig")
version := flag.Bool("version", false, "display delta version")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
flag.Parse()
switch {
case *version:
fmt.Println("delta", VERSION)
return
case *install:
installGit()
return
case *uninstall:
uninstallGit()
return
}
if flag.NArg() < 2 {
flag.PrintDefaults()
return
}
pathFrom := flag.Arg(0)
pathTo := flag.Arg(1)
pathBase := flag.Arg(1)
if flag.NArg() > 2 {
pathBase = flag.Arg(2)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *open {
openDiff(pathFrom, pathTo, pathBase)
} else {
printDiff(pathFrom, pathTo, *html)
}
}
func getAsset(path string) string {
a, err := static.Asset(path)
if err != nil {
panic(err)
}
return string(a)
}
// openDiffs diffs the given files and writes the result to a tempfile,
// then opens it in the gui.
func openDiff(pathFrom, pathTo, pathBase string) {
d, err := diff(pathFrom, pathTo)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
change := changeModified
if pathTo == "/dev/null" {
change = changeDeleted
} else if pathFrom == "/dev/null" {
change = changeAdded
}
// normalize paths so we don't have tmp on the path
tmpFrom := strings.HasPrefix(pathFrom, os.TempDir())
tmpTo := strings.HasPrefix(pathTo, os.TempDir())
if tmpFrom && !tmpTo {
pathFrom = pathTo
} else if !tmpFrom && tmpTo {
pathTo = pathFrom
}
wd, _ := os.Getwd()
html := formatter.HTML(d)
m := &Metadata{
From: pathFrom,
To: pathTo,
Merged: pathBase,
Dir: wd,
Change: change,
Hash: md5sum(html),
DirHash: md5sum(wd),
Timestamp: time.Now().UnixNano() / 1000000, // convert to millis
}
meta, _ := json.Marshal(m)
tmpl := template.Must(template.New("compare").Parse(getAsset("compare.html")))
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, map[string]interface{}{
"metadata": template.JS(string(meta)),
"content": template.HTML(html),
"CSS": template.CSS(getAsset("app.css")),
"JS": map[string]interface{}{
"mithril": template.JS(getAsset("vendor/mithril.min.js")),
"mousetrap": template.JS(getAsset("vendor/mousetrap.min.js")),
"highlight": template.JS(getAsset("vendor/highlight.js")),
"pouchdb": template.JS(getAsset("vendor/pouchdb.min.js")),
"app": template.JS(getAsset("app.js")),
},
})
if err != nil {
panic(err)
}
browser.OpenReader(buf)
}
// diff reads in files in pathFrom and pathTo, and returns a diff
func diff(pathFrom, pathTo string) (*delta.DiffSolution, error) {
from, err := ioutil.ReadFile(pathFrom)
if err != nil {
return nil, fmt.Errorf("error reading %q: %v", pathFrom, err)
}
to, err := ioutil.ReadFile(pathTo)
if err != nil {
return nil, fmt.Errorf("error reading %q: %v", pathTo, err)
}
return delta.HistogramDiff(string(from), string(to)), nil
}
func printDiff(pathFrom, pathTo string, html bool) {
d, err := diff(pathFrom, pathTo)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
if html {
fmt.Println(formatter.HTML(d))
return
}
fmt.Println(formatter.ColoredText(d))
}
func installGit() {
commands := [][]string{
{"git", "config", "--global", "diff.tool", "delta"},
{"git", "config", "--global", "difftool.prompt", "false"},
{"git", "config", "--global", "difftool.delta.cmd", `delta -open "$LOCAL" "$REMOTE" "$MERGED"`},
}
for _, c := range commands {
fmt.Println(strings.Join(c, " "))
o, _ := exec.Command(c[0], c[1:]...).CombinedOutput()
fmt.Print(string(o))
}
}
// known issue: this does not remove the gitconfig section if the unset
// operation causes the section to become empty.
func uninstallGit() {
commands := [][]string{
{"git", "config", "--global", "--unset", "diff.tool"},
{"git", "config", "--global", "--unset", "difftool.prompt"},
{"git", "config", "--global", "--remove-section", "difftool.delta"},
}
for _, c := range commands {
fmt.Println(strings.Join(c, " "))
o, _ := exec.Command(c[0], c[1:]...).CombinedOutput()
fmt.Print(string(o))
}
}
func md5sum(s string) string {
hash := md5.New()
_, _ = hash.Write([]byte(s))
return hex.EncodeToString(hash.Sum(nil))
}
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
/*
delta is a command-line diff utility.
Usage:
`delta <file1> <file2> <merged>`
file1 is set to the name of the temporary file containing the contents of the diff pre-image.
file2 is set to the name of the temporary file containing the contents of the diff post-image.
merged is the name of the file which is being compared.
*/
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/exec"
"runtime/pprof"
"strings"
"time"
"github.com/octavore/delta"
"github.com/octavore/delta/delta/static"
"github.com/octavore/delta/delta/vendor/browser"
"github.com/octavore/delta/formatter"
)
const VERSION = "0.4.0"
func main() {
// only one of the following should be provided
cli := flag.Bool("cli", false, "open the file in the terminal")
html := flag.Bool("html", false, "print out html")
install := flag.Bool("install", false, "install to gitconfig")
uninstall := flag.Bool("uninstall", false, "remove from gitconfig")
version := flag.Bool("version", false, "display delta version")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
flag.Parse()
switch {
case *version:
fmt.Println("delta", VERSION)
return
case *install:
installGit()
return
case *uninstall:
uninstallGit()
return
}
if flag.NArg() < 2 {
flag.PrintDefaults()
return
}
pathFrom := flag.Arg(0)
pathTo := flag.Arg(1)
pathBase := flag.Arg(1)
if flag.NArg() > 2 {
pathBase = flag.Arg(2)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if !*cli {
openDiff(pathFrom, pathTo, pathBase)
} else {
printDiff(pathFrom, pathTo, *html)
}
}
func getAsset(path string) string {
a, err := static.Asset(path)
if err != nil {
panic(err)
}
return string(a)
}
// openDiffs diffs the given files and writes the result to a tempfile,
// then opens it in the gui.
func openDiff(pathFrom, pathTo, pathBase string) {
d, err := diff(pathFrom, pathTo)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
change := changeModified
if pathTo == "/dev/null" {
change = changeDeleted
} else if pathFrom == "/dev/null" {
change = changeAdded
}
// normalize paths so we don't have tmp on the path
tmpFrom := strings.HasPrefix(pathFrom, os.TempDir())
tmpTo := strings.HasPrefix(pathTo, os.TempDir())
if tmpFrom && !tmpTo {
pathFrom = pathTo
} else if !tmpFrom && tmpTo {
pathTo = pathFrom
}
wd, _ := os.Getwd()
html := formatter.HTML(d)
m := &Metadata{
From: pathFrom,
To: pathTo,
Merged: pathBase,
Dir: wd,
Change: change,
Hash: md5sum(html),
DirHash: md5sum(wd),
Timestamp: time.Now().UnixNano() / 1000000, // convert to millis
}
meta, _ := json.Marshal(m)
tmpl := template.Must(template.New("compare").Parse(getAsset("compare.html")))
buf := &bytes.Buffer{}
err = tmpl.Execute(buf, map[string]interface{}{
"metadata": template.JS(string(meta)),
"content": template.HTML(html),
"CSS": template.CSS(getAsset("app.css")),
"JS": map[string]interface{}{
"mithril": template.JS(getAsset("vendor/mithril.min.js")),
"mousetrap": template.JS(getAsset("vendor/mousetrap.min.js")),
"highlight": template.JS(getAsset("vendor/highlight.js")),
"pouchdb": template.JS(getAsset("vendor/pouchdb.min.js")),
"app": template.JS(getAsset("app.js")),
},
})
if err != nil {
panic(err)
}
browser.OpenReader(buf)
}
// diff reads in files in pathFrom and pathTo, and returns a diff
func diff(pathFrom, pathTo string) (*delta.DiffSolution, error) {
from, err := ioutil.ReadFile(pathFrom)
if err != nil {
return nil, fmt.Errorf("error reading %q: %v", pathFrom, err)
}
to, err := ioutil.ReadFile(pathTo)
if err != nil {
return nil, fmt.Errorf("error reading %q: %v", pathTo, err)
}
return delta.HistogramDiff(string(from), string(to)), nil
}
func printDiff(pathFrom, pathTo string, html bool) {
d, err := diff(pathFrom, pathTo)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
if html {
fmt.Println(formatter.HTML(d))
return
}
fmt.Println(formatter.ColoredText(d))
}
func installGit() {
commands := [][]string{
{"git", "config", "--global", "diff.tool", "delta"},
{"git", "config", "--global", "difftool.prompt", "false"},
{"git", "config", "--global", "difftool.delta.cmd", `delta "$LOCAL" "$REMOTE" "$MERGED"`},
}
for _, c := range commands {
fmt.Println(strings.Join(c, " "))
o, _ := exec.Command(c[0], c[1:]...).CombinedOutput()
fmt.Print(string(o))
}
}
// known issue: this does not remove the gitconfig section if the unset
// operation causes the section to become empty.
func uninstallGit() {
commands := [][]string{
{"git", "config", "--global", "--unset", "diff.tool"},
{"git", "config", "--global", "--unset", "difftool.prompt"},
{"git", "config", "--global", "--remove-section", "difftool.delta"},
}
for _, c := range commands {
fmt.Println(strings.Join(c, " "))
o, _ := exec.Command(c[0], c[1:]...).CombinedOutput()
fmt.Print(string(o))
}
}
func md5sum(s string) string {
hash := md5.New()
_, _ = hash.Write([]byte(s))
return hex.EncodeToString(hash.Sum(nil))
}