From d1cdbd22778c040a5b6620d463092c735262d2e7 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 18 Apr 2013 10:46:23 +0300 Subject: GitRepository: add describe() method Change GitRepository.find_tag() to its original simple version. Signed-off-by: Markus Lehtonen --- gbp/git/repository.py | 49 ++++++++++++++++++++++++++++------------- gbp/scripts/buildpackage_rpm.py | 4 ++-- tests/test_GitRepository.py | 33 ++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 22 deletions(-) diff --git a/gbp/git/repository.py b/gbp/git/repository.py index 77c7cf7a..31a4eb49 100644 --- a/gbp/git/repository.py +++ b/gbp/git/repository.py @@ -624,37 +624,56 @@ class GitRepository(object): out, ret = self._git_getoutput('tag', [ '-l', tag ]) return [ False, True ][len(out)] - def find_tag(self, commit, pattern=None, longfmt=False, always=False): + def describe(self, commitish, pattern=None, longfmt=False, always=False, + abbrev=None): """ - Find the closest tag to a given commit + Describe commit, relative to the latest tag reachable from it. - @param commit: the commit to describe - @type commit: C{str} + @param commitish: the commit-ish to describe + @type commitish: C{str} @param pattern: only look for tags matching I{pattern} @type pattern: C{str} - @param longfmt: return the tag in the long format + @param longfmt: describe the commit in the long format @type longfmt: C{bool} - @param always: return commit sha1 as fallback + @param always: return commit sha1 as fallback if no tag is found @type always: C{bool} - @return: the found tag + @param abbrev: abbreviate sha1 to given length instead of the default + @type abbrev: None or C{long} + @return: tag name plus/or the abbreviated sha1 @rtype: C{str} """ args = GitArgs() args.add_true(pattern, ['--match' , pattern]) - if not longfmt and not always: - args.add('--abbrev=0') - else: - args.add_true(longfmt, '--long') - args.add_true(always, '--always') - args.add(commit) + args.add_true(longfmt, '--long') + # 'long' and 'abbrev=0' are incompatible, behave similar to + # 'always' and 'abbrev=0' + if longfmt and abbrev == 0: + args.add('--abbrev=40') + elif abbrev is not None: + args.add('--abbrev=%s' % abbrev) + args.add_true(always, '--always') + args.add(commitish) tag, err, ret = self._git_inout('describe', args.args, capture_stderr=True) if ret: - raise GitRepositoryError("Can't find tag for %s. Git error: %s" % \ - (commit, err.strip())) + raise GitRepositoryError("Can't describe %s. Git error: %s" % \ + (commitish, err.strip())) return tag.strip() + def find_tag(self, commit, pattern=None): + """ + Find the closest tag to a given commit + + @param commit: the commit to describe + @type commit: C{str} + @param pattern: only look for tags matching I{pattern} + @type pattern: C{str} + @return: the found tag + @rtype: C{str} + """ + return self.describe(commit, pattern, abbrev=0) + def get_tags(self, pattern=None): """ List tags diff --git a/gbp/scripts/buildpackage_rpm.py b/gbp/scripts/buildpackage_rpm.py index 33c5f447..831d85f2 100755 --- a/gbp/scripts/buildpackage_rpm.py +++ b/gbp/scripts/buildpackage_rpm.py @@ -603,10 +603,10 @@ def main(argv): 'GBP_SHA1': sha})() else: try: - tree_name = repo.find_tag(tree, longfmt=True, always=True) + tree_name = repo.describe(tree, longfmt=True, always=True) except GitRepositoryError: # If tree is not commit-ish, expect it to be from current HEAD - tree_name = repo.find_tag('HEAD', longfmt=True, always=True) +\ + tree_name = repo.describe('HEAD', longfmt=True, always=True) +\ '-dirty' # Put 'VCS:' tag to .spec spec.set_tag('VCS', None, diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py index 3b6b924b..443c81dc 100644 --- a/tests/test_GitRepository.py +++ b/tests/test_GitRepository.py @@ -252,6 +252,33 @@ def test_tag(): ['tag', 'tag2'] """ +def test_describe(): + """ + Describe commit-ish + + Methods tested: + - L{gbp.git.GitRepository.describe} + + >>> import gbp.git + >>> repo = gbp.git.GitRepository(repo_dir) + >>> sha = repo.rev_parse('HEAD') + >>> repo.describe('HEAD') + 'tag2' + >>> repo.describe('HEAD', longfmt=True) == 'tag2-0-g%s' % sha[:7] + True + >>> repo.describe('HEAD', pattern='foo*') + Traceback (most recent call last): + ... + GitRepositoryError: Can't describe HEAD. Git error: fatal: No names found, cannot describe anything. + >>> repo.describe('HEAD', pattern='foo*', always=True) == sha[:7] + True + >>> repo.describe('HEAD', always=True, abbrev=16) + 'tag2' + >>> repo.describe('HEAD', pattern='foo*', always=True, abbrev=16) == sha[:16] + True + >>> tag = repo.describe('HEAD', longfmt=True, abbrev=16) == 'tag2-0-g%s' % sha[:16] + """ + def test_find_tag(): """ Find tags @@ -263,14 +290,10 @@ def test_find_tag(): >>> repo = gbp.git.GitRepository(repo_dir) >>> repo.find_tag('HEAD') 'tag2' - >>> repo.find_tag('HEAD', longfmt=True)[:7] - 'tag2-0-' >>> repo.find_tag('HEAD', pattern='foo*') Traceback (most recent call last): ... - GitRepositoryError: Can't find tag for HEAD. Git error: fatal: No names found, cannot describe anything. - >>> len(repo.find_tag('HEAD', pattern='foo*', always=True)) - 7 + GitRepositoryError: Can't describe HEAD. Git error: fatal: No names found, cannot describe anything. """ def test_move_tag(): -- cgit v1.2.3