[bugzilla-gnome-org-extensions] Mark files as ADDED/REMOVED/CHANGED



commit b402a57ef02a04ae27f2124597ee6002976d3556
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Sat Oct 3 16:20:18 2009 -0400

    Mark files as ADDED/REMOVED/CHANGED
    
    When parsing patches, determine a status of ADDED/REMOVED/CHANGED
    for each file, either by looking at the file headers or by using
    heuristics.

 js/patch.js     |   31 +++++++++++++++++++++++++------
 tests/patch.jst |   52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 74 insertions(+), 9 deletions(-)
---
diff --git a/js/patch.js b/js/patch.js
index 3e2e412..b4b3058 100644
--- a/js/patch.js
+++ b/js/patch.js
@@ -166,13 +166,14 @@ Hunk.prototype = {
     }
 };
 
-function File(filename, hunks) {
-    this._init(filename, hunks);
+function File(filename, status, hunks) {
+    this._init(filename, status, hunks);
 }
 
 File.prototype = {
-    _init : function(filename, hunks) {
+    _init : function(filename, status, hunks) {
         this.filename = filename;
+        this.status = status;
         this.hunks = hunks;
 
         var l = 0;
@@ -273,11 +274,17 @@ Patch.prototype = {
             // 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]) || /^\/dev\/null/.test(m[2]))) {
+            var status = undefined;
+
+            if (/^a\//.test(m[1]) && /^b\//.test(m[2])) {
+                filename = m[1].substring(2);
+                status = CHANGED;
+            } else if (/^a\//.test(m[1]) && /^\/dev\/null/.test(m[2])) {
                 filename = m[1].substring(2);
+                status = REMOVED;
             } else if (/^\/dev\/null/.test(m[1]) && /^b\//.test(m[2])) {
                 filename = m[2].substring(2);
+                status = ADDED;
             } else {
                 filename = m[1];
             }
@@ -299,7 +306,19 @@ Patch.prototype = {
                 hunks.push(new Hunk(oldStart, oldCount, newStart, newCount, m2[5], m2[6]));
             }
 
-            this.files.push(new File(filename, hunks));
+            if (status === undefined) {
+                // For non-Hg/Git we use assume patch was generated non-zero context
+                // and just look at the patch to detect added/removed. Bzr actually
+                // says added/removed in the diff, but SVN/CVS don't
+                if (hunks.length == 1 && hunks[0].oldCount == 0)
+                    status = ADDED;
+                else if (hunks.length == 1 && hunks[0].newCount == 0)
+                    status = REMOVED;
+                else
+                    status = CHANGED;
+            }
+
+            this.files.push(new File(filename, status, hunks));
 
             FILE_START_RE.lastIndex = pos;
             m = FILE_START_RE.exec(text);
diff --git a/tests/patch.jst b/tests/patch.jst
index 3032dd7..dde0338 100644
--- a/tests/patch.jst
+++ b/tests/patch.jst
@@ -37,7 +37,20 @@ function hunkToString(hunk) {
 }
 
 function fileToString(file) {
-    return ('::: ' + file.filename + '\n' +
+    var statusString;
+    switch (file.status) {
+    case Patch.ADDED:
+        statusString = " (added)";
+       break;
+    case Patch.REMOVED:
+        statusString = " (removed)";
+       break;
+    case Patch.CHANGED:
+        statusString = "";
+       break;
+    }
+
+    return ('::: ' + file.filename + statusString + '\n' +
             [hunkToString(hunk) for each (hunk in file.hunks)].join(""));
 }
 
@@ -201,13 +214,46 @@ index 0000000..4eac5f6
);
 
 assertEquals(<<<
-::: foo/README
+::: foo/README (removed)
+@@ -1,3 +0,0
+- 1 Some
+- 2 Readme
+- 3 File
+
+::: bar/README (added)
+@@ -0,0 +1,3
+   + 1 Some
+   + 2 Readme
+   + 3 File
+>>>, patchToString(patch));
+
+// Additions/removals in a Bzr diff
+
+patch = new Patch.Patch(<<<
+=== removed file 'foo/README'
+--- foo/README     2002-04-24 06:27:23 +0000
++++ foo/REAMDE     1970-01-01 00:00:00 +0000
+@@ -1,3 +0,0 @@
+-Some
+-Readme
+-File
+=== added file 'Bugzilla/AttachmentStatus.pm'
+--- bar/README        1970-01-01 00:00:00 +0000
++++ bar/README        2009-05-29 01:17:24 +0000
+@@ -0,0 +1,3 @@
++Some
++Readme
++File
+>>>);
+
+assertEquals(<<<
+::: foo/README (removed)
 @@ -1,3 +0,0
 - 1 Some
 - 2 Readme
 - 3 File
 
-::: bar/README
+::: bar/README (added)
 @@ -0,0 +1,3
    + 1 Some
    + 2 Readme


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