From cb9271fe113bae56088c9c1d07870c408a518a52 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 14 Sep 2012 13:40:14 +0300 Subject: GitRepository: add diff_status method 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 --- gbp/git/repository.py | 27 +++++++++++++++++++++++++++ tests/test_GitRepository.py | 13 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 23f9482a..edb8e21f 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -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): diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py index 427370da..c5c58493 100644 --- a/tests/test_GitRepository.py +++ b/tests/test_GitRepository.py @@ -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(, {}) + >>> repo.diff_status("HEAD~1", "HEAD") + defaultdict(, {'M': ['testfile']}) + """ + def test_mirror_clone(): """ Mirror a repository -- cgit v1.2.3