1
2
3
4
5
6
7 package diff
8
9 import (
10 "bytes"
11 exec "internal/execabs"
12 "io/ioutil"
13 "os"
14 "runtime"
15 )
16
17
18 func Diff(prefix string, b1, b2 []byte) ([]byte, error) {
19 f1, err := writeTempFile(prefix, b1)
20 if err != nil {
21 return nil, err
22 }
23 defer os.Remove(f1)
24
25 f2, err := writeTempFile(prefix, b2)
26 if err != nil {
27 return nil, err
28 }
29 defer os.Remove(f2)
30
31 cmd := "diff"
32 if runtime.GOOS == "plan9" {
33 cmd = "/bin/ape/diff"
34 }
35
36 data, err := exec.Command(cmd, "-u", f1, f2).CombinedOutput()
37 if len(data) > 0 {
38
39
40 err = nil
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 if len(data) > 0 && !bytes.HasPrefix(data, []byte("--- ")) {
55 i := bytes.Index(data, []byte("\n--- "))
56 if i >= 0 && i < 80*10 && bytes.Contains(data[:i], []byte("://cygwin.com/")) {
57 data = data[i+1:]
58 }
59 }
60
61 return data, err
62 }
63
64 func writeTempFile(prefix string, data []byte) (string, error) {
65 file, err := ioutil.TempFile("", prefix)
66 if err != nil {
67 return "", err
68 }
69 _, err = file.Write(data)
70 if err1 := file.Close(); err == nil {
71 err = err1
72 }
73 if err != nil {
74 os.Remove(file.Name())
75 return "", err
76 }
77 return file.Name(), nil
78 }
79
View as plain text