~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Robert Collins
  • Date: 2005-10-18 05:26:22 UTC
  • mto: This revision was merged to the branch mainline in revision 1463.
  • Revision ID: robertc@robertcollins.net-20051018052622-653d638c9e26fde4
fix broken tests

Show diffs side-by-side

added added

removed removed

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