~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

(garyvdm for gagern) Add --directory (-d) option to a number of
        commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
232
232
    return view_info
233
233
 
234
234
 
 
235
def _open_directory_or_containing_tree_or_branch(filename, directory):
 
236
    """Open the tree or branch containing the specified file, unless
 
237
    the --directory option is used to specify a different branch."""
 
238
    if directory is not None:
 
239
        return (None, Branch.open(directory), filename)
 
240
    return bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
241
 
 
242
 
235
243
# TODO: Make sure no commands unconditionally use the working directory as a
236
244
# branch.  If a filename argument is used, the first of them should be used to
237
245
# specify the branch.  (Perhaps this can be factored out into some kind of
340
348
 
341
349
    hidden = True
342
350
    takes_args = ['revision_id?']
343
 
    takes_options = ['revision']
 
351
    takes_options = ['directory', 'revision']
344
352
    # cat-revision is more for frontends so should be exact
345
353
    encoding = 'strict'
346
354
 
353
361
        self.outf.write(revtext.decode('utf-8'))
354
362
 
355
363
    @display_command
356
 
    def run(self, revision_id=None, revision=None):
 
364
    def run(self, revision_id=None, revision=None, directory=u'.'):
357
365
        if revision_id is not None and revision is not None:
358
366
            raise errors.BzrCommandError('You can only supply one of'
359
367
                                         ' revision_id or --revision')
360
368
        if revision_id is None and revision is None:
361
369
            raise errors.BzrCommandError('You must supply either'
362
370
                                         ' --revision or a revision_id')
363
 
        b = WorkingTree.open_containing(u'.')[0].branch
 
371
        b = WorkingTree.open_containing(directory)[0].branch
364
372
 
365
373
        revisions = b.repository.revisions
366
374
        if revisions is None:
554
562
    takes_args = ['revision_info*']
555
563
    takes_options = [
556
564
        'revision',
557
 
        Option('directory',
 
565
        custom_help('directory',
558
566
            help='Branch to examine, '
559
 
                 'rather than the one containing the working directory.',
560
 
            short_name='d',
561
 
            type=unicode,
562
 
            ),
 
567
                 'rather than the one containing the working directory.'),
563
568
        Option('tree', help='Show revno of working tree'),
564
569
        ]
565
570
 
960
965
    takes_options = ['remember', 'overwrite', 'revision',
961
966
        custom_help('verbose',
962
967
            help='Show logs of pulled revisions.'),
963
 
        Option('directory',
 
968
        custom_help('directory',
964
969
            help='Branch to pull into, '
965
 
                 'rather than the one containing the working directory.',
966
 
            short_name='d',
967
 
            type=unicode,
968
 
            ),
 
970
                 'rather than the one containing the working directory.'),
969
971
        Option('local',
970
972
            help="Perform a local pull in a bound "
971
973
                 "branch.  Local pulls are not applied to "
1088
1090
        Option('create-prefix',
1089
1091
               help='Create the path leading up to the branch '
1090
1092
                    'if it does not already exist.'),
1091
 
        Option('directory',
 
1093
        custom_help('directory',
1092
1094
            help='Branch to push from, '
1093
 
                 'rather than the one containing the working directory.',
1094
 
            short_name='d',
1095
 
            type=unicode,
1096
 
            ),
 
1095
                 'rather than the one containing the working directory.'),
1097
1096
        Option('use-existing-dir',
1098
1097
               help='By default push will fail if the target'
1099
1098
                    ' directory exists, but does not already'
2003
2002
    # level of effort but possibly much less IO.  (Or possibly not,
2004
2003
    # if the directories are very large...)
2005
2004
    _see_also = ['status', 'ls']
2006
 
    takes_options = ['show-ids']
 
2005
    takes_options = ['directory', 'show-ids']
2007
2006
 
2008
2007
    @display_command
2009
 
    def run(self, show_ids=False):
2010
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2008
    def run(self, show_ids=False, directory=u'.'):
 
2009
        tree = WorkingTree.open_containing(directory)[0]
2011
2010
        tree.lock_read()
2012
2011
        self.add_cleanup(tree.unlock)
2013
2012
        old = tree.basis_tree()
2028
2027
 
2029
2028
    hidden = True
2030
2029
    _see_also = ['status', 'ls']
2031
 
    takes_options = [
 
2030
    takes_options = ['directory',
2032
2031
            Option('null',
2033
2032
                   help='Write an ascii NUL (\\0) separator '
2034
2033
                   'between files rather than a newline.')
2035
2034
            ]
2036
2035
 
2037
2036
    @display_command
2038
 
    def run(self, null=False):
2039
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2037
    def run(self, null=False, directory=u'.'):
 
2038
        tree = WorkingTree.open_containing(directory)[0]
2040
2039
        td = tree.changes_from(tree.basis_tree())
2041
2040
        for path, id, kind, text_modified, meta_modified in td.modified:
2042
2041
            if null:
2051
2050
 
2052
2051
    hidden = True
2053
2052
    _see_also = ['status', 'ls']
2054
 
    takes_options = [
 
2053
    takes_options = ['directory',
2055
2054
            Option('null',
2056
2055
                   help='Write an ascii NUL (\\0) separator '
2057
2056
                   'between files rather than a newline.')
2058
2057
            ]
2059
2058
 
2060
2059
    @display_command
2061
 
    def run(self, null=False):
2062
 
        wt = WorkingTree.open_containing(u'.')[0]
 
2060
    def run(self, null=False, directory=u'.'):
 
2061
        wt = WorkingTree.open_containing(directory)[0]
2063
2062
        wt.lock_read()
2064
2063
        self.add_cleanup(wt.unlock)
2065
2064
        basis = wt.basis_tree()
2073
2072
            if inv.is_root(file_id) and len(basis_inv) == 0:
2074
2073
                continue
2075
2074
            path = inv.id2path(file_id)
2076
 
            if not os.access(osutils.abspath(path), os.F_OK):
 
2075
            if not os.access(osutils.pathjoin(wt.basedir, path), os.F_OK):
2077
2076
                continue
2078
2077
            if null:
2079
2078
                self.outf.write(path + '\0')
2539
2538
                   help='List entries of a particular kind: file, directory, symlink.',
2540
2539
                   type=unicode),
2541
2540
            'show-ids',
 
2541
            'directory',
2542
2542
            ]
2543
2543
    @display_command
2544
2544
    def run(self, revision=None, verbose=False,
2545
2545
            recursive=False, from_root=False,
2546
2546
            unknown=False, versioned=False, ignored=False,
2547
 
            null=False, kind=None, show_ids=False, path=None):
 
2547
            null=False, kind=None, show_ids=False, path=None, directory=None):
2548
2548
 
2549
2549
        if kind and kind not in ('file', 'directory', 'symlink'):
2550
2550
            raise errors.BzrCommandError('invalid kind specified')
2562
2562
                raise errors.BzrCommandError('cannot specify both --from-root'
2563
2563
                                             ' and PATH')
2564
2564
            fs_path = path
2565
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
2566
 
            fs_path)
 
2565
        tree, branch, relpath = \
 
2566
            _open_directory_or_containing_tree_or_branch(fs_path, directory)
2567
2567
 
2568
2568
        # Calculate the prefix to use
2569
2569
        prefix = None
2638
2638
 
2639
2639
    hidden = True
2640
2640
    _see_also = ['ls']
 
2641
    takes_options = ['directory']
2641
2642
 
2642
2643
    @display_command
2643
 
    def run(self):
2644
 
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
 
2644
    def run(self, directory=u'.'):
 
2645
        for f in WorkingTree.open_containing(directory)[0].unknowns():
2645
2646
            self.outf.write(osutils.quotefn(f) + '\n')
2646
2647
 
2647
2648
 
2712
2713
 
2713
2714
    _see_also = ['status', 'ignored', 'patterns']
2714
2715
    takes_args = ['name_pattern*']
2715
 
    takes_options = [
 
2716
    takes_options = ['directory',
2716
2717
        Option('default-rules',
2717
2718
               help='Display the default ignore rules that bzr uses.')
2718
2719
        ]
2719
2720
 
2720
 
    def run(self, name_pattern_list=None, default_rules=None):
 
2721
    def run(self, name_pattern_list=None, default_rules=None,
 
2722
            directory=u'.'):
2721
2723
        from bzrlib import ignores
2722
2724
        if default_rules is not None:
2723
2725
            # dump the default rules and exit
2734
2736
                (len(name_pattern) > 1 and name_pattern[1] == ':')):
2735
2737
                raise errors.BzrCommandError(
2736
2738
                    "NAME_PATTERN should not be an absolute path")
2737
 
        tree, relpath = WorkingTree.open_containing(u'.')
 
2739
        tree, relpath = WorkingTree.open_containing(directory)
2738
2740
        ignores.tree_ignores_add_patterns(tree, name_pattern_list)
2739
2741
        ignored = globbing.Globster(name_pattern_list)
2740
2742
        matches = []
2766
2768
 
2767
2769
    encoding_type = 'replace'
2768
2770
    _see_also = ['ignore', 'ls']
 
2771
    takes_options = ['directory']
2769
2772
 
2770
2773
    @display_command
2771
 
    def run(self):
2772
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2774
    def run(self, directory=u'.'):
 
2775
        tree = WorkingTree.open_containing(directory)[0]
2773
2776
        tree.lock_read()
2774
2777
        self.add_cleanup(tree.unlock)
2775
2778
        for path, file_class, kind, file_id, entry in tree.list_files():
2788
2791
    """
2789
2792
    hidden = True
2790
2793
    takes_args = ['revno']
 
2794
    takes_options = ['directory']
2791
2795
 
2792
2796
    @display_command
2793
 
    def run(self, revno):
 
2797
    def run(self, revno, directory=u'.'):
2794
2798
        try:
2795
2799
            revno = int(revno)
2796
2800
        except ValueError:
2797
2801
            raise errors.BzrCommandError("not a valid revision-number: %r"
2798
2802
                                         % revno)
2799
 
        revid = WorkingTree.open_containing(u'.')[0].branch.get_rev_id(revno)
 
2803
        revid = WorkingTree.open_containing(directory)[0].branch.get_rev_id(revno)
2800
2804
        self.outf.write("%s\n" % revid)
2801
2805
 
2802
2806
 
2829
2833
      =================       =========================
2830
2834
    """
2831
2835
    takes_args = ['dest', 'branch_or_subdir?']
2832
 
    takes_options = [
 
2836
    takes_options = ['directory',
2833
2837
        Option('format',
2834
2838
               help="Type of file to export to.",
2835
2839
               type=unicode),
2844
2848
                    'revision in which it was changed.'),
2845
2849
        ]
2846
2850
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
2847
 
        root=None, filters=False, per_file_timestamps=False):
 
2851
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
2848
2852
        from bzrlib.export import export
2849
2853
 
2850
2854
        if branch_or_subdir is None:
2851
 
            tree = WorkingTree.open_containing(u'.')[0]
 
2855
            tree = WorkingTree.open_containing(directory)[0]
2852
2856
            b = tree.branch
2853
2857
            subdir = None
2854
2858
        else:
2873
2877
    """
2874
2878
 
2875
2879
    _see_also = ['ls']
2876
 
    takes_options = [
 
2880
    takes_options = ['directory',
2877
2881
        Option('name-from-revision', help='The path name in the old tree.'),
2878
2882
        Option('filters', help='Apply content filters to display the '
2879
2883
                'convenience form.'),
2884
2888
 
2885
2889
    @display_command
2886
2890
    def run(self, filename, revision=None, name_from_revision=False,
2887
 
            filters=False):
 
2891
            filters=False, directory=None):
2888
2892
        if revision is not None and len(revision) != 1:
2889
2893
            raise errors.BzrCommandError("bzr cat --revision takes exactly"
2890
2894
                                         " one revision specifier")
2891
2895
        tree, branch, relpath = \
2892
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
2896
            _open_directory_or_containing_tree_or_branch(filename, directory)
2893
2897
        branch.lock_read()
2894
2898
        self.add_cleanup(branch.unlock)
2895
2899
        return self._run(tree, branch, relpath, filename, revision,
3380
3384
 
3381
3385
    _see_also = ['info']
3382
3386
    takes_args = ['nickname?']
3383
 
    def run(self, nickname=None):
3384
 
        branch = Branch.open_containing(u'.')[0]
 
3387
    takes_options = ['directory']
 
3388
    def run(self, nickname=None, directory=u'.'):
 
3389
        branch = Branch.open_containing(directory)[0]
3385
3390
        if nickname is None:
3386
3391
            self.printme(branch)
3387
3392
        else:
3793
3798
                ' completely merged into the source, pull from the'
3794
3799
                ' source rather than merging.  When this happens,'
3795
3800
                ' you do not need to commit the result.'),
3796
 
        Option('directory',
 
3801
        custom_help('directory',
3797
3802
               help='Branch to merge into, '
3798
 
                    'rather than the one containing the working directory.',
3799
 
               short_name='d',
3800
 
               type=unicode,
3801
 
               ),
 
3803
                    'rather than the one containing the working directory.'),
3802
3804
        Option('preview', help='Instead of merging, show a diff of the'
3803
3805
               ' merge.'),
3804
3806
        Option('interactive', help='Select changes interactively.',
4578
4580
                     Option('long', help='Show commit date in annotations.'),
4579
4581
                     'revision',
4580
4582
                     'show-ids',
 
4583
                     'directory',
4581
4584
                     ]
4582
4585
    encoding_type = 'exact'
4583
4586
 
4584
4587
    @display_command
4585
4588
    def run(self, filename, all=False, long=False, revision=None,
4586
 
            show_ids=False):
 
4589
            show_ids=False, directory=None):
4587
4590
        from bzrlib.annotate import annotate_file, annotate_file_tree
4588
4591
        wt, branch, relpath = \
4589
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
4592
            _open_directory_or_containing_tree_or_branch(filename, directory)
4590
4593
        if wt is not None:
4591
4594
            wt.lock_read()
4592
4595
            self.add_cleanup(wt.unlock)
4619
4622
 
4620
4623
    hidden = True # is this right ?
4621
4624
    takes_args = ['revision_id*']
4622
 
    takes_options = ['revision']
 
4625
    takes_options = ['directory', 'revision']
4623
4626
 
4624
 
    def run(self, revision_id_list=None, revision=None):
 
4627
    def run(self, revision_id_list=None, revision=None, directory=u'.'):
4625
4628
        if revision_id_list is not None and revision is not None:
4626
4629
            raise errors.BzrCommandError('You can only supply one of revision_id or --revision')
4627
4630
        if revision_id_list is None and revision is None:
4628
4631
            raise errors.BzrCommandError('You must supply either --revision or a revision_id')
4629
 
        b = WorkingTree.open_containing(u'.')[0].branch
 
4632
        b = WorkingTree.open_containing(directory)[0].branch
4630
4633
        b.lock_write()
4631
4634
        self.add_cleanup(b.unlock)
4632
4635
        return self._run(b, revision_id_list, revision)
4693
4696
 
4694
4697
    _see_also = ['checkouts', 'unbind']
4695
4698
    takes_args = ['location?']
4696
 
    takes_options = []
 
4699
    takes_options = ['directory']
4697
4700
 
4698
 
    def run(self, location=None):
4699
 
        b, relpath = Branch.open_containing(u'.')
 
4701
    def run(self, location=None, directory=u'.'):
 
4702
        b, relpath = Branch.open_containing(directory)
4700
4703
        if location is None:
4701
4704
            try:
4702
4705
                location = b.get_old_bound_location()
4729
4732
 
4730
4733
    _see_also = ['checkouts', 'bind']
4731
4734
    takes_args = []
4732
 
    takes_options = []
 
4735
    takes_options = ['directory']
4733
4736
 
4734
 
    def run(self):
4735
 
        b, relpath = Branch.open_containing(u'.')
 
4737
    def run(self, directory=u'.'):
 
4738
        b, relpath = Branch.open_containing(directory)
4736
4739
        if not b.unbind():
4737
4740
            raise errors.BzrCommandError('Local branch is not bound')
4738
4741
 
4902
4905
                    'result in a dynamically allocated port.  The default port '
4903
4906
                    'depends on the protocol.',
4904
4907
               type=str),
4905
 
        Option('directory',
4906
 
               help='Serve contents of this directory.',
4907
 
               type=unicode),
 
4908
        custom_help('directory',
 
4909
               help='Serve contents of this directory.'),
4908
4910
        Option('allow-writes',
4909
4911
               help='By default the server is a readonly server.  Supplying '
4910
4912
                    '--allow-writes enables write access to the contents of '
5339
5341
        Option('delete',
5340
5342
            help='Delete this tag rather than placing it.',
5341
5343
            ),
5342
 
        Option('directory',
5343
 
            help='Branch in which to place the tag.',
5344
 
            short_name='d',
5345
 
            type=unicode,
5346
 
            ),
 
5344
        custom_help('directory',
 
5345
            help='Branch in which to place the tag.'),
5347
5346
        Option('force',
5348
5347
            help='Replace existing tags.',
5349
5348
            ),
5392
5391
 
5393
5392
    _see_also = ['tag']
5394
5393
    takes_options = [
5395
 
        Option('directory',
5396
 
            help='Branch whose tags should be displayed.',
5397
 
            short_name='d',
5398
 
            type=unicode,
5399
 
            ),
 
5394
        custom_help('directory',
 
5395
            help='Branch whose tags should be displayed.'),
5400
5396
        RegistryOption.from_kwargs('sort',
5401
5397
            'Sort tags by different criteria.', title='Sorting',
5402
5398
            alpha='Sort tags lexicographically (default).',
5966
5962
 
5967
5963
    To check what clean-tree will do, use --dry-run.
5968
5964
    """
5969
 
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
5965
    takes_options = ['directory',
 
5966
                     Option('ignored', help='Delete all ignored files.'),
5970
5967
                     Option('detritus', help='Delete conflict files, merge'
5971
5968
                            ' backups, and failed selftest dirs.'),
5972
5969
                     Option('unknown',
5975
5972
                            ' deleting them.'),
5976
5973
                     Option('force', help='Do not prompt before deleting.')]
5977
5974
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
5978
 
            force=False):
 
5975
            force=False, directory=u'.'):
5979
5976
        from bzrlib.clean_tree import clean_tree
5980
5977
        if not (unknown or ignored or detritus):
5981
5978
            unknown = True
5982
5979
        if dry_run:
5983
5980
            force = True
5984
 
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus,
5985
 
                   dry_run=dry_run, no_prompt=force)
 
5981
        clean_tree(directory, unknown=unknown, ignored=ignored,
 
5982
                   detritus=detritus, dry_run=dry_run, no_prompt=force)
5986
5983
 
5987
5984
 
5988
5985
class cmd_reference(Command):