~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

(lifeless) Generalise probing for executables used in selftest. (Martin von
 Gagern)

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:
552
560
    takes_args = ['revision_info*']
553
561
    takes_options = [
554
562
        'revision',
555
 
        Option('directory',
 
563
        custom_help('directory',
556
564
            help='Branch to examine, '
557
 
                 'rather than the one containing the working directory.',
558
 
            short_name='d',
559
 
            type=unicode,
560
 
            ),
 
565
                 'rather than the one containing the working directory.'),
561
566
        Option('tree', help='Show revno of working tree'),
562
567
        ]
563
568
 
951
956
    takes_options = ['remember', 'overwrite', 'revision',
952
957
        custom_help('verbose',
953
958
            help='Show logs of pulled revisions.'),
954
 
        Option('directory',
 
959
        custom_help('directory',
955
960
            help='Branch to pull into, '
956
 
                 'rather than the one containing the working directory.',
957
 
            short_name='d',
958
 
            type=unicode,
959
 
            ),
 
961
                 'rather than the one containing the working directory.'),
960
962
        Option('local',
961
963
            help="Perform a local pull in a bound "
962
964
                 "branch.  Local pulls are not applied to "
1076
1078
        Option('create-prefix',
1077
1079
               help='Create the path leading up to the branch '
1078
1080
                    'if it does not already exist.'),
1079
 
        Option('directory',
 
1081
        custom_help('directory',
1080
1082
            help='Branch to push from, '
1081
 
                 'rather than the one containing the working directory.',
1082
 
            short_name='d',
1083
 
            type=unicode,
1084
 
            ),
 
1083
                 'rather than the one containing the working directory.'),
1085
1084
        Option('use-existing-dir',
1086
1085
               help='By default push will fail if the target'
1087
1086
                    ' directory exists, but does not already'
1987
1986
    # level of effort but possibly much less IO.  (Or possibly not,
1988
1987
    # if the directories are very large...)
1989
1988
    _see_also = ['status', 'ls']
1990
 
    takes_options = ['show-ids']
 
1989
    takes_options = ['directory', 'show-ids']
1991
1990
 
1992
1991
    @display_command
1993
 
    def run(self, show_ids=False):
1994
 
        tree = WorkingTree.open_containing(u'.')[0]
 
1992
    def run(self, show_ids=False, directory=u'.'):
 
1993
        tree = WorkingTree.open_containing(directory)[0]
1995
1994
        self.add_cleanup(tree.lock_read().unlock)
1996
1995
        old = tree.basis_tree()
1997
1996
        self.add_cleanup(old.lock_read().unlock)
2010
2009
 
2011
2010
    hidden = True
2012
2011
    _see_also = ['status', 'ls']
2013
 
    takes_options = [
2014
 
            Option('null',
2015
 
                   help='Write an ascii NUL (\\0) separator '
2016
 
                   'between files rather than a newline.')
2017
 
            ]
 
2012
    takes_options = ['directory', 'null']
2018
2013
 
2019
2014
    @display_command
2020
 
    def run(self, null=False):
2021
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2015
    def run(self, null=False, directory=u'.'):
 
2016
        tree = WorkingTree.open_containing(directory)[0]
2022
2017
        td = tree.changes_from(tree.basis_tree())
2023
2018
        for path, id, kind, text_modified, meta_modified in td.modified:
2024
2019
            if null:
2033
2028
 
2034
2029
    hidden = True
2035
2030
    _see_also = ['status', 'ls']
2036
 
    takes_options = [
2037
 
            Option('null',
2038
 
                   help='Write an ascii NUL (\\0) separator '
2039
 
                   'between files rather than a newline.')
2040
 
            ]
 
2031
    takes_options = ['directory', 'null']
2041
2032
 
2042
2033
    @display_command
2043
 
    def run(self, null=False):
2044
 
        wt = WorkingTree.open_containing(u'.')[0]
 
2034
    def run(self, null=False, directory=u'.'):
 
2035
        wt = WorkingTree.open_containing(directory)[0]
2045
2036
        self.add_cleanup(wt.lock_read().unlock)
2046
2037
        basis = wt.basis_tree()
2047
2038
        self.add_cleanup(basis.lock_read().unlock)
2053
2044
            if inv.is_root(file_id) and len(basis_inv) == 0:
2054
2045
                continue
2055
2046
            path = inv.id2path(file_id)
2056
 
            if not os.access(osutils.abspath(path), os.F_OK):
 
2047
            if not os.access(osutils.pathjoin(wt.basedir, path), os.F_OK):
2057
2048
                continue
2058
2049
            if null:
2059
2050
                self.outf.write(path + '\0')
2259
2250
                   help='Show just the specified revision.'
2260
2251
                   ' See also "help revisionspec".'),
2261
2252
            'log-format',
 
2253
            RegistryOption('authors',
 
2254
                'What names to list as authors - first, all or committer.',
 
2255
                title='Authors',
 
2256
                lazy_registry=('bzrlib.log', 'author_list_registry'),
 
2257
            ),
2262
2258
            Option('levels',
2263
2259
                   short_name='n',
2264
2260
                   help='Number of levels to display - 0 for all, 1 for flat.',
2299
2295
            limit=None,
2300
2296
            show_diff=False,
2301
2297
            include_merges=False,
 
2298
            authors=None,
2302
2299
            exclude_common_ancestry=False,
2303
2300
            ):
2304
2301
        from bzrlib.log import (
2382
2379
                        show_timezone=timezone,
2383
2380
                        delta_format=get_verbosity_level(),
2384
2381
                        levels=levels,
2385
 
                        show_advice=levels is None)
 
2382
                        show_advice=levels is None,
 
2383
                        author_list_handler=authors)
2386
2384
 
2387
2385
        # Choose the algorithm for doing the logging. It's annoying
2388
2386
        # having multiple code paths like this but necessary until
2505
2503
                   help='Recurse into subdirectories.'),
2506
2504
            Option('from-root',
2507
2505
                   help='Print paths relative to the root of the branch.'),
2508
 
            Option('unknown', help='Print unknown files.'),
 
2506
            Option('unknown', short_name='u',
 
2507
                help='Print unknown files.'),
2509
2508
            Option('versioned', help='Print versioned files.',
2510
2509
                   short_name='V'),
2511
 
            Option('ignored', help='Print ignored files.'),
2512
 
            Option('null',
2513
 
                   help='Write an ascii NUL (\\0) separator '
2514
 
                   'between files rather than a newline.'),
2515
 
            Option('kind',
 
2510
            Option('ignored', short_name='i',
 
2511
                help='Print ignored files.'),
 
2512
            Option('kind', short_name='k',
2516
2513
                   help='List entries of a particular kind: file, directory, symlink.',
2517
2514
                   type=unicode),
 
2515
            'null',
2518
2516
            'show-ids',
 
2517
            'directory',
2519
2518
            ]
2520
2519
    @display_command
2521
2520
    def run(self, revision=None, verbose=False,
2522
2521
            recursive=False, from_root=False,
2523
2522
            unknown=False, versioned=False, ignored=False,
2524
 
            null=False, kind=None, show_ids=False, path=None):
 
2523
            null=False, kind=None, show_ids=False, path=None, directory=None):
2525
2524
 
2526
2525
        if kind and kind not in ('file', 'directory', 'symlink'):
2527
2526
            raise errors.BzrCommandError('invalid kind specified')
2539
2538
                raise errors.BzrCommandError('cannot specify both --from-root'
2540
2539
                                             ' and PATH')
2541
2540
            fs_path = path
2542
 
        tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch(
2543
 
            fs_path)
 
2541
        tree, branch, relpath = \
 
2542
            _open_directory_or_containing_tree_or_branch(fs_path, directory)
2544
2543
 
2545
2544
        # Calculate the prefix to use
2546
2545
        prefix = None
2614
2613
 
2615
2614
    hidden = True
2616
2615
    _see_also = ['ls']
 
2616
    takes_options = ['directory']
2617
2617
 
2618
2618
    @display_command
2619
 
    def run(self):
2620
 
        for f in WorkingTree.open_containing(u'.')[0].unknowns():
 
2619
    def run(self, directory=u'.'):
 
2620
        for f in WorkingTree.open_containing(directory)[0].unknowns():
2621
2621
            self.outf.write(osutils.quotefn(f) + '\n')
2622
2622
 
2623
2623
 
2688
2688
 
2689
2689
    _see_also = ['status', 'ignored', 'patterns']
2690
2690
    takes_args = ['name_pattern*']
2691
 
    takes_options = [
 
2691
    takes_options = ['directory',
2692
2692
        Option('default-rules',
2693
2693
               help='Display the default ignore rules that bzr uses.')
2694
2694
        ]
2695
2695
 
2696
 
    def run(self, name_pattern_list=None, default_rules=None):
 
2696
    def run(self, name_pattern_list=None, default_rules=None,
 
2697
            directory=u'.'):
2697
2698
        from bzrlib import ignores
2698
2699
        if default_rules is not None:
2699
2700
            # dump the default rules and exit
2710
2711
                (len(name_pattern) > 1 and name_pattern[1] == ':')):
2711
2712
                raise errors.BzrCommandError(
2712
2713
                    "NAME_PATTERN should not be an absolute path")
2713
 
        tree, relpath = WorkingTree.open_containing(u'.')
 
2714
        tree, relpath = WorkingTree.open_containing(directory)
2714
2715
        ignores.tree_ignores_add_patterns(tree, name_pattern_list)
2715
2716
        ignored = globbing.Globster(name_pattern_list)
2716
2717
        matches = []
2742
2743
 
2743
2744
    encoding_type = 'replace'
2744
2745
    _see_also = ['ignore', 'ls']
 
2746
    takes_options = ['directory']
2745
2747
 
2746
2748
    @display_command
2747
 
    def run(self):
2748
 
        tree = WorkingTree.open_containing(u'.')[0]
 
2749
    def run(self, directory=u'.'):
 
2750
        tree = WorkingTree.open_containing(directory)[0]
2749
2751
        self.add_cleanup(tree.lock_read().unlock)
2750
2752
        for path, file_class, kind, file_id, entry in tree.list_files():
2751
2753
            if file_class != 'I':
2763
2765
    """
2764
2766
    hidden = True
2765
2767
    takes_args = ['revno']
 
2768
    takes_options = ['directory']
2766
2769
 
2767
2770
    @display_command
2768
 
    def run(self, revno):
 
2771
    def run(self, revno, directory=u'.'):
2769
2772
        try:
2770
2773
            revno = int(revno)
2771
2774
        except ValueError:
2772
2775
            raise errors.BzrCommandError("not a valid revision-number: %r"
2773
2776
                                         % revno)
2774
 
        revid = WorkingTree.open_containing(u'.')[0].branch.get_rev_id(revno)
 
2777
        revid = WorkingTree.open_containing(directory)[0].branch.get_rev_id(revno)
2775
2778
        self.outf.write("%s\n" % revid)
2776
2779
 
2777
2780
 
2804
2807
      =================       =========================
2805
2808
    """
2806
2809
    takes_args = ['dest', 'branch_or_subdir?']
2807
 
    takes_options = [
 
2810
    takes_options = ['directory',
2808
2811
        Option('format',
2809
2812
               help="Type of file to export to.",
2810
2813
               type=unicode),
2819
2822
                    'revision in which it was changed.'),
2820
2823
        ]
2821
2824
    def run(self, dest, branch_or_subdir=None, revision=None, format=None,
2822
 
        root=None, filters=False, per_file_timestamps=False):
 
2825
        root=None, filters=False, per_file_timestamps=False, directory=u'.'):
2823
2826
        from bzrlib.export import export
2824
2827
 
2825
2828
        if branch_or_subdir is None:
2826
 
            tree = WorkingTree.open_containing(u'.')[0]
 
2829
            tree = WorkingTree.open_containing(directory)[0]
2827
2830
            b = tree.branch
2828
2831
            subdir = None
2829
2832
        else:
2848
2851
    """
2849
2852
 
2850
2853
    _see_also = ['ls']
2851
 
    takes_options = [
 
2854
    takes_options = ['directory',
2852
2855
        Option('name-from-revision', help='The path name in the old tree.'),
2853
2856
        Option('filters', help='Apply content filters to display the '
2854
2857
                'convenience form.'),
2859
2862
 
2860
2863
    @display_command
2861
2864
    def run(self, filename, revision=None, name_from_revision=False,
2862
 
            filters=False):
 
2865
            filters=False, directory=None):
2863
2866
        if revision is not None and len(revision) != 1:
2864
2867
            raise errors.BzrCommandError("bzr cat --revision takes exactly"
2865
2868
                                         " one revision specifier")
2866
2869
        tree, branch, relpath = \
2867
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
2870
            _open_directory_or_containing_tree_or_branch(filename, directory)
2868
2871
        self.add_cleanup(branch.lock_read().unlock)
2869
2872
        return self._run(tree, branch, relpath, filename, revision,
2870
2873
                         name_from_revision, filters)
3353
3356
 
3354
3357
    _see_also = ['info']
3355
3358
    takes_args = ['nickname?']
3356
 
    def run(self, nickname=None):
3357
 
        branch = Branch.open_containing(u'.')[0]
 
3359
    takes_options = ['directory']
 
3360
    def run(self, nickname=None, directory=u'.'):
 
3361
        branch = Branch.open_containing(directory)[0]
3358
3362
        if nickname is None:
3359
3363
            self.printme(branch)
3360
3364
        else:
3585
3589
            self.additional_selftest_args['runner_class'] = SubUnitBzrRunner
3586
3590
            # On Windows, disable automatic conversion of '\n' to '\r\n' in
3587
3591
            # stdout, which would corrupt the subunit stream. 
3588
 
            if sys.platform == "win32" and sys.stdout.fileno() >= 0:
 
3592
            # FIXME: This has been fixed in subunit trunk (>0.0.5) so the
 
3593
            # following code can be deleted when it's sufficiently deployed
 
3594
            # -- vila/mgz 20100514
 
3595
            if (sys.platform == "win32"
 
3596
                and getattr(sys.stdout, 'fileno', None) is not None):
3589
3597
                import msvcrt
3590
3598
                msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
3591
3599
        if parallel:
3764
3772
                ' completely merged into the source, pull from the'
3765
3773
                ' source rather than merging.  When this happens,'
3766
3774
                ' you do not need to commit the result.'),
3767
 
        Option('directory',
 
3775
        custom_help('directory',
3768
3776
               help='Branch to merge into, '
3769
 
                    'rather than the one containing the working directory.',
3770
 
               short_name='d',
3771
 
               type=unicode,
3772
 
               ),
 
3777
                    'rather than the one containing the working directory.'),
3773
3778
        Option('preview', help='Instead of merging, show a diff of the'
3774
3779
               ' merge.'),
3775
3780
        Option('interactive', help='Select changes interactively.',
4542
4547
                     Option('long', help='Show commit date in annotations.'),
4543
4548
                     'revision',
4544
4549
                     'show-ids',
 
4550
                     'directory',
4545
4551
                     ]
4546
4552
    encoding_type = 'exact'
4547
4553
 
4548
4554
    @display_command
4549
4555
    def run(self, filename, all=False, long=False, revision=None,
4550
 
            show_ids=False):
 
4556
            show_ids=False, directory=None):
4551
4557
        from bzrlib.annotate import annotate_file, annotate_file_tree
4552
4558
        wt, branch, relpath = \
4553
 
            bzrdir.BzrDir.open_containing_tree_or_branch(filename)
 
4559
            _open_directory_or_containing_tree_or_branch(filename, directory)
4554
4560
        if wt is not None:
4555
4561
            self.add_cleanup(wt.lock_read().unlock)
4556
4562
        else:
4580
4586
 
4581
4587
    hidden = True # is this right ?
4582
4588
    takes_args = ['revision_id*']
4583
 
    takes_options = ['revision']
 
4589
    takes_options = ['directory', 'revision']
4584
4590
 
4585
 
    def run(self, revision_id_list=None, revision=None):
 
4591
    def run(self, revision_id_list=None, revision=None, directory=u'.'):
4586
4592
        if revision_id_list is not None and revision is not None:
4587
4593
            raise errors.BzrCommandError('You can only supply one of revision_id or --revision')
4588
4594
        if revision_id_list is None and revision is None:
4589
4595
            raise errors.BzrCommandError('You must supply either --revision or a revision_id')
4590
 
        b = WorkingTree.open_containing(u'.')[0].branch
 
4596
        b = WorkingTree.open_containing(directory)[0].branch
4591
4597
        self.add_cleanup(b.lock_write().unlock)
4592
4598
        return self._run(b, revision_id_list, revision)
4593
4599
 
4653
4659
 
4654
4660
    _see_also = ['checkouts', 'unbind']
4655
4661
    takes_args = ['location?']
4656
 
    takes_options = []
 
4662
    takes_options = ['directory']
4657
4663
 
4658
 
    def run(self, location=None):
4659
 
        b, relpath = Branch.open_containing(u'.')
 
4664
    def run(self, location=None, directory=u'.'):
 
4665
        b, relpath = Branch.open_containing(directory)
4660
4666
        if location is None:
4661
4667
            try:
4662
4668
                location = b.get_old_bound_location()
4689
4695
 
4690
4696
    _see_also = ['checkouts', 'bind']
4691
4697
    takes_args = []
4692
 
    takes_options = []
 
4698
    takes_options = ['directory']
4693
4699
 
4694
 
    def run(self):
4695
 
        b, relpath = Branch.open_containing(u'.')
 
4700
    def run(self, directory=u'.'):
 
4701
        b, relpath = Branch.open_containing(directory)
4696
4702
        if not b.unbind():
4697
4703
            raise errors.BzrCommandError('Local branch is not bound')
4698
4704
 
4860
4866
                    'result in a dynamically allocated port.  The default port '
4861
4867
                    'depends on the protocol.',
4862
4868
               type=str),
4863
 
        Option('directory',
4864
 
               help='Serve contents of this directory.',
4865
 
               type=unicode),
 
4869
        custom_help('directory',
 
4870
               help='Serve contents of this directory.'),
4866
4871
        Option('allow-writes',
4867
4872
               help='By default the server is a readonly server.  Supplying '
4868
4873
                    '--allow-writes enables write access to the contents of '
5297
5302
        Option('delete',
5298
5303
            help='Delete this tag rather than placing it.',
5299
5304
            ),
5300
 
        Option('directory',
5301
 
            help='Branch in which to place the tag.',
5302
 
            short_name='d',
5303
 
            type=unicode,
5304
 
            ),
 
5305
        custom_help('directory',
 
5306
            help='Branch in which to place the tag.'),
5305
5307
        Option('force',
5306
5308
            help='Replace existing tags.',
5307
5309
            ),
5349
5351
 
5350
5352
    _see_also = ['tag']
5351
5353
    takes_options = [
5352
 
        Option('directory',
5353
 
            help='Branch whose tags should be displayed.',
5354
 
            short_name='d',
5355
 
            type=unicode,
5356
 
            ),
 
5354
        custom_help('directory',
 
5355
            help='Branch whose tags should be displayed.'),
5357
5356
        RegistryOption.from_kwargs('sort',
5358
5357
            'Sort tags by different criteria.', title='Sorting',
5359
5358
            alpha='Sort tags lexicographically (default).',
5921
5920
 
5922
5921
    To check what clean-tree will do, use --dry-run.
5923
5922
    """
5924
 
    takes_options = [Option('ignored', help='Delete all ignored files.'),
 
5923
    takes_options = ['directory',
 
5924
                     Option('ignored', help='Delete all ignored files.'),
5925
5925
                     Option('detritus', help='Delete conflict files, merge'
5926
5926
                            ' backups, and failed selftest dirs.'),
5927
5927
                     Option('unknown',
5930
5930
                            ' deleting them.'),
5931
5931
                     Option('force', help='Do not prompt before deleting.')]
5932
5932
    def run(self, unknown=False, ignored=False, detritus=False, dry_run=False,
5933
 
            force=False):
 
5933
            force=False, directory=u'.'):
5934
5934
        from bzrlib.clean_tree import clean_tree
5935
5935
        if not (unknown or ignored or detritus):
5936
5936
            unknown = True
5937
5937
        if dry_run:
5938
5938
            force = True
5939
 
        clean_tree('.', unknown=unknown, ignored=ignored, detritus=detritus,
5940
 
                   dry_run=dry_run, no_prompt=force)
 
5939
        clean_tree(directory, unknown=unknown, ignored=ignored,
 
5940
                   detritus=detritus, dry_run=dry_run, no_prompt=force)
5941
5941
 
5942
5942
 
5943
5943
class cmd_reference(Command):