git.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2011 Guido Günther <agx@sigxcpu.org>
  4. # (C) 2012 Intel Corporation <markus.lehtonen@linux.intel.com>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, please see
  17. # <http://www.gnu.org/licenses/>
  18. import re
  19. from gbp.format import format_str
  20. from gbp.errors import GbpError
  21. from gbp.git import GitRepository, GitRepositoryError
  22. from gbp.pkg.pristinetar import PristineTar
  23. from gbp.rpm import compose_version_str
  24. class RpmGitRepository(GitRepository):
  25. """A git repository that holds the source of an RPM package"""
  26. def __init__(self, path):
  27. super(RpmGitRepository, self).__init__(path)
  28. self.pristine_tar = PristineTar(self)
  29. def find_version(self, format, str_fields):
  30. """
  31. Check if a certain version is stored in this repo and return the SHA1
  32. of the related commit. That is, an annotated tag is dereferenced to the
  33. commit object it points to.
  34. @param format: tag pattern
  35. @type format: C{str}
  36. @param str_fields: arguments for format string ('upstreamversion', 'release', 'vendor'...)
  37. @type str_fields: C{dict} of C{str}
  38. @return: sha1 of the commit the tag references to
  39. """
  40. try:
  41. tag = self.version_to_tag(format, str_fields)
  42. except GbpError:
  43. return None
  44. if self.has_tag(tag): # new tags are injective
  45. # dereference to a commit object
  46. return self.rev_parse("%s^0" % tag)
  47. return None
  48. @staticmethod
  49. def version_to_tag(format, str_fields):
  50. """
  51. Generate a tag from a given format and a version
  52. @param format: tag pattern
  53. @type format: C{str}
  54. @param str_fields: arguments for format string ('upstreamversion', 'release', 'vendor'...)
  55. @type str_fields: C{dict} of C{str}
  56. @return: version tag
  57. >>> RpmGitRepository.version_to_tag("packaging/%(version)s", dict(epoch='0', upstreamversion='0~0'))
  58. 'packaging/0%0_0'
  59. >>> RpmGitRepository.version_to_tag("%(vendor)s/v%(version)s", dict(upstreamversion='1.0', release='2', vendor="myvendor"))
  60. 'myvendor/v1.0-2'
  61. """
  62. version_tag = format_str(format,
  63. dict(str_fields,
  64. version=compose_version_str(str_fields)))
  65. return RpmGitRepository._sanitize_tag(version_tag)
  66. @staticmethod
  67. def _sanitize_tag(tag):
  68. """sanitize a version so git accepts it as a tag
  69. >>> RpmGitRepository._sanitize_tag("0.0.0")
  70. '0.0.0'
  71. >>> RpmGitRepository._sanitize_tag("0.0~0")
  72. '0.0_0'
  73. >>> RpmGitRepository._sanitize_tag("0:0.0")
  74. '0%0.0'
  75. >>> RpmGitRepository._sanitize_tag("0%0~0")
  76. '0%0_0'
  77. """
  78. return tag.replace('~', '_').replace(':', '%')
  79. @property
  80. def pristine_tar_branch(self):
  81. """
  82. The name of the pristine-tar branch, whether it already exists or
  83. not.
  84. """
  85. return PristineTar.branch
  86. def has_pristine_tar_branch(self):
  87. """
  88. Wheter the repo has a I{pristine-tar} branch.
  89. @return: C{True} if the repo has pristine-tar commits already, C{False}
  90. otherwise
  91. @rtype: C{Bool}
  92. """
  93. return True if self.has_branch(self.pristine_tar_branch) else False
  94. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: