aboutsummaryrefslogtreecommitdiffstats
path: root/repository/tree_entry.go
blob: 6716522c8036ce655995539e2e03f9c969b393a2 (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
package repository

import (
	"fmt"
	"github.com/MichaelMure/git-bug/util"
	"strings"
)

type TreeEntry struct {
	ObjectType ObjectType
	Hash       util.Hash
	Name       string
}

type ObjectType int

const (
	Unknown ObjectType = iota
	Blob
	Tree
)

func ParseTreeEntry(line string) (TreeEntry, error) {
	fields := strings.Fields(line)

	if len(fields) < 4 {
		return TreeEntry{}, fmt.Errorf("Invalid input to parse as a TreeEntry")
	}

	objType, err := ParseObjectType(fields[0], fields[1])

	if err != nil {
		return TreeEntry{}, err
	}

	hash := util.Hash(fields[2])
	name := strings.Join(fields[3:], "")

	return TreeEntry{
		ObjectType: objType,
		Hash:       hash,
		Name:       name,
	}, nil
}

// Format the entry as a git ls-tree compatible line
func (entry TreeEntry) Format() string {
	return fmt.Sprintf("%s %s\t%s\n", entry.ObjectType.Format(), entry.Hash, entry.Name)
}

func (ot ObjectType) Format() string {
	switch ot {
	case Blob:
		return "100644 blob"
	case Tree:
		return "040000 tree"
	default:
		panic("Unknown git object type")
	}
}

func ParseObjectType(mode, objType string) (ObjectType, error) {
	switch {
	case mode == "100644" && objType == "blob":
		return Blob, nil
	case mode == "040000" && objType == "tree":
		return Tree, nil
	default:
		return Unknown, fmt.Errorf("Unknown git object type %s %s", mode, objType)
	}
}