changeset 14453:ea3d548132cc

patch: do not patch unknown files (issue752)
author Patrick Mezard <pmezard@gmail.com>
date Fri, 27 May 2011 21:50:11 +0200
parents ee574cfd0c32
children 243acc7111b5
files mercurial/patch.py tests/test-import-unknown.t tests/test-mq-symlinks.t
diffstat 3 files changed, 73 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/patch.py	Fri May 27 21:50:10 2011 +0200
+++ b/mercurial/patch.py	Fri May 27 21:50:11 2011 +0200
@@ -445,13 +445,19 @@
         self.changed = set()
         self.copied = []
 
+    def _checkknown(self, fname):
+        if self.repo.dirstate[fname] == '?' and self.exists(fname):
+            raise PatchError(_('cannot patch %s: file is not tracked') % fname)
+
     def setfile(self, fname, data, mode, copysource):
+        self._checkknown(fname)
         super(workingbackend, self).setfile(fname, data, mode, copysource)
         if copysource is not None:
             self.copied.append((copysource, fname))
         self.changed.add(fname)
 
     def unlink(self, fname):
+        self._checkknown(fname)
         super(workingbackend, self).unlink(fname)
         self.removed.add(fname)
         self.changed.add(fname)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-import-unknown.t	Fri May 27 21:50:11 2011 +0200
@@ -0,0 +1,67 @@
+  $ cat <<EOF >> $HGRCPATH
+  > [extensions]
+  > purge =
+  > EOF
+
+  $ hg init test
+  $ cd test
+  $ echo a > changed
+  $ echo a > removed
+  $ echo a > source
+  $ hg ci -Am addfiles
+  adding changed
+  adding removed
+  adding source
+  $ echo a >> changed
+  $ echo a > added
+  $ hg add added
+  $ hg rm removed
+  $ hg cp source copied
+  $ hg diff --git > ../unknown.diff
+
+Test adding on top of an unknown file
+
+  $ hg up -qC 0
+  $ hg purge
+  $ echo a > added
+  $ hg import --no-commit ../unknown.diff
+  applying ../unknown.diff
+  file added already exists
+  1 out of 1 hunks FAILED -- saving rejects to file added.rej
+  abort: patch failed to apply
+  [255]
+
+Test modifying an unknown file
+
+  $ hg revert -aq
+  $ hg purge
+  $ hg rm changed
+  $ hg ci -m removechanged
+  $ echo a > changed
+  $ hg import --no-commit ../unknown.diff
+  applying ../unknown.diff
+  abort: cannot patch changed: file is not tracked
+  [255]
+
+Test removing an unknown file
+
+  $ hg up -qC 0
+  $ hg purge
+  $ hg rm removed
+  $ hg ci -m removeremoved
+  created new head
+  $ echo a > removed
+  $ hg import --no-commit ../unknown.diff
+  applying ../unknown.diff
+  abort: cannot patch removed: file is not tracked
+  [255]
+
+Test copying onto an unknown file
+
+  $ hg up -qC 0
+  $ hg purge
+  $ echo a > copied
+  $ hg import --no-commit ../unknown.diff
+  applying ../unknown.diff
+  abort: cannot create copied: destination already exists
+  [255]
--- a/tests/test-mq-symlinks.t	Fri May 27 21:50:10 2011 +0200
+++ b/tests/test-mq-symlinks.t	Fri May 27 21:50:11 2011 +0200
@@ -106,18 +106,3 @@
   now at: movelink
   $ $TESTDIR/readlink.py linkb
   linkb -> linkb
-
-check patch does not overwrite untracked symlinks
-
-  $ hg qpop
-  popping movelink
-  now at: link
-  $ ln -s linkbb linkb
-  $ hg qpush
-  applying movelink
-  cannot create linkb: destination already exists
-  1 out of 1 hunks FAILED -- saving rejects to file linkb.rej
-  patch failed, unable to continue (try -v)
-  patch failed, rejects left in working dir
-  errors during apply, please fix and refresh movelink
-  [2]