cmd_submit.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. #
  4. # Copyright (c) 2012 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. """Implementation of subcmd: submit"""
  19. import os
  20. import re
  21. import time
  22. from gitbuildsys.utils import edit
  23. from gitbuildsys.errors import GbsError
  24. from gitbuildsys.log import LOGGER as log
  25. from gbp.rpm.git import GitRepositoryError, RpmGitRepository
  26. def _lookup_submit_template():
  27. """
  28. Look for submit templates in current project,
  29. user and system settings location
  30. """
  31. lookup_paths = (
  32. '.gbs/templates/submit_message',
  33. '~/.gbs/templates/submit_message',
  34. '/etc/gbs/templates/submit_message')
  35. for path in lookup_paths:
  36. abs_path = os.path.abspath(os.path.expanduser(path))
  37. if os.path.exists(abs_path):
  38. return abs_path
  39. return None
  40. def get_message():
  41. '''
  42. get message from editor
  43. '''
  44. prompt = '''
  45. # Please enter the message for your tag. Lines starting with '#'
  46. # will be ignored, and an empty message aborts the submission.
  47. #'''
  48. #check for additional submit template
  49. template_path = _lookup_submit_template()
  50. if template_path:
  51. with open(template_path, 'r') as template_file:
  52. submit_template = template_file.read()
  53. prompt = submit_template
  54. raw = edit(prompt)
  55. useful = [i for i in raw.splitlines() if not i.startswith('#') ]
  56. return os.linesep.join(useful).strip()
  57. def main(args):
  58. """gbs submit entry point."""
  59. workdir = args.gitdir
  60. message = args.msg
  61. if message is None:
  62. message = get_message()
  63. if not message:
  64. raise GbsError("tag message is required")
  65. try:
  66. repo = RpmGitRepository(workdir)
  67. commit = repo.rev_parse(args.commit)
  68. current_branch = repo.get_branch()
  69. except GitRepositoryError, err:
  70. raise GbsError(str(err))
  71. try:
  72. upstream = repo.get_upstream_branch(current_branch)
  73. except GitRepositoryError:
  74. upstream = None
  75. if not args.remote:
  76. if upstream:
  77. args.remote = upstream.split('/')[0]
  78. else:
  79. log.info("no upstream set for the current branch, using "
  80. "'origin' as the remote server")
  81. args.remote = 'origin'
  82. if args.tag:
  83. tagname = args.tag
  84. tag_re = re.compile(r'^submit/\S+/\d{8}\.\d{6}$')
  85. if not tag_re.match(tagname):
  86. raise GbsError("invalid tag %s, valid tag format is "
  87. "submit/$target/$date.$time. For example:\n "
  88. "submit/trunk/20130128.022439 " % tagname)
  89. else:
  90. target = args.target
  91. if not target:
  92. if upstream and upstream.startswith(args.remote):
  93. target = re.sub('^%s/' % args.remote, '', upstream)
  94. else:
  95. log.warning("Can't find upstream branch for current branch "
  96. "%s. Gbs uses the local branch name as the target. "
  97. "Please consider to use git-branch --set-upstream "
  98. "to set upstream remote branch." % current_branch)
  99. target = current_branch
  100. if target == 'master':
  101. target = 'trunk'
  102. tagname = 'submit/%s/%s' % (target, time.strftime( \
  103. '%Y%m%d.%H%M%S', time.gmtime()))
  104. log.info('creating tag: %s' % tagname)
  105. try:
  106. repo.create_tag(tagname, msg=message, commit=commit, sign=args.sign,
  107. keyid=args.user_key)
  108. except GitRepositoryError, err:
  109. raise GbsError('failed to create tag %s: %s ' % (tagname, str(err)))
  110. log.info("pushing tag to remote '%s'" % args.remote)
  111. try:
  112. repo.push_tag(args.remote, tagname)
  113. except GitRepositoryError, err:
  114. repo.delete_tag(tagname)
  115. raise GbsError('failed to push tag %s :%s' % (tagname, str(err)))
  116. log.info('done.')