Move moves or rename a file in the worktree and the index, directories are not supported.
(from, to string)
| 711 | // Move moves or rename a file in the worktree and the index, directories are |
| 712 | // not supported. |
| 713 | func (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 | } |