This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
/
objects.go
233 lines (196 loc) · 4.5 KB
/
objects.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
package gig
import (
"encoding/hex"
"fmt"
"io"
"os"
"strings"
"time"
)
//SHA1 is the object identifying checksum of
// the object data
type SHA1 [20]byte
func (oid SHA1) String() string {
return hex.EncodeToString(oid[:])
}
//ParseSHA1 expects a string with a hex encoded sha1.
//It will trim the string of newline and space before
//parsing.
func ParseSHA1(input string) (sha SHA1, err error) {
data, err := hex.DecodeString(strings.Trim(input, " \n"))
if err != nil {
return
} else if len(data) != 20 {
err = fmt.Errorf("git: sha1 must be 20 bytes")
return
}
copy(sha[:], data)
return
}
//Signature is a combination of who (Name, Email) and when (Date, Offset).
//Used by Commit, Tag to link an action (committer, author, tagger, ...)
//with a person in a point in time.
type Signature struct {
Name string
Email string
Date time.Time
Offset *time.Location
}
func (s Signature) String() string {
return fmt.Sprintf("%s <%s> %d %s", s.Name, s.Email, s.Date.Unix(), s.Offset)
}
//ObjectType is to the git object type
type ObjectType byte
//The defined bits match the ones used in
//the git pack file format.
const (
_ = iota
ObjCommit = ObjectType(iota)
ObjTree
ObjBlob
ObjTag
ObjOFSDelta = ObjectType(0x6)
ObjRefDelta = ObjectType(0x7)
)
//ParseObjectType takes a string and converts it
//to the corresponding ObjectType or error if
//the string doesn't match any type.
func ParseObjectType(s string) (ObjectType, error) {
s = strings.Trim(s, "\n ")
switch s {
case "commit":
return ObjCommit, nil
case "tree":
return ObjTree, nil
case "blob":
return ObjBlob, nil
case "tag":
return ObjTag, nil
}
return ObjectType(0), fmt.Errorf("git: unknown object: %q", s)
}
func (ot ObjectType) String() string {
switch ot {
case ObjCommit:
return "commit"
case ObjTree:
return "tree"
case ObjBlob:
return "blob"
case ObjTag:
return "tag"
case ObjOFSDelta:
return "delta-ofs"
case ObjRefDelta:
return "delta-ref"
}
return "unknown"
}
//IsStandardObject checks if an object is
//one of the four common objects such as
//commit, tree, blob, tag.
func IsStandardObject(ot ObjectType) bool {
return ot > 0 && ot < 5
}
//IsDeltaObject checks if an object is a
//delta object, i.e. OFSDelta or RefDelta
func IsDeltaObject(ot ObjectType) bool {
return ot == ObjOFSDelta || ot == ObjRefDelta
}
//Object represents a git object. It has
//information common to all git objects,
//like their type and their size. Also,
//all git objects should be closed via
//Close().
type Object interface {
Type() ObjectType
Size() int64
io.WriterTo
io.Closer
}
type gitObject struct {
otype ObjectType
size int64
source io.ReadCloser
}
func (o *gitObject) Type() ObjectType {
return o.otype
}
func (o *gitObject) Size() int64 {
return o.size
}
func (o *gitObject) Close() error {
if o.source == nil {
return nil
}
return o.source.Close()
}
//Commit represents one git commit.
type Commit struct {
gitObject
Tree SHA1
Parent []SHA1
Author Signature
Committer Signature
Message string
GPGSig string
}
//Date returns the commit timestamps (with the correct location).
func (c *Commit) Date() time.Time {
return c.Committer.Date.In(c.Committer.Offset)
}
//Tree represents the git tree object.
type Tree struct {
gitObject
entry *TreeEntry
err error
}
//TreeEntry holds information about a single
//entry in the git Tree object.
type TreeEntry struct {
Mode os.FileMode
Type ObjectType
ID SHA1
Name string
}
//Next advances the pointer to the next TreeEntry
//within the Tree object. Returns false if it was
//pointing to the last element (EOF condition), or
//if there was an error while advacing. Use Err()
//to resolve between the to conditions.
func (tree *Tree) Next() bool {
tree.entry, tree.err = parseTreeEntry(tree.source)
return tree.err == nil
}
//Err returns the last error non-EOF error encountered.
func (tree *Tree) Err() error {
if err := tree.err; err != nil && err != io.EOF {
return err
}
return nil
}
//Entry returns the current TreeEntry.
func (tree *Tree) Entry() *TreeEntry {
return tree.entry
}
//Blob represents a git blob object.
type Blob struct {
gitObject
}
func (b *Blob) Read(data []byte) (n int, err error) {
n, err = b.source.Read(data)
return
}
func MakeAnnexBlob(fp *os.File, size int64) *Blob {
return &Blob{gitObject{otype: ObjBlob, size: size, source: fp}}
}
//Tag represents a git tag object.
type Tag struct {
gitObject
Object SHA1
ObjType ObjectType
Tag string
Tagger Signature
Message string
GPGSig string
}