1
1
package git
2
2
3
3
import (
4
- "io"
5
- "os"
6
4
"path/filepath"
7
5
"strings"
8
6
@@ -11,7 +9,6 @@ import (
11
9
"gopkg.in/src-d/go-git.v4/plumbing/format/index"
12
10
"gopkg.in/src-d/go-git.v4/plumbing/object"
13
11
"gopkg.in/src-d/go-git.v4/storage"
14
- "gopkg.in/src-d/go-git.v4/utils/ioutil"
15
12
16
13
"gopkg.in/src-d/go-billy.v3"
17
14
)
@@ -34,12 +31,12 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
34
31
return plumbing .ZeroHash , err
35
32
}
36
33
37
- h := & commitIndexHelper {
34
+ h := & buildTreeHelper {
38
35
fs : w .fs ,
39
36
s : w .r .Storer ,
40
37
}
41
38
42
- tree , err := h .buildTreeAndBlobObjects (idx )
39
+ tree , err := h .BuildTree (idx )
43
40
if err != nil {
44
41
return plumbing .ZeroHash , err
45
42
}
@@ -103,20 +100,20 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
103
100
return w .r .Storer .SetEncodedObject (obj )
104
101
}
105
102
106
- // commitIndexHelper converts a given index.Index file into multiple git objects
103
+ // buildTreeHelper converts a given index.Index file into multiple git objects
107
104
// reading the blobs from the given filesystem and creating the trees from the
108
105
// index structure. The created objects are pushed to a given Storer.
109
- type commitIndexHelper struct {
106
+ type buildTreeHelper struct {
110
107
fs billy.Filesystem
111
108
s storage.Storer
112
109
113
110
trees map [string ]* object.Tree
114
111
entries map [string ]* object.TreeEntry
115
112
}
116
113
117
- // buildTreesAndBlobs builds the objects and push its to the storer, the hash
114
+ // BuildTree builds the tree objects and push its to the storer, the hash
118
115
// of the root tree is returned.
119
- func (h * commitIndexHelper ) buildTreeAndBlobObjects (idx * index.Index ) (plumbing.Hash , error ) {
116
+ func (h * buildTreeHelper ) BuildTree (idx * index.Index ) (plumbing.Hash , error ) {
120
117
const rootNode = ""
121
118
h .trees = map [string ]* object.Tree {rootNode : {}}
122
119
h .entries = map [string ]* object.TreeEntry {}
@@ -130,33 +127,27 @@ func (h *commitIndexHelper) buildTreeAndBlobObjects(idx *index.Index) (plumbing.
130
127
return h .copyTreeToStorageRecursive (rootNode , h .trees [rootNode ])
131
128
}
132
129
133
- func (h * commitIndexHelper ) commitIndexEntry (e * index.Entry ) error {
130
+ func (h * buildTreeHelper ) commitIndexEntry (e * index.Entry ) error {
134
131
parts := strings .Split (e .Name , string (filepath .Separator ))
135
132
136
133
var path string
137
134
for _ , part := range parts {
138
135
parent := path
139
136
path = filepath .Join (path , part )
140
137
141
- if ! h .buildTree (e , parent , path ) {
142
- continue
143
- }
144
-
145
- if err := h .copyIndexEntryToStorage (e ); err != nil {
146
- return err
147
- }
138
+ h .doBuildTree (e , parent , path )
148
139
}
149
140
150
141
return nil
151
142
}
152
143
153
- func (h * commitIndexHelper ) buildTree (e * index.Entry , parent , path string ) bool {
144
+ func (h * buildTreeHelper ) doBuildTree (e * index.Entry , parent , path string ) {
154
145
if _ , ok := h .trees [path ]; ok {
155
- return false
146
+ return
156
147
}
157
148
158
149
if _ , ok := h .entries [path ]; ok {
159
- return false
150
+ return
160
151
}
161
152
162
153
te := object.TreeEntry {Name : filepath .Base (path )}
@@ -170,84 +161,9 @@ func (h *commitIndexHelper) buildTree(e *index.Entry, parent, path string) bool
170
161
}
171
162
172
163
h .trees [parent ].Entries = append (h .trees [parent ].Entries , te )
173
- return true
174
- }
175
-
176
- func (h * commitIndexHelper ) copyIndexEntryToStorage (e * index.Entry ) error {
177
- _ , err := h .s .EncodedObject (plumbing .BlobObject , e .Hash )
178
- if err == nil {
179
- return nil
180
- }
181
-
182
- if err != plumbing .ErrObjectNotFound {
183
- return err
184
- }
185
-
186
- return h .doCopyIndexEntryToStorage (e )
187
- }
188
-
189
- func (h * commitIndexHelper ) doCopyIndexEntryToStorage (e * index.Entry ) (err error ) {
190
- fi , err := h .fs .Lstat (e .Name )
191
- if err != nil {
192
- return err
193
- }
194
-
195
- if fi .Mode ()& os .ModeSymlink != 0 {
196
- return h .doCopyIndexEntryFromSymlinkToStorage (e , fi )
197
- }
198
-
199
- obj := h .s .NewEncodedObject ()
200
- obj .SetType (plumbing .BlobObject )
201
- obj .SetSize (fi .Size ())
202
-
203
- reader , err := h .fs .Open (e .Name )
204
- if err != nil {
205
- return err
206
- }
207
-
208
- defer ioutil .CheckClose (reader , & err )
209
-
210
- writer , err := obj .Writer ()
211
- if err != nil {
212
- return err
213
- }
214
-
215
- defer ioutil .CheckClose (writer , & err )
216
-
217
- if _ , err := io .Copy (writer , reader ); err != nil {
218
- return err
219
- }
220
-
221
- _ , err = h .s .SetEncodedObject (obj )
222
- return err
223
- }
224
-
225
- func (h * commitIndexHelper ) doCopyIndexEntryFromSymlinkToStorage (e * index.Entry , fi os.FileInfo ) error {
226
- obj := h .s .NewEncodedObject ()
227
- obj .SetType (plumbing .BlobObject )
228
- obj .SetSize (fi .Size ())
229
-
230
- writer , err := obj .Writer ()
231
- if err != nil {
232
- return err
233
- }
234
-
235
- defer ioutil .CheckClose (writer , & err )
236
-
237
- target , err := h .fs .Readlink (e .Name )
238
- if err != nil {
239
- return err
240
- }
241
-
242
- if _ , err := writer .Write ([]byte (target )); err != nil {
243
- return err
244
- }
245
-
246
- _ , err = h .s .SetEncodedObject (obj )
247
- return err
248
164
}
249
165
250
- func (h * commitIndexHelper ) copyTreeToStorageRecursive (parent string , t * object.Tree ) (plumbing.Hash , error ) {
166
+ func (h * buildTreeHelper ) copyTreeToStorageRecursive (parent string , t * object.Tree ) (plumbing.Hash , error ) {
251
167
for i , e := range t .Entries {
252
168
if e .Mode != filemode .Dir && ! e .Hash .IsZero () {
253
169
continue
0 commit comments