aboutsummaryrefslogtreecommitdiff
path: root/object/object.go
blob: c40769a70dbc5af4000f66918d91a301215a8b74 (about) (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
// Package object provides Git object models and codecs.
package object

import (
	"bytes"
	"errors"
	"fmt"
	"strconv"
)

var (
	// ErrInvalidObject indicates malformed serialized data.
	ErrInvalidObject = errors.New("object: invalid object encoding")
	// ErrNotFound indicates missing entries in in-memory lookups.
	ErrNotFound = errors.New("object: not found")
)

// Type mirrors Git object type tags.
type Type uint8

const (
	TypeInvalid  Type = 0
	TypeCommit   Type = 1
	TypeTree     Type = 2
	TypeBlob     Type = 3
	TypeTag      Type = 4
	TypeFuture   Type = 5
	TypeOfsDelta Type = 6
	TypeRefDelta Type = 7
)

const (
	typeNameBlob   = "blob"
	typeNameTree   = "tree"
	typeNameCommit = "commit"
	typeNameTag    = "tag"
)

// Object is a Git object that can serialize itself.
type Object interface {
	ObjectType() Type
	Serialize() ([]byte, error)
}

// ParseTypeName parses a canonical Git object type name.
func ParseTypeName(name string) (Type, error) {
	switch name {
	case typeNameBlob:
		return TypeBlob, nil
	case typeNameTree:
		return TypeTree, nil
	case typeNameCommit:
		return TypeCommit, nil
	case typeNameTag:
		return TypeTag, nil
	default:
		return TypeInvalid, ErrInvalidObject
	}
}

func typeName(ty Type) (string, error) {
	switch ty {
	case TypeBlob:
		return typeNameBlob, nil
	case TypeTree:
		return typeNameTree, nil
	case TypeCommit:
		return typeNameCommit, nil
	case TypeTag:
		return typeNameTag, nil
	default:
		return "", fmt.Errorf("object: unsupported type %d", ty)
	}
}

func headerForType(ty Type, body []byte) ([]byte, error) {
	tyStr, err := typeName(ty)
	if err != nil {
		return nil, err
	}
	size := strconv.Itoa(len(body))
	var buf bytes.Buffer
	buf.Grow(len(tyStr) + len(size) + 2)
	buf.WriteString(tyStr)
	buf.WriteByte(' ')
	buf.WriteString(size)
	buf.WriteByte(0)
	return buf.Bytes(), nil
}