GitRepository: add diff_status method
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 14 Sep 2012 10:40:14 +0000 (13:40 +0300)
committerGuido Günther <agx@sigxcpu.org>
Fri, 5 Dec 2014 14:35:14 +0000 (15:35 +0100)
This is a method of getting the filename and status information of a
diff. That is, a list of files that changed and their status, "added",
"modified" etc.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/git/repository.py
tests/test_GitRepository.py

index 23f9482a3ffa29c10e2ecbae66395e78941ec8f9..edb8e21f23fe577d590d13b06e26cdbe0d7410ba 100644 (file)
@@ -1639,6 +1639,33 @@ class GitRepository(object):
         if ret:
             raise GitRepositoryError("Git diff failed")
         return output
+
+    def diff_status(self, obj1, obj2):
+        """
+        Get file-status of two git repository objects
+
+        @param obj1: first object
+        @type obj1: C{str}
+        @param obj2: second object
+        @type obj2: C{str}
+        @return: name-status
+        @rtype: C{defaultdict} of C{str}
+        """
+        options = GitArgs('--name-status', '-z', obj1, obj2)
+        output, stderr, ret = self._git_inout('diff', options.args)
+
+        elements = output.split('\x00')
+        result = defaultdict(list)
+
+        while elements[0] != '':
+            status = elements.pop(0)[0]
+            filepath = elements.pop(0)
+            # Expect to have two filenames for renames and copies
+            if status in ['R', 'C']:
+                filepath = elements.pop(0) + '\x00' + filepath
+            result[status].append(filepath)
+
+        return result
 #}
 
     def archive(self, format, prefix, output, treeish, **kwargs):
index 427370da84d5a4595c1e8d17cff91c2324215439..c5c584931accfadcd5c355e6325250d16fa1b1d2 100644 (file)
@@ -493,6 +493,19 @@ def test_diff():
     True
     """
 
+def test_diff_status():
+    """
+    Methods tested:
+        - L{gbp.git.GitRepository.diff_status}
+
+    >>> import gbp.git
+    >>> repo = gbp.git.GitRepository(repo_dir)
+    >>> repo.diff_status("HEAD", "HEAD")
+    defaultdict(<type 'list'>, {})
+    >>> repo.diff_status("HEAD~1", "HEAD")
+    defaultdict(<type 'list'>, {'M': ['testfile']})
+    """
+
 def test_mirror_clone():
     """
     Mirror a repository