-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (217 loc) · 4.58 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
)
func gitdir() string {
pwd, err := os.Getwd()
if err != nil {
return ""
}
for {
candidate := path.Join(pwd, ".git")
fileInfo, err := os.Stat(candidate)
if err == nil && fileInfo.IsDir() {
return candidate
}
if pwd == "/" {
break
}
pwd = path.Dir(pwd)
}
panic("no git dir")
}
func gitBranches(gitdir string) []string {
refsHeads := path.Join(gitdir, "refs/heads")
if _, err := os.Open(path.Join(refsHeads)); err != nil {
panic("Open git dir failed")
}
return dirFiles(refsHeads)
}
func dirFiles(baseDir string) (files []string) {
base, _ := os.Open(baseDir)
fi, _ := base.Readdir(-1)
for _, f := range fi {
if f.IsDir() {
names := dirFiles(path.Join(baseDir, f.Name()))
for _, name := range names {
files = append(files, path.Join(f.Name(), name))
}
} else {
files = append(files, f.Name())
}
}
return
}
func readBranch(gitdir, branch string) (string, bool) {
file, err := os.Open(path.Join(gitdir, "refs/heads", branch))
if err != nil {
return "", false
}
defer file.Close()
buf := make([]byte, 40)
if n, _ := file.Read(buf); n < 40 {
return "", false
}
return string(buf), true
}
func readObject(gitdir, objId string) []byte {
objPath := path.Join(gitdir, "objects", objId[:2], objId[2:])
file, err := os.Open(objPath)
if err != nil {
panic("failed to open Object: " + objId + ":" + hex.EncodeToString([]byte(objId)))
}
r, _ := zlib.NewReader(file)
bytes, _ := ioutil.ReadAll(r)
r.Close()
return bytes
}
func readInt(b []byte) (value int, byteLength int) {
for _, d := range b {
if d >= '0' && d <= '9' {
byteLength++
value = value * 10 + int(d) - '0'
} else {
return
}
}
return
}
type commit struct {
id string
tree string
parent []string
message string
}
type tag struct {
objectType string
size int
}
func atoi(s string) int {
n, err := strconv.ParseInt(s, 10, 0)
if err != nil {
panic(err)
}
return int(n)
}
func readTag(b []byte) (tag, []byte) {
index := bytes.IndexByte(b, byte(0))
if index == -1 {
panic("no tag")
}
elements := strings.Split(string(b[:index]), " ")
return tag{elements[0], atoi(elements[1])}, b[index+1:]
}
func parseCommit(b []byte) *commit {
c := new(commit)
_, rest := readTag(b)
buf := bytes.NewBuffer(rest)
r := bufio.NewReader(buf)
for {
line, _, _ := r.ReadLine()
if string(line) == "" {
c.message = buf.String()
break
}
field, value := split(string(line), ' ')
switch field {
case "tree":
c.tree = value
case "parent":
c.parent = append(c.parent, value)
}
}
return c
}
type entry struct {
mode int
name string
id string
}
func (e *entry) isBlob() bool {
return e.mode != 40000
}
type tree struct {
entries []*entry
}
func (t *tree) Print() {
for _, e := range t.entries {
fmt.Println(e.mode, e.id, e.name)
}
}
func split(s string, sep rune) (a, b string) {
index := strings.IndexRune(s, sep)
if index == -1 {
return s, ""
}
return s[:index], s[index+1:]
}
func parseTree(b []byte) *tree {
var entries []*entry
_, rest := readTag(b)
for rest != nil {
index := bytes.IndexByte(rest, byte(0))
if index == -1 {
break
}
mode, name := split(string(rest[:index]), ' ')
objId := hex.EncodeToString(rest[index+1:][:20])
entries = append(entries, &entry{atoi(mode), name, objId})
rest = rest[index+21:]
}
return &tree{entries}
}
func lsTree(gitdir, commitish string) *tree {
c := commitFor(gitdir, commitish)
treeObject := readObject(gitdir, c.tree)
return parseTree(treeObject)
}
func catFile(dir, id string) {
object := readObject(dir, id)
_, rest := readTag(object)
fmt.Println(string(rest[:10]))
}
// <commitish> can be a branch name or commit-id
// if it is a branch name, read commit-id from it
// returns commit-id
func derefCommitish(dir, commitish string) string {
if id, ok := readBranch(dir, commitish); ok {
return id
}
return commitish
}
func commitFor(dir, commitish string) *commit {
id := derefCommitish(dir, commitish)
c := parseCommit(readObject(dir, id))
c.id = id
return c
}
func revList(dir, branch string) {
c := commitFor(dir, branch)
fmt.Println(c.id)
for _, p := range c.parent {
revList(dir, p)
}
}
// cat-file -t <SHA-1>
// : prints SHA-1's type (commit, blob, tree, tag)
// cat-file <type> <SHA-1>
// : prints SHA-1's contents according to the type
func main() {
dir := gitdir()
branches := gitBranches(dir)
firstBranch := branches[0]
lsTree(dir, firstBranch)
revList(dir, firstBranch)
fmt.Print("ok")
io.Copy(os.Stdout, strings.NewReader("\n"))
}