]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/parser.py
tox: Add tox.ini file
[patchwork] / apps / patchwork / parser.py
index 24631b739822fde096f3f748343410be8ec2be3d..a51a7b609af0c1df36a85f0d9fac4670efd9322e 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 #
 # Patchwork - automated patch tracking system
 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
@@ -45,6 +45,7 @@ def parse_patch(text):
     # 3: patch header line 2 (+++)
     # 4: patch hunk header line (@@ line)
     # 5: patch hunk content
+    # 6: patch meta header (rename from/rename to)
     #
     # valid transitions:
     #  0 -> 1 (diff, ===, Index:)
@@ -54,6 +55,9 @@ def parse_patch(text):
     #  3 -> 4 (@@ line)
     #  4 -> 5 (patch content)
     #  5 -> 1 (run out of lines from @@-specifed count)
+    #  1 -> 6 (rename from / rename to)
+    #  6 -> 2 (---)
+    #  6 -> 1 (other text)
     #
     # Suspected patch header is stored into buf, and appended to
     # patchbuf if we find a following hunk. Otherwise, append to
@@ -63,11 +67,12 @@ def parse_patch(text):
     lc = (0, 0)
     hunk = 0
 
-    for line in text.decode('utf-8').split('\n'):
+
+    for line in text.split('\n'):
         line += '\n'
 
         if state == 0:
-            if line.startswith('diff') or line.startswith('===') \
+            if line.startswith('diff ') or line.startswith('===') \
                     or line.startswith('Index: '):
                 state = 1
                 buf += line
@@ -84,6 +89,9 @@ def parse_patch(text):
             if line.startswith('--- '):
                 state = 2
 
+            if line.startswith('rename from ') or line.startswith('rename to '):
+                state = 6
+
         elif state == 2:
             if line.startswith('+++ '):
                 state = 3
@@ -118,6 +126,11 @@ def parse_patch(text):
                 buf = ''
                 state = 2
 
+            elif hunk and line.startswith('\ No newline at end of file'):
+                # If we had a hunk and now we see this, it's part of the patch,
+                # and we're still expecting another @@ line.
+                patchbuf += line
+
             elif hunk:
                 state = 1
                 buf += line
@@ -147,6 +160,20 @@ def parse_patch(text):
             else:
                 state = 5
 
+        elif state == 6:
+            if line.startswith('rename to ') or line.startswith('rename from '):
+                patchbuf += buf + line
+                buf = ''
+
+            elif line.startswith('--- '):
+                patchbuf += buf + line
+                buf = ''
+                state = 2
+
+            else:
+                buf += line
+                state = 1
+
         else:
             raise Exception("Unknown state %d! (line '%s')" % (state, line))