GitRepository: Implement status method
authorEd Bartosh <eduard.bartosh@intel.com>
Wed, 6 Jun 2012 11:45:44 +0000 (14:45 +0300)
committerGuido Günther <agx@sigxcpu.org>
Fri, 5 Dec 2014 14:45:00 +0000 (15:45 +0100)
Simple wrapper to the git-status command.

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

index 96921b1a22053982bcdc7a5dd69a574fbde15205..65599213819a6cd3e392fcb0ed025f07e160722e 100644 (file)
@@ -820,6 +820,39 @@ class GitRepository(object):
         if ret:
             raise GitRepositoryError("Can't execute repository clean: %s" % err)
 
+    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 = GitArgs('--porcelain', '-z')
+        if pathlist:
+            for path in pathlist:
+                options.add(path)
+
+        out, err, ret = self._git_inout('status', options.args,
+                                        extra_env={'LC_ALL': 'C'})
+        if ret:
+            raise GitRepositoryError("Can't get repository status: %s" % err)
+
+        elements = out.split('\x00')
+        result = defaultdict(list)
+
+        while elements[0] != '':
+            element = elements.pop(0)
+            status = element[:2]
+            filepath = element[3:]
+            # Expect to have two filenames for renames and copies
+            if status[0] in ['R', 'C']:
+                filepath = elements.pop(0) + '\x00' + filepath
+            result[status].append(filepath)
+
+        return result
+
     def is_empty(self):
         """
         Is the repository empty?
index c5c584931accfadcd5c355e6325250d16fa1b1d2..d9fc5392c874112e9ae411f2bdec936099fe9bab 100644 (file)
@@ -881,6 +881,28 @@ def test_get_merge_base():
     GitRepositoryError: Failed to get common ancestor: fatal: Not a valid object name doesnotexist
     """
 
+def test_status():
+    r"""
+    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\x00test_statusnew'])]
+    """
+
 def test_cmd_has_feature():
     r"""
     Methods tested: