Merged Anton Batenev's report of Nicolas Alvarez' unicode-in-be-new bug
[be.git] / libbe / command / commit.py
1 # Copyright (C) 2009-2010 W. Trevor King <wking@drexel.edu>
2 #
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.
7 #
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.
12 #
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.
16
17 import sys
18
19 import libbe
20 import libbe.bugdir
21 import libbe.command
22 import libbe.command.util
23 import libbe.storage
24 import libbe.ui.util.editor
25
26
27 class Commit (libbe.command.Command):
28     """Commit the currently pending changes to the repository
29
30     >>> import sys
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)
38
39     >>> bd.extra_strings = ['hi there']
40     >>> bd.flush_reload()
41     >>> ui.run(cmd, args=['Making a commit']) # doctest: +ELLIPSIS
42     Committed ...
43     >>> ui.cleanup()
44     >>> bd.cleanup()
45     """
46     name = 'commit'
47
48     def __init__(self, *args, **kwargs):
49         libbe.command.Command.__init__(self, *args, **kwargs)
50         self.options.extend([
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'),
57                  ])
58         self.args.extend([
59                 libbe.command.Argument(
60                     name='comment', metavar='COMMENT', default=None),
61                 ])
62
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()
68         else:
69             summary = params['comment']
70         storage = self._get_storage()
71         if params['body'] == None:
72             body = None
73         elif params['body'] == 'EDITOR':
74             body = libbe.ui.util.editor.editor_string(
75                 'Please enter your commit message above')
76         else:
77             self._check_restricted_access(storage, params['body'])
78             body = libbe.util.encoding.get_file_contents(
79                 params['body'], decode=True)
80         try:
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
86             return 1
87
88     def _long_help(self):
89         return """
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.
93 """