~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-10 00:43:37 UTC
  • mto: This revision was merged to the branch mainline in revision 1926.
  • Revision ID: john@arbash-meinel.com-20060810004337-6aa4d7ea80e85093
Moving everything into a new location so that we can cache more than just revision ids

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import errno
18
18
import os
 
19
import re
19
20
import subprocess
20
21
import sys
21
22
import tempfile
22
23
import time
23
24
 
 
25
# compatability - plugins import compare_trees from diff!!!
 
26
# deprecated as of 0.10
24
27
from bzrlib.delta import compare_trees
25
28
from bzrlib.errors import BzrError
26
29
import bzrlib.errors as errors
88
91
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
89
92
                  diff_opts):
90
93
    """Display a diff by calling out to the external diff program."""
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
97
 
    
98
94
    # make sure our own output is properly ordered before the diff
99
95
    to_file.flush()
100
96
 
154
150
        try:
155
151
            pipe = subprocess.Popen(diffcmd,
156
152
                                    stdin=subprocess.PIPE,
157
 
                                    stdout=out_file)
 
153
                                    stdout=subprocess.PIPE)
158
154
        except OSError, e:
159
155
            if e.errno == errno.ENOENT:
160
156
                raise errors.NoDiff(str(e))
161
157
            raise
162
158
        pipe.stdin.close()
163
159
 
164
 
        if not have_fileno:
165
 
            bzrlib.osutils.pumpfile(pipe.stdout, to_file)
 
160
        first_line = pipe.stdout.readline()
 
161
        to_file.write(first_line)
 
162
        bzrlib.osutils.pumpfile(pipe.stdout, to_file)
166
163
        rc = pipe.wait()
167
164
        
168
 
        if rc != 0 and rc != 1:
 
165
        if rc == 2:
 
166
            # 'diff' gives retcode == 2 for all sorts of errors
 
167
            # one of those is 'Binary files differ'.
 
168
            # Bad options could also be the problem.
 
169
            # 'Binary files' is not a real error, so we suppress that error
 
170
            m = re.match('^binary files.*differ$', first_line, re.I)
 
171
            if not m:
 
172
                raise BzrError('external diff failed with exit code 2;'
 
173
                               ' command: %r' % (diffcmd,))
 
174
        elif rc not in (0, 1):
169
175
            # returns 1 if files differ; that's OK
170
176
            if rc < 0:
171
177
                msg = 'signal %d' % (-rc)
172
178
            else:
173
179
                msg = 'exit code %d' % rc
174
180
                
175
 
            raise BzrError('external diff failed with %s; command: %r' % (rc, diffcmd))
 
181
            raise BzrError('external diff failed with %s; command: %r' 
 
182
                           % (rc, diffcmd))
 
183
 
 
184
        # internal_diff() adds a trailing newline, add one here for consistency
 
185
        to_file.write('\n')
 
186
 
176
187
    finally:
177
188
        oldtmpf.close()                 # and delete
178
189
        newtmpf.close()
332
343
    else:
333
344
        diff_file = internal_diff
334
345
    
335
 
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
336
 
                          specific_files=specific_files, 
337
 
                          extra_trees=extra_trees, require_versioned=True)
 
346
    delta = new_tree.changes_from(old_tree,
 
347
        specific_files=specific_files,
 
348
        extra_trees=extra_trees, require_versioned=True)
338
349
 
339
350
    has_changes = 0
340
351
    for path, file_id, kind in delta.removed: