~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Robert Collins
  • Date: 2005-10-05 05:29:27 UTC
  • mfrom: (1393.1.51)
  • Revision ID: robertc@robertcollins.net-20051005052926-03d3a49e1dbcb670
mergeĀ fromĀ martin

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
"""Commit message editor support."""
21
21
 
22
22
import os
23
 
import errno
24
23
from subprocess import call
25
 
 
26
 
import bzrlib.config as config
27
24
from bzrlib.errors import BzrError
28
25
 
29
26
def _get_editor():
30
27
    """Return a sequence of possible editor binaries for the current platform"""
 
28
    from bzrlib.osutils import _read_config_value
 
29
    
31
30
    try:
32
31
        yield os.environ["BZR_EDITOR"]
33
32
    except KeyError:
34
33
        pass
35
34
 
36
 
    e = config.GlobalConfig().get_editor()
 
35
    e = _read_config_value("editor")
37
36
    if e is not None:
38
37
        yield e
39
38
        
52
51
    """Try to execute an editor to edit the commit message."""
53
52
    for e in _get_editor():
54
53
        edargs = e.split(' ')
55
 
        try:
56
 
            x = call(edargs + [filename])
57
 
        except OSError, e:
58
 
           # ENOENT means no such editor
59
 
           if e.errno == errno.ENOENT:
60
 
               continue
61
 
           raise
 
54
        x = call(edargs + [filename])
62
55
        if x == 0:
63
56
            return True
64
57
        elif x == 127:
67
60
            break
68
61
    raise BzrError("Could not start any editor. "
69
62
                   "Please specify $EDITOR or use ~/.bzr.conf/editor")
70
 
 
71
 
 
72
 
DEFAULT_IGNORE_LINE = "%(bar)s %(msg)s %(bar)s" % \
73
 
    { 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
74
 
 
75
 
 
76
 
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
 
63
                          
 
64
 
 
65
def edit_commit_message(infotext, ignoreline=None):
77
66
    """Let the user edit a commit message in a temp file.
78
67
 
79
68
    This is run if they don't give a message or
85
74
        'bzr status'.
86
75
    """
87
76
    import tempfile
88
 
 
 
77
    
 
78
    if ignoreline is None:
 
79
        ignoreline = "-- This line and the following will be ignored --"
 
80
        
89
81
    try:
90
 
        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
 
82
        tmp_fileno, msgfilename = tempfile.mkstemp()
91
83
        msgfile = os.close(tmp_fileno)
92
84
        if infotext is not None and infotext != "":
93
85
            hasinfo = True
122
114
            msg.append(line)
123
115
            
124
116
        if len(msg) == 0:
125
 
            return ""
 
117
            return None
126
118
        # delete empty lines at the end
127
119
        del msg[lastline:]
128
120
        # add a newline at the end, if needed
135
127
        try: os.unlink(msgfilename)
136
128
        except IOError: pass
137
129
 
138
 
 
139
 
def make_commit_message_template(working_tree, specific_files):
140
 
    """Prepare a template file for a commit into a branch.
141
 
 
142
 
    Returns a unicode string containing the template.
143
 
    """
144
 
    # TODO: Should probably be given the WorkingTree not the branch
145
 
    #
146
 
    # TODO: make provision for this to be overridden or modified by a hook
147
 
    #
148
 
    # TODO: Rather than running the status command, should prepare a draft of
149
 
    # the revision to be committed, then pause and ask the user to
150
 
    # confirm/write a message.
151
 
    from StringIO import StringIO       # must be unicode-safe
152
 
    from bzrlib.status import show_tree_status
153
 
    status_tmp = StringIO()
154
 
    show_tree_status(working_tree, specific_files=specific_files, 
155
 
                     to_file=status_tmp)
156
 
    return status_tmp.getvalue()