~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

  • Committer: Ian Clatworthy
  • Date: 2008-07-30 13:48:31 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-20080730134831-swfuxgaoeeyf60ua
first cut at view command

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
from bzrlib.trace import mutter, note, warning, is_quiet, info
60
60
 
61
61
 
62
 
def tree_files(file_list, default_branch=u'.'):
 
62
def tree_files(file_list, default_branch=u'.', apply_view=True):
63
63
    try:
64
 
        return internal_tree_files(file_list, default_branch)
 
64
        return internal_tree_files(file_list, default_branch, apply_view)
65
65
    except errors.FileInWrongBranch, e:
66
66
        raise errors.BzrCommandError("%s is not in the same branch as %s" %
67
67
                                     (e.path, file_list[0]))
69
69
 
70
70
# XXX: Bad function name; should possibly also be a class method of
71
71
# WorkingTree rather than a function.
72
 
def internal_tree_files(file_list, default_branch=u'.'):
 
72
def internal_tree_files(file_list, default_branch=u'.', apply_view=True):
73
73
    """Convert command-line paths to a WorkingTree and relative paths.
74
74
 
75
75
    This is typically used for command-line processors that take one or
82
82
    :param default_branch: Fallback tree path to use if file_list is empty or
83
83
        None.
84
84
 
 
85
    :param apply_view: if True and a view is set, apply it or check that
 
86
        specified files are within it
 
87
 
85
88
    :return: workingtree, [relative_paths]
86
89
    """
87
90
    if file_list is None or len(file_list) == 0:
88
 
        return WorkingTree.open_containing(default_branch)[0], file_list
 
91
        tree = WorkingTree.open_containing(default_branch)[0]
 
92
        if tree.supports_views() and apply_view:
 
93
            view_files = tree.views.lookup_view()
 
94
            if view_files:
 
95
                file_list = view_files
 
96
                view_str = ", ".join(view_files)
 
97
                note("ignoring files outside view: %s" % view_str)
 
98
        return tree, file_list
89
99
    tree = WorkingTree.open_containing(osutils.realpath(file_list[0]))[0]
 
100
    if tree.supports_views() and apply_view:
 
101
        view_files = tree.views.lookup_view()
 
102
    else:
 
103
        view_files = []
90
104
    new_list = []
91
105
    for filename in file_list:
92
106
        try:
93
 
            new_list.append(tree.relpath(osutils.dereference_path(filename)))
 
107
            relpath = tree.relpath(osutils.dereference_path(filename))
 
108
            if  view_files and not is_inside_any(view_files, relpath):
 
109
                raise errors.FileOutsideView(filename, view_files)
 
110
            new_list.append(relpath)
94
111
        except errors.PathNotChild:
95
112
            raise errors.FileInWrongBranch(tree.branch, filename)
96
113
    return tree, new_list
4488
4505
            urlutils.unescape_for_display(to_branch.base, 'utf-8'))
4489
4506
 
4490
4507
 
 
4508
class cmd_view(Command):
 
4509
    """Manage filtered views.
 
4510
    
 
4511
    Views provide a mask over the tree so that users can focus on
 
4512
    a subset of a tree when doing their work. After creating a view,
 
4513
    commands that support a list of files - status, diff, commit, etc -
 
4514
    effectively have that list of files implicitly given each time.
 
4515
    An explicit list of files can still be given but those files
 
4516
    must be within the current view.
 
4517
 
 
4518
    In most cases, a view has a short life-span: it is created to make
 
4519
    a selected change and is deleted once that change is committed.
 
4520
    At other times, you may wish to create one or more named views
 
4521
    and switch between them. To see the list of named views defined
 
4522
    for a tree, use the ``info`` command.
 
4523
    
 
4524
    To disable the current view without deleting it, you can switch to
 
4525
    the pseudo view called ``off``. This can be useful when you need
 
4526
    to see the whole tree for an operation or two (e.g. merge) but
 
4527
    want to switch back to your view after that.
 
4528
 
 
4529
    :Examples:
 
4530
      To change the current view::
 
4531
 
 
4532
        bzr view file1 dir1 ...
 
4533
 
 
4534
      To list the current view::
 
4535
 
 
4536
        bzr view
 
4537
 
 
4538
      To delete the current view::
 
4539
 
 
4540
        bzr view --delete
 
4541
 
 
4542
      To disable the current view without deleting it::
 
4543
 
 
4544
        bzr view --switch off
 
4545
 
 
4546
      To change a named view and switch to it::
 
4547
 
 
4548
        bzr view --name view-name file1 dir1 ...
 
4549
 
 
4550
      To list a named view::
 
4551
 
 
4552
        bzr view --name view-name
 
4553
 
 
4554
      To delete a named view::
 
4555
 
 
4556
        bzr view --name view-name --delete
 
4557
 
 
4558
      To switch to a named view::
 
4559
 
 
4560
        bzr view --switch view-name
 
4561
    """
 
4562
 
 
4563
    _see_also = ['info']
 
4564
    takes_args = ['file*']
 
4565
    takes_options = [
 
4566
        Option('delete',
 
4567
            help='Delete the view.',
 
4568
            ),
 
4569
        Option('name',
 
4570
            help='Name of the view to list, save or delete.',
 
4571
            type=unicode,
 
4572
            ),
 
4573
        Option('switch',
 
4574
            help='Name of the view to switch to.',
 
4575
            type=unicode,
 
4576
            ),
 
4577
        ]
 
4578
 
 
4579
    def run(self, file_list,
 
4580
            delete=False,
 
4581
            name=None,
 
4582
            switch=None,
 
4583
            ):
 
4584
        tree, file_list = tree_files(file_list, apply_view=False)
 
4585
        current_view, view_dict = tree.views.get_view_info()
 
4586
        if name is None:
 
4587
            name = current_view
 
4588
        if delete:
 
4589
            if file_list:
 
4590
                raise BzrCommandError("Both --delete and a file list specified")
 
4591
            elif switch:
 
4592
                raise BzrCommandError("Both --delete and --switch specified")
 
4593
            elif name is None:
 
4594
                raise BzrCommandError("No current view to delete")
 
4595
            else:
 
4596
                tree.views.delete_view(name)
 
4597
                self.outf.write("Deleted '%s' view.\n" % name)
 
4598
        elif switch:
 
4599
            if file_list:
 
4600
                raise BzrCommandError("Both --switch and a file list specified")
 
4601
            elif switch == 'off':
 
4602
                if current_view is None:
 
4603
                    raise BzrCommandError("No current view to disable")
 
4604
                tree.views.set_view_info(None, view_dict)
 
4605
                self.outf.write("Disabled '%s' view.\n" % (switch))
 
4606
            else:
 
4607
                tree.views.set_view_info(switch, view_dict)
 
4608
                view_str = ", ".join(tree.views.lookup_view())
 
4609
                self.outf.write("Switched to '%s' view: %s\n" % (switch,view_str))
 
4610
        elif file_list:
 
4611
            if name is None:
 
4612
                # No name given and no current view set
 
4613
                name = 'my'
 
4614
            elif name == 'off':
 
4615
                raise BzrCommandError("Cannot change the 'off' pseudo view")
 
4616
            tree.views.set_view(name, sorted(file_list))
 
4617
            view_str = ", ".join(tree.views.lookup_view())
 
4618
            self.outf.write("Switched to '%s' view: %s\n" % (name, view_str))
 
4619
        else:
 
4620
            # list the files
 
4621
            if current_view is None:
 
4622
                self.outf.write('No current view.\n')
 
4623
            else:
 
4624
                view_str = ", ".join(tree.views.lookup_view(name))
 
4625
                self.outf.write("Using '%s' view: %s\n" % (name, view_str))
 
4626
 
 
4627
 
4491
4628
class cmd_hooks(Command):
4492
4629
    """Show a branch's currently registered hooks.
4493
4630
    """