~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Robert Collins
  • Date: 2007-07-04 08:08:13 UTC
  • mfrom: (2572 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2587.
  • Revision ID: robertc@robertcollins.net-20070704080813-wzebx0r88fvwj5rq
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 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
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 - $EDITOR" % \
 
74
                   " - $BZR_EDITOR\n - editor=/some/path in %s\n"
 
75
                   " - $VISUAL\n - $EDITOR" % \
75
76
                    config.config_filename())
76
77
 
77
78
 
79
80
    { 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
80
81
 
81
82
 
82
 
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
 
83
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE,
 
84
                        start_message=None):
83
85
    """Let the user edit a commit message in a temp file.
84
86
 
85
87
    This is run if they don't give a message or
86
88
    message-containing file on the command line.
87
89
 
88
 
    infotext:
89
 
        Text to be displayed at bottom of message for
90
 
        the user's reference; currently similar to
91
 
        'bzr status'.
 
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.
92
101
    """
93
 
    import tempfile
94
 
 
95
102
    msgfilename = None
96
103
    try:
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
 
104
        msgfilename, hasinfo = _create_temp_file_with_commit_template(
 
105
                                    infotext, ignoreline, start_message)
107
106
 
108
 
        if not _run_editor(msgfilename):
 
107
        if not msgfilename or not _run_editor(msgfilename):
109
108
            return None
110
109
        
111
110
        started = False
112
111
        msg = []
113
112
        lastline, nlines = 0, 0
114
 
        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
 
113
        # codecs.open() ALWAYS opens file in binary mode but we need text mode
 
114
        # 'rU' mode useful when bzr.exe used on Cygwin (bialix 20070430)
 
115
        f = file(msgfilename, 'rU')
 
116
        for line in codecs.getreader(bzrlib.user_encoding)(f):
115
117
            stripped_line = line.strip()
116
118
            # strip empty line before the log message starts
117
119
            if not started:
128
130
            if stripped_line != "":
129
131
                lastline = nlines
130
132
            msg.append(line)
131
 
            
 
133
        f.close()
 
134
 
132
135
        if len(msg) == 0:
133
136
            return ""
134
137
        # delete empty lines at the end
147
150
                warning("failed to unlink %s: %s; ignored", msgfilename, e)
148
151
 
149
152
 
 
153
def _create_temp_file_with_commit_template(infotext,
 
154
                                           ignoreline=DEFAULT_IGNORE_LINE,
 
155
                                           start_message=None):
 
156
    """Create temp file and write commit template in it.
 
157
 
 
158
    :param infotext:    Text to be displayed at bottom of message
 
159
                        for the user's reference;
 
160
                        currently similar to 'bzr status'.
 
161
 
 
162
    :param ignoreline:  The separator to use above the infotext.
 
163
 
 
164
    :param start_message:   The text to place above the separator, if any.
 
165
                            This will not be removed from the message
 
166
                            after the user has edited it.
 
167
 
 
168
    :return:    2-tuple (temp file name, hasinfo)
 
169
    """
 
170
    import tempfile
 
171
    tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.',
 
172
                                               dir=u'.',
 
173
                                               text=True)
 
174
    msgfile = os.fdopen(tmp_fileno, 'w')
 
175
    try:
 
176
        if start_message is not None:
 
177
            msgfile.write("%s\n" % start_message.encode(
 
178
                                       bzrlib.user_encoding, 'replace'))
 
179
 
 
180
        if infotext is not None and infotext != "":
 
181
            hasinfo = True
 
182
            msgfile.write("\n\n%s\n\n%s" % (ignoreline,
 
183
                          infotext.encode(bzrlib.user_encoding,
 
184
                                                'replace')))
 
185
        else:
 
186
            hasinfo = False
 
187
    finally:
 
188
        msgfile.close()
 
189
 
 
190
    return (msgfilename, hasinfo)
 
191
 
 
192
 
150
193
def make_commit_message_template(working_tree, specific_files):
151
194
    """Prepare a template file for a commit into a branch.
152
195