-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
140 lines (132 loc) · 3.52 KB
/
writer.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package objects
import (
"bytes"
"compress/zlib"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"github.com/richardjennings/mygit/internal/mygit/config"
"github.com/richardjennings/mygit/internal/mygit/refs"
"io"
"io/fs"
"os"
"path/filepath"
)
// WriteTree writes an Object Tree to the object store.
func (o *Object) WriteTree() ([]byte, error) {
// resolve child tree Objects
for i, v := range o.Objects {
if v.Typ == ObjectTree {
// if the tree only has blobs, write them and then
// add the corresponding tree returning the Sha
sha, err := v.WriteTree()
if err != nil {
return nil, err
}
o.Objects[i].Sha = sha
}
}
// write a tree obj with the resolved children
return o.writeTree()
}
func (o *Object) writeTree() ([]byte, error) {
var content []byte
var mode string
for _, fo := range o.Objects {
// @todo add executable support
if fo.Typ == ObjectTree {
mode = "40000"
} else {
mode = "100644"
}
// @todo replace base..
content = append(content, []byte(fmt.Sprintf("%s %s%s%s", mode, filepath.Base(fo.Path), string(byte(0)), fo.Sha))...)
}
header := []byte(fmt.Sprintf("tree %d%s", len(content), string(byte(0))))
return WriteObject(header, content, "", config.ObjectPath())
}
// WriteObject writes an object to the object store
func WriteObject(header []byte, content []byte, contentFile string, path string) ([]byte, error) {
var f *os.File
var err error
buf := bytes.NewBuffer(nil)
h := sha1.New()
z := zlib.NewWriter(buf)
r := io.MultiWriter(h, z)
if _, err := r.Write(header); err != nil {
return nil, err
}
if len(content) > 0 {
if _, err := r.Write(content); err != nil {
return nil, err
}
}
if contentFile != "" {
f, err = os.Open(contentFile)
if err != nil {
return nil, err
}
if _, err := io.Copy(r, f); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, err
}
}
sha := h.Sum(nil)
path = filepath.Join(path, hex.EncodeToString(sha)[:2])
// create object sha[:2] directory if needed
if err := os.MkdirAll(path, 0744); err != nil {
return nil, err
}
path = filepath.Join(path, hex.EncodeToString(sha)[2:])
// if object exists with Sha already we can avoid writing again
_, err = os.Stat(path)
if err == nil || !errors.Is(err, fs.ErrNotExist) {
// file exists
return sha, err
}
if err := z.Close(); err != nil {
return nil, err
}
return sha, os.WriteFile(path, buf.Bytes(), 0655)
}
// WriteBlob writes a file to the object store as a blob and returns
// a Blob Object representation.
func WriteBlob(path string) (*Object, error) {
path = filepath.Join(config.Path(), path)
finfo, err := os.Stat(path)
if err != nil {
return nil, err
}
header := []byte(fmt.Sprintf("blob %d%s", finfo.Size(), string(byte(0))))
sha, err := WriteObject(header, nil, path, config.ObjectPath())
return &Object{Sha: sha, Path: path}, err
}
func WriteCommit(c *Commit) ([]byte, error) {
var parentCommits string
for _, v := range c.Parents {
parentCommits += fmt.Sprintf("parent %s\n", v)
}
content := []byte(fmt.Sprintf(
"tree %s\n%sauthor %s %d +0000\ncommitter %s %d +0000\n\n%s",
hex.EncodeToString(c.Tree),
parentCommits,
c.Author,
c.AuthoredTime.Unix(),
c.Committer,
c.CommittedTime.Unix(),
c.Message,
))
header := []byte(fmt.Sprintf("commit %d%s", len(content), string(byte(0))))
sha, err := WriteObject(header, content, "", config.ObjectPath())
if err != nil {
return nil, err
}
branch, err := refs.CurrentBranch()
if err != nil {
return nil, err
}
return sha, refs.UpdateBranchHead(branch, sha)
}