Split out building a debian version from an upstream commit
authorGuido Günther <agx@sigxcpu.org>
Wed, 27 Mar 2013 15:58:47 +0000 (16:58 +0100)
committerGuido Günther <agx@sigxcpu.org>
Wed, 27 Mar 2013 16:07:16 +0000 (17:07 +0100)
based on a patch by Daniel Dehennin

Needed for #672954, #646684, #669171

gbp/deb/git.py
gbp/scripts/dch.py
tests/03_test_dch_guess_version.py

index c9004ef..c7b6e77 100644 (file)
@@ -62,6 +62,28 @@ class DebianGitRepository(GitRepository):
                     return None
         return None
 
+    def debian_version_from_upstream(self, upstream_tag_format, commit='HEAD',
+                                     epoch=None):
+        """
+        Build the Debian version that a package based on upstream commit
+        I{commit} would carry taking into account a possible epoch.
+
+        @param upstream_tag_format; the tag format on the upstream branch
+        @type upstream_tag_format; C{str}
+        @param commit: the commit to search for the latest upstream version
+        @param epoch: an epoch to use
+        @returns: a new debian version
+        @raises: L{GitRepositoryError} if no upstream tag was found
+        """
+        pattern = upstream_tag_format % dict(version='*')
+        tag = self.find_tag(commit, pattern=pattern)
+        version = self.tag_to_version(tag, upstream_tag_format)
+
+        version += "-1"
+        if epoch:
+            version = "%s:%s" % (epoch, version)
+        return version
+
     @staticmethod
     def _build_legacy_tag(format, version):
         """
index f6bcfb1..86c0547 100644 (file)
@@ -105,18 +105,14 @@ def guess_version_from_upstream(repo, upstream_tag_format, cp):
     """
     Guess the version based on the latest version on the upstream branch
     """
-    pattern = upstream_tag_format % dict(version='*')
     try:
-        tag = repo.find_tag('HEAD', pattern=pattern)
-        version = repo.tag_to_version(tag, upstream_tag_format)
-        if version:
-            gbp.log.debug("Found upstream version %s." % version)
-            if cp.has_epoch():
-                version = "%s:%s" % (cp.epoch, version)
-            if compare_versions(version, cp.version) > 0:
-                return "%s-1" % version
+        version = repo.debian_version_from_upstream(upstream_tag_format,
+                                                    epoch=cp.epoch)
+        gbp.log.debug("Found upstream version %s." % version)
+        if compare_versions(version, cp.version) > 0:
+            return version
     except GitRepositoryError:
-        gbp.log.debug("No tag found matching pattern %s." % pattern)
+        gbp.log.debug("No upstream tag found")
     return None
 
 
index d954459..8bcd4e5 100644 (file)
@@ -2,22 +2,10 @@
 
 """Test L{Changelog}'s guess_version_from_upstream"""
 
-import unittest
+import testutils
 
 from gbp.scripts import dch
-from gbp.errors import GbpError
 from gbp.deb.changelog import ChangeLog
-from gbp.deb.git import DebianGitRepository
-
-class MockGitRepository(object):
-    def __init__(self, upstream_tag):
-        self.upstream_tag = upstream_tag
-
-    def find_tag(self, branch, pattern):
-        return self.upstream_tag
-
-    def tag_to_version(self, tag, format):
-        return DebianGitRepository.tag_to_version(tag, format)
 
 
 class MockedChangeLog(ChangeLog):
@@ -31,24 +19,41 @@ class MockedChangeLog(ChangeLog):
         ChangeLog.__init__(self, contents=self.contents % version)
 
 
-class TestGuessVersionFromUpstream(unittest.TestCase):
+class TestGuessVersionFromUpstream(testutils.DebianGitTestRepo):
     """Test guess_version_from_upstream"""
+
     def test_guess_no_epoch(self):
         """Guess the new version from the upstream tag"""
-        repo = MockGitRepository(upstream_tag='upstream/1.1')
         cp = MockedChangeLog('1.0-1')
-        guessed = dch.guess_version_from_upstream(repo,
-                                                  'upstream/%(version)s',
+        tagformat = 'upstream/%(version)s'
+        uversion = '1.1'
+
+        self.add_file('doesnot', 'matter')
+        tag = self.repo.version_to_tag(tagformat, uversion)
+        self.repo.create_tag(name=tag, msg="Upstream release %s" % uversion,
+                             sign=False)
+
+        guessed = dch.guess_version_from_upstream(self.repo,
+                                                  tagformat,
                                                   cp)
         self.assertEqual('1.1-1', guessed)
 
     def test_guess_epoch(self):
         """Check if we picked up the epoch correctly (#652366)"""
-        repo = MockGitRepository(upstream_tag='upstream/1.1')
         cp = MockedChangeLog('1:1.0-1')
-        guessed = dch.guess_version_from_upstream(repo,
-                                                  'upstream/%(version)s',
+
+        tagformat = 'upstream/%(version)s'
+        uversion = '1.1'
+
+        self.add_file('doesnot', 'matter')
+        tag = self.repo.version_to_tag(tagformat, uversion)
+        self.repo.create_tag(name=tag, msg="Upstream release %s" % uversion,
+                             sign=False)
+
+        guessed = dch.guess_version_from_upstream(self.repo,
+                                                  tagformat,
                                                   cp)
+
         self.assertEqual('1:1.1-1', guessed)