~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

Late bind to PatienceSequenceMatcher to allow plugin to override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: UTF-8 -*-
 
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
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
from bzrlib.delta import compare_trees
 
18
from bzrlib.errors import BzrError
 
19
import bzrlib.errors as errors
 
20
from bzrlib.patiencediff import unified_diff
 
21
import bzrlib.patiencediff
 
22
from bzrlib.symbol_versioning import *
 
23
from bzrlib.textfile import check_text_lines
17
24
from bzrlib.trace import mutter
18
 
from bzrlib.errors import BzrError
19
 
from bzrlib.delta import compare_trees
 
25
 
20
26
 
21
27
# TODO: Rather than building a changeset object, we should probably
22
28
# invoke callbacks on an object.  That object can either accumulate a
23
29
# list, write them out directly, etc etc.
24
30
 
25
 
def internal_diff(old_label, oldlines, new_label, newlines, to_file):
26
 
    import difflib
27
 
    
 
31
def internal_diff(old_filename, oldlines, new_filename, newlines, to_file,
 
32
                  allow_binary=False, sequence_matcher=None):
28
33
    # FIXME: difflib is wrong if there is no trailing newline.
29
34
    # The syntax used by patch seems to be "\ No newline at
30
35
    # end of file" following the last diff line from that
40
45
    # both sequences are empty.
41
46
    if not oldlines and not newlines:
42
47
        return
 
48
    
 
49
    if allow_binary is False:
 
50
        check_text_lines(oldlines)
 
51
        check_text_lines(newlines)
43
52
 
44
 
    ud = difflib.unified_diff(oldlines, newlines,
45
 
                              fromfile=old_label, tofile=new_label)
 
53
    if sequence_matcher is None:
 
54
        sequence_matcher = bzrlib.patiencediff.PatienceSequenceMatcher
 
55
    ud = unified_diff(oldlines, newlines,
 
56
                      fromfile=old_filename+'\t', 
 
57
                      tofile=new_filename+'\t',
 
58
                      sequencematcher=sequence_matcher)
46
59
 
47
60
    ud = list(ud)
48
61
    # work-around for difflib being too smart for its own good
62
75
    print >>to_file
63
76
 
64
77
 
65
 
def external_diff(old_label, oldlines, new_label, newlines, to_file,
 
78
def external_diff(old_filename, oldlines, new_filename, newlines, to_file,
66
79
                  diff_opts):
67
80
    """Display a diff by calling out to the external diff program."""
68
81
    import sys
97
110
        if not diff_opts:
98
111
            diff_opts = []
99
112
        diffcmd = ['diff',
100
 
                   '--label', old_label,
 
113
                   '--label', old_filename+'\t',
101
114
                   oldtmpf.name,
102
 
                   '--label', new_label,
 
115
                   '--label', new_filename+'\t',
103
116
                   newtmpf.name]
104
117
 
105
118
        # diff only allows one style to be specified; they don't override.
140
153
        oldtmpf.close()                 # and delete
141
154
        newtmpf.close()
142
155
 
 
156
 
 
157
@deprecated_function(zero_eight)
143
158
def show_diff(b, from_spec, specific_files, external_diff_options=None,
144
 
              revision2=None, output=None):
 
159
              revision2=None, output=None, b2=None):
145
160
    """Shortcut for showing the diff to the working tree.
146
161
 
 
162
    Please use show_diff_trees instead.
 
163
 
147
164
    b
148
165
        Branch.
149
166
 
158
175
        output = sys.stdout
159
176
 
160
177
    if from_spec is None:
161
 
        old_tree = b.basis_tree()
 
178
        old_tree = b.bzrdir.open_workingtree()
 
179
        if b2 is None:
 
180
            old_tree = old_tree = old_tree.basis_tree()
162
181
    else:
163
 
        old_tree = b.revision_tree(from_spec.in_history(b).rev_id)
 
182
        old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
164
183
 
165
184
    if revision2 is None:
166
 
        new_tree = b.working_tree()
167
 
    else:
168
 
        new_tree = b.revision_tree(revision2.in_history(b).rev_id)
169
 
 
170
 
    show_diff_trees(old_tree, new_tree, output, specific_files,
171
 
                    external_diff_options)
172
 
 
 
185
        if b2 is None:
 
186
            new_tree = b.bzrdir.open_workingtree()
 
187
        else:
 
188
            new_tree = b2.bzrdir.open_workingtree()
 
189
    else:
 
190
        new_tree = b.repository.revision_tree(revision2.in_history(b).rev_id)
 
191
 
 
192
    return show_diff_trees(old_tree, new_tree, output, specific_files,
 
193
                           external_diff_options)
 
194
 
 
195
 
 
196
def diff_cmd_helper(tree, specific_files, external_diff_options, 
 
197
                    old_revision_spec=None, new_revision_spec=None,
 
198
                    old_label='a/', new_label='b/'):
 
199
    """Helper for cmd_diff.
 
200
 
 
201
   tree 
 
202
        A WorkingTree
 
203
 
 
204
    specific_files
 
205
        The specific files to compare, or None
 
206
 
 
207
    external_diff_options
 
208
        If non-None, run an external diff, and pass it these options
 
209
 
 
210
    old_revision_spec
 
211
        If None, use basis tree as old revision, otherwise use the tree for
 
212
        the specified revision. 
 
213
 
 
214
    new_revision_spec
 
215
        If None, use working tree as new revision, otherwise use the tree for
 
216
        the specified revision.
 
217
    
 
218
    The more general form is show_diff_trees(), where the caller
 
219
    supplies any two trees.
 
220
    """
 
221
    import sys
 
222
    output = sys.stdout
 
223
    def spec_tree(spec):
 
224
        revision_id = spec.in_store(tree.branch).rev_id
 
225
        return tree.branch.repository.revision_tree(revision_id)
 
226
    if old_revision_spec is None:
 
227
        old_tree = tree.basis_tree()
 
228
    else:
 
229
        old_tree = spec_tree(old_revision_spec)
 
230
 
 
231
    if new_revision_spec is None:
 
232
        new_tree = tree
 
233
    else:
 
234
        new_tree = spec_tree(new_revision_spec)
 
235
 
 
236
    return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
 
237
                           external_diff_options,
 
238
                           old_label=old_label, new_label=new_label)
173
239
 
174
240
 
175
241
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
176
 
                    external_diff_options=None):
 
242
                    external_diff_options=None,
 
243
                    old_label='a/', new_label='b/'):
177
244
    """Show in text form the changes from one tree to another.
178
245
 
179
246
    to_files
182
249
    external_diff_options
183
250
        If set, use an external GNU diff and pass these options.
184
251
    """
185
 
 
186
 
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
187
 
    old_label = ''
188
 
    new_label = ''
 
252
    old_tree.lock_read()
 
253
    try:
 
254
        new_tree.lock_read()
 
255
        try:
 
256
            return _show_diff_trees(old_tree, new_tree, to_file,
 
257
                                    specific_files, external_diff_options,
 
258
                                    old_label=old_label, new_label=new_label)
 
259
        finally:
 
260
            new_tree.unlock()
 
261
    finally:
 
262
        old_tree.unlock()
 
263
 
 
264
 
 
265
def _show_diff_trees(old_tree, new_tree, to_file,
 
266
                     specific_files, external_diff_options, 
 
267
                     old_label='a/', new_label='b/' ):
189
268
 
190
269
    DEVNULL = '/dev/null'
191
270
    # Windows users, don't panic about this filename -- it is a
195
274
    # TODO: Generation of pseudo-diffs for added/deleted files could
196
275
    # be usefully made into a much faster special case.
197
276
 
 
277
    _raise_if_doubly_unversioned(specific_files, old_tree, new_tree)
 
278
 
198
279
    if external_diff_options:
199
280
        assert isinstance(external_diff_options, basestring)
200
281
        opts = external_diff_options.split()
203
284
    else:
204
285
        diff_file = internal_diff
205
286
    
206
 
 
207
287
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
208
288
                          specific_files=specific_files)
209
289
 
 
290
    has_changes = 0
210
291
    for path, file_id, kind in delta.removed:
 
292
        has_changes = 1
211
293
        print >>to_file, '=== removed %s %r' % (kind, path)
212
294
        old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
213
295
                                         DEVNULL, None, None, to_file)
214
296
    for path, file_id, kind in delta.added:
 
297
        has_changes = 1
215
298
        print >>to_file, '=== added %s %r' % (kind, path)
216
299
        new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
217
300
                                         DEVNULL, None, None, to_file, 
218
301
                                         reverse=True)
219
302
    for (old_path, new_path, file_id, kind,
220
303
         text_modified, meta_modified) in delta.renamed:
 
304
        has_changes = 1
221
305
        prop_str = get_prop_change(meta_modified)
222
306
        print >>to_file, '=== renamed %s %r => %r%s' % (
223
 
                          kind, old_path, new_path, prop_str)
 
307
                    kind, old_path, new_path, prop_str)
224
308
        _maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
225
309
                                    new_label, new_path, new_tree,
226
310
                                    text_modified, kind, to_file, diff_file)
227
311
    for path, file_id, kind, text_modified, meta_modified in delta.modified:
 
312
        has_changes = 1
228
313
        prop_str = get_prop_change(meta_modified)
229
314
        print >>to_file, '=== modified %s %r%s' % (kind, path, prop_str)
230
315
        if text_modified:
231
316
            _maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
232
317
                                        new_label, path, new_tree,
233
318
                                        True, kind, to_file, diff_file)
234
 
    
 
319
 
 
320
    return has_changes
 
321
 
 
322
 
 
323
def _raise_if_doubly_unversioned(specific_files, old_tree, new_tree):
 
324
    """Complain if paths are not versioned in either tree."""
 
325
    if not specific_files:
 
326
        return
 
327
    old_unversioned = old_tree.filter_unversioned_files(specific_files)
 
328
    new_unversioned = new_tree.filter_unversioned_files(specific_files)
 
329
    unversioned = old_unversioned.intersection(new_unversioned)
 
330
    if unversioned:
 
331
        raise errors.PathsNotVersionedError(sorted(unversioned))
 
332
    
 
333
 
 
334
def _raise_if_nonexistent(paths, old_tree, new_tree):
 
335
    """Complain if paths are not in either inventory or tree.
 
336
 
 
337
    It's OK with the files exist in either tree's inventory, or 
 
338
    if they exist in the tree but are not versioned.
 
339
    
 
340
    This can be used by operations such as bzr status that can accept
 
341
    unknown or ignored files.
 
342
    """
 
343
    mutter("check paths: %r", paths)
 
344
    if not paths:
 
345
        return
 
346
    s = old_tree.filter_unversioned_files(paths)
 
347
    s = new_tree.filter_unversioned_files(s)
 
348
    s = [path for path in s if not new_tree.has_filename(path)]
 
349
    if s:
 
350
        raise errors.PathsDoNotExist(sorted(s))
 
351
 
235
352
 
236
353
def get_prop_change(meta_modified):
237
354
    if meta_modified: