~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Vincent Ladeuil
  • Date: 2010-04-28 10:33:44 UTC
  • mfrom: (5171.2.3 401599-strict-warnings)
  • mto: This revision was merged to the branch mainline in revision 5191.
  • Revision ID: v.ladeuil+lp@free.fr-20100428103344-e32qf3cn1avdd2cb
Don't mention --no-strict when we just issue the warning about unclean trees

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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
21
22
import os
22
23
from subprocess import call
23
24
import sys
24
25
 
25
26
from bzrlib import (
26
 
    cmdline,
27
27
    config,
28
28
    osutils,
29
29
    trace,
31
31
    ui,
32
32
    )
33
33
from bzrlib.errors import BzrError, BadCommitMessageEncoding
34
 
from bzrlib.hooks import Hooks
 
34
from bzrlib.hooks import HookPoint, Hooks
35
35
 
36
36
 
37
37
def _get_editor():
41
41
    except KeyError:
42
42
        pass
43
43
 
44
 
    e = config.GlobalStack().get('editor')
 
44
    e = config.GlobalConfig().get_editor()
45
45
    if e is not None:
46
46
        yield e, config.config_filename()
47
47
 
60
60
def _run_editor(filename):
61
61
    """Try to execute an editor to edit the commit message."""
62
62
    for candidate, candidate_source in _get_editor():
63
 
        edargs = cmdline.split(candidate)
 
63
        edargs = candidate.split(' ')
64
64
        try:
65
65
            ## mutter("trying editor: %r", (edargs +[filename]))
66
66
            x = call(edargs + [filename])
144
144
        if not msgfilename:
145
145
            return None
146
146
        basename = osutils.basename(msgfilename)
147
 
        msg_transport = transport.get_transport_from_path(osutils.dirname(msgfilename))
 
147
        msg_transport = transport.get_transport(osutils.dirname(msgfilename))
148
148
        reference_content = msg_transport.get_bytes(basename)
149
149
        if not _run_editor(msgfilename):
150
150
            return None
151
151
        edited_content = msg_transport.get_bytes(basename)
152
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
 
                {}):
 
153
            if not ui.ui_factory.get_boolean(
 
154
                "Commit message was not edited, use anyway"):
157
155
                # Returning "" makes cmd_commit raise 'empty commit message
158
156
                # specified' which is a reasonable error, given the user has
159
157
                # rejected using the unedited template.
209
207
 
210
208
def _create_temp_file_with_commit_template(infotext,
211
209
                                           ignoreline=DEFAULT_IGNORE_LINE,
212
 
                                           start_message=None,
213
 
                                           tmpdir=None):
 
210
                                           start_message=None):
214
211
    """Create temp file and write commit template in it.
215
212
 
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.
 
213
    :param infotext:    Text to be displayed at bottom of message
 
214
                        for the user's reference;
 
215
                        currently similar to 'bzr status'.
 
216
                        The text is already encoded.
219
217
 
220
218
    :param ignoreline:  The separator to use above the infotext.
221
219
 
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
 
220
    :param start_message:   The text to place above the separator, if any.
 
221
                            This will not be removed from the message
 
222
                            after the user has edited it.
 
223
                            The string is already encoded
225
224
 
226
225
    :return:    2-tuple (temp file name, hasinfo)
227
226
    """
228
227
    import tempfile
229
228
    tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
230
 
                                               dir=tmpdir, text=True)
 
229
                                               dir='.',
 
230
                                               text=True)
 
231
    msgfilename = osutils.basename(msgfilename)
231
232
    msgfile = os.fdopen(tmp_fileno, 'w')
232
233
    try:
233
234
        if start_message is not None:
302
303
 
303
304
        These are all empty initially.
304
305
        """
305
 
        Hooks.__init__(self, "bzrlib.msgeditor", "hooks")
306
 
        self.add_hook('set_commit_message',
307
 
            "Set a fixed commit message. "
308
 
            "set_commit_message is called with the "
309
 
            "bzrlib.commit.Commit object (so you can also change e.g. revision "
310
 
            "properties by editing commit.builder._revprops) and the message "
311
 
            "so far. set_commit_message must return the message to use or None"
312
 
            " if it should use the message editor as normal.", (2, 4))
313
 
        self.add_hook('commit_message_template',
 
306
        Hooks.__init__(self)
 
307
        self.create_hook(HookPoint('commit_message_template',
314
308
            "Called when a commit message is being generated. "
315
309
            "commit_message_template is called with the bzrlib.commit.Commit "
316
310
            "object and the message that is known so far. "
317
311
            "commit_message_template must return a new message to use (which "
318
 
            "could be the same as it was given). When there are multiple "
 
312
            "could be the same as it was given. When there are multiple "
319
313
            "hooks registered for commit_message_template, they are chained "
320
314
            "with the result from the first passed into the second, and so "
321
 
            "on.", (1, 10))
 
315
            "on.", (1, 10), None))
322
316
 
323
317
 
324
318
hooks = MessageEditorHooks()
325
319
 
326
320
 
327
 
def set_commit_message(commit, start_message=None):
328
 
    """Sets the commit message.
329
 
    :param commit: Commit object for the active commit.
330
 
    :return: The commit message or None to continue using the message editor
331
 
    """
332
 
    start_message = None
333
 
    for hook in hooks['set_commit_message']:
334
 
        start_message = hook(commit, start_message)
335
 
    return start_message
336
 
 
337
 
 
338
321
def generate_commit_message_template(commit, start_message=None):
339
322
    """Generate a commit message template.
340
323