~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Aaron Bentley
  • Date: 2007-03-07 23:15:10 UTC
  • mto: (1551.19.24 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2325.
  • Revision ID: abentley@panoramicfeedback.com-20070307231510-jae63zsli83db3eb
Make ChangeReporter private

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
"""Commit message editor support."""
19
19
 
20
 
 
 
20
import codecs
21
21
import errno
22
22
import os
23
23
from subprocess import call
24
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
28
30
 
29
31
 
30
32
def _get_editor():
39
41
        yield e
40
42
        
41
43
    for varname in 'VISUAL', 'EDITOR':
42
 
        if os.environ.has_key(varname):
 
44
        if varname in os.environ:
43
45
            yield os.environ[varname]
44
46
 
45
47
    if sys.platform == 'win32':
55
57
    for e in _get_editor():
56
58
        edargs = e.split(' ')
57
59
        try:
 
60
            ## mutter("trying editor: %r", (edargs +[filename]))
58
61
            x = call(edargs + [filename])
59
62
        except OSError, e:
60
63
           # We're searching for an editor, so catch safe errors and continue
68
71
        else:
69
72
            break
70
73
    raise BzrError("Could not start any editor.\nPlease specify one with:\n"
71
 
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n - $EDITOR" % \
 
74
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n"
 
75
                   " - $VISUAL\n - $EDITOR" % \
72
76
                    config.config_filename())
73
77
 
74
78
 
76
80
    { 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
77
81
 
78
82
 
79
 
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
 
83
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE,
 
84
                        start_message=None):
80
85
    """Let the user edit a commit message in a temp file.
81
86
 
82
87
    This is run if they don't give a message or
86
91
        Text to be displayed at bottom of message for
87
92
        the user's reference; currently similar to
88
93
        'bzr status'.
 
94
 
 
95
    ignoreline:
 
96
        The separator to use above the infotext.
 
97
 
 
98
    start_message:
 
99
        The text to place above the separator, if any. This will not be
 
100
        removed from the message after the user has edited it.
89
101
    """
90
102
    import tempfile
91
103
 
92
104
    msgfilename = None
93
105
    try:
94
106
        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
95
 
        msgfile = os.close(tmp_fileno)
96
 
        if infotext is not None and infotext != "":
97
 
            hasinfo = True
98
 
            msgfile = file(msgfilename, "w")
99
 
            msgfile.write("\n%s\n\n%s" % (ignoreline, infotext))
 
107
        msgfile = os.fdopen(tmp_fileno, 'w')
 
108
        try:
 
109
            if start_message is not None:
 
110
                msgfile.write("%s\n" % start_message.encode(
 
111
                                           bzrlib.user_encoding, 'replace'))
 
112
 
 
113
            if infotext is not None and infotext != "":
 
114
                hasinfo = True
 
115
                msgfile.write("\n\n%s\n\n%s" % (ignoreline,
 
116
                              infotext.encode(bzrlib.user_encoding,
 
117
                                                    'replace')))
 
118
            else:
 
119
                hasinfo = False
 
120
        finally:
100
121
            msgfile.close()
101
 
        else:
102
 
            hasinfo = False
103
122
 
104
123
        if not _run_editor(msgfilename):
105
124
            return None
107
126
        started = False
108
127
        msg = []
109
128
        lastline, nlines = 0, 0
110
 
        for line in file(msgfilename, "r"):
 
129
        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
111
130
            stripped_line = line.strip()
112
131
            # strip empty line before the log message starts
113
132
            if not started:
140
159
            try:
141
160
                os.unlink(msgfilename)
142
161
            except IOError, e:
143
 
                mutter("failed to unlink %s: %s; ignored", msgfilename, e)
 
162
                warning("failed to unlink %s: %s; ignored", msgfilename, e)
144
163
 
145
164
 
146
165
def make_commit_message_template(working_tree, specific_files):