summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2012-09-27 12:33:10 +0300
committerGuido Günther <agx@sigxcpu.org>2012-11-07 20:56:03 +0100
commitf4d2e21f475a7f6f9039f7b7ce223b9179f9cfbe (patch)
tree3d1ccbb59c489e2f2000dfd4a8b7a96b89b2a1ba
parent323bdcfa2f7bfa2f2402c22c4c90ca060243daaf (diff)
downloadgit-buildpackage-f4d2e21f475a7f6f9039f7b7ce223b9179f9cfbe.tar.gz
git-buildpackage-f4d2e21f475a7f6f9039f7b7ce223b9179f9cfbe.tar.bz2
git-buildpackage-f4d2e21f475a7f6f9039f7b7ce223b9179f9cfbe.zip
GitRepository: implement _cmd_has_feature() method
This method is intended for checking if the local git (client) command supports a certain feature. The "feature" is considered to be a command line option. E.g. does "merge" command have the "edit" feature translates to does git-merge support the '--edit' command line option. To figure this out, _cmd_has_feature() parses through the "OPTIONS" section of the man page of the git command. Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r--gbp/git/repository.py34
-rw-r--r--tests/test_GitRepository.py25
2 files changed, 59 insertions, 0 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py
index 4c087a89..e31584c8 100644
--- a/gbp/git/repository.py
+++ b/gbp/git/repository.py
@@ -18,6 +18,7 @@
import subprocess
import os.path
+import re
from collections import defaultdict
import gbp.log as log
@@ -163,6 +164,39 @@ class GitRepository(object):
raise GitRepositoryError("Error running git %s: %s" %
(command, excobj))
+ def _cmd_has_feature(self, command, feature):
+ """
+ Check if the git command has certain feature enabled.
+
+ @param command: git command
+ @type command: C{str}
+ @param feature: feature / command option to check
+ @type feature: C{str}
+ @return: True if feature is supported
+ @rtype: C{bool}
+ """
+ args = GitArgs(command, '-m')
+ help, foo, ret = self._git_inout('help', args.args)
+ if ret:
+ raise GitRepositoryError("Invalid git command: %s" % command)
+
+ # Parse git command man page
+ section_re = re.compile(r'^(?P<section>[A-Z].*)')
+ option_re = re.compile(r'--?(?P<name>[a-zA-Z\-]+).*')
+ man_section = None
+ for line in help.splitlines():
+ if man_section == "OPTIONS" and line.startswith(' -'):
+ opts = line.split(',')
+ for opt in opts:
+ match = option_re.match(opt.strip())
+ if match and match.group('name') == feature:
+ return True
+ # Check man section
+ match = section_re.match(line)
+ if match:
+ man_section = match.group('section')
+ return False
+
@property
def path(self):
"""The absolute path to the repository"""
diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py
index aa20e755..f1b50131 100644
--- a/tests/test_GitRepository.py
+++ b/tests/test_GitRepository.py
@@ -753,6 +753,31 @@ def test_get_merge_base():
GitRepositoryError: Failed to get common ancestor: fatal: Not a valid object name doesnotexist
"""
+def test_cmd_has_feature():
+ r"""
+ Methods tested:
+ - L{gbp.git.GitRepository._cmd_has_feature}
+
+ >>> import gbp.git
+ >>> repo = gbp.git.GitRepository(repo_dir)
+ >>> repo._cmd_has_feature("commit", "a")
+ True
+ >>> repo._cmd_has_feature("commit", "reuse-message")
+ True
+ >>> repo._cmd_has_feature("merge", "n")
+ True
+ >>> repo._cmd_has_feature("merge", "stat")
+ True
+ >>> repo._cmd_has_feature("format-patch", "cc")
+ True
+ >>> repo._cmd_has_feature("merge", "foobaroption")
+ False
+ >>> repo._cmd_has_feature("foobarcmd", "foobaroption")
+ Traceback (most recent call last):
+ ...
+ GitRepositoryError: Invalid git command: foobarcmd
+ """
+
def test_teardown():
"""
Perform the teardown