1 # Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 import libbe.command.util
24 import libbe.ui.util.editor
27 class Commit (libbe.command.Command):
28 """Commit the currently pending changes to the repository
31 >>> import libbe.bugdir
32 >>> bd = libbe.bugdir.SimpleBugDir(memory=False, versioned=True)
33 >>> io = libbe.command.StringInputOutput()
34 >>> io.stdout = sys.stdout
35 >>> ui = libbe.command.UserInterface(io=io)
36 >>> ui.storage_callbacks.set_storage(bd.storage)
37 >>> cmd = Commit(ui=ui)
39 >>> bd.extra_strings = ['hi there']
41 >>> ui.run(cmd, args=['Making a commit']) # doctest: +ELLIPSIS
48 def __init__(self, *args, **kwargs):
49 libbe.command.Command.__init__(self, *args, **kwargs)
51 libbe.command.Option(name='body', short_name='b',
52 help='Provide the detailed body for the commit message. In the special case that FILE == "EDITOR", spawn an editor to enter the body text (in which case you cannot use stdin for the summary)',
53 arg=libbe.command.Argument(name='body', metavar='FILE',
54 completion_callback=libbe.command.util.complete_path)),
55 libbe.command.Option(name='allow-empty', short_name='a',
56 help='Allow empty commits'),
59 libbe.command.Argument(
60 name='comment', metavar='COMMENT', default=None),
63 def _run(self, **params):
64 if params['comment'] == '-': # read summary from stdin
65 assert params['body'] != 'EDITOR', \
66 'Cannot spawn and editor when the summary is using stdin.'
67 summary = sys.stdin.readline()
69 summary = params['comment']
70 storage = self._get_storage()
71 if params['body'] == None:
73 elif params['body'] == 'EDITOR':
74 body = libbe.ui.util.editor.editor_string(
75 'Please enter your commit message above')
77 self._check_restricted_access(storage, params['body'])
78 body = libbe.util.encoding.get_file_contents(
79 params['body'], decode=True)
81 revision = storage.commit(summary, body=body,
82 allow_empty=params['allow-empty'])
83 print >> self.stdout, 'Committed %s' % revision
84 except libbe.storage.EmptyCommit, e:
85 print >> self.stdout, e
90 Commit the current repository status. The summary specified on the
91 commandline is a string (only one line) that describes the commit
92 briefly or "-", in which case the string will be read from stdin.