GitRepository: Implemented status method
authorEd Bartosh <eduard.bartosh@intel.com>
Wed, 6 Jun 2012 11:45:44 +0000 (14:45 +0300)
committerEd Bartosh <eduard.bartosh@intel.com>
Tue, 12 Jun 2012 12:27:26 +0000 (15:27 +0300)
Change-Id: I2fef97a40f8cb04cc5a354df8211469e6d9f6eff

gbp/git/repository.py
tests/test_GitRepository.py

index 4d8ad1f003413b8e7695a74e8e5aea07ff2c523a..dd7384f81a09a36954e4f67167c6bb4e3e6c2de5 100644 (file)
@@ -16,6 +16,7 @@
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 """A Git repository"""
 
+import os
 import re
 import subprocess
 import os.path
@@ -602,6 +603,31 @@ class GitRepository(object):
             break
         return (ret, "".join(out))
 
+    def status(self, pathlist=None):
+        """
+        Check status of repository.
+
+        @param pathlist: List of paths to check status for
+        @type pathlist: C{list}
+        @return C{dict} of C{lists} of paths, where key is a git status flag.
+        @rtype C{dict}
+        """
+
+        options = ['--porcelain']
+        if pathlist:
+            options.extend(pathlist)
+
+        out, err, ret = self._git_inout('status', options,
+                                        extra_env={'LC_ALL': 'C'})
+        if ret:
+            raise GbpError("Can't get repository status: %s" % err)
+
+        result = defaultdict(list)
+
+        for line in out.splitlines():
+            result[line[:2]].append(line[3:])
+        return result
+
     def is_empty(self):
         """
         Is the repository empty?
index 73bb9fd64dae95df1de1662e4ae766178d8278bf..a950fc52b71c8536f0019885b847994981b19b0b 100644 (file)
@@ -631,6 +631,28 @@ def test_update_submodules():
     >>> repo.update_submodules()
     """
 
+def test_status():
+    """
+    Methods tested:
+        - L{gbp.git.GitRepository.status}
+
+    >>> import gbp.git, os, shutil
+    >>> repo = gbp.git.GitRepository(repo_dir)
+    >>> fname = os.path.join(repo.path, "test_status")
+    >>> shutil.copy(os.path.join(repo.path, ".git/HEAD"), fname)
+    >>> repo.status().items()
+    [('??', ['test_status'])]
+    >>> repo.status(['bla*']).items()
+    []
+    >>> repo.status(['te*']).items()
+    [('??', ['test_status'])]
+    >>> repo.add_files(repo.path, force=True)
+    >>> repo.commit_all(msg='added %s' % fname)
+    >>> _ = repo._git_inout('mv', [fname, fname + 'new'])
+    >>> repo.status().items()
+    [('R ', ['test_status -> test_statusnew'])]
+    """
+
 def test_teardown():
     """
     Perform the teardown