summaryrefslogtreecommitdiff
path: root/gbp/rpm/git.py
blob: c7cc023b925d72e6d026d633fb352148e0fdd1df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# vim: set fileencoding=utf-8 :
#
# (C) 2011 Guido Günther <agx@sigxcpu.org>
# (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
import re

from gbp.git import GitRepository, GitRepositoryError
from gbp.pkg.pristinetar import PristineTar
from gbp.rpm import compose_version_str

class RpmGitRepository(GitRepository):
    """A git repository that holds the source of an RPM package"""

    def __init__(self, path):
        super(RpmGitRepository, self).__init__(path)
        self.pristine_tar = PristineTar(self)

    def find_version(self, format, str_fields):
        """
        Check if a certain version is stored in this repo and return the SHA1
        of the related commit. That is, an annotated tag is dereferenced to the
        commit object it points to.

        @param format: tag pattern
        @type format: C{str}
        @param str_fields: arguments for format string ('upstreamversion', 'release', 'vendor'...)
        @type str_fields: C{dict} of C{str}
        @return: sha1 of the commit the tag references to
        """
        try:
            tag = self.version_to_tag(format, str_fields)
        except KeyError:
            return None
        if self.has_tag(tag): # new tags are injective
            # dereference to a commit object
            return self.rev_parse("%s^0" % tag)
        return None

    @staticmethod
    def version_to_tag(format, str_fields):
        """
        Generate a tag from a given format and a version

        @param format: tag pattern
        @type format: C{str}
        @param str_fields: arguments for format string ('upstreamversion', 'release', 'vendor'...)
        @type str_fields: C{dict} of C{str}
        @return: version tag

        >>> RpmGitRepository.version_to_tag("packaging/%(version)s", dict(epoch='0', upstreamversion='0~0'))
        'packaging/0%0_0'
        >>> RpmGitRepository.version_to_tag("%(vendor)s/v%(version)s", dict(upstreamversion='1.0', release='2', vendor="myvendor"))
        'myvendor/v1.0-2'
        """
        version_tag = format % dict(str_fields,
                                    version=compose_version_str(str_fields))
        return RpmGitRepository._sanitize_tag(version_tag)

    @staticmethod
    def _sanitize_tag(tag):
        """sanitize a version so git accepts it as a tag

        >>> RpmGitRepository._sanitize_tag("0.0.0")
        '0.0.0'
        >>> RpmGitRepository._sanitize_tag("0.0~0")
        '0.0_0'
        >>> RpmGitRepository._sanitize_tag("0:0.0")
        '0%0.0'
        >>> RpmGitRepository._sanitize_tag("0%0~0")
        '0%0_0'
        """
        return tag.replace('~', '_').replace(':', '%')

    @property
    def pristine_tar_branch(self):
        """
        The name of the pristine-tar branch, whether it already exists or
        not.
        """
        return PristineTar.branch

    def has_pristine_tar_branch(self):
        """
        Wheter the repo has a I{pristine-tar} branch.

        @return: C{True} if the repo has pristine-tar commits already, C{False}
            otherwise
        @rtype: C{Bool}
        """
        return True if self.has_branch(self.pristine_tar_branch) else False

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: