aboutsummaryrefslogtreecommitdiff
path: root/examples/gbp-add-patch
blob: bf07fd75ce24fa8e9aef0b54eb58f3e110e8bede (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python -u
# vim: set fileencoding=utf-8 :
#
# (C) 2010 Guido Guenther <agx@sigxcpu.org>
#    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
#
"""Add a patch from debian/patches and autocreate the commit message from the
patch info

running

    gbp-add-patch debian/patches/0010-bla-fasel

commits debian/patches/0010-bla-fasel with this changelog message:

    New patch 0010-bla-fasel

    <patch summary>
    Closes: <bugs>
    Thanks: <author>
"""

import re
import sys
import os, os.path
from gbp.command_wrappers import (Command,
                                  CommandExecFailed,
                                  GitCommand)
from gbp.config import (GbpOptionParserDebian, GbpOptionGroup)
from gbp.errors import GbpError
from gbp.git import (GitRepositoryError, GitRepository)
from gbp.patch_series import Patch


def build_commit_msg(repo, patch, options):
    bug_r = r'(?:bug)?\#?\s?\d+'
    bts_closes = re.compile(r'(?P<bts>%s):\s+%s' % (options.meta_closes, bug_r), re.I)
    thanks = ''
    closes = ''

    author = repo.get_author_info()
    if author.name != patch.author:
        thanks = "Thanks: %s" % patch.author

    for line in patch.long_desc.split('\n'):
        if bts_closes.match(line):
            closes += line + '\n'

    patch_name = os.path.basename(patch.path)
    msg="""New patch %s

%s
%s
%s""" % (patch_name, patch.subject, thanks, closes)
    return msg


def main(argv):
    retval = 0

    parser = GbpOptionParserDebian(command=os.path.basename(argv[0]), prefix='',
                             usage='%prog [options] - add a new patch')
    parser.add_config_file_option(option_name="meta-closes", dest="meta_closes",
                      help="Meta tags for the bts close commands, default is '%(meta-closes)s'")
    parser.add_option("-e", "--edit", action="store_true", dest="edit", default=False,
                      help="edit commit message before committing")
    parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
                      help="verbose command execution")

    (options, args) = parser.parse_args(argv)

    if options.verbose:
        Command.verbose = True

    try:
        repo = GitRepository(os.path.curdir)
    except GitRepositoryError:
        print >>sys.stderr, "%s is not a git repository" % (os.path.abspath('.'))
        return 1

    try:
        if len(args) != 2:
            parser.print_help()
            raise GbpError
        else:
            patchfile = args[1]

        patch = Patch(patchfile)

        repo.add_files(patchfile)
        msg = build_commit_msg(repo, patch, options)
        repo.commit_staged(edit=options.edit, msg=msg)
        # FIXME: handle the series file

    except CommandExecFailed:
        retval = 1
    except GbpError, err:
        if len(err.__str__()):
            print >>sys.stderr, err
        retval = 1

    return retval

if __name__ == '__main__':
    sys.exit(main(sys.argv))

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