diff mercurial/patch.py @ 14383:1bd52cb12a55

patch: refactor iterhunks() regular and binary files emission
author Patrick Mezard <pmezard@gmail.com>
date Thu, 19 May 2011 22:44:01 +0200
parents 2d16f15da7bd
children 9d59c596eb9e
line wrap: on
line diff
--- a/mercurial/patch.py	Thu May 19 22:44:01 2011 +0200
+++ b/mercurial/patch.py	Thu May 19 22:44:01 2011 +0200
@@ -1129,28 +1129,26 @@
         x = lr.readline()
         if not x:
             break
-        if (state == BFILE and ((not context and x[0] == '@') or
-            ((context is not False) and x.startswith('***************')))):
-            if context is None and x.startswith('***************'):
-                context = True
-            gpatch = changed.get(bfile)
-            create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD'
-            remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE'
-            h = hunk(x, hunknum + 1, lr, context, create, remove)
+        if state == BFILE and (
+            (not context and x[0] == '@')
+            or (context is not False and x.startswith('***************'))
+            or x.startswith('GIT binary patch')):
+            gp = changed.get(bfile)
+            if x.startswith('GIT binary patch'):
+                h = binhunk(gp)
+                h.extract(lr)
+                afile = 'a/' + afile
+                bfile = 'b/' + bfile
+            else:
+                if context is None and x.startswith('***************'):
+                    context = True
+                create = afile == '/dev/null' or gp and gp.op == 'ADD'
+                remove = bfile == '/dev/null' or gp and gp.op == 'DELETE'
+                h = hunk(x, hunknum + 1, lr, context, create, remove)
             hunknum += 1
             if emitfile:
                 emitfile = False
-                yield 'file', (afile, bfile, h, gpatch and gpatch.mode or None)
-            yield 'hunk', h
-        elif state == BFILE and x.startswith('GIT binary patch'):
-            gpatch = changed[bfile]
-            h = binhunk(gpatch)
-            hunknum += 1
-            if emitfile:
-                emitfile = False
-                yield 'file', ('a/' + afile, 'b/' + bfile, h,
-                               gpatch and gpatch.mode or None)
-            h.extract(lr)
+                yield 'file', (afile, bfile, h, gp and gp.mode or None)
             yield 'hunk', h
         elif x.startswith('diff --git'):
             # check for git diff, scanning the whole patch file if needed