/
commit.go
45 lines (40 loc) · 1.08 KB
/
commit.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
package mingit
import (
"bytes"
"fmt"
)
// Commit represents the data for a commit, which you can then encode into
// a Git object using the Object method.
type Commit struct {
TreeID ObjectID
ParentIDs []ObjectID
Author *Identity
Committer *Identity
Message string
}
// Object encodes the data from the Commit into a Git object ready to be
// stored into a repository.
func (c *Commit) Object() Object {
content := c.ForStorage()
var buf bytes.Buffer
fmt.Fprintf(&buf, "commit %d\x00%s", len(content), content)
return Object{
raw: buf.Bytes(),
}
}
// ForStorage returns a byte slice containing the commit data in the raw
// format that git would use inside a generated object.
func (c *Commit) ForStorage() []byte {
return c.appendRaw(nil)
}
func (c *Commit) appendRaw(to []byte) []byte {
buf := bytes.NewBuffer(to)
fmt.Fprintf(buf, "tree %s\n", c.TreeID)
for _, id := range c.ParentIDs {
fmt.Fprintf(buf, "parent %s\n", id)
}
fmt.Fprintf(buf, "author %s\n", c.Author)
fmt.Fprintf(buf, "committer %s\n\n", c.Committer)
buf.WriteString(c.Message)
return buf.Bytes()
}