1
# Bazaar-NG -- distributed version control
3
# Copyright (C) 2005 by Canonical Ltd
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.
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.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
"""Commit message editor support."""
25
from subprocess import call
28
import bzrlib.config as config
29
from bzrlib.errors import BzrError
32
"""Return a sequence of possible editor binaries for the current platform"""
34
yield os.environ["BZR_EDITOR"]
38
e = config.GlobalConfig().get_editor()
43
yield os.environ["EDITOR"]
49
elif os.name == "posix":
53
def _run_editor(filename):
54
"""Try to execute an editor to edit the commit message."""
55
for e in _get_editor():
58
x = call(edargs + [filename])
60
# ENOENT means no such editor
61
if e.errno == errno.ENOENT:
70
raise BzrError("Could not start any editor. "
71
"Please specify $EDITOR or use ~/.bzr.conf/editor")
74
DEFAULT_IGNORE_LINE = "%(bar)s %(msg)s %(bar)s" % \
75
{ 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
78
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
79
"""Let the user edit a commit message in a temp file.
81
This is run if they don't give a message or
82
message-containing file on the command line.
85
Text to be displayed at bottom of message for
86
the user's reference; currently similar to
92
tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
93
msgfile = os.close(tmp_fileno)
94
if infotext is not None and infotext != "":
96
msgfile = file(msgfilename, "w")
97
msgfile.write("\n\n%s\n\n%s" % (ignoreline,
98
infotext.encode(bzrlib.user_encoding, 'replace')))
103
if not _run_editor(msgfilename):
108
lastline, nlines = 0, 0
109
for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
110
stripped_line = line.strip()
111
# strip empty line before the log message starts
113
if stripped_line != "":
117
# check for the ignore line only if there
118
# is additional information at the end
119
if hasinfo and stripped_line == ignoreline:
122
# keep track of the last line that had some content
123
if stripped_line != "":
129
# delete empty lines at the end
131
# add a newline at the end, if needed
132
if not msg[-1].endswith("\n"):
133
return "%s%s" % ("".join(msg), "\n")
137
# delete the msg file in any case
138
try: os.unlink(msgfilename)
139
except (IOError, OSError), e:
140
if (not hasattr(e, 'errno')
141
or e.errno not in (errno.ENOENT, errno.ENOTDIR,
142
errno.EPERM, errno.EACCES)):
146
def make_commit_message_template(working_tree, specific_files):
147
"""Prepare a template file for a commit into a branch.
149
Returns a unicode string containing the template.
151
# TODO: Should probably be given the WorkingTree not the branch
153
# TODO: make provision for this to be overridden or modified by a hook
155
# TODO: Rather than running the status command, should prepare a draft of
156
# the revision to be committed, then pause and ask the user to
157
# confirm/write a message.
158
from StringIO import StringIO # must be unicode-safe
159
from bzrlib.status import show_tree_status
160
status_tmp = StringIO()
161
show_tree_status(working_tree, specific_files=specific_files,
163
return status_tmp.getvalue()