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