-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdelta.go
214 lines (192 loc) · 5.47 KB
/
delta.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
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
212
213
214
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"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"strings"
"time"
"github.com/octavore/delta/lib"
"github.com/octavore/delta/lib/formatter"
"github.com/pkg/browser"
)
// constants for command line options
const (
OutputOptionCLI = "cli"
OutputOptionBrowser = "browser"
OutputOptionGist = "gist"
FormatOptionHTML = "html"
FormatOptionText = "text"
FormatOptionDefault = "default"
)
var (
// commands
install = flag.Bool("install", false, "Install to gitconfig.")
uninstall = flag.Bool("uninstall", false, "Remove from gitconfig.")
version = flag.Bool("version", false, "Display delta version.")
// diff settings
output = flag.String("output", "cli", "Where to send the output. Valid values: browser (default), cli, gist.")
format = flag.String("format", "default", `Format of the output. `)
)
func main() {
flag.CommandLine.Usage = printHelp
flag.Parse()
if *install || *uninstall || *version {
switch {
case *version:
printVersion()
case *install:
installGit()
case *uninstall:
uninstallGit()
}
return
}
if flag.NArg() < 2 {
printVersion()
printHelp()
return
}
pathFrom, pathTo := flag.Arg(0), flag.Arg(1)
pathBase := pathTo
if flag.NArg() > 2 {
pathBase = flag.Arg(2)
}
runDiff(pathFrom, pathTo, pathBase)
}
func printHelp() {
fmt.Println()
fmt.Println("USAGE:")
fmt.Println()
fmt.Println("delta OPTION_COMMAND")
fmt.Printf("%-20s %s\n", " --install", "Install delta to gitconfig.")
fmt.Printf("%-20s %s\n", " --uninstall", "Remove delta from gitconfig.")
fmt.Printf("%-20s %s\n", " --version", "Display delta version.")
// diff settings
fmt.Println("\ndelta [OPTIONS] FILE1 FILE2")
fmt.Printf("%-20s %s\n", " --output", "Where to send the output. Valid values: browser, cli (default), gist.")
fmt.Printf("%-20s %s\n", " --format", `Valid values: default (text for cli, html otherwise), html, text.`)
fmt.Println()
}
func printVersion() {
fmt.Println("delta", Version)
}
func runDiff(pathFrom, pathTo, pathBase string) {
config, err := loadConfig()
if err != nil {
os.Stderr.WriteString("warning: error parsing .deltarc file")
}
d, err := diff(pathFrom, pathTo)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
if *format == FormatOptionDefault {
switch *output {
case OutputOptionBrowser, OutputOptionGist:
*format = FormatOptionHTML
case OutputOptionCLI:
*format = FormatOptionText
}
}
switch *format {
case FormatOptionHTML:
page, err := html(d, pathFrom, pathTo, pathBase, config)
if err != nil {
os.Stderr.WriteString(err.Error())
return
}
switch *output {
case OutputOptionCLI:
page.WriteTo(os.Stdout)
case OutputOptionGist:
uploadGist(page.Bytes())
case OutputOptionBrowser:
browser.OpenReader(page)
}
case FormatOptionText:
switch *output {
case OutputOptionCLI:
fmt.Println(formatter.ColoredText(d))
case OutputOptionGist:
uploadGist([]byte(formatter.Text(d)))
case OutputOptionBrowser:
browser.OpenReader(bytes.NewBufferString(formatter.Text(d)))
}
}
}
// openDiffs diffs the given files and writes the result to a tempfile,
// then opens it in the gui.
func html(d *delta.DiffSolution, pathFrom, pathTo, pathBase string, config Config) (*bytes.Buffer, error) {
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)
cfg, _ := json.Marshal(config)
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)),
"config": template.JS(cfg),
"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.min.js")),
"pouchdb": template.JS(getAsset("vendor/pouchdb.min.js")),
"app": template.JS(getAsset("app.js")),
},
})
return buf, err
}
// 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
}