gbp-add-patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python -u
  2. # vim: set fileencoding=utf-8 :
  3. #
  4. # (C) 2010,2015 Guido Guenther <agx@sigxcpu.org>
  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. #
  19. """Add a patch from debian/patches and autocreate the commit message from the
  20. patch info
  21. running
  22. gbp-add-patch debian/patches/0010-bla-fasel
  23. commits debian/patches/0010-bla-fasel with this changelog message:
  24. New patch 0010-bla-fasel
  25. <patch summary>
  26. Closes: <bugs>
  27. Thanks: <author>
  28. """
  29. from __future__ import print_function
  30. import re
  31. import sys
  32. import os, os.path
  33. from gbp.command_wrappers import (Command)
  34. from gbp.config import (GbpOptionParserDebian, GbpOptionGroup)
  35. from gbp.errors import GbpError
  36. from gbp.git import (GitRepositoryError, GitRepository)
  37. from gbp.patch_series import Patch
  38. def build_commit_msg(repo, patch, options):
  39. bug_r = r'(?:bug)?\#?\s?\d+'
  40. bts_closes = re.compile(r'(?P<bts>%s):\s+%s' % (options.meta_closes, bug_r), re.I)
  41. thanks = ''
  42. closes = ''
  43. author = repo.get_author_info()
  44. if author.name != patch.author:
  45. thanks = "Thanks: %s" % patch.author
  46. for line in patch.long_desc.split('\n'):
  47. if bts_closes.match(line):
  48. closes += line + '\n'
  49. patch_name = os.path.basename(patch.path)
  50. msg="""New patch %s
  51. %s
  52. %s
  53. %s""" % (patch_name, patch.subject, thanks, closes)
  54. return msg
  55. def main(argv):
  56. retval = 0
  57. parser = GbpOptionParserDebian(command=os.path.basename(argv[0]), prefix='',
  58. usage='%prog [options] - add a new patch')
  59. parser.add_config_file_option(option_name="meta-closes", dest="meta_closes",
  60. help="Meta tags for the bts close commands, default is '%(meta-closes)s'")
  61. parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
  62. help="edit commit message before committing")
  63. parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
  64. help="verbose command execution")
  65. (options, args) = parser.parse_args(argv)
  66. if options.verbose:
  67. Command.verbose = True
  68. try:
  69. repo = GitRepository(os.path.curdir)
  70. except GitRepositoryError:
  71. print("%s is not a git repository" % os.path.abspath('.'),
  72. file=sys.stderr)
  73. return 1
  74. try:
  75. if len(args) != 2:
  76. parser.print_help()
  77. raise GbpError
  78. else:
  79. patchfile = args[1]
  80. patch = Patch(patchfile)
  81. repo.add_files(patchfile)
  82. msg = build_commit_msg(repo, patch, options)
  83. repo.commit_staged(edit=options.edit, msg=msg)
  84. # FIXME: handle the series file
  85. except GitRepositoryError:
  86. retval = 1
  87. except GbpError as err:
  88. if str(err):
  89. print(err, file=sys.stderr)
  90. retval = 1
  91. return retval
  92. if __name__ == '__main__':
  93. sys.exit(main(sys.argv))
  94. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: