forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
blob.go
160 lines (136 loc) · 3.55 KB
/
blob.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package git
import (
"bytes"
"compress/zlib"
libsha1 "crypto/sha1"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type Blob struct {
*TreeEntry
}
func (b *Blob) Data() (io.ReadCloser, error) {
_, _, dataRc, err := b.ptree.repo.getRawObject(b.Id, false)
if err != nil {
return nil, err
}
return dataRc, nil
}
// Write `r` in git's compressed object format into `w`.
func copyCompressed(w io.Writer, r io.Reader) error {
cw, err := zlib.NewWriterLevel(w, zlib.BestSpeed)
if err != nil {
return err
}
_, err = io.Copy(cw, r)
if err != nil {
return err
}
err = cw.Close()
return err
}
// Add '<type> <size>\x00' to beginning of `r`.
func PrependObjectHeader(objectType ObjectType, r io.ReadSeeker) (io.Reader, error) {
size, err := r.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
_, err = r.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
buf := bytes.NewBufferString(fmt.Sprintf("%s %d\x00", objectType, size))
return io.MultiReader(buf, r), nil
}
// Write an the object contents in `r` in compressed form to `w` and return the
// hash.
// Special case: if `w` is `ioutil.Discard`, the data is not compressed,
// since the data is not consumed anywhere.
func StoreObjectSHA(
objectType ObjectType,
w io.Writer,
r io.ReadSeeker,
) (sha1, error) {
reader, err := PrependObjectHeader(objectType, r)
if err != nil {
return [20]byte{}, err
}
hash := libsha1.New()
reader = io.TeeReader(reader, hash)
if w == ioutil.Discard {
_, err = io.Copy(w, r)
} else {
err = copyCompressed(w, reader)
}
if err != nil {
return [20]byte{}, err
}
return NewId(hash.Sum(nil))
}
// Returns true if the content in `io.ReadSeeker` is aleady present in the
// object database. Leaves the initial seek position of `r` intact.
func (repo *Repository) HaveObjectFromReadSeeker(
objectType ObjectType,
r io.ReadSeeker,
) (found bool, id sha1, err error) {
initialPosition, err := r.Seek(0, io.SeekCurrent)
if err != nil {
return false, [20]byte{}, err
}
defer func() {
_, err1 := r.Seek(initialPosition, io.SeekStart)
if err == nil && err1 != nil {
err = err1 // If there was no other error, send the seek error.
}
}()
id, err = StoreObjectSHA(objectType, ioutil.Discard, r)
if err != nil {
return false, [20]byte{}, err
}
found, _, err = repo.haveObject(id)
if err != nil {
return false, id, err
}
return found, id, err
}
// Write an object into git's loose database.
// If the object already exists in the database, it is not overwritten, though
// the compression is still performed. To avoid the compression, call
// HaveObjectFromReader first, which is fast and just does the SHA.
func (repo *Repository) StoreObjectLoose(
objectType ObjectType,
r io.ReadSeeker,
) (sha1, error) {
fd, err := ioutil.TempFile(filepath.Join(repo.Path, "objects"), ".gogit_")
if err != nil {
return [20]byte{}, fmt.Errorf("failed to make tmpfile: %v", err)
}
id, err := StoreObjectSHA(objectType, fd, r)
if err != nil {
fd.Close()
return [20]byte{}, err
}
fd.Close() // Not deferred, intentionally.
objectPath := filepathFromSHA1(repo.Path, id.String())
if _, err = os.Stat(objectPath); err == nil {
// Object already exists. Delete the temporary file.
err = os.Remove(fd.Name())
if err != nil {
return [20]byte{}, err
}
return id, nil
}
err = os.Mkdir(filepath.Dir(objectPath), 0775)
if err != nil && !os.IsExist(err) {
// Failed to create the directory, and not because it already exists.
return [20]byte{}, err
}
err = os.Rename(fd.Name(), objectPath)
if err != nil {
return [20]byte{}, err
}
return id, nil
}