1167
by Martin Pool
- split commit message editor functions out into own file |
1 |
# Bazaar-NG -- distributed version control
|
2 |
||
3 |
# Copyright (C) 2005 by Canonical Ltd
|
|
4 |
||
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.
|
|
9 |
||
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.
|
|
14 |
||
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
|
|
18 |
||
19 |
||
20 |
"""Commit message editor support."""
|
|
21 |
||
22 |
import os |
|
1185.62.19
by John Arbash Meinel
Fix error when we can't find an editor |
23 |
import errno |
1185.1.2
by Martin Pool
- merge various windows and other fixes from Ollie Rutherfurd |
24 |
from subprocess import call |
1442.1.3
by Robert Collins
move editor into the config file too |
25 |
|
26 |
import bzrlib.config as config |
|
1167
by Martin Pool
- split commit message editor functions out into own file |
27 |
from bzrlib.errors import BzrError |
28 |
||
29 |
def _get_editor(): |
|
30 |
"""Return a sequence of possible editor binaries for the current platform"""
|
|
1185.1.30
by Robert Collins
Accept and tweak David Clymers BZREDITOR support patch |
31 |
try: |
32 |
yield os.environ["BZR_EDITOR"] |
|
33 |
except KeyError: |
|
34 |
pass
|
|
35 |
||
1442.1.6
by Robert Collins
first stage major overhaul of configs, giving use BranchConfigs, LocationConfigs and GlobalConfigs |
36 |
e = config.GlobalConfig().get_editor() |
1167
by Martin Pool
- split commit message editor functions out into own file |
37 |
if e is not None: |
38 |
yield e |
|
39 |
||
1185.1.2
by Martin Pool
- merge various windows and other fixes from Ollie Rutherfurd |
40 |
try: |
41 |
yield os.environ["EDITOR"] |
|
42 |
except KeyError: |
|
1185.1.30
by Robert Collins
Accept and tweak David Clymers BZREDITOR support patch |
43 |
pass
|
44 |
||
45 |
if os.name == "nt": |
|
46 |
yield "notepad.exe" |
|
47 |
elif os.name == "posix": |
|
48 |
yield "/usr/bin/vi" |
|
1167
by Martin Pool
- split commit message editor functions out into own file |
49 |
|
50 |
||
51 |
def _run_editor(filename): |
|
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
52 |
"""Try to execute an editor to edit the commit message."""
|
1167
by Martin Pool
- split commit message editor functions out into own file |
53 |
for e in _get_editor(): |
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
54 |
edargs = e.split(' ') |
1185.62.19
by John Arbash Meinel
Fix error when we can't find an editor |
55 |
try: |
56 |
x = call(edargs + [filename]) |
|
57 |
except OSError, e: |
|
58 |
# ENOENT means no such editor
|
|
59 |
if e.errno == errno.ENOENT: |
|
60 |
continue
|
|
61 |
raise
|
|
1167
by Martin Pool
- split commit message editor functions out into own file |
62 |
if x == 0: |
63 |
return True |
|
64 |
elif x == 127: |
|
65 |
continue
|
|
66 |
else: |
|
67 |
break
|
|
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
68 |
raise BzrError("Could not start any editor. " |
69 |
"Please specify $EDITOR or use ~/.bzr.conf/editor") |
|
1167
by Martin Pool
- split commit message editor functions out into own file |
70 |
|
71 |
||
72 |
def edit_commit_message(infotext, ignoreline=None): |
|
73 |
"""Let the user edit a commit message in a temp file.
|
|
74 |
||
75 |
This is run if they don't give a message or
|
|
76 |
message-containing file on the command line.
|
|
77 |
||
78 |
infotext:
|
|
79 |
Text to be displayed at bottom of message for
|
|
80 |
the user's reference; currently similar to
|
|
81 |
'bzr status'.
|
|
82 |
"""
|
|
83 |
import tempfile |
|
84 |
||
85 |
if ignoreline is None: |
|
86 |
ignoreline = "-- This line and the following will be ignored --" |
|
87 |
||
88 |
try: |
|
1185.33.66
by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko) |
89 |
tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.') |
1167
by Martin Pool
- split commit message editor functions out into own file |
90 |
msgfile = os.close(tmp_fileno) |
91 |
if infotext is not None and infotext != "": |
|
92 |
hasinfo = True |
|
93 |
msgfile = file(msgfilename, "w") |
|
94 |
msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext)) |
|
95 |
msgfile.close() |
|
96 |
else: |
|
97 |
hasinfo = False |
|
98 |
||
99 |
if not _run_editor(msgfilename): |
|
100 |
return None |
|
101 |
||
102 |
started = False |
|
103 |
msg = [] |
|
104 |
lastline, nlines = 0, 0 |
|
105 |
for line in file(msgfilename, "r"): |
|
106 |
stripped_line = line.strip() |
|
107 |
# strip empty line before the log message starts
|
|
108 |
if not started: |
|
109 |
if stripped_line != "": |
|
110 |
started = True |
|
111 |
else: |
|
112 |
continue
|
|
113 |
# check for the ignore line only if there
|
|
114 |
# is additional information at the end
|
|
115 |
if hasinfo and stripped_line == ignoreline: |
|
116 |
break
|
|
117 |
nlines += 1 |
|
118 |
# keep track of the last line that had some content
|
|
119 |
if stripped_line != "": |
|
120 |
lastline = nlines |
|
121 |
msg.append(line) |
|
122 |
||
1393.3.3
by Jelmer Vernooij
Add test for empty commit messages. |
123 |
if len(msg) == 0: |
1393.3.2
by Jelmer Vernooij
Fix error message when an empty commit message was specified (when using an editor). Previously bzr warned that it wanted either --message or --file. |
124 |
return "" |
1167
by Martin Pool
- split commit message editor functions out into own file |
125 |
# delete empty lines at the end
|
126 |
del msg[lastline:] |
|
127 |
# add a newline at the end, if needed
|
|
128 |
if not msg[-1].endswith("\n"): |
|
129 |
return "%s%s" % ("".join(msg), "\n") |
|
130 |
else: |
|
131 |
return "".join(msg) |
|
132 |
finally: |
|
133 |
# delete the msg file in any case
|
|
134 |
try: os.unlink(msgfilename) |
|
135 |
except IOError: pass |
|
136 |
||
1185.33.72
by Martin Pool
Fix commit message template for non-ascii files, and add test for handling of |
137 |
|
138 |
def make_commit_message_template(working_tree, specific_files): |
|
139 |
"""Prepare a template file for a commit into a branch.
|
|
140 |
||
141 |
Returns a unicode string containing the template.
|
|
142 |
"""
|
|
143 |
# TODO: Should probably be given the WorkingTree not the branch
|
|
144 |
#
|
|
145 |
# TODO: make provision for this to be overridden or modified by a hook
|
|
146 |
#
|
|
147 |
# TODO: Rather than running the status command, should prepare a draft of
|
|
148 |
# the revision to be committed, then pause and ask the user to
|
|
149 |
# confirm/write a message.
|
|
150 |
from StringIO import StringIO # must be unicode-safe |
|
151 |
from bzrlib.status import show_status |
|
152 |
status_tmp = StringIO() |
|
153 |
show_status(working_tree.branch, specific_files=specific_files, to_file=status_tmp) |
|
154 |
return status_tmp.getvalue() |