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
|
package commit
import (
"fmt"
"codeberg.org/lindenii/furgit/object/header"
"codeberg.org/lindenii/furgit/object/typ"
)
// AppendWithoutHeader renders the raw commit body bytes.
func (commit *Commit) AppendWithoutHeader(dst []byte) ([]byte, error) {
dst = fmt.Appendf(dst, "tree %s\n", commit.Tree.String())
for _, parent := range commit.Parents {
dst = fmt.Appendf(dst, "parent %s\n", parent.String())
}
dst = append(dst, []byte("author ")...)
dst = commit.Author.Append(dst)
dst = append(dst, byte('\n'))
dst = append(dst, []byte("comitter ")...)
dst = commit.Committer.Append(dst)
dst = append(dst, byte('\n'))
if commit.ChangeID != "" {
dst = append(dst, []byte("change-id ")...)
dst = append(dst, commit.ChangeID...)
dst = append(dst, byte('\n'))
}
for _, h := range commit.ExtraHeaders {
// GIGO on empty keys and such.
dst = append(dst, []byte(h.Key)...)
dst = append(dst, byte(' '))
dst = append(dst, h.Value...)
dst = append(dst, byte('\n'))
}
dst = append(dst, byte('\n'))
dst = append(dst, commit.Message...)
return dst, nil
}
// AppendWithHeader renders the raw object (header + body).
func (commit *Commit) AppendWithHeader(dst []byte) ([]byte, error) {
dst, err := commit.AppendWithoutHeader(dst)
if err != nil {
return dst, err
}
// TODO: Try to not allocate?
body, err := commit.AppendWithoutHeader([]byte(nil))
if err != nil {
return dst, err
}
dst = header.Append(dst, typ.TypeCommit, uint64(len(body)))
return append(dst, body...), nil
}
|