import: abort instead of crashing when copy source does not exist (issue5375)
authorRyan McElroy <rmcelroy@fb.com>
Sat, 08 Oct 2016 05:26:58 -0700
changeset 30078 173bdb502503
parent 30077 8f42d8c412c8
child 30079 84debea79903
import: abort instead of crashing when copy source does not exist (issue5375) Previously, when a patch contained a move or copy from a source that did not exist, `hg import` would crash. This patch changes import to raise a PatchError with an explanantion of what is wrong with the patch to avoid the stack trace and bad user experience.
mercurial/patch.py
tests/test-import.t
--- a/mercurial/patch.py	Sat Oct 08 08:54:05 2016 -0700
+++ b/mercurial/patch.py	Sat Oct 08 05:26:58 2016 -0700
@@ -1952,8 +1952,10 @@
                 data, mode = None, None
                 if gp.op in ('RENAME', 'COPY'):
                     data, mode = store.getfile(gp.oldpath)[:2]
-                    # FIXME: failing getfile has never been handled here
-                    assert data is not None
+                    if data is None:
+                        # This means that the old path does not exist
+                        raise PatchError(_("source file '%s' does not exist")
+                                           % gp.oldpath)
                 if gp.mode:
                     mode = gp.mode
                     if gp.op == 'ADD':
--- a/tests/test-import.t	Sat Oct 08 08:54:05 2016 -0700
+++ b/tests/test-import.t	Sat Oct 08 05:26:58 2016 -0700
@@ -1793,3 +1793,13 @@
   1 out of 1 hunks FAILED -- saving rejects to file file1.rej
   abort: patch failed to apply
   [255]
+
+test import crash (issue5375)
+  $ cd ..
+  $ hg init repo
+  $ cd repo
+  $ printf "diff --git a/a b/b\nrename from a\nrename to b" | hg import -
+  applying patch from stdin
+  a not tracked!
+  abort: source file 'a' does not exist
+  [255]