Patchwork [1,of,2] record: abort on malformed patches instead of crashing

login
register
mail settings
Submitter Mads Kiilerich
Date April 11, 2013, 8:34 p.m.
Message ID <fe1960882df771438b58.1365712448@mk-desktop>
Download mbox | patch
Permalink /patch/1275/
State Superseded
Commit e4ae397595e84ad89c9e70591d772f5389450b70
Headers show

Comments

Mads Kiilerich - April 11, 2013, 8:34 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@unity3d.com>
# Date 1365699813 -7200
#      Thu Apr 11 19:03:33 2013 +0200
# Node ID fe1960882df771438b584169b37608f2ffb74923
# Parent  8c0a7eeda06d2773ec92b14527280db3e0167588
record: abort on malformed patches instead of crashing
Pierre-Yves David - April 11, 2013, 11:09 p.m.
On 11 avr. 2013, at 22:34, Mads Kiilerich wrote:

> # HG changeset patch
> # User Mads Kiilerich <madski@unity3d.com>
> # Date 1365699813 -7200
> #      Thu Apr 11 19:03:33 2013 +0200
> # Node ID fe1960882df771438b584169b37608f2ffb74923
> # Parent  8c0a7eeda06d2773ec92b14527280db3e0167588
> record: abort on malformed patches instead of crashing

Not crashing is definitly a plus. Not fan of the new error message (but the old one were not better).

Patch

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -531,7 +531,11 @@ 
         fp.seek(0)
 
         # 1. filter patch, so we have intending-to apply subset of it
-        chunks = filterpatch(ui, parsepatch(fp))
+        try:
+            chunks = filterpatch(ui, parsepatch(fp))
+        except patch.PatchError, err:
+            raise util.Abort(_('error parsing patch: %s') % err)
+
         del fp
 
         contenders = set()
diff --git a/tests/test-record.t b/tests/test-record.t
--- a/tests/test-record.t
+++ b/tests/test-record.t
@@ -1176,6 +1176,53 @@ 
   +That change will not be committed
   +That is the second line
   +That line has been added
+
+Malformed patch - error handling
+
+  $ cat > editor.sh << '__EOF__'
+  > sed -e '/^@/p' "$1" > tmp
+  > mv tmp "$1"
+  > __EOF__
+  $ HGEDITOR="\"sh\" \"`pwd`/editor.sh\"" hg record <<EOF
+  > y
+  > e
+  > EOF
+  diff --git a/editedfile b/editedfile
+  1 hunks, 3 lines changed
+  examine changes to 'editedfile'? [Ynesfdaq?] 
+  @@ -1,3 +1,3 @@
+  -This is the first line
+  -This change will be committed
+  -This is the third line
+  +This change will not be committed
+  +This is the second line
+  +This line has been added
+  record this change to 'editedfile'? [Ynesfdaq?] 
+  abort: error parsing patch: unhandled transition: range -> range
+  [255]
+
+  $ cat > editor.sh << '__EOF__'
+  > sed -e '/^@/iother' "$1" > tmp
+  > mv tmp "$1"
+  > __EOF__
+  $ HGEDITOR="\"sh\" \"`pwd`/editor.sh\"" hg record <<EOF
+  > y
+  > e
+  > EOF
+  diff --git a/editedfile b/editedfile
+  1 hunks, 3 lines changed
+  examine changes to 'editedfile'? [Ynesfdaq?] 
+  @@ -1,3 +1,3 @@
+  -This is the first line
+  -This change will be committed
+  -This is the third line
+  +This change will not be committed
+  +This is the second line
+  +This line has been added
+  record this change to 'editedfile'? [Ynesfdaq?] 
+  abort: error parsing patch: unknown patch content: 'other\n'
+  [255]
+
   $ hg up -C
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved