Patchwork [2,of,2] copy: distinguish "file exists" cases and add a hint (BC)

login
register
mail settings
Submitter Augie Fackler
Date Sept. 19, 2016, 11:24 p.m.
Message ID <cd367d3b7f4d6fa3cda1.1474327497@augie-macbookair2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/16680/
State Accepted
Headers show

Comments

Augie Fackler - Sept. 19, 2016, 11:24 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1474319739 14400
#      Mon Sep 19 17:15:39 2016 -0400
# Node ID cd367d3b7f4d6fa3cda1afa010919f950b06c3de
# Parent  0ee023b61175e76d51726a51c09af6fccf1e8846
copy: distinguish "file exists" cases and add a hint (BC)

Users that want to add a copy record to an existing commit with 'hg
commit --amend' should be guided towards this workflow, rather than
reaching for some sort of uncommit-recommit flow. As part of this,
distinguish in the top-line error message whether the file merely
already exists (untracked) on disk or the file already exists in
history.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -640,8 +640,15 @@  def copy(ui, repo, pats, opts, rename=Fa
 
         if not after and exists or after and state in 'mn':
             if not opts['force']:
-                ui.warn(_('%s: not overwriting - file exists\n') %
-                        reltarget)
+                if state in 'mn':
+                    msg = _('%s: not overwriting - file already committed\n')
+                    hint = _('(hg copy --after --force to replace the '
+                             'file by recording a copy)\n')
+                else:
+                    msg = _('%s: not overwriting - file exists\n')
+                    hint = _('(hg copy --after to record the copy)\n')
+                ui.warn(msg % reltarget)
+                ui.warn(hint)
                 return
 
         if after:
diff --git a/tests/test-copy.t b/tests/test-copy.t
--- a/tests/test-copy.t
+++ b/tests/test-copy.t
@@ -226,11 +226,23 @@  foo was clean:
   C foo
 Trying to copy on top of an existing file fails,
   $ hg copy -A bar foo
-  foo: not overwriting - file exists
+  foo: not overwriting - file already committed
+  (hg copy --after --force to replace the file by recording a copy)
+same error without the --after, so the user doesn't have to go through
+two hints:
+  $ hg copy bar foo
+  foo: not overwriting - file already committed
+  (hg copy --after --force to replace the file by recording a copy)
 but it's considered modified after a copy --after --force
   $ hg copy -Af bar foo
   $ hg st -AC foo
   M foo
     bar
+The hint for a file that exists but is not in file history doesn't
+mention --force:
+  $ touch xyzzy
+  $ hg cp bar xyzzy
+  xyzzy: not overwriting - file exists
+  (hg copy --after to record the copy)
 
   $ cd ..
diff --git a/tests/test-rename.t b/tests/test-rename.t
--- a/tests/test-rename.t
+++ b/tests/test-rename.t
@@ -265,7 +265,8 @@  move everything under directory d1 to ex
 overwrite existing files (d2/b)
 
   $ hg rename d1/* d2
-  d2/b: not overwriting - file exists
+  d2/b: not overwriting - file already committed
+  (hg copy --after --force to replace the file by recording a copy)
   moving d1/d11/a1 to d2/d11/a1 (glob)
   $ hg status -C
   A d2/a
@@ -370,6 +371,7 @@  attempt to overwrite an existing file
   $ echo "ca" > d1/ca
   $ hg rename d1/ba d1/ca
   d1/ca: not overwriting - file exists
+  (hg copy --after to record the copy)
   $ hg status -C
   ? d1/ca
   $ hg update -C
@@ -393,6 +395,7 @@  attempt to overwrite an existing broken 
   $ ln -s ba d1/ca
   $ hg rename --traceback d1/ba d1/ca
   d1/ca: not overwriting - file exists
+  (hg copy --after to record the copy)
   $ hg status -C
   ? d1/ca
   $ hg update -C