/
tree.go
52 lines (44 loc) · 1.23 KB
/
tree.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
package mingit
import (
"bytes"
"fmt"
"os"
)
// Tree represents the data for a git tree, which you can then encode into
// a Git object using the Object method.
type Tree []TreeItem
// Object encodes the data from the Tree into a Git object ready to be
// stored into a repository.
func (t Tree) Object() Object {
var raw []byte
for _, item := range t {
raw = item.appendRaw(raw)
}
var buf bytes.Buffer
fmt.Fprintf(&buf, "tree %d\x00%s", len(raw), raw)
return Object{
raw: buf.Bytes(),
}
}
// TreeItem represents a single item in a tree.
type TreeItem struct {
Mode os.FileMode
Name string
TargetID ObjectID
}
func (ti *TreeItem) appendRaw(to []byte) []byte {
buf := bytes.NewBuffer(to)
fmt.Fprintf(buf, "%o %s\x00%s", ti.Mode, ti.Name, ti.TargetID.hash)
return buf.Bytes()
}
const (
// ModeRegular is the file mode Git uses for regular (non-executable) files.
// The target must be a blob in this case.
ModeRegular os.FileMode = 0100644
// ModeExecutable is the file mode Git uses for executable files.
// The target must be a blob in this case.
ModeExecutable os.FileMode = 0100755
// ModeDir is the file mode Git uses for directories.
// The target must be a tree in this case.
ModeDir os.FileMode = 040000
)