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

import (
	"bytes"
	"fmt"
	"strings"
)

type TreeEntry struct {
	ObjectType ObjectType
	Hash       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 := 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)
	}
}

func prepareTreeEntries(entries []TreeEntry) bytes.Buffer {
	var buffer bytes.Buffer

	for _, entry := range entries {
		buffer.WriteString(entry.Format())
	}

	return buffer
}

func readTreeEntries(s string) ([]TreeEntry, error) {
	split := strings.Split(strings.TrimSpace(s), "\n")

	casted := make([]TreeEntry, len(split))
	for i, line := range split {
		if line == "" {
			continue
		}

		entry, err := ParseTreeEntry(line)

		if err != nil {
			return nil, err
		}

		casted[i] = entry
	}

	return casted, nil
}