summaryrefslogtreecommitdiff
path: root/src/edit/git.go
blob: 4ced7b7abc400b561cec9c7d87c00d1f5f05f292 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os/exec"
	"strconv"
	"time"

	"os"
)

type GitDir string

func (dir GitDir) cmd(args ...string) *exec.Cmd {
	cmd := exec.Command(args[0], args[1:]...)
	cmd.Dir = string(dir)
	return cmd
}

func (dir GitDir) output(args ...string) ([]byte, error) {
	return dir.cmd(args...).Output()
}

func (dir GitDir) run(args ...string) error {
	// Use .Output() instead of .Run() so that it populates
	// ExitError.Stderr.
	_, err := dir.output(args...)
	return err
}	

func (dir GitDir) Pull() error {
	return dir.run("git", "fetch")
}

func (dir GitDir) Push() error {
	return dir.run("git", "push")
}

func (dir GitDir) Commit(commit GitCommit) error {
	cmd := dir.cmd("git", "fast-import", "--done")

	// stdin
	pw, err := cmd.StdinPipe()
	if err != nil {
		return err
	}

	// stderr
	var buf bytes.Buffer
	cmd.Stderr = &buf

	// run
	if err = cmd.Start(); err != nil {
		return err
	}
	werr := commit.FastExport(pw)
	if werr != nil { goto end; }
	_, werr = io.WriteString(pw, "done\n")
	if werr != nil { goto end; }
end:
	cerr := pw.Close()
	if err = cmd.Wait(); err != nil {
		if ee, ok := err.(*exec.ExitError); ok {
			ee.Stderr = buf.Bytes()
		}
	} else if werr != nil {
		err = werr
	} else {
		err = cerr
	}
	return err
}

type GitCommit struct {
	UserName  string
	UserEmail string
	Time      time.Time
	Message   string
	Files     map[string][]byte
}

func gitTime(t time.Time) string {
	return fmt.Sprintf("%d %s", t.Unix(), t.Format("-0700"))
}

func (edit GitCommit) FastExport(w io.Writer) error {
	var err error
	commit := make([]string, len(edit.Files))
	i := 0
	for name, content := range edit.Files {
		commit[i] = name
		if content != nil {
			if _, err = fmt.Fprintf(w, "blob\nmark :%d\ndata %d\n%s", i+1, len(content), content); err != nil {
				return err
			}
		}
	}
	_, err = fmt.Fprintf(w, `commit refs/heads/master
author %s <%s> %s
committer %s <%s> %s
data %d
%sfrom HEAD
`,
		edit.UserName, edit.UserEmail, gitTime(edit.Time),
		edit.UserName, edit.UserEmail, gitTime(edit.Time),
		len(edit.Message), edit.Message)
	if err != nil {
		return err
	}
	for i, filename := range commit {
		if edit.Files[filename] != nil {
			if _, err = fmt.Fprintf(w, "M 100644 :%d %s\n", i+1, filename); err != nil {
				return err
			}
		} else {
			if _, err = fmt.Fprintf(w, "D %s\n", filename); err != nil {
				return err
			}
		}
	}
	return nil
}

type GitFile struct {
	Dir  GitDir
	Mode int32 // 18 bits unsigned
	Type string
	Hash string
	Size int64
}

func (f GitFile) Cat() ([]byte, error) {
	return f.Dir.output("git", "cat-file", "blob", f.Hash)
}

type GitTree map[string]GitFile

var (
	ParseErrorSpaces = errors.New("parse: git ls-tree: not enough spaces")
	ParseErrorTab    = errors.New("parse: git ls-tree: no tab")
	ParseErrorNull   = errors.New("parse: git ls-tree: no trailing null")
)

func parseGitTreeLine(line []byte) (rname string, rfile GitFile, err error) {
	// There is probably a better, shorter way of doing this

	// line = mode SP type SP hash SP size TAB name
	a := bytes.SplitN(line, []byte{' '}, 4)
	if len(a) != 4 {
		err = ParseErrorSpaces
		return
	}
	b := bytes.SplitN(a[3], []byte{'\t'}, 2)
	if len(b) != 2 {
		err = ParseErrorTab
		return
	}
	fmode := a[0]
	ftype := a[1]
	fhash := a[2]
	fsize := b[0]
	fname := b[1]

	// convert datatypes
	fmodeN, err := strconv.ParseInt(string(fmode), 10, 19)
	if err != nil {
		return
	}
	fsizeN := int64(-1)
	fsizeS := string(bytes.TrimLeft(fsize, " "))
	if fsizeS != "-" {
		fsizeN, err = strconv.ParseInt(fsizeS, 10, 64)
		if err != nil {
			return
		}
	}

	rname = string(fname)
	rfile = GitFile{
		Mode: int32(fmodeN),
		Type: string(ftype),
		Hash: string(fhash),
		Size: fsizeN,
	}
	return
}

func (dir GitDir) LsTree() (GitTree, error) {
	// Get the root tree-ish
	data, err := dir.output("git", "rev-parse", "HEAD:")
	if err != nil {
		return nil, err
	}
	treeish := string(bytes.TrimSuffix(data, []byte{'\n'}))

	// Recursively read said tree-ish
	data, err = dir.output("git", "ls-tree", "-trlz", treeish)
	if err != nil {
		return nil, err
	}
	lines := bytes.Split(data, []byte{0})
	// The last line is empty line so se can ignore it
	// (`len(lines)-1`), but because it doesn't include a line for
	// the root tree, we have to +1.
	ret := make(GitTree, len(lines))
	// Add the root tree
	ret["/"] = GitFile{
		Mode: int32(40000),
		Type: "tree",
		Hash: treeish,
		Size: -1,
	}
	// Parse the lines from git ls-tree
	for _, line := range lines {
		if len(line) == 0 {
			continue
		}
		name, file, err := parseGitTreeLine(line)
		if err != nil {
			panic(err)
			return nil, err
		}
		file.Dir = dir
		ret["/"+name] = file
	}
	return ret, nil
}