MCPcopy
hub / github.com/go-git/go-git / Move

Method Move

worktree_status.go:713–742  ·  view source on GitHub ↗

Move moves or rename a file in the worktree and the index, directories are not supported.

(from, to string)

Source from the content-addressed store, hash-verified

711// Move moves or rename a file in the worktree and the index, directories are
712// not supported.
713func (w *Worktree) Move(from, to string) (plumbing.Hash, error) {
714 // TODO(mcuadros): support directories and/or implement support for glob
715 if _, err := w.Filesystem.Lstat(from); err != nil {
716 return plumbing.ZeroHash, err
717 }
718
719 if _, err := w.Filesystem.Lstat(to); err == nil {
720 return plumbing.ZeroHash, ErrDestinationExists
721 }
722
723 idx, err := w.r.Storer.Index()
724 if err != nil {
725 return plumbing.ZeroHash, err
726 }
727
728 hash, err := w.deleteFromIndex(idx, from)
729 if err != nil {
730 return plumbing.ZeroHash, err
731 }
732
733 if err := w.Filesystem.Rename(from, to); err != nil {
734 return hash, err
735 }
736
737 if err := w.addOrUpdateFileToIndex(idx, to, hash); err != nil {
738 return hash, err
739 }
740
741 return hash, w.r.Storer.SetIndex(idx)
742}

Callers 4

TestMoveMethod · 0.95
TestMoveToExistentMethod · 0.95
TestStatsWithRenameMethod · 0.80

Calls 6

deleteFromIndexMethod · 0.95
IndexMethod · 0.65
SetIndexMethod · 0.65
LstatMethod · 0.45
RenameMethod · 0.45

Tested by 4

TestMoveMethod · 0.76
TestMoveToExistentMethod · 0.76
TestStatsWithRenameMethod · 0.64