~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/msgeditor.py

  • Committer: Marius Kruger
  • Date: 2007-04-16 01:00:20 UTC
  • mto: This revision was merged to the branch mainline in revision 2455.
  • Revision ID: amanic@gmail.com-20070416010020-3jan8hje4qmbwdsh
* Add utility method delta.get_changes_as_text to get the output of .show()
  as a string.
* Add new errors.BzrRemoveChangedFilesError to raise when 'bzr remove'
  should refuse to delete stuff.
* Add workingtree.canicalpath(filename) to normalize file names.
* Changed the working of workingtree.remove(...) to check if any files
  are changed before it starts deleting anything. Will raise exception
  now if changed files are passed to be removed.
* workingtree_implementations/test_remove.py
  - Checked all tests and add more cases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Bazaar-NG -- distributed version control
2
 
 
3
 
# Copyright (C) 2005 by Canonical Ltd
4
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
9
 
 
 
7
#
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
11
# GNU General Public License for more details.
14
 
 
 
12
#
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
17
 
20
18
"""Commit message editor support."""
21
19
 
 
20
import codecs
 
21
import errno
22
22
import os
23
 
import errno
24
23
from subprocess import call
 
24
import sys
25
25
 
 
26
import bzrlib
26
27
import bzrlib.config as config
27
28
from bzrlib.errors import BzrError
 
29
from bzrlib.trace import warning, mutter
 
30
 
28
31
 
29
32
def _get_editor():
30
33
    """Return a sequence of possible editor binaries for the current platform"""
37
40
    if e is not None:
38
41
        yield e
39
42
        
40
 
    try:
41
 
        yield os.environ["EDITOR"]
42
 
    except KeyError:
43
 
        pass
 
43
    for varname in 'VISUAL', 'EDITOR':
 
44
        if varname in os.environ:
 
45
            yield os.environ[varname]
44
46
 
45
 
    if os.name == "nt":
46
 
        yield "notepad.exe"
47
 
    elif os.name == "posix":
48
 
        yield "/usr/bin/vi"
 
47
    if sys.platform == 'win32':
 
48
        for editor in 'wordpad.exe', 'notepad.exe':
 
49
            yield editor
 
50
    else:
 
51
        for editor in ['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe']:
 
52
            yield editor
49
53
 
50
54
 
51
55
def _run_editor(filename):
53
57
    for e in _get_editor():
54
58
        edargs = e.split(' ')
55
59
        try:
 
60
            ## mutter("trying editor: %r", (edargs +[filename]))
56
61
            x = call(edargs + [filename])
57
62
        except OSError, e:
58
 
           # ENOENT means no such editor
59
 
           if e.errno == errno.ENOENT:
 
63
           # We're searching for an editor, so catch safe errors and continue
 
64
           if e.errno in (errno.ENOENT, ):
60
65
               continue
61
66
           raise
62
67
        if x == 0:
65
70
            continue
66
71
        else:
67
72
            break
68
 
    raise BzrError("Could not start any editor. "
69
 
                   "Please specify $EDITOR or use ~/.bzr.conf/editor")
 
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" % \
 
76
                    config.config_filename())
70
77
 
71
78
 
72
79
DEFAULT_IGNORE_LINE = "%(bar)s %(msg)s %(bar)s" % \
73
80
    { 'bar' : '-' * 14, 'msg' : 'This line and the following will be ignored' }
74
81
 
75
82
 
76
 
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE):
 
83
def edit_commit_message(infotext, ignoreline=DEFAULT_IGNORE_LINE,
 
84
                        start_message=None):
77
85
    """Let the user edit a commit message in a temp file.
78
86
 
79
87
    This is run if they don't give a message or
83
91
        Text to be displayed at bottom of message for
84
92
        the user's reference; currently similar to
85
93
        'bzr status'.
 
94
 
 
95
    ignoreline:
 
96
        The separator to use above the infotext.
 
97
 
 
98
    start_message:
 
99
        The text to place above the separator, if any. This will not be
 
100
        removed from the message after the user has edited it.
86
101
    """
87
102
    import tempfile
88
103
 
 
104
    msgfilename = None
89
105
    try:
90
106
        tmp_fileno, msgfilename = tempfile.mkstemp(prefix='bzr_log.', dir=u'.')
91
 
        msgfile = os.close(tmp_fileno)
92
 
        if infotext is not None and infotext != "":
93
 
            hasinfo = True
94
 
            msgfile = file(msgfilename, "w")
95
 
            msgfile.write("\n\n%s\n\n%s" % (ignoreline, infotext))
 
107
        msgfile = os.fdopen(tmp_fileno, 'w')
 
108
        try:
 
109
            if start_message is not None:
 
110
                msgfile.write("%s\n" % start_message.encode(
 
111
                                           bzrlib.user_encoding, 'replace'))
 
112
 
 
113
            if infotext is not None and infotext != "":
 
114
                hasinfo = True
 
115
                msgfile.write("\n\n%s\n\n%s" % (ignoreline,
 
116
                              infotext.encode(bzrlib.user_encoding,
 
117
                                                    'replace')))
 
118
            else:
 
119
                hasinfo = False
 
120
        finally:
96
121
            msgfile.close()
97
 
        else:
98
 
            hasinfo = False
99
122
 
100
123
        if not _run_editor(msgfilename):
101
124
            return None
103
126
        started = False
104
127
        msg = []
105
128
        lastline, nlines = 0, 0
106
 
        for line in file(msgfilename, "r"):
 
129
        for line in codecs.open(msgfilename, 'r', bzrlib.user_encoding):
107
130
            stripped_line = line.strip()
108
131
            # strip empty line before the log message starts
109
132
            if not started:
132
155
            return "".join(msg)
133
156
    finally:
134
157
        # delete the msg file in any case
135
 
        try: os.unlink(msgfilename)
136
 
        except IOError: pass
 
158
        if msgfilename is not None:
 
159
            try:
 
160
                os.unlink(msgfilename)
 
161
            except IOError, e:
 
162
                warning("failed to unlink %s: %s; ignored", msgfilename, e)
137
163
 
138
164
 
139
165
def make_commit_message_template(working_tree, specific_files):