[bugzilla-gnome-org-extensions] Handle additions and deletions in Git patches



commit 3cf9313fd28d8848f5dd20d7d5ceabda695d4610
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Wed Sep 30 16:49:04 2009 -0400

    Handle additions and deletions in Git patches
    
    For additions and deletions, Git shows a diff between the file
    and /dev/null. Handle this when parsing patches and add a test case.

 js/patch.js     |   10 ++++++++--
 tests/patch.jst |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)
---
diff --git a/js/patch.js b/js/patch.js
index 31b5b28..99f221a 100644
--- a/js/patch.js
+++ b/js/patch.js
@@ -267,11 +267,17 @@ Patch.prototype = {
 
         while (m != null) {
             // git and hg show a diff between a/foo/bar.c and b/foo/bar.c
+            // or between a/foo/bar.c and /dev/null for removals and the
+            // reverse for additions.
             var filename;
-            if (/^a\//.test(m[1]) && /^b\//.test(m[2]))
+            if (/^a\//.test(m[1]) &&
+                (/^b\//.test(m[2]) || /^\/dev\/null/.test(m[2]))) {
                 filename = m[1].substring(2);
-            else
+            } else if (/^\/dev\/null/.test(m[1]) && /^b\//.test(m[2])) {
+                filename = m[2].substring(2);
+            } else {
                 filename = m[1];
+            }
 
             var hunks = [];
             var pos = FILE_START_RE.lastIndex;
diff --git a/tests/patch.jst b/tests/patch.jst
index a8647ac..2b096dc 100644
--- a/tests/patch.jst
+++ b/tests/patch.jst
@@ -174,3 +174,41 @@ Subject: [PATCH] Fix missing semicolons in flattener.py output
 
 Add semicolons after generated 'a = b' assignments.
, patch.intro);
+
+// Git patch representing a move
+
+patch = new Patch.Patch(<<<
+diff --git a/foo/README b/foo/README
+deleted file mode 100644
+index 4eac5f6..0000000
+--- a/foo/README
++++ /dev/null
+@@ -1,3 +0,0 @@
+-Some
+-Readme
+-File
+diff --git a/bar/README b/bar/README
+new file mode 100644
+index 0000000..4eac5f6
+--- /dev/null
++++ b/bar/README
+@@ -0,0 +1,3 @@
++Some
++Readme
++File
+>>>);
+
+assertEquals(<<<
+::: foo/README
+@@ -1,3 +0,0
+- 1 Some
+- 2 Readme
+- 3 File
+
+::: bar/README
+@@ -0,0 +1,3
+   + 1 Some
+   + 2 Readme
+   + 3 File
+>>>, patchToString(patch));
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]