1
# Copyright (C) 2005, 2006 Canonical Ltd
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
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Commit message editor support."""
23
from subprocess import call
31
from bzrlib.errors import BzrError, BadCommitMessageEncoding
32
from bzrlib.hooks import HookPoint, Hooks
36
"""Return a sequence of possible editor binaries for the current platform"""
38
yield os.environ["BZR_EDITOR"], '$BZR_EDITOR'
42
e = config.GlobalConfig().get_editor()
44
yield e, config.config_filename()
46
for varname in 'VISUAL', 'EDITOR':
47
if varname in os.environ:
48
yield os.environ[varname], '$' + varname
50
if sys.platform == 'win32':
51
for editor in 'wordpad.exe', 'notepad.exe':
54
for editor in ['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe']:
58
def _run_editor(filename):
59
"""Try to execute an editor to edit the commit message."""
60
for candidate, candidate_source in _get_editor():
61
edargs = candidate.split(' ')
63
## mutter("trying editor: %r", (edargs +[filename]))
64
x = call(edargs + [filename])
66
# We're searching for an editor, so catch safe errors and continue
67
# errno 193 is ERROR_BAD_EXE_FORMAT on Windows. Python2.4 uses the
68
# winerror for errno. Python2.5+ use errno ENOEXEC and set winerror
69
# to 193. However, catching 193 here should be fine. Other
70
# platforms aren't likely to have that high of an error. And even
71
# if they do, it is still reasonable to fall back to the next
73
if e.errno in (errno.ENOENT, errno.EACCES, errno.ENOEXEC, 193):
74
if candidate_source is not None:
75
# We tried this editor because some user configuration (an
76
# environment variable or config file) said to try it. Let
77
# the user know their configuration is broken.
79
'Could not start editor "%s" (specified by %s): %s\n'
80
% (candidate, candidate_source, str(e)))
89
raise BzrError("Could not start any editor.\nPlease specify one with:\n"
90
" - $BZR_EDITOR\n - editor=/some/path in %s\n"
91
" - $VISUAL\n - $EDITOR" % \
92
config.config_filename())
95
DEFAULT_IGNORE_LINE = "%(bar)s %(msg)s %(bar)s" % \
96
{ 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
99
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE,
101
"""Let the user edit a commit message in a temp file.
103
This is run if they don't give a message or
104
message-containing file on the command line.
106
:param infotext: Text to be displayed at bottom of message
107
for the user's reference;
108
currently similar to 'bzr status'.
110
:param ignoreline: The separator to use above the infotext.
112
:param start_message: The text to place above the separator, if any.
113
This will not be removed from the message
114
after the user has edited it.
116
:return: commit message or None.
119
if not start_message is None:
120
start_message = start_message.encode(osutils.get_user_encoding())
121
infotext = infotext.encode(osutils.get_user_encoding(), 'replace')
122
return edit_commit_message_encoded(infotext, ignoreline, start_message)
125
def edit_commit_message_encoded(infotext, ignoreline=DEFAULT_IGNORE_LINE,
127
"""Let the user edit a commit message in a temp file.
129
This is run if they don't give a message or
130
message-containing file on the command line.
132
:param infotext: Text to be displayed at bottom of message
133
for the user's reference;
134
currently similar to 'bzr status'.
135
The string is already encoded
137
:param ignoreline: The separator to use above the infotext.
139
:param start_message: The text to place above the separator, if any.
140
This will not be removed from the message
141
after the user has edited it.
142
The string is already encoded
144
:return: commit message or None.
148
msgfilename, hasinfo = _create_temp_file_with_commit_template(
149
infotext, ignoreline, start_message)
151
if not msgfilename or not _run_editor(msgfilename):
156
lastline, nlines = 0, 0
157
# codecs.open() ALWAYS opens file in binary mode but we need text mode
158
# 'rU' mode useful when bzr.exe used on Cygwin (bialix 20070430)
159
f = file(msgfilename, 'rU')
162
for line in codecs.getreader(osutils.get_user_encoding())(f):
163
stripped_line = line.strip()
164
# strip empty line before the log message starts
166
if stripped_line != "":
170
# check for the ignore line only if there
171
# is additional information at the end
172
if hasinfo and stripped_line == ignoreline:
175
# keep track of the last line that had some content
176
if stripped_line != "":
179
except UnicodeDecodeError:
180
raise BadCommitMessageEncoding()
186
# delete empty lines at the end
188
# add a newline at the end, if needed
189
if not msg[-1].endswith("\n"):
190
return "%s%s" % ("".join(msg), "\n")
194
# delete the msg file in any case
195
if msgfilename is not None:
197
os.unlink(msgfilename)
200
"failed to unlink %s: %s; ignored", msgfilename, e)
203
def _create_temp_file_with_commit_template(infotext,
204
ignoreline=DEFAULT_IGNORE_LINE,
206
"""Create temp file and write commit template in it.
208
:param infotext: Text to be displayed at bottom of message
209
for the user's reference;
210
currently similar to 'bzr status'.
211
The text is already encoded.
213
:param ignoreline: The separator to use above the infotext.
215
:param start_message: The text to place above the separator, if any.
216
This will not be removed from the message
217
after the user has edited it.
218
The string is already encoded
220
:return: 2-tuple (temp file name, hasinfo)
223
tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
226
msgfilename = osutils.basename(msgfilename)
227
msgfile = os.fdopen(tmp_fileno, 'w')
229
if start_message is not None:
230
msgfile.write("%s\n" % start_message)
232
if infotext is not None and infotext != "":
234
msgfile.write("\n\n%s\n\n%s" %(ignoreline, infotext))
240
return (msgfilename, hasinfo)
243
def make_commit_message_template(working_tree, specific_files):
244
"""Prepare a template file for a commit into a branch.
246
Returns a unicode string containing the template.
248
# TODO: make provision for this to be overridden or modified by a hook
250
# TODO: Rather than running the status command, should prepare a draft of
251
# the revision to be committed, then pause and ask the user to
252
# confirm/write a message.
253
from StringIO import StringIO # must be unicode-safe
254
from bzrlib.status import show_tree_status
255
status_tmp = StringIO()
256
show_tree_status(working_tree, specific_files=specific_files,
257
to_file=status_tmp, verbose=True)
258
return status_tmp.getvalue()
261
def make_commit_message_template_encoded(working_tree, specific_files,
262
diff=None, output_encoding='utf-8'):
263
"""Prepare a template file for a commit into a branch.
265
Returns an encoded string.
267
# TODO: make provision for this to be overridden or modified by a hook
269
# TODO: Rather than running the status command, should prepare a draft of
270
# the revision to be committed, then pause and ask the user to
271
# confirm/write a message.
272
from StringIO import StringIO # must be unicode-safe
273
from bzrlib.diff import show_diff_trees
275
template = make_commit_message_template(working_tree, specific_files)
276
template = template.encode(output_encoding, "replace")
280
show_diff_trees(working_tree.basis_tree(),
281
working_tree, stream, specific_files,
282
path_encoding=output_encoding)
283
template = template + '\n' + stream.getvalue()
288
class MessageEditorHooks(Hooks):
289
"""A dictionary mapping hook name to a list of callables for message editor
292
e.g. ['commit_message_template'] is the list of items to be called to
293
generate a commit message template
297
"""Create the default hooks.
299
These are all empty initially.
302
self.create_hook(HookPoint('commit_message_template',
303
"Called when a commit message is being generated. "
304
"commit_message_template is called with the bzrlib.commit.Commit "
305
"object and the message that is known so far. "
306
"commit_message_template must return a new message to use (which "
307
"could be the same as it was given. When there are multiple "
308
"hooks registered for commit_message_template, they are chained "
309
"with the result from the first passed into the second, and so "
310
"on.", (1, 10), None))
313
hooks = MessageEditorHooks()
316
def generate_commit_message_template(commit, start_message=None):
317
"""Generate a commit message template.
319
:param commit: Commit object for the active commit.
320
:param start_message: Message to start with.
321
:return: A start commit message or None for an empty start commit message.
324
for hook in hooks['commit_message_template']:
325
start_message = hook(commit, start_message)