~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-12 18:05:01 UTC
  • mto: This revision was merged to the branch mainline in revision 1871.
  • Revision ID: john@arbash-meinel.com-20060712180501-1a638c0c5b1e7646
Updated WorkingTree to use the new user-level ignores.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
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.
12
 
 
 
12
#
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
16
16
 
 
17
import errno
 
18
import os
 
19
import subprocess
 
20
import sys
 
21
import tempfile
17
22
import time
18
23
 
19
24
from bzrlib.delta import compare_trees
20
25
from bzrlib.errors import BzrError
21
26
import bzrlib.errors as errors
 
27
import bzrlib.osutils
22
28
from bzrlib.patiencediff import unified_diff
23
29
import bzrlib.patiencediff
24
 
from bzrlib.symbol_versioning import *
 
30
from bzrlib.symbol_versioning import (deprecated_function,
 
31
        zero_eight)
25
32
from bzrlib.textfile import check_text_lines
26
 
from bzrlib.trace import mutter
 
33
from bzrlib.trace import mutter, warning
27
34
 
28
35
 
29
36
# TODO: Rather than building a changeset object, we should probably
81
88
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
82
89
                  diff_opts):
83
90
    """Display a diff by calling out to the external diff program."""
84
 
    import sys
 
91
    if hasattr(to_file, 'fileno'):
 
92
        out_file = to_file
 
93
        have_fileno = True
 
94
    else:
 
95
        out_file = subprocess.PIPE
 
96
        have_fileno = False
85
97
    
86
 
    if to_file != sys.stdout:
87
 
        raise NotImplementedError("sorry, can't send external diff other than to stdout yet",
88
 
                                  to_file)
89
 
 
90
98
    # make sure our own output is properly ordered before the diff
91
99
    to_file.flush()
92
100
 
93
 
    from tempfile import NamedTemporaryFile
94
 
    import os
95
 
 
96
 
    oldtmpf = NamedTemporaryFile()
97
 
    newtmpf = NamedTemporaryFile()
 
101
    oldtmp_fd, old_abspath = tempfile.mkstemp(prefix='bzr-diff-old-')
 
102
    newtmp_fd, new_abspath = tempfile.mkstemp(prefix='bzr-diff-new-')
 
103
    oldtmpf = os.fdopen(oldtmp_fd, 'wb')
 
104
    newtmpf = os.fdopen(newtmp_fd, 'wb')
98
105
 
99
106
    try:
100
107
        # TODO: perhaps a special case for comparing to or from the empty
107
114
        oldtmpf.writelines(oldlines)
108
115
        newtmpf.writelines(newlines)
109
116
 
110
 
        oldtmpf.flush()
111
 
        newtmpf.flush()
 
117
        oldtmpf.close()
 
118
        newtmpf.close()
112
119
 
113
120
        if not diff_opts:
114
121
            diff_opts = []
115
122
        diffcmd = ['diff',
116
123
                   '--label', old_filename,
117
 
                   oldtmpf.name,
 
124
                   old_abspath,
118
125
                   '--label', new_filename,
119
 
                   newtmpf.name]
 
126
                   new_abspath,
 
127
                   '--binary',
 
128
                  ]
120
129
 
121
130
        # diff only allows one style to be specified; they don't override.
122
131
        # note that some of these take optargs, and the optargs can be
142
151
        if diff_opts:
143
152
            diffcmd.extend(diff_opts)
144
153
 
145
 
        rc = os.spawnvp(os.P_WAIT, 'diff', diffcmd)
 
154
        try:
 
155
            pipe = subprocess.Popen(diffcmd,
 
156
                                    stdin=subprocess.PIPE,
 
157
                                    stdout=out_file)
 
158
        except OSError, e:
 
159
            if e.errno == errno.ENOENT:
 
160
                raise errors.NoDiff(str(e))
 
161
            raise
 
162
        pipe.stdin.close()
 
163
 
 
164
        if not have_fileno:
 
165
            bzrlib.osutils.pumpfile(pipe.stdout, to_file)
 
166
        rc = pipe.wait()
146
167
        
147
168
        if rc != 0 and rc != 1:
148
169
            # returns 1 if files differ; that's OK
155
176
    finally:
156
177
        oldtmpf.close()                 # and delete
157
178
        newtmpf.close()
 
179
        # Clean up. Warn in case the files couldn't be deleted
 
180
        # (in case windows still holds the file open, but not
 
181
        # if the files have already been deleted)
 
182
        try:
 
183
            os.remove(old_abspath)
 
184
        except OSError, e:
 
185
            if e.errno not in (errno.ENOENT,):
 
186
                warning('Failed to delete temporary file: %s %s',
 
187
                        old_abspath, e)
 
188
        try:
 
189
            os.remove(new_abspath)
 
190
        except OSError:
 
191
            if e.errno not in (errno.ENOENT,):
 
192
                warning('Failed to delete temporary file: %s %s',
 
193
                        new_abspath, e)
158
194
 
159
195
 
160
196
@deprecated_function(zero_eight)
174
210
    supplies any two trees.
175
211
    """
176
212
    if output is None:
177
 
        import sys
178
213
        output = sys.stdout
179
214
 
180
215
    if from_spec is None:
221
256
    The more general form is show_diff_trees(), where the caller
222
257
    supplies any two trees.
223
258
    """
224
 
    import sys
225
259
    output = sys.stdout
226
260
    def spec_tree(spec):
227
261
        revision_id = spec.in_store(tree.branch).rev_id
235
269
        new_tree = tree
236
270
    else:
237
271
        new_tree = spec_tree(new_revision_spec)
 
272
    if new_tree is not tree:
 
273
        extra_trees = (tree,)
 
274
    else:
 
275
        extra_trees = None
238
276
 
239
277
    return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
240
278
                           external_diff_options,
241
 
                           old_label=old_label, new_label=new_label)
 
279
                           old_label=old_label, new_label=new_label,
 
280
                           extra_trees=extra_trees)
242
281
 
243
282
 
244
283
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
245
284
                    external_diff_options=None,
246
 
                    old_label='a/', new_label='b/'):
 
285
                    old_label='a/', new_label='b/',
 
286
                    extra_trees=None):
247
287
    """Show in text form the changes from one tree to another.
248
288
 
249
289
    to_files
251
291
 
252
292
    external_diff_options
253
293
        If set, use an external GNU diff and pass these options.
 
294
 
 
295
    extra_trees
 
296
        If set, more Trees to use for looking up file ids
254
297
    """
255
298
    old_tree.lock_read()
256
299
    try:
258
301
        try:
259
302
            return _show_diff_trees(old_tree, new_tree, to_file,
260
303
                                    specific_files, external_diff_options,
261
 
                                    old_label=old_label, new_label=new_label)
 
304
                                    old_label=old_label, new_label=new_label,
 
305
                                    extra_trees=extra_trees)
262
306
        finally:
263
307
            new_tree.unlock()
264
308
    finally:
267
311
 
268
312
def _show_diff_trees(old_tree, new_tree, to_file,
269
313
                     specific_files, external_diff_options, 
270
 
                     old_label='a/', new_label='b/' ):
 
314
                     old_label='a/', new_label='b/', extra_trees=None):
271
315
 
272
316
    # GNU Patch uses the epoch date to detect files that are being added
273
317
    # or removed in a diff.
276
320
    # TODO: Generation of pseudo-diffs for added/deleted files could
277
321
    # be usefully made into a much faster special case.
278
322
 
279
 
    _raise_if_doubly_unversioned(specific_files, old_tree, new_tree)
280
 
 
281
323
    if external_diff_options:
282
324
        assert isinstance(external_diff_options, basestring)
283
325
        opts = external_diff_options.split()
287
329
        diff_file = internal_diff
288
330
    
289
331
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
290
 
                          specific_files=specific_files)
 
332
                          specific_files=specific_files, 
 
333
                          extra_trees=extra_trees, require_versioned=True)
291
334
 
292
335
    has_changes = 0
293
336
    for path, file_id, kind in delta.removed:
345
388
    return time.strftime('%Y-%m-%d %H:%M:%S +0000', tm)
346
389
 
347
390
 
348
 
def _raise_if_doubly_unversioned(specific_files, old_tree, new_tree):
349
 
    """Complain if paths are not versioned in either tree."""
350
 
    if not specific_files:
351
 
        return
352
 
    old_unversioned = old_tree.filter_unversioned_files(specific_files)
353
 
    new_unversioned = new_tree.filter_unversioned_files(specific_files)
354
 
    unversioned = old_unversioned.intersection(new_unversioned)
355
 
    if unversioned:
356
 
        raise errors.PathsNotVersionedError(sorted(unversioned))
357
 
    
358
 
 
359
391
def _raise_if_nonexistent(paths, old_tree, new_tree):
360
392
    """Complain if paths are not in either inventory or tree.
361
393