aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packfile/objects.go
blob: 92860902196c6eeab3ae3ded03eddb37b85f91bd (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package packfile

import (
	"bytes"
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"strconv"
	"time"
)

type ObjectType int8

const (
	CommitObject   ObjectType = 1
	TreeObject     ObjectType = 2
	BlobObject     ObjectType = 3
	TagObject      ObjectType = 4
	OFSDeltaObject ObjectType = 6
	REFDeltaObject ObjectType = 7
)

func (t ObjectType) String() string {
	switch t {
	case CommitObject:
		return "commit"
	case TreeObject:
		return "tree"
	case BlobObject:
		return "blob"
	default:
		return "-"
	}
}

type RAWObject struct {
	Hash  Hash
	Type  ObjectType
	Size  uint64
	Bytes []byte
}

// Object generic object interface
type Object interface {
	Type() ObjectType
	Hash() Hash
}

// Hash SHA1 hased content
type Hash [20]byte

// ComputeHash compute the hash for a given objType and content
func ComputeHash(t ObjectType, content []byte) Hash {
	h := []byte(t.String())
	h = append(h, ' ')
	h = strconv.AppendInt(h, int64(len(content)), 10)
	h = append(h, 0)
	h = append(h, content...)

	return Hash(sha1.Sum(h))
}

func NewHash(s string) Hash {
	b, _ := hex.DecodeString(s)

	var h Hash
	copy(h[:], b)

	return h
}

func (h Hash) String() string {
	return hex.EncodeToString(h[:])
}

// Commit points to a single tree, marking it as what the project looked like
// at a certain point in time. It contains meta-information about that point
// in time, such as a timestamp, the author of the changes since the last
// commit, a pointer to the previous commit(s), etc.
// http://schacon.github.io/gitbook/1_the_git_object_model.html
type Commit struct {
	Tree      Hash
	Parents   []Hash
	Author    Signature
	Committer Signature
	Message   string
	hash      Hash
}

// ParseCommit transform a byte slice into a Commit struct
func ParseCommit(b []byte) (*Commit, error) {
	o := &Commit{hash: ComputeHash(CommitObject, b)}

	lines := bytes.Split(b, []byte{'\n'})
	for i := range lines {
		if len(lines[i]) > 0 {
			var err error

			split := bytes.SplitN(lines[i], []byte{' '}, 2)
			switch string(split[0]) {
			case "tree":
				_, err = hex.Decode(o.Tree[:], split[1])
			case "parent":
				var h Hash
				_, err = hex.Decode(h[:], split[1])
				if err == nil {
					o.Parents = append(o.Parents, h)
				}
			case "author":
				o.Author = ParseSignature(split[1])
			case "committer":
				o.Committer = ParseSignature(split[1])
			}

			if err != nil {
				return nil, err
			}
		} else {
			o.Message = string(bytes.Join(append(lines[i+1:]), []byte{'\n'}))
			break
		}

	}

	return o, nil
}

// Type returns the object type
func (o *Commit) Type() ObjectType {
	return CommitObject
}

// Hash returns the computed hash of the commit
func (o *Commit) Hash() Hash {
	return o.hash
}

// Tree is basically like a directory - it references a bunch of other trees
// and/or blobs (i.e. files and sub-directories)
type Tree struct {
	Entries []TreeEntry
	hash    Hash
}

// TreeEntry represents a file
type TreeEntry struct {
	Name string
	Hash Hash
}

// ParseTree transform a byte slice into a Tree struct
func ParseTree(b []byte) (*Tree, error) {
	o := &Tree{hash: ComputeHash(TreeObject, b)}

	if len(b) == 0 {
		return o, nil
	}

	for {
		split := bytes.SplitN(b, []byte{0}, 2)
		split1 := bytes.SplitN(split[0], []byte{' '}, 2)

		entry := TreeEntry{}
		entry.Name = string(split1[1])
		copy(entry.Hash[:], split[1][0:20])

		o.Entries = append(o.Entries, entry)

		b = split[1][20:]
		if len(split[1]) == 20 {
			break
		}
	}

	return o, nil
}

// Type returns the object type
func (o *Tree) Type() ObjectType {
	return TreeObject
}

// Hash returns the computed hash of the tree
func (o *Tree) Hash() Hash {
	return o.hash
}

// Blob is used to store file data - it is generally a file.
type Blob struct {
	Len  int
	hash Hash
}

// ParseBlob transform a byte slice into a Blob struct
func ParseBlob(b []byte) (*Blob, error) {
	return &Blob{
		Len:  len(b),
		hash: ComputeHash(BlobObject, b),
	}, nil
}

// Type returns the object type
func (o *Blob) Type() ObjectType {
	return BlobObject
}

// Hash returns the computed hash of the blob
func (o *Blob) Hash() Hash {
	return o.hash
}

type ContentCallback func(hash Hash, content []byte)

// Signature represents an action signed by a person
type Signature struct {
	Name  string
	Email string
	When  time.Time
}

// ParseSignature parse a byte slice returning a new action signature.
func ParseSignature(signature []byte) Signature {
	ret := Signature{}
	if len(signature) == 0 {
		return ret
	}

	from := 0
	state := 'n' // n: name, e: email, t: timestamp, z: timezone
	for i := 0; ; i++ {
		var c byte
		var end bool
		if i < len(signature) {
			c = signature[i]
		} else {
			end = true
		}

		switch state {
		case 'n':
			if c == '<' || end {
				if i == 0 {
					break
				}
				ret.Name = string(signature[from : i-1])
				state = 'e'
				from = i + 1
			}
		case 'e':
			if c == '>' || end {
				ret.Email = string(signature[from:i])
				i++
				state = 't'
				from = i + 1
			}
		case 't':
			if c == ' ' || end {
				t, err := strconv.ParseInt(string(signature[from:i]), 10, 64)
				if err == nil {
					ret.When = time.Unix(t, 0)
				}
				end = true
			}
		}

		if end {
			break
		}
	}

	return ret
}

func (s *Signature) String() string {
	return fmt.Sprintf("%q <%s> @ %s", s.Name, s.Email, s.When)
}