~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-17 07:52:09 UTC
  • mfrom: (1910.3.4 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20060817075209-e85a1f9e05ff8b87
(andrew) Trivial fixes to NotImplemented errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 by Canonical Ltd
2
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
25
25
 
26
26
import bzrlib
27
27
import bzrlib.config as config
28
 
from bzrlib.errors import BzrError, BadCommitMessageEncoding
 
28
from bzrlib.errors import BzrError
29
29
from bzrlib.trace import warning, mutter
30
30
 
31
31
 
41
41
        yield e
42
42
        
43
43
    for varname in 'VISUAL', 'EDITOR':
44
 
        if varname in os.environ:
 
44
        if os.environ.has_key(varname):
45
45
            yield os.environ[varname]
46
46
 
47
47
    if sys.platform == 'win32':
71
71
        else:
72
72
            break
73
73
    raise BzrError("Could not start any editor.\nPlease specify one with:\n"
74
 
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n"
75
 
                   " - $VISUAL\n - $EDITOR" % \
 
74
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n - $EDITOR" % \
76
75
                    config.config_filename())
77
76
 
78
77
 
80
79
    { 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
81
80
 
82
81
 
83
 
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE,
84
 
                        start_message=None):
85
 
    """Let the user edit a commit message in a temp file.
86
 
 
87
 
    This is run if they don't give a message or
88
 
    message-containing file on the command line.
89
 
 
90
 
    :param infotext:    Text to be displayed at bottom of message
91
 
                        for the user's reference;
92
 
                        currently similar to 'bzr status'.
93
 
 
94
 
    :param ignoreline:  The separator to use above the infotext.
95
 
 
96
 
    :param start_message:   The text to place above the separator, if any.
97
 
                            This will not be removed from the message
98
 
                            after the user has edited it.
99
 
 
100
 
    :return:    commit message or None.
101
 
    """
102
 
 
103
 
    if not start_message is None:
104
 
        start_message = start_message.encode(bzrlib.user_encoding)
105
 
    infotext = infotext.encode(bzrlib.user_encoding, 'replace')
106
 
    return edit_commit_message_encoded(infotext, ignoreline, start_message)
107
 
 
108
 
 
109
 
def edit_commit_message_encoded(infotext, ignoreline=DEFAULT_IGNORE_LINE,
110
 
                                start_message=None):
111
 
    """Let the user edit a commit message in a temp file.
112
 
 
113
 
    This is run if they don't give a message or
114
 
    message-containing file on the command line.
115
 
 
116
 
    :param infotext:    Text to be displayed at bottom of message
117
 
                        for the user's reference;
118
 
                        currently similar to 'bzr status'.
119
 
                        The string is already encoded
120
 
 
121
 
    :param ignoreline:  The separator to use above the infotext.
122
 
 
123
 
    :param start_message:   The text to place above the separator, if any.
124
 
                            This will not be removed from the message
125
 
                            after the user has edited it.
126
 
                            The string is already encoded
127
 
 
128
 
    :return:    commit message or None.
129
 
    """
 
82
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
 
83
    """Let the user edit a commit message in a temp file.
 
84
 
 
85
    This is run if they don't give a message or
 
86
    message-containing file on the command line.
 
87
 
 
88
    infotext:
 
89
        Text to be displayed at bottom of message for
 
90
        the user's reference; currently similar to
 
91
        'bzr status'.
 
92
    """
 
93
    import tempfile
 
94
 
130
95
    msgfilename = None
131
96
    try:
132
 
        msgfilename, hasinfo = _create_temp_file_with_commit_template(
133
 
                                    infotext, ignoreline, start_message)
 
97
        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
 
98
        msgfile = os.close(tmp_fileno)
 
99
        if infotext is not None and infotext != "":
 
100
            hasinfo = True
 
101
            msgfile = file(msgfilename, "w")
 
102
            msgfile.write("\n\n%s\n\n%s" % (ignoreline,
 
103
                infotext.encode(bzrlib.user_encoding, 'replace')))
 
104
            msgfile.close()
 
105
        else:
 
106
            hasinfo = False
134
107
 
135
 
        if not msgfilename or not _run_editor(msgfilename):
 
108
        if not _run_editor(msgfilename):
136
109
            return None
137
110
        
138
111
        started = False
139
112
        msg = []
140
113
        lastline, nlines = 0, 0
141
 
        # codecs.open() ALWAYS opens file in binary mode but we need text mode
142
 
        # 'rU' mode useful when bzr.exe used on Cygwin (bialix 20070430)
143
 
        f = file(msgfilename, 'rU')
144
 
        try:
145
 
            try:
146
 
                for line in codecs.getreader(bzrlib.user_encoding)(f):
147
 
                    stripped_line = line.strip()
148
 
                    # strip empty line before the log message starts
149
 
                    if not started:
150
 
                        if stripped_line != "":
151
 
                            started = True
152
 
                        else:
153
 
                            continue
154
 
                    # check for the ignore line only if there
155
 
                    # is additional information at the end
156
 
                    if hasinfo and stripped_line == ignoreline:
157
 
                        break
158
 
                    nlines += 1
159
 
                    # keep track of the last line that had some content
160
 
                    if stripped_line != "":
161
 
                        lastline = nlines
162
 
                    msg.append(line)
163
 
            except UnicodeDecodeError:
164
 
                raise BadCommitMessageEncoding()
165
 
        finally:
166
 
            f.close()
167
 
 
 
114
        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
 
115
            stripped_line = line.strip()
 
116
            # strip empty line before the log message starts
 
117
            if not started:
 
118
                if stripped_line != "":
 
119
                    started = True
 
120
                else:
 
121
                    continue
 
122
            # check for the ignore line only if there
 
123
            # is additional information at the end
 
124
            if hasinfo and stripped_line == ignoreline:
 
125
                break
 
126
            nlines += 1
 
127
            # keep track of the last line that had some content
 
128
            if stripped_line != "":
 
129
                lastline = nlines
 
130
            msg.append(line)
 
131
            
168
132
        if len(msg) == 0:
169
133
            return ""
170
134
        # delete empty lines at the end
183
147
                warning("failed to unlink %s: %s; ignored", msgfilename, e)
184
148
 
185
149
 
186
 
def _create_temp_file_with_commit_template(infotext,
187
 
                                           ignoreline=DEFAULT_IGNORE_LINE,
188
 
                                           start_message=None):
189
 
    """Create temp file and write commit template in it.
190
 
 
191
 
    :param infotext:    Text to be displayed at bottom of message
192
 
                        for the user's reference;
193
 
                        currently similar to 'bzr status'.
194
 
                        The text is already encoded.
195
 
 
196
 
    :param ignoreline:  The separator to use above the infotext.
197
 
 
198
 
    :param start_message:   The text to place above the separator, if any.
199
 
                            This will not be removed from the message
200
 
                            after the user has edited it.
201
 
                            The string is already encoded
202
 
 
203
 
    :return:    2-tuple (temp file name, hasinfo)
204
 
    """
205
 
    import tempfile
206
 
    tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
207
 
                                               dir=u'.',
208
 
                                               text=True)
209
 
    msgfile = os.fdopen(tmp_fileno, 'w')
210
 
    try:
211
 
        if start_message is not None:
212
 
            msgfile.write("%s\n" % start_message)
213
 
 
214
 
        if infotext is not None and infotext != "":
215
 
            hasinfo = True
216
 
            msgfile.write("\n\n%s\n\n%s" %(ignoreline, infotext))
217
 
        else:
218
 
            hasinfo = False
219
 
    finally:
220
 
        msgfile.close()
221
 
 
222
 
    return (msgfilename, hasinfo)
223
 
 
224
 
 
225
150
def make_commit_message_template(working_tree, specific_files):
226
151
    """Prepare a template file for a commit into a branch.
227
152
 
228
153
    Returns a unicode string containing the template.
229
154
    """
 
155
    # TODO: Should probably be given the WorkingTree not the branch
 
156
    #
230
157
    # TODO: make provision for this to be overridden or modified by a hook
231
158
    #
232
159
    # TODO: Rather than running the status command, should prepare a draft of
238
165
    show_tree_status(working_tree, specific_files=specific_files, 
239
166
                     to_file=status_tmp)
240
167
    return status_tmp.getvalue()
241
 
 
242
 
 
243
 
def make_commit_message_template_encoded(working_tree, specific_files,
244
 
                                         diff=None, output_encoding='utf-8'):
245
 
    """Prepare a template file for a commit into a branch.
246
 
 
247
 
    Returns an encoded string.
248
 
    """
249
 
    # TODO: make provision for this to be overridden or modified by a hook
250
 
    #
251
 
    # TODO: Rather than running the status command, should prepare a draft of
252
 
    # the revision to be committed, then pause and ask the user to
253
 
    # confirm/write a message.
254
 
    from StringIO import StringIO       # must be unicode-safe
255
 
    from bzrlib.diff import show_diff_trees
256
 
 
257
 
    template = make_commit_message_template(working_tree, specific_files)
258
 
    template = template.encode(output_encoding, "replace")
259
 
 
260
 
    if diff:
261
 
        stream = StringIO()
262
 
        show_diff_trees(working_tree.basis_tree(),
263
 
                        working_tree, stream, specific_files,
264
 
                        path_encoding=output_encoding)
265
 
        template = template + '\n' + stream.getvalue()
266
 
 
267
 
    return template