Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo a move/rename action in mercurial?

I did a hg mv -A oldFile newFile and accidentally got the new file wrong.

I did a hg revert newFile but now the file which did show as missing (and then as removed after the move command) now no longer shows up at all.

How can I undo that and get back to the state I was in, so that the new file shows as unknown and the old file shows as missing?

like image 481
Sam Holder Avatar asked Jul 11 '11 11:07

Sam Holder


2 Answers

$ hg st
$ mv a b
$ hg st
! a
? b
$ hg mv -A a b
$ hg st
A b
R a
$ hg revert b
$ hg st
? b
$ ls
a  b

So hg revert b restored the old file a. Now all you have to do is hg mv a correct-name.

like image 129
Idan K Avatar answered Nov 13 '22 04:11

Idan K


Here's an "unmove" script written in bash. There's some help text at the top. The most important point is that the script has an option that forces any file removal to be done interactively.

#!/bin/bash

# Syntax: $0 [OPTION] DEST
#
# This script is mainly intended to undo the effects of: hg move SOURCE DEST
#
# It first determines which files have been added globally (as if by
# hg add), then it runs:

# hg -v revert DEST

# Finally, it removes all the files that had been added,
# and then reverts them as well if possible.
#
# OPTIONS:
# -i :: remove files using rm -i

if [ "$1" = -i ] ; then
    INTERACT=$1
    shift
fi


TODO=()
while read -r f ; do
  TODO+=("$f")
done < <( hg st -an "$@" )

function go {
    hg -v revert "$@"
    for f in "${TODO[@]}" ; do
      rm $INTERACT "$f"
      hg -q files "$f" > /dev/null && hg revert "$f"
    done
}

go
like image 32
peak Avatar answered Nov 13 '22 05:11

peak