@@ -244,11 +244,15 @@ func (w *Worktree) Add(path string) (plumbing.Hash, error) {
244244}
245245
246246func (w * Worktree ) calculateBlobHash (filename string ) (hash plumbing.Hash , err error ) {
247- fi , err := w .fs .Stat (filename )
247+ fi , err := w .fs .Lstat (filename )
248248 if err != nil {
249249 return plumbing .ZeroHash , err
250250 }
251251
252+ if fi .Mode ()& os .ModeSymlink != 0 {
253+ return w .calculateBlobHashFromSymlink (filename )
254+ }
255+
252256 f , err := w .fs .Open (filename )
253257 if err != nil {
254258 return plumbing .ZeroHash , err
@@ -265,6 +269,21 @@ func (w *Worktree) calculateBlobHash(filename string) (hash plumbing.Hash, err e
265269 return
266270}
267271
272+ func (w * Worktree ) calculateBlobHashFromSymlink (link string ) (plumbing.Hash , error ) {
273+ target , err := w .fs .Readlink (link )
274+ if err != nil {
275+ return plumbing .ZeroHash , err
276+ }
277+
278+ h := plumbing .NewHasher (plumbing .BlobObject , int64 (len (target )))
279+ _ , err = h .Write ([]byte (target ))
280+ if err != nil {
281+ return plumbing .ZeroHash , err
282+ }
283+
284+ return h .Sum (), nil
285+ }
286+
268287func (w * Worktree ) addOrUpdateFileToIndex (filename string , h plumbing.Hash ) error {
269288 idx , err := w .r .Storer .Index ()
270289 if err != nil {
@@ -297,20 +316,22 @@ func (w *Worktree) doAddFileToIndex(idx *index.Index, filename string, h plumbin
297316}
298317
299318func (w * Worktree ) doUpdateFileToIndex (e * index.Entry , filename string , h plumbing.Hash ) error {
300- info , err := w .fs .Stat (filename )
319+ info , err := w .fs .Lstat (filename )
301320 if err != nil {
302321 return err
303322 }
304323
305324 e .Hash = h
306325 e .ModifiedAt = info .ModTime ()
307326 e .Mode , err = filemode .NewFromOSFileMode (info .Mode ())
308- e .Size = uint32 (info .Size ())
309-
310327 if err != nil {
311328 return err
312329 }
313330
331+ if e .Mode .IsRegular () {
332+ e .Size = uint32 (info .Size ())
333+ }
334+
314335 fillSystemInfo (e , info .Sys ())
315336 return nil
316337}
@@ -351,11 +372,11 @@ func (w *Worktree) deleteFromFilesystem(path string) error {
351372// Move moves or rename a file in the worktree and the index, directories are
352373// not supported.
353374func (w * Worktree ) Move (from , to string ) (plumbing.Hash , error ) {
354- if _ , err := w .fs .Stat (from ); err != nil {
375+ if _ , err := w .fs .Lstat (from ); err != nil {
355376 return plumbing .ZeroHash , err
356377 }
357378
358- if _ , err := w .fs .Stat (to ); err == nil {
379+ if _ , err := w .fs .Lstat (to ); err == nil {
359380 return plumbing .ZeroHash , ErrDestinationExists
360381 }
361382
0 commit comments