~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Martin von Gagern
  • Date: 2011-06-01 07:01:17 UTC
  • mto: This revision was merged to the branch mainline in revision 5963.
  • Revision ID: martin.vgagern@gmx.net-20110601070117-3b08kqy51062ep0y
Allow -s as shorthand for --log-format=short.

This is implemented by allowing a keyword argument named
short_value_switches for the RegistryOption initializer.  It maps long names
to short names.  The newly added short option affects the commands log and
missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
18
18
"""Commit message editor support."""
19
19
 
20
20
import codecs
21
 
import errno
22
21
import os
23
22
from subprocess import call
24
23
import sys
25
24
 
26
25
from bzrlib import (
 
26
    cmdline,
27
27
    config,
28
28
    osutils,
29
29
    trace,
 
30
    transport,
 
31
    ui,
30
32
    )
31
33
from bzrlib.errors import BzrError, BadCommitMessageEncoding
32
 
from bzrlib.hooks import HookPoint, Hooks
 
34
from bzrlib.hooks import Hooks
33
35
 
34
36
 
35
37
def _get_editor():
39
41
    except KeyError:
40
42
        pass
41
43
 
42
 
    e = config.GlobalConfig().get_editor()
 
44
    e = config.GlobalStack().get('editor')
43
45
    if e is not None:
44
46
        yield e, config.config_filename()
45
47
 
58
60
def _run_editor(filename):
59
61
    """Try to execute an editor to edit the commit message."""
60
62
    for candidate, candidate_source in _get_editor():
61
 
        edargs = candidate.split(' ')
 
63
        edargs = cmdline.split(candidate)
62
64
        try:
63
65
            ## mutter("trying editor: %r", (edargs +[filename]))
64
66
            x = call(edargs + [filename])
139
141
    try:
140
142
        msgfilename, hasinfo = _create_temp_file_with_commit_template(
141
143
                                    infotext, ignoreline, start_message)
142
 
 
143
 
        if not msgfilename or not _run_editor(msgfilename):
144
 
            return None
145
 
 
 
144
        if not msgfilename:
 
145
            return None
 
146
        basename = osutils.basename(msgfilename)
 
147
        msg_transport = transport.get_transport(osutils.dirname(msgfilename))
 
148
        reference_content = msg_transport.get_bytes(basename)
 
149
        if not _run_editor(msgfilename):
 
150
            return None
 
151
        edited_content = msg_transport.get_bytes(basename)
 
152
        if edited_content == reference_content:
 
153
            if not ui.ui_factory.confirm_action(
 
154
                u"Commit message was not edited, use anyway",
 
155
                "bzrlib.msgeditor.unchanged",
 
156
                {}):
 
157
                # Returning "" makes cmd_commit raise 'empty commit message
 
158
                # specified' which is a reasonable error, given the user has
 
159
                # rejected using the unedited template.
 
160
                return ""
146
161
        started = False
147
162
        msg = []
148
163
        lastline, nlines = 0, 0
194
209
 
195
210
def _create_temp_file_with_commit_template(infotext,
196
211
                                           ignoreline=DEFAULT_IGNORE_LINE,
197
 
                                           start_message=None):
 
212
                                           start_message=None,
 
213
                                           tmpdir=None):
198
214
    """Create temp file and write commit template in it.
199
215
 
200
 
    :param infotext:    Text to be displayed at bottom of message
201
 
                        for the user's reference;
202
 
                        currently similar to 'bzr status'.
203
 
                        The text is already encoded.
 
216
    :param infotext: Text to be displayed at bottom of message for the
 
217
        user's reference; currently similar to 'bzr status'.  The text is
 
218
        already encoded.
204
219
 
205
220
    :param ignoreline:  The separator to use above the infotext.
206
221
 
207
 
    :param start_message:   The text to place above the separator, if any.
208
 
                            This will not be removed from the message
209
 
                            after the user has edited it.
210
 
                            The string is already encoded
 
222
    :param start_message: The text to place above the separator, if any.
 
223
        This will not be removed from the message after the user has edited
 
224
        it.  The string is already encoded
211
225
 
212
226
    :return:    2-tuple (temp file name, hasinfo)
213
227
    """
214
228
    import tempfile
215
229
    tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
216
 
                                               dir='.',
217
 
                                               text=True)
218
 
    msgfilename = osutils.basename(msgfilename)
 
230
                                               dir=tmpdir, text=True)
219
231
    msgfile = os.fdopen(tmp_fileno, 'w')
220
232
    try:
221
233
        if start_message is not None:
290
302
 
291
303
        These are all empty initially.
292
304
        """
293
 
        Hooks.__init__(self)
294
 
        self.create_hook(HookPoint('commit_message_template',
 
305
        Hooks.__init__(self, "bzrlib.msgeditor", "hooks")
 
306
        self.add_hook('commit_message_template',
295
307
            "Called when a commit message is being generated. "
296
308
            "commit_message_template is called with the bzrlib.commit.Commit "
297
309
            "object and the message that is known so far. "
298
310
            "commit_message_template must return a new message to use (which "
299
 
            "could be the same as it was given. When there are multiple "
 
311
            "could be the same as it was given). When there are multiple "
300
312
            "hooks registered for commit_message_template, they are chained "
301
313
            "with the result from the first passed into the second, and so "
302
 
            "on.", (1, 10), None))
 
314
            "on.", (1, 10))
303
315
 
304
316
 
305
317
hooks = MessageEditorHooks()