11package git
22
33import (
4- "io"
5- "os"
64 "path/filepath"
75 "strings"
86
@@ -11,7 +9,6 @@ import (
119 "gopkg.in/src-d/go-git.v4/plumbing/format/index"
1210 "gopkg.in/src-d/go-git.v4/plumbing/object"
1311 "gopkg.in/src-d/go-git.v4/storage"
14- "gopkg.in/src-d/go-git.v4/utils/ioutil"
1512
1613 "gopkg.in/src-d/go-billy.v3"
1714)
@@ -34,12 +31,12 @@ func (w *Worktree) Commit(msg string, opts *CommitOptions) (plumbing.Hash, error
3431 return plumbing .ZeroHash , err
3532 }
3633
37- h := & commitIndexHelper {
34+ h := & buildTreeHelper {
3835 fs : w .fs ,
3936 s : w .r .Storer ,
4037 }
4138
42- tree , err := h .buildTreeAndBlobObjects (idx )
39+ tree , err := h .BuildTree (idx )
4340 if err != nil {
4441 return plumbing .ZeroHash , err
4542 }
@@ -103,20 +100,20 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
103100 return w .r .Storer .SetEncodedObject (obj )
104101}
105102
106- // commitIndexHelper converts a given index.Index file into multiple git objects
103+ // buildTreeHelper converts a given index.Index file into multiple git objects
107104// reading the blobs from the given filesystem and creating the trees from the
108105// index structure. The created objects are pushed to a given Storer.
109- type commitIndexHelper struct {
106+ type buildTreeHelper struct {
110107 fs billy.Filesystem
111108 s storage.Storer
112109
113110 trees map [string ]* object.Tree
114111 entries map [string ]* object.TreeEntry
115112}
116113
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
118115// 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 ) {
120117 const rootNode = ""
121118 h .trees = map [string ]* object.Tree {rootNode : {}}
122119 h .entries = map [string ]* object.TreeEntry {}
@@ -130,33 +127,27 @@ func (h *commitIndexHelper) buildTreeAndBlobObjects(idx *index.Index) (plumbing.
130127 return h .copyTreeToStorageRecursive (rootNode , h .trees [rootNode ])
131128}
132129
133- func (h * commitIndexHelper ) commitIndexEntry (e * index.Entry ) error {
130+ func (h * buildTreeHelper ) commitIndexEntry (e * index.Entry ) error {
134131 parts := strings .Split (e .Name , string (filepath .Separator ))
135132
136133 var path string
137134 for _ , part := range parts {
138135 parent := path
139136 path = filepath .Join (path , part )
140137
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 )
148139 }
149140
150141 return nil
151142}
152143
153- func (h * commitIndexHelper ) buildTree (e * index.Entry , parent , path string ) bool {
144+ func (h * buildTreeHelper ) doBuildTree (e * index.Entry , parent , path string ) {
154145 if _ , ok := h .trees [path ]; ok {
155- return false
146+ return
156147 }
157148
158149 if _ , ok := h .entries [path ]; ok {
159- return false
150+ return
160151 }
161152
162153 te := object.TreeEntry {Name : filepath .Base (path )}
@@ -170,84 +161,9 @@ func (h *commitIndexHelper) buildTree(e *index.Entry, parent, path string) bool
170161 }
171162
172163 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
248164}
249165
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 ) {
251167 for i , e := range t .Entries {
252168 if e .Mode != filemode .Dir && ! e .Hash .IsZero () {
253169 continue
0 commit comments