/
object.go
190 lines (154 loc) · 3.36 KB
/
object.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
// don't do go generate ./... //go:generate stringer -type=ObjectType
package object
import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"io"
"strings"
"github.com/izhujiang/gogit/common"
)
type ObjectKind byte
const (
Kind_Blob ObjectKind = iota
Kind_Tree
Kind_Commit
Kind_Tag
Kind_Unknow
)
var (
ErrGitObjectDataCorruptted = errors.New("Git object data corruptted.")
ErrInvalidObject = errors.New("Invalidate Object.")
)
func (t ObjectKind) String() string {
switch t {
case Kind_Blob:
return "blob"
case Kind_Tree:
return "tree"
case Kind_Commit:
return "commit"
case Kind_Tag:
return "tag"
default:
// TODO: Others object type should be implemented.
return "unknown"
}
}
func ParseObjectKind(kind string) ObjectKind {
switch kind {
case "blob":
return Kind_Blob
case "tree":
return Kind_Tree
case "commit":
return Kind_Commit
case "tag":
return Kind_Tag
default:
return Kind_Unknow
}
}
func ObjectKindFromFilemode(fm common.FileMode) ObjectKind {
if common.IsFile(fm) {
return Kind_Blob
} else if common.IsDir(fm) {
return Kind_Tree
} else {
return Kind_Unknow
}
}
type Object interface {
Id() common.Hash
Kind() ObjectKind
Content() string
}
// GitObject, unmodifiable object
type GitObject struct {
oid common.Hash
// header
objectKind ObjectKind
content []byte // unzipped content
}
func NewGitObject(t ObjectKind, content []byte) *GitObject {
s := len(content)
c := make([]byte, s)
copy(c, content)
g := &GitObject{
objectKind: t,
content: c,
}
// TODO: add condition to control hash or not.
g.Hash()
return g
}
func (g *GitObject) Id() common.Hash {
return g.oid
}
func (g *GitObject) Kind() ObjectKind {
return g.objectKind
}
func (g *GitObject) Size() int64 {
return int64(len(g.content))
}
func (g *GitObject) Content() string {
return string(g.content)
}
func (g *GitObject) Hash() common.Hash {
g.oid = common.HashObject(g.objectKind.String(), g.content)
return g.oid
}
// Load GitObject from stream (git repository)
func Load(r io.Reader, oid common.Hash) (*GitObject, error) {
zr, _ := zlib.NewReader(r)
defer zr.Close()
var objtype string
var size int64
// read header
fmt.Fscanf(zr, "%s %d\x00", &objtype, &size)
kind := ParseObjectKind(objtype)
content := make([]byte, size)
n, _ := zr.Read(content)
g := NewGitObject(kind, content)
if n != len(g.content) || oid != g.Id() {
return nil, ErrGitObjectDataCorruptted
}
return g, nil
}
// Save GitObject to stream (git repository)
func (g *GitObject) Save(w io.Writer) error {
wt := zlib.NewWriter(w)
defer wt.Close()
// write header and content
size := int64(len(g.content))
header := fmt.Sprintf("%s %d\x00", strings.ToLower(g.objectKind.String()), size)
_, err := wt.Write([]byte(header))
_, err = wt.Write(g.content)
err = wt.Flush()
return err
}
// Dump object in .git repository
func DumpGitObject(r io.Reader, w io.Writer) {
buf := &bytes.Buffer{}
reader, _ := zlib.NewReader(r)
defer reader.Close()
io.Copy(buf, reader)
// fmt.Println(string(buf.Bytes()))
b := make([]byte, 16)
empty := make([]byte, 16)
addr := 0x00
for {
n, err := buf.Read(b)
if err == io.EOF {
break
}
if n < 16 {
copy(b[n:], empty[n:])
}
bb := strings.ReplaceAll(string(b), "\t", "\\t")
bb = strings.ReplaceAll(bb, "\n", "\\n")
fmt.Fprintf(w, "%08x % x % x %s\n", addr, b[:8], b[8:], bb)
addr += 16
}
}