MCPcopy Create free account
hub / github.com/go-git/go-git / Move

Method Move

worktree_status.go:684–713  ·  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

682// Move moves or rename a file in the worktree and the index, directories are
683// not supported.
684func (w *Worktree) Move(from, to string) (plumbing.Hash, error) {
685 // TODO(mcuadros): support directories and/or implement support for glob
686 if _, err := w.Filesystem.Lstat(from); err != nil {
687 return plumbing.ZeroHash, err
688 }
689
690 if _, err := w.Filesystem.Lstat(to); err == nil {
691 return plumbing.ZeroHash, ErrDestinationExists
692 }
693
694 idx, err := w.r.Storer.Index()
695 if err != nil {
696 return plumbing.ZeroHash, err
697 }
698
699 hash, err := w.deleteFromIndex(idx, from)
700 if err != nil {
701 return plumbing.ZeroHash, err
702 }
703
704 if err := w.Filesystem.Rename(from, to); err != nil {
705 return hash, err
706 }
707
708 if err := w.addOrUpdateFileToIndex(idx, to, hash); err != nil {
709 return hash, err
710 }
711
712 return hash, w.r.Storer.SetIndex(idx)
713}

Callers 4

TestMoveMethod · 0.95
TestMoveToExistentMethod · 0.95
TestStatsWithRenameMethod · 0.80

Calls 6

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

Tested by 4

TestMoveMethod · 0.76
TestMoveToExistentMethod · 0.76
TestStatsWithRenameMethod · 0.64