~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/diff.py

  • Committer: Ian Clatworthy
  • Date: 2008-08-07 14:21:59 UTC
  • mto: (4029.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 4030.
  • Revision ID: ian.clatworthy@canonical.com-20080807142159-psfims4e5q0ay4hv
enhance diff to support views

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    patiencediff,
37
37
    textfile,
38
38
    timestamp,
 
39
    views,
39
40
    )
40
41
""")
41
42
 
43
44
        deprecated_function,
44
45
        one_three
45
46
        )
46
 
from bzrlib.trace import mutter, warning
 
47
from bzrlib.trace import mutter, note, warning
47
48
 
48
49
 
49
50
# TODO: Rather than building a changeset object, we should probably
275
276
                        new_abspath, e)
276
277
 
277
278
 
278
 
def _get_trees_to_diff(path_list, revision_specs, old_url, new_url):
 
279
def _get_trees_to_diff(path_list, revision_specs, old_url, new_url,
 
280
    apply_view=True):
279
281
    """Get the trees and specific files to diff given a list of paths.
280
282
 
281
283
    This method works out the trees to be diff'ed and the files of
292
294
    :param new_url:
293
295
        The url of the new branch or tree. If None, the tree to use is
294
296
        taken from the first path, if any, or the current working tree.
 
297
    :param apply_view:
 
298
        if True and a view is set, apply the view or check that the paths
 
299
        are within it
295
300
    :returns:
296
301
        a tuple of (old_tree, new_tree, specific_files, extra_trees) where
297
302
        extra_trees is a sequence of additional trees to search in for
331
336
    working_tree, branch, relpath = \
332
337
        bzrdir.BzrDir.open_containing_tree_or_branch(old_url)
333
338
    if consider_relpath and relpath != '':
 
339
        if working_tree is not None and apply_view:
 
340
            _check_path_in_view(working_tree, relpath)
334
341
        specific_files.append(relpath)
335
342
    old_tree = _get_tree_to_diff(old_revision_spec, working_tree, branch)
336
343
 
341
348
        working_tree, branch, relpath = \
342
349
            bzrdir.BzrDir.open_containing_tree_or_branch(new_url)
343
350
        if consider_relpath and relpath != '':
 
351
            if working_tree is not None and apply_view:
 
352
                _check_path_in_view(working_tree, relpath)
344
353
            specific_files.append(relpath)
345
354
    new_tree = _get_tree_to_diff(new_revision_spec, working_tree, branch,
346
355
        basis_is_default=working_tree is None)
347
356
 
348
357
    # Get the specific files (all files is None, no files is [])
349
358
    if make_paths_wt_relative and working_tree is not None:
350
 
        other_paths = _relative_paths_in_tree(working_tree, other_paths)
 
359
        try:
 
360
            from bzrlib.builtins import safe_relpath_files
 
361
            other_paths = safe_relpath_files(working_tree, other_paths,
 
362
            apply_view=apply_view)
 
363
        except errors.FileInWrongBranch:
 
364
            raise errors.BzrCommandError("Files are in different branches")
351
365
    specific_files.extend(other_paths)
352
366
    if len(specific_files) == 0:
353
367
        specific_files = None
 
368
        if (working_tree is not None and working_tree.supports_views()
 
369
            and apply_view):
 
370
            view_files = working_tree.views.lookup_view()
 
371
            if view_files:
 
372
                specific_files = view_files
 
373
                view_str = views.view_display_str(view_files)
 
374
                note("*** ignoring files outside view: %s" % view_str)
354
375
 
355
376
    # Get extra trees that ought to be searched for file-ids
356
377
    extra_trees = None
359
380
    return old_tree, new_tree, specific_files, extra_trees
360
381
 
361
382
 
 
383
def _check_path_in_view(tree, relpath):
 
384
    """If a working tree has a view enabled, check the path is within it."""
 
385
    if tree.supports_views():
 
386
        view_files = tree.views.lookup_view()
 
387
        if  view_files and not osutils.is_inside_any(view_files, relpath):
 
388
            raise errors.FileOutsideView(relpath, view_files)
 
389
 
 
390
 
362
391
def _get_tree_to_diff(spec, tree=None, branch=None, basis_is_default=True):
363
392
    if branch is None and tree is not None:
364
393
        branch = tree.branch
376
405
    return branch.repository.revision_tree(revision_id)
377
406
 
378
407
 
379
 
def _relative_paths_in_tree(tree, paths):
380
 
    """Get the relative paths within a working tree.
381
 
 
382
 
    Each path may be either an absolute path or a path relative to the
383
 
    current working directory.
384
 
    """
385
 
    result = []
386
 
    for filename in paths:
387
 
        try:
388
 
            result.append(tree.relpath(osutils.dereference_path(filename)))
389
 
        except errors.PathNotChild:
390
 
            raise errors.BzrCommandError("Files are in different branches")
391
 
    return result
392
 
 
393
 
 
394
408
def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
395
409
                    external_diff_options=None,
396
410
                    old_label='a/', new_label='b/',