changeset 1513:5c3b93b244aa

copy & rename don't overwrite unless --force is specified
author Robin Farine <robin.farine@terminus.org>
date Tue, 08 Nov 2005 10:35:09 -0800
parents 53ad6ee6ede4
children faf46d810a85
files mercurial/commands.py tests/test-rename tests/test-rename.out
diffstat 3 files changed, 36 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Nov 08 10:35:05 2005 -0800
+++ b/mercurial/commands.py	Tue Nov 08 10:35:09 2005 -0800
@@ -803,10 +803,13 @@
     def copy(abssrc, relsrc, target, exact):
         abstarget = util.canonpath(repo.root, cwd, target)
         reltarget = util.pathto(cwd, abstarget)
-        if not opts['force'] and repo.dirstate.state(abstarget) not in 'a?':
-            ui.warn(_('%s: not overwriting - file already managed\n') %
-                    reltarget)
-            return
+        if os.path.exists(reltarget):
+            if opts['force']:
+                os.unlink(reltarget)
+            else:
+                ui.warn(_('%s: not overwriting - file exists\n') %
+                        reltarget)
+                return
         if ui.verbose or not exact:
             ui.status(_('copying %s to %s\n') % (relsrc, reltarget))
         if not opts['after']:
--- a/tests/test-rename	Tue Nov 08 10:35:05 2005 -0800
+++ b/tests/test-rename	Tue Nov 08 10:35:09 2005 -0800
@@ -58,3 +58,21 @@
 hg rename 're:d1/([^a][^/]*/)*a.*' d2/d21
 hg status
 hg update -C
+
+echo "# attempt to overwrite an existing file"
+echo "ca" > d1/ca
+hg rename d1/ba d1/ca
+hg status
+hg update -C
+
+echo "# forced overwrite of an existing file"
+echo "ca" > d1/ca
+hg rename --force d1/ba d1/ca
+hg status
+hg update -C
+
+echo "# replace a symlink with a file"
+ln -s ba d1/ca
+hg rename --force d1/ba d1/ca
+hg status
+hg update -C
--- a/tests/test-rename.out	Tue Nov 08 10:35:05 2005 -0800
+++ b/tests/test-rename.out	Tue Nov 08 10:35:09 2005 -0800
@@ -49,7 +49,7 @@
 R d2/b
 # move everything under directory d1 to existing directory d2, do not
 # overwrite existing files (d2/b)
-d2/b: not overwriting - file already managed
+d2/b: not overwriting - file exists
 copying d1/d11/a1 to d2/d11/a1
 removing d1/d11/a1
 A d2/a
@@ -91,3 +91,13 @@
 A d2/d21/a1
 R d1/a
 R d1/d11/a1
+# attempt to overwrite an existing file
+d1/ca: not overwriting - file exists
+abort: no files to copy
+? d1/ca
+# forced overwrite of an existing file
+A d1/ca
+R d1/ba
+# replace a symlink with a file
+A d1/ca
+R d1/ba