~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Robert Collins
  • Date: 2005-09-27 07:24:40 UTC
  • mfrom: (1185.1.41)
  • Revision ID: robertc@robertcollins.net-20050927072440-1bf4d99c3e1db5b3
pair programming worx... merge integration and weave

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
 
 
66
 
 
67
def external_diff(old_label, oldlines, new_label, newlines, to_file,
75
68
                  diff_opts):
76
69
    """Display a diff by calling out to the external diff program."""
77
70
    import sys
106
99
        if not diff_opts:
107
100
            diff_opts = []
108
101
        diffcmd = ['diff',
109
 
                   '--label', old_filename+'\t',
 
102
                   '--label', old_label,
110
103
                   oldtmpf.name,
111
 
                   '--label', new_filename+'\t',
 
104
                   '--label', new_label,
112
105
                   newtmpf.name]
113
106
 
114
107
        # diff only allows one style to be specified; they don't override.
148
141
    finally:
149
142
        oldtmpf.close()                 # and delete
150
143
        newtmpf.close()
151
 
 
152
 
 
153
 
@deprecated_function(zero_eight)
154
 
def show_diff(b, from_spec, specific_files, external_diff_options=None,
155
 
              revision2=None, output=None, b2=None):
 
144
    
 
145
 
 
146
 
 
147
def show_diff(b, revision, specific_files, external_diff_options=None,
 
148
              revision2=None, output=None):
156
149
    """Shortcut for showing the diff to the working tree.
157
150
 
158
 
    Please use show_diff_trees instead.
159
 
 
160
151
    b
161
152
        Branch.
162
153
 
163
154
    revision
164
 
        None for 'basis tree', or otherwise the old revision to compare against.
 
155
        None for each, or otherwise the old revision to compare against.
165
156
    
166
157
    The more general form is show_diff_trees(), where the caller
167
158
    supplies any two trees.
170
161
        import sys
171
162
        output = sys.stdout
172
163
 
173
 
    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()
 
164
    if revision is None:
 
165
        old_tree = b.basis_tree()
177
166
    else:
178
 
        old_tree = b.repository.revision_tree(from_spec.in_history(b).rev_id)
 
167
        old_tree = b.revision_tree(revision.in_history(b).rev_id)
179
168
 
180
169
    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)
 
170
        new_tree = b.working_tree()
 
171
    else:
 
172
        new_tree = b.revision_tree(revision2.in_history(b).rev_id)
 
173
 
 
174
    show_diff_trees(old_tree, new_tree, output, specific_files,
 
175
                    external_diff_options)
 
176
 
235
177
 
236
178
 
237
179
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/'):
 
180
                    external_diff_options=None):
240
181
    """Show in text form the changes from one tree to another.
241
182
 
242
183
    to_files
245
186
    external_diff_options
246
187
        If set, use an external GNU diff and pass these options.
247
188
    """
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/' ):
 
189
 
 
190
    # TODO: Options to control putting on a prefix or suffix, perhaps as a format string
 
191
    old_label = ''
 
192
    new_label = ''
264
193
 
265
194
    DEVNULL = '/dev/null'
266
195
    # Windows users, don't panic about this filename -- it is a
270
199
    # TODO: Generation of pseudo-diffs for added/deleted files could
271
200
    # be usefully made into a much faster special case.
272
201
 
273
 
    _raise_if_doubly_unversioned(specific_files, old_tree, new_tree)
274
 
 
275
202
    if external_diff_options:
276
203
        assert isinstance(external_diff_options, basestring)
277
204
        opts = external_diff_options.split()
280
207
    else:
281
208
        diff_file = internal_diff
282
209
    
 
210
 
283
211
    delta = compare_trees(old_tree, new_tree, want_unchanged=False,
284
212
                          specific_files=specific_files)
285
213
 
286
 
    has_changes = 0
287
214
    for path, file_id, kind in delta.removed:
288
 
        has_changes = 1
289
 
        print >>to_file, '=== removed %s %r' % (kind, old_label + path)
290
 
        old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
291
 
                                         DEVNULL, None, None, to_file)
 
215
        print >>to_file, '=== removed %s %r' % (kind, path)
 
216
        if kind == 'file':
 
217
            diff_file(old_label + path,
 
218
                      old_tree.get_file(file_id).readlines(),
 
219
                      DEVNULL, 
 
220
                      [],
 
221
                      to_file)
 
222
 
292
223
    for path, file_id, kind in delta.added:
293
 
        has_changes = 1
294
 
        print >>to_file, '=== added %s %r' % (kind, new_label + path)
295
 
        new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
296
 
                                         DEVNULL, None, None, to_file, 
297
 
                                         reverse=True)
298
 
    for (old_path, new_path, file_id, kind,
299
 
         text_modified, meta_modified) in delta.renamed:
300
 
        has_changes = 1
301
 
        prop_str = get_prop_change(meta_modified)
302
 
        print >>to_file, '=== renamed %s %r => %r%s' % (
303
 
                    kind, old_label + old_path, new_label + new_path, prop_str)
304
 
        _maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
305
 
                                    new_label, new_path, new_tree,
306
 
                                    text_modified, kind, to_file, diff_file)
307
 
    for path, file_id, kind, text_modified, meta_modified in delta.modified:
308
 
        has_changes = 1
309
 
        prop_str = get_prop_change(meta_modified)
310
 
        print >>to_file, '=== modified %s %r%s' % (kind, old_label + path,
311
 
                    prop_str)
 
224
        print >>to_file, '=== added %s %r' % (kind, path)
 
225
        if kind == 'file':
 
226
            diff_file(DEVNULL,
 
227
                      [],
 
228
                      new_label + path,
 
229
                      new_tree.get_file(file_id).readlines(),
 
230
                      to_file)
 
231
 
 
232
    for old_path, new_path, file_id, kind, text_modified in delta.renamed:
 
233
        print >>to_file, '=== renamed %s %r => %r' % (kind, old_path, new_path)
312
234
        if text_modified:
313
 
            _maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
314
 
                                        new_label, path, new_tree,
315
 
                                        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
 
 
349
 
 
350
 
def get_prop_change(meta_modified):
351
 
    if meta_modified:
352
 
        return " (properties changed)"
353
 
    else:
354
 
        return  ""
355
 
 
356
 
 
357
 
def _maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
358
 
                                new_label, new_path, new_tree, text_modified,
359
 
                                kind, to_file, diff_file):
360
 
    if text_modified:
361
 
        new_entry = new_tree.inventory[file_id]
362
 
        old_tree.inventory[file_id].diff(diff_file,
363
 
                                         old_label + old_path, old_tree,
364
 
                                         new_label + new_path, new_entry, 
365
 
                                         new_tree, to_file)
 
235
            diff_file(old_label + old_path,
 
236
                      old_tree.get_file(file_id).readlines(),
 
237
                      new_label + new_path,
 
238
                      new_tree.get_file(file_id).readlines(),
 
239
                      to_file)
 
240
 
 
241
    for path, file_id, kind in delta.modified:
 
242
        print >>to_file, '=== modified %s %r' % (kind, path)
 
243
        if kind == 'file':
 
244
            diff_file(old_label + path,
 
245
                      old_tree.get_file(file_id).readlines(),
 
246
                      new_label + path,
 
247
                      new_tree.get_file(file_id).readlines(),
 
248
                      to_file)
 
249
 
 
250
 
 
251
 
 
252
 
 
253