1
1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd.
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
24
from bzrlib.delta import compare_trees
20
25
from bzrlib.errors import BzrError
21
26
import bzrlib.errors as errors
22
28
from bzrlib.patiencediff import unified_diff
23
29
import bzrlib.patiencediff
24
30
from bzrlib.symbol_versioning import *
25
31
from bzrlib.textfile import check_text_lines
26
from bzrlib.trace import mutter
32
from bzrlib.trace import mutter, warning
29
35
# TODO: Rather than building a changeset object, we should probably
81
87
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
83
89
"""Display a diff by calling out to the external diff program."""
90
if hasattr(to_file, 'fileno'):
94
out_file = subprocess.PIPE
86
if to_file != sys.stdout:
87
raise NotImplementedError("sorry, can't send external diff other than to stdout yet",
90
97
# make sure our own output is properly ordered before the diff
93
from tempfile import NamedTemporaryFile
96
oldtmpf = NamedTemporaryFile()
97
newtmpf = NamedTemporaryFile()
100
oldtmp_fd, old_abspath = tempfile.mkstemp(prefix='bzr-diff-old-')
101
newtmp_fd, new_abspath = tempfile.mkstemp(prefix='bzr-diff-new-')
102
oldtmpf = os.fdopen(oldtmp_fd, 'wb')
103
newtmpf = os.fdopen(newtmp_fd, 'wb')
100
106
# TODO: perhaps a special case for comparing to or from the empty
107
113
oldtmpf.writelines(oldlines)
108
114
newtmpf.writelines(newlines)
113
119
if not diff_opts:
115
121
diffcmd = ['diff',
116
122
'--label', old_filename,
118
124
'--label', new_filename,
121
129
# diff only allows one style to be specified; they don't override.
122
130
# note that some of these take optargs, and the optargs can be
143
151
diffcmd.extend(diff_opts)
145
rc = os.spawnvp(os.P_WAIT, 'diff', diffcmd)
154
pipe = subprocess.Popen(diffcmd,
155
stdin=subprocess.PIPE,
158
if e.errno == errno.ENOENT:
159
raise errors.NoDiff(str(e))
164
bzrlib.osutils.pumpfile(pipe.stdout, to_file)
147
167
if rc != 0 and rc != 1:
148
168
# returns 1 if files differ; that's OK
156
176
oldtmpf.close() # and delete
178
# Clean up. Warn in case the files couldn't be deleted
179
# (in case windows still holds the file open, but not
180
# if the files have already been deleted)
182
os.remove(old_abspath)
184
if e.errno not in (errno.ENOENT,):
185
warning('Failed to delete temporary file: %s %s',
188
os.remove(new_abspath)
190
if e.errno not in (errno.ENOENT,):
191
warning('Failed to delete temporary file: %s %s',
160
195
@deprecated_function(zero_eight)