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 |
|
23 |
from bzrlib.errors import BzrError |
|
24 |
||
25 |
def _get_editor(): |
|
26 |
"""Return a sequence of possible editor binaries for the current platform"""
|
|
27 |
from bzrlib.osutils import _read_config_value |
|
28 |
||
29 |
e = _read_config_value("editor") |
|
30 |
if e is not None: |
|
31 |
yield e |
|
32 |
||
33 |
if os.name == "windows": |
|
34 |
yield "notepad.exe" |
|
35 |
elif os.name == "posix": |
|
36 |
try: |
|
37 |
yield os.environ["EDITOR"] |
|
38 |
except KeyError: |
|
39 |
yield "/usr/bin/vi" |
|
40 |
||
41 |
||
42 |
def _run_editor(filename): |
|
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
43 |
"""Try to execute an editor to edit the commit message."""
|
1167
by Martin Pool
- split commit message editor functions out into own file |
44 |
for e in _get_editor(): |
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
45 |
edargs = e.split(' ') |
46 |
x = os.spawnvp(os.P_WAIT, edargs[0], |
|
47 |
edargs + [filename]) |
|
1167
by Martin Pool
- split commit message editor functions out into own file |
48 |
if x == 0: |
49 |
return True |
|
50 |
elif x == 127: |
|
51 |
continue
|
|
52 |
else: |
|
53 |
break
|
|
1168
by Martin Pool
- work properly when $EDITOR contains multiple words |
54 |
raise BzrError("Could not start any editor. " |
55 |
"Please specify $EDITOR or use ~/.bzr.conf/editor") |
|
1167
by Martin Pool
- split commit message editor functions out into own file |
56 |
|
57 |
||
58 |
def edit_commit_message(infotext, ignoreline=None): |
|
59 |
"""Let the user edit a commit message in a temp file.
|
|
60 |
||
61 |
This is run if they don't give a message or
|
|
62 |
message-containing file on the command line.
|
|
63 |
||
64 |
infotext:
|
|
65 |
Text to be displayed at bottom of message for
|
|
66 |
the user's reference; currently similar to
|
|
67 |
'bzr status'.
|
|
68 |
"""
|
|
69 |
import tempfile |
|
70 |
||
71 |
if ignoreline is None: |
|
72 |
ignoreline = "-- This line and the following will be ignored --" |
|
73 |
||
74 |
try: |
|
75 |
tmp_fileno, msgfilename = tempfile.mkstemp() |
|
76 |
msgfile = os.close(tmp_fileno) |
|
77 |
if infotext is not None and infotext != "": |
|
78 |
hasinfo = True |
|
79 |
msgfile = file(msgfilename, "w") |
|
80 |
msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext)) |
|
81 |
msgfile.close() |
|
82 |
else: |
|
83 |
hasinfo = False |
|
84 |
||
85 |
if not _run_editor(msgfilename): |
|
86 |
return None |
|
87 |
||
88 |
started = False |
|
89 |
msg = [] |
|
90 |
lastline, nlines = 0, 0 |
|
91 |
for line in file(msgfilename, "r"): |
|
92 |
stripped_line = line.strip() |
|
93 |
# strip empty line before the log message starts
|
|
94 |
if not started: |
|
95 |
if stripped_line != "": |
|
96 |
started = True |
|
97 |
else: |
|
98 |
continue
|
|
99 |
# check for the ignore line only if there
|
|
100 |
# is additional information at the end
|
|
101 |
if hasinfo and stripped_line == ignoreline: |
|
102 |
break
|
|
103 |
nlines += 1 |
|
104 |
# keep track of the last line that had some content
|
|
105 |
if stripped_line != "": |
|
106 |
lastline = nlines |
|
107 |
msg.append(line) |
|
108 |
||
109 |
if len(msg) == 0: |
|
110 |
return None |
|
111 |
# delete empty lines at the end
|
|
112 |
del msg[lastline:] |
|
113 |
# add a newline at the end, if needed
|
|
114 |
if not msg[-1].endswith("\n"): |
|
115 |
return "%s%s" % ("".join(msg), "\n") |
|
116 |
else: |
|
117 |
return "".join(msg) |
|
118 |
finally: |
|
119 |
# delete the msg file in any case
|
|
120 |
try: os.unlink(msgfilename) |
|
121 |
except IOError: pass |
|
122 |