/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Martin Pool
  • Date: 2006-10-06 02:04:17 UTC
  • mfrom: (1908.10.1 bench_usecases.merge2)
  • mto: This revision was merged to the branch mainline in revision 2068.
  • Revision ID: mbp@sourcefrog.net-20061006020417-4949ca86f4417a4d
merge additional fix from cfbolz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Bazaar-NG -- distributed version control
2
 
 
3
 
# Copyright (C) 2005 by Canonical Ltd
4
 
 
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
 
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
9
 
 
 
7
#
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
11
# GNU General Public License for more details.
14
 
 
 
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
17
 
20
18
"""Commit message editor support."""
21
19
 
 
20
import codecs
 
21
import errno
22
22
import os
23
 
import errno
24
23
from subprocess import call
 
24
import sys
25
25
 
 
26
import bzrlib
26
27
import bzrlib.config as config
27
28
from bzrlib.errors import BzrError
 
29
from bzrlib.trace import warning, mutter
 
30
 
28
31
 
29
32
def _get_editor():
30
33
    """Return a sequence of possible editor binaries for the current platform"""
37
40
    if e is not None:
38
41
        yield e
39
42
        
40
 
    try:
41
 
        yield os.environ["EDITOR"]
42
 
    except KeyError:
43
 
        pass
 
43
    for varname in 'VISUAL', 'EDITOR':
 
44
        if varname in os.environ:
 
45
            yield os.environ[varname]
44
46
 
45
 
    if os.name == "nt":
46
 
        yield "notepad.exe"
47
 
    elif os.name == "posix":
48
 
        yield "/usr/bin/vi"
 
47
    if sys.platform == 'win32':
 
48
        for editor in 'wordpad.exe', 'notepad.exe':
 
49
            yield editor
 
50
    else:
 
51
        for editor in ['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe']:
 
52
            yield editor
49
53
 
50
54
 
51
55
def _run_editor(filename):
53
57
    for e in _get_editor():
54
58
        edargs = e.split(' ')
55
59
        try:
 
60
            ## mutter("trying editor: %r", (edargs +[filename]))
56
61
            x = call(edargs + [filename])
57
62
        except OSError, e:
58
 
           # ENOENT means no such editor
59
 
           if e.errno == errno.ENOENT:
 
63
           # We're searching for an editor, so catch safe errors and continue
 
64
           if e.errno in (errno.ENOENT, ):
60
65
               continue
61
66
           raise
62
67
        if x == 0:
65
70
            continue
66
71
        else:
67
72
            break
68
 
    raise BzrError("Could not start any editor. "
69
 
                   "Please specify $EDITOR or use ~/.bzr.conf/editor")
 
73
    raise BzrError("Could not start any editor.\nPlease specify one with:\n"
 
74
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n - $EDITOR" % \
 
75
                    config.config_filename())
70
76
 
71
77
 
72
78
DEFAULT_IGNORE_LINE = "%(bar)s %(msg)s %(bar)s" % \
86
92
    """
87
93
    import tempfile
88
94
 
 
95
    msgfilename = None
89
96
    try:
90
97
        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
91
98
        msgfile = os.close(tmp_fileno)
92
99
        if infotext is not None and infotext != "":
93
100
            hasinfo = True
94
101
            msgfile = file(msgfilename, "w")
95
 
            msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
 
102
            msgfile.write("\n\n%s\n\n%s" % (ignoreline,
 
103
                infotext.encode(bzrlib.user_encoding, 'replace')))
96
104
            msgfile.close()
97
105
        else:
98
106
            hasinfo = False
103
111
        started = False
104
112
        msg = []
105
113
        lastline, nlines = 0, 0
106
 
        for line in file(msgfilename, "r"):
 
114
        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
107
115
            stripped_line = line.strip()
108
116
            # strip empty line before the log message starts
109
117
            if not started:
132
140
            return "".join(msg)
133
141
    finally:
134
142
        # delete the msg file in any case
135
 
        try: os.unlink(msgfilename)
136
 
        except IOError: pass
 
143
        if msgfilename is not None:
 
144
            try:
 
145
                os.unlink(msgfilename)
 
146
            except IOError, e:
 
147
                warning("failed to unlink %s: %s; ignored", msgfilename, e)
137
148
 
138
149
 
139
150
def make_commit_message_template(working_tree, specific_files):