[Pkg-bazaar-commits] ./bzr/unstable r763: - Patch from Torsten Marek to take commit messages through an
Martin Pool
mbp at sourcefrog.net
Fri Apr 10 08:20:58 UTC 2009
------------------------------------------------------------
revno: 763
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Fri 2005-06-24 14:48:14 +1000
message:
- Patch from Torsten Marek to take commit messages through an
editor.
modified:
NEWS
bzrlib/commands.py
bzrlib/osutils.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS 2005-06-22 07:18:01 +0000
+++ b/NEWS 2005-06-24 04:48:14 +0000
@@ -7,6 +7,9 @@
* New 'bzr mkdir' command.
+ * Commit mesage is fetched from an editor if not given on the
+ command line; patch from Torsten Marek.
+
CHANGES:
* New ``bzr upgrade`` command to upgrade the format of a branch,
=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py 2005-06-22 09:34:11 +0000
+++ b/bzrlib/commands.py 2005-06-24 04:48:14 +0000
@@ -1073,11 +1073,22 @@
def run(self, message=None, file=None, verbose=True, selected_list=None):
from bzrlib.commit import commit
+ from bzrlib.osutils import get_text_message
## Warning: shadows builtin file()
if not message and not file:
- raise BzrCommandError("please specify a commit message",
- ["use either --message or --file"])
+ import cStringIO
+ stdout = sys.stdout
+ catcher = cStringIO.StringIO()
+ sys.stdout = catcher
+ cmd_status({"file_list":selected_list}, {})
+ info = catcher.getvalue()
+ sys.stdout = stdout
+ message = get_text_message(info)
+
+ if message is None:
+ raise BzrCommandError("please specify a commit message",
+ ["use either --message or --file"])
elif message and file:
raise BzrCommandError("please specify either --message or --file")
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py 2005-06-17 07:28:53 +0000
+++ b/bzrlib/osutils.py 2005-06-24 04:48:14 +0000
@@ -384,3 +384,100 @@
if not ignore_errors:
raise BzrError('command failed')
+
+def _read_config_value(name):
+ """Read a config value from the file ~/.bzr.conf/<name>
+ Return None if the file does not exist"""
+ try:
+ f = file(os.path.join(config_dir(), name), "r")
+ return f.read().decode(bzrlib.user_encoding).rstrip("\r\n")
+ except IOError, e:
+ if e.errno == errno.ENOENT:
+ return None
+ raise
+
+
+def _get_editor():
+ """Return a sequence of possible editor binaries for the current platform"""
+ e = _read_config_value("editor")
+ if e is not None:
+ yield e
+
+ if os.name == "windows":
+ yield "notepad.exe"
+ elif os.name == "posix":
+ try:
+ yield os.environ["EDITOR"]
+ except KeyError:
+ yield "/usr/bin/vi"
+
+
+def _run_editor(filename):
+ """Try to execute an editor to edit the commit message. Returns True on success,
+ False on failure"""
+ for e in _get_editor():
+ x = os.spawnvp(os.P_WAIT, e, (e, filename))
+ if x == 0:
+ return True
+ elif x == 127:
+ continue
+ else:
+ break
+ raise BzrError("Could not start any editor. Please specify $EDITOR or use ~/.bzr.conf/editor")
+ return False
+
+
+def get_text_message(infotext, ignoreline = "default"):
+ import tempfile
+
+ if ignoreline == "default":
+ ignoreline = "-- This line and the following will be ignored --"
+
+ try:
+ tmp_fileno, msgfilename = tempfile.mkstemp()
+ msgfile = os.close(tmp_fileno)
+ if infotext is not None and infotext != "":
+ hasinfo = True
+ msgfile = file(msgfilename, "w")
+ msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
+ msgfile.close()
+ else:
+ hasinfo = False
+
+ if not _run_editor(msgfilename):
+ return None
+
+ started = False
+ msg = []
+ lastline, nlines = 0, 0
+ for line in file(msgfilename, "r"):
+ stripped_line = line.strip()
+ # strip empty line before the log message starts
+ if not started:
+ if stripped_line != "":
+ started = True
+ else:
+ continue
+ # check for the ignore line only if there
+ # is additional information at the end
+ if hasinfo and stripped_line == ignoreline:
+ break
+ nlines += 1
+ # keep track of the last line that had some content
+ if stripped_line != "":
+ lastline = nlines
+ msg.append(line)
+
+ if len(msg) == 0:
+ return None
+ # delete empty lines at the end
+ del msg[lastline:]
+ # add a newline at the end, if needed
+ if not msg[-1].endswith("\n"):
+ return "%s%s" % ("".join(msg), "\n")
+ else:
+ return "".join(msg)
+ finally:
+ # delete the msg file in any case
+ try: os.unlink(msgfilename)
+ except IOError: pass
More information about the Pkg-bazaar-commits
mailing list