/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/builtins.py

  • Committer: Jelmer Vernooij
  • Date: 2019-03-04 05:10:44 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7294.
  • Revision ID: jelmer@jelmer.uk-20190304051044-vph4s8p9qvpy2qe9
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
    symbol_versioning,
55
55
    timestamp,
56
56
    transport,
 
57
    tree as _mod_tree,
57
58
    ui,
58
59
    urlutils,
59
60
    views,
205
206
    try:
206
207
        reference = control_dir.get_branch_reference()
207
208
    except errors.NotBranchError:
208
 
        # There is no active branch, just return the colocated branches.
209
 
        for name, branch in viewitems(control_dir.get_branches()):
210
 
            yield name, branch
211
 
        return
 
209
        reference = None
212
210
    if reference is not None:
213
 
        ref_branch = Branch.open(
214
 
            reference, possible_transports=possible_transports)
 
211
        try:
 
212
            ref_branch = Branch.open(
 
213
                reference, possible_transports=possible_transports)
 
214
        except errors.NotBranchError:
 
215
            ref_branch = None
215
216
    else:
216
217
        ref_branch = None
217
218
    if ref_branch is None or ref_branch.name:
1479
1480
            revision_id = br_from.last_revision()
1480
1481
        if to_location is None:
1481
1482
            to_location = urlutils.derive_to_location(from_location)
1482
 
        to_transport = transport.get_transport(to_location)
 
1483
        to_transport = transport.get_transport(to_location, purpose='write')
1483
1484
        try:
1484
1485
            to_transport.mkdir('.')
1485
1486
        except errors.FileExists:
1573
1574
 
1574
1575
    def run(self, location=".", recursive=False):
1575
1576
        if recursive:
1576
 
            t = transport.get_transport(location)
 
1577
            t = transport.get_transport(location, purpose='read')
1577
1578
            if not t.listable():
1578
1579
                raise errors.BzrCommandError(
1579
1580
                    "Can't scan this type of location.")
1922
1923
                    force=(file_deletion_strategy == 'no-backup'))
1923
1924
 
1924
1925
 
1925
 
class cmd_file_id(Command):
1926
 
    __doc__ = """Print file_id of a particular file or directory.
1927
 
 
1928
 
    The file_id is assigned when the file is first added and remains the
1929
 
    same through all revisions where the file exists, even when it is
1930
 
    moved or renamed.
1931
 
    """
1932
 
 
1933
 
    hidden = True
1934
 
    _see_also = ['inventory', 'ls']
1935
 
    takes_args = ['filename']
1936
 
 
1937
 
    @display_command
1938
 
    def run(self, filename):
1939
 
        tree, relpath = WorkingTree.open_containing(filename)
1940
 
        file_id = tree.path2id(relpath)
1941
 
        if file_id is None:
1942
 
            raise errors.NotVersionedError(filename)
1943
 
        else:
1944
 
            self.outf.write(file_id.decode('utf-8') + '\n')
1945
 
 
1946
 
 
1947
 
class cmd_file_path(Command):
1948
 
    __doc__ = """Print path of file_ids to a file or directory.
1949
 
 
1950
 
    This prints one line for each directory down to the target,
1951
 
    starting at the branch root.
1952
 
    """
1953
 
 
1954
 
    hidden = True
1955
 
    takes_args = ['filename']
1956
 
 
1957
 
    @display_command
1958
 
    def run(self, filename):
1959
 
        tree, relpath = WorkingTree.open_containing(filename)
1960
 
        fid = tree.path2id(relpath)
1961
 
        if fid is None:
1962
 
            raise errors.NotVersionedError(filename)
1963
 
        segments = osutils.splitpath(relpath)
1964
 
        for pos in range(1, len(segments) + 1):
1965
 
            path = osutils.joinpath(segments[:pos])
1966
 
            self.outf.write("%s\n" % tree.path2id(path))
1967
 
 
1968
 
 
1969
1926
class cmd_reconcile(Command):
1970
1927
    __doc__ = """Reconcile brz metadata in a branch.
1971
1928
 
2101
2058
        if location is None:
2102
2059
            location = u'.'
2103
2060
 
2104
 
        to_transport = transport.get_transport(location)
 
2061
        to_transport = transport.get_transport(location, purpose='write')
2105
2062
 
2106
2063
        # The path has to exist to initialize a
2107
2064
        # branch inside of it.
2221
2178
        if location is None:
2222
2179
            location = '.'
2223
2180
 
2224
 
        to_transport = transport.get_transport(location)
 
2181
        to_transport = transport.get_transport(location, purpose='write')
2225
2182
 
2226
2183
        if format.fixed_components:
2227
2184
            repo_format_name = None
2485
2442
class cmd_root(Command):
2486
2443
    __doc__ = """Show the tree root directory.
2487
2444
 
2488
 
    The root is the nearest enclosing directory with a .bzr control
 
2445
    The root is the nearest enclosing directory with a control
2489
2446
    directory."""
2490
2447
 
2491
2448
    takes_args = ['filename?']
3058
3015
                note(gettext("Ignoring files outside view. View is %s") % view_str)
3059
3016
 
3060
3017
        self.add_cleanup(tree.lock_read().unlock)
3061
 
        for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
3062
 
                                                         from_dir=relpath, recursive=recursive):
 
3018
        for fp, fc, fkind, entry in tree.list_files(
 
3019
                include_root=False, from_dir=relpath, recursive=recursive):
3063
3020
            # Apply additional masking
3064
3021
            if not all and not selection[fc]:
3065
3022
                continue
3083
3040
            ui.ui_factory.clear_term()
3084
3041
            if verbose:
3085
3042
                outstring = '%-8s %s' % (fc, outstring)
3086
 
                if show_ids and fid is not None:
3087
 
                    outstring = "%-50s %s" % (outstring, fid.decode('utf-8'))
 
3043
                if show_ids and getattr(entry, 'file_id', None) is not None:
 
3044
                    outstring = "%-50s %s" % (outstring, entry.file_id.decode('utf-8'))
3088
3045
                self.outf.write(outstring + '\n')
3089
3046
            elif null:
3090
3047
                self.outf.write(fp + '\0')
3091
3048
                if show_ids:
3092
 
                    if fid is not None:
3093
 
                        self.outf.write(fid.decode('utf-8'))
 
3049
                    if getattr(entry, 'file_id', None) is not None:
 
3050
                        self.outf.write(entry.file_id.decode('utf-8'))
3094
3051
                    self.outf.write('\0')
3095
3052
                self.outf.flush()
3096
3053
            else:
3097
3054
                if show_ids:
3098
 
                    if fid is not None:
3099
 
                        my_id = fid.decode('utf-8')
 
3055
                    if getattr(entry, 'file_id', None) is not None:
 
3056
                        my_id = entry.file_id.decode('utf-8')
3100
3057
                    else:
3101
3058
                        my_id = ''
3102
3059
                    self.outf.write('%-50s %s\n' % (outstring, my_id))
3234
3191
        ignored = globbing.Globster(name_pattern_list)
3235
3192
        matches = []
3236
3193
        self.add_cleanup(tree.lock_read().unlock)
3237
 
        for entry in tree.list_files():
3238
 
            id = entry[3]
 
3194
        for filename, fc, fkind, entry in tree.list_files():
 
3195
            id = getattr(entry, 'file_id', None)
3239
3196
            if id is not None:
3240
 
                filename = entry[0]
3241
3197
                if ignored.match(filename):
3242
3198
                    matches.append(filename)
3243
3199
        if len(matches) > 0:
3266
3222
    def run(self, directory=u'.'):
3267
3223
        tree = WorkingTree.open_containing(directory)[0]
3268
3224
        self.add_cleanup(tree.lock_read().unlock)
3269
 
        for path, file_class, kind, file_id, entry in tree.list_files():
 
3225
        for path, file_class, kind, entry in tree.list_files():
3270
3226
            if file_class != 'I':
3271
3227
                continue
3272
3228
            # XXX: Slightly inefficient since this was already calculated
3432
3388
        rev_tree = _get_one_revision_tree('cat', revision, branch=b)
3433
3389
        self.add_cleanup(rev_tree.lock_read().unlock)
3434
3390
 
3435
 
        old_file_id = rev_tree.path2id(relpath)
3436
 
 
3437
 
        # TODO: Split out this code to something that generically finds the
3438
 
        # best id for a path across one or more trees; it's like
3439
 
        # find_ids_across_trees but restricted to find just one. -- mbp
3440
 
        # 20110705.
3441
3391
        if name_from_revision:
3442
3392
            # Try in revision if requested
3443
 
            if old_file_id is None:
 
3393
            if not rev_tree.is_versioned(relpath):
3444
3394
                raise errors.BzrCommandError(gettext(
3445
3395
                    "{0!r} is not present in revision {1}").format(
3446
3396
                        filename, rev_tree.get_revision_id()))
3447
 
            else:
3448
 
                actual_file_id = old_file_id
 
3397
            rev_tree_path = relpath
3449
3398
        else:
3450
 
            cur_file_id = tree.path2id(relpath)
3451
 
            if cur_file_id is not None and rev_tree.has_id(cur_file_id):
3452
 
                actual_file_id = cur_file_id
3453
 
            elif old_file_id is not None:
3454
 
                actual_file_id = old_file_id
3455
 
            else:
3456
 
                raise errors.BzrCommandError(gettext(
3457
 
                    "{0!r} is not present in revision {1}").format(
3458
 
                        filename, rev_tree.get_revision_id()))
3459
 
        relpath = rev_tree.id2path(actual_file_id)
 
3399
            try:
 
3400
                rev_tree_path = _mod_tree.find_previous_path(
 
3401
                    tree, rev_tree, relpath)
 
3402
            except errors.NoSuchFile:
 
3403
                rev_tree_path = None
 
3404
 
 
3405
            if rev_tree_path is None:
 
3406
                # Path didn't exist in working tree
 
3407
                if not rev_tree.is_versioned(relpath):
 
3408
                    raise errors.BzrCommandError(gettext(
 
3409
                        "{0!r} is not present in revision {1}").format(
 
3410
                            filename, rev_tree.get_revision_id()))
 
3411
                else:
 
3412
                    # Fall back to the same path in the basis tree, if present.
 
3413
                    rev_tree_path = relpath
 
3414
 
3460
3415
        if filtered:
3461
3416
            from .filter_tree import ContentFilterTree
3462
3417
            filter_tree = ContentFilterTree(
3463
3418
                rev_tree, rev_tree._content_filter_stack)
3464
 
            fileobj = filter_tree.get_file(relpath)
 
3419
            fileobj = filter_tree.get_file(rev_tree_path)
3465
3420
        else:
3466
 
            fileobj = rev_tree.get_file(relpath)
 
3421
            fileobj = rev_tree.get_file(rev_tree_path)
3467
3422
        shutil.copyfileobj(fileobj, self.outf)
3468
3423
        self.cleanup_now()
3469
3424
 
3524
3479
 
3525
3480
    :Things to note:
3526
3481
 
3527
 
      If you accidentially commit the wrong changes or make a spelling
 
3482
      If you accidentally commit the wrong changes or make a spelling
3528
3483
      mistake in the commit message say, you can use the uncommit command
3529
3484
      to undo it. See ``brz help uncommit`` for details.
3530
3485
 
3857
3812
    tried.
3858
3813
 
3859
3814
    For more information on upgrades, see the Bazaar Upgrade Guide,
3860
 
    http://doc.bazaar.canonical.com/latest/en/upgrade-guide/.
 
3815
    https://www.breezy-vcs.org/doc/en/upgrade-guide/.
3861
3816
    """
3862
3817
 
3863
3818
    _see_also = ['check', 'reconcile', 'formats']
4843
4798
    update command.
4844
4799
 
4845
4800
    Uncommitted changes to files that are reverted will be discarded.
4846
 
    Howver, by default, any files that have been manually changed will be
 
4801
    However, by default, any files that have been manually changed will be
4847
4802
    backed up first.  (Files changed only by merge are not backed up.)  Backup
4848
4803
    files have '.~#~' appended to their name, where # is a number.
4849
4804
 
5211
5166
 
5212
5167
    @display_command
5213
5168
    def run(self, branch=u'.', revision=None, long=False, strict=False):
5214
 
        from .testament import Testament, StrictTestament
 
5169
        from .bzr.testament import Testament, StrictTestament
5215
5170
        if strict is True:
5216
5171
            testament_class = StrictTestament
5217
5172
        else:
5269
5224
        tree = _get_one_revision_tree('annotate', revision, branch=branch)
5270
5225
        self.add_cleanup(tree.lock_read().unlock)
5271
5226
        if wt is not None and revision is None:
5272
 
            file_id = wt.path2id(relpath)
5273
 
        else:
5274
 
            file_id = tree.path2id(relpath)
5275
 
        if file_id is None:
5276
 
            raise errors.NotVersionedError(filename)
5277
 
        if wt is not None and revision is None:
 
5227
            if not wt.is_versioned(relpath):
 
5228
                raise errors.NotVersionedError(relpath)
5278
5229
            # If there is a tree and we're not annotating historical
5279
5230
            # versions, annotate the working tree's content.
5280
5231
            annotate_file_tree(wt, relpath, self.outf, long, all,
5281
5232
                               show_ids=show_ids)
5282
5233
        else:
 
5234
            if not tree.is_versioned(relpath):
 
5235
                raise errors.NotVersionedError(relpath)
5283
5236
            annotate_file_tree(tree, relpath, self.outf, long, all,
5284
5237
                               show_ids=show_ids, branch=branch)
5285
5238
 
5626
5579
 
5627
5580
    def run(self, listen=None, port=None, inet=False, directory=None,
5628
5581
            allow_writes=False, protocol=None, client_timeout=None):
5629
 
        from . import transport
 
5582
        from . import location, transport
5630
5583
        if directory is None:
5631
5584
            directory = osutils.getcwd()
5632
5585
        if protocol is None:
5633
5586
            protocol = transport.transport_server_registry.get()
5634
 
        url = transport.location_to_url(directory)
 
5587
        url = location.location_to_url(directory)
5635
5588
        if not allow_writes:
5636
5589
            url = 'readonly+' + url
5637
5590
        t = transport.get_transport_from_url(url)
6365
6318
        if had_explicit_nick:
6366
6319
            branch = control_dir.open_branch()  # get the new branch!
6367
6320
            branch.nick = to_branch.nick
6368
 
        note(gettext('Switched to branch: %s'),
6369
 
             urlutils.unescape_for_display(to_branch.base, 'utf-8'))
 
6321
        if to_branch.name:
 
6322
            if to_branch.controldir.control_url != control_dir.control_url:
 
6323
                note(gettext('Switched to branch %s at %s'),
 
6324
                     to_branch.name, urlutils.unescape_for_display(to_branch.base, 'utf-8'))
 
6325
            else:
 
6326
                note(gettext('Switched to branch %s'), to_branch.name)
 
6327
        else:
 
6328
            note(gettext('Switched to branch at %s'),
 
6329
                 urlutils.unescape_for_display(to_branch.base, 'utf-8'))
6370
6330
 
6371
6331
 
6372
6332
class cmd_view(Command):
6871
6831
            cmd_reconcile().run(".")
6872
6832
 
6873
6833
 
 
6834
class cmd_grep(Command):
 
6835
    """Print lines matching PATTERN for specified files and revisions.
 
6836
 
 
6837
    This command searches the specified files and revisions for a given
 
6838
    pattern.  The pattern is specified as a Python regular expressions[1].
 
6839
 
 
6840
    If the file name is not specified, the revisions starting with the
 
6841
    current directory are searched recursively. If the revision number is
 
6842
    not specified, the working copy is searched. To search the last committed
 
6843
    revision, use the '-r -1' or '-r last:1' option.
 
6844
 
 
6845
    Unversioned files are not searched unless explicitly specified on the
 
6846
    command line. Unversioned directores are not searched.
 
6847
 
 
6848
    When searching a pattern, the output is shown in the 'filepath:string'
 
6849
    format. If a revision is explicitly searched, the output is shown as
 
6850
    'filepath~N:string', where N is the revision number.
 
6851
 
 
6852
    --include and --exclude options can be used to search only (or exclude
 
6853
    from search) files with base name matches the specified Unix style GLOB
 
6854
    pattern.  The GLOB pattern an use *, ?, and [...] as wildcards, and \\
 
6855
    to quote wildcard or backslash character literally. Note that the glob
 
6856
    pattern is not a regular expression.
 
6857
 
 
6858
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
 
6859
    """
 
6860
 
 
6861
    encoding_type = 'replace'
 
6862
    takes_args = ['pattern', 'path*']
 
6863
    takes_options = [
 
6864
        'verbose',
 
6865
        'revision',
 
6866
        Option('color', type=text_type, argname='when',
 
6867
               help='Show match in color. WHEN is never, always or auto.'),
 
6868
        Option('diff', short_name='p',
 
6869
               help='Grep for pattern in changeset for each revision.'),
 
6870
        ListOption('exclude', type=text_type, argname='glob', short_name='X',
 
6871
                   help="Skip files whose base name matches GLOB."),
 
6872
        ListOption('include', type=text_type, argname='glob', short_name='I',
 
6873
                   help="Search only files whose base name matches GLOB."),
 
6874
        Option('files-with-matches', short_name='l',
 
6875
               help='Print only the name of each input file in '
 
6876
               'which PATTERN is found.'),
 
6877
        Option('files-without-match', short_name='L',
 
6878
               help='Print only the name of each input file in '
 
6879
               'which PATTERN is not found.'),
 
6880
        Option('fixed-string', short_name='F',
 
6881
               help='Interpret PATTERN is a single fixed string (not regex).'),
 
6882
        Option('from-root',
 
6883
               help='Search for pattern starting from the root of the branch. '
 
6884
               '(implies --recursive)'),
 
6885
        Option('ignore-case', short_name='i',
 
6886
               help='Ignore case distinctions while matching.'),
 
6887
        Option('levels',
 
6888
               help='Number of levels to display - 0 for all, 1 for collapsed '
 
6889
               '(1 is default).',
 
6890
               argname='N',
 
6891
               type=_parse_levels),
 
6892
        Option('line-number', short_name='n',
 
6893
               help='Show 1-based line number.'),
 
6894
        Option('no-recursive',
 
6895
               help="Don't recurse into subdirectories. (default is --recursive)"),
 
6896
        Option('null', short_name='Z',
 
6897
               help='Write an ASCII NUL (\\0) separator '
 
6898
               'between output lines rather than a newline.'),
 
6899
        ]
 
6900
 
 
6901
    @display_command
 
6902
    def run(self, verbose=False, ignore_case=False, no_recursive=False,
 
6903
            from_root=False, null=False, levels=None, line_number=False,
 
6904
            path_list=None, revision=None, pattern=None, include=None,
 
6905
            exclude=None, fixed_string=False, files_with_matches=False,
 
6906
            files_without_match=False, color=None, diff=False):
 
6907
        from breezy import _termcolor
 
6908
        from . import grep
 
6909
        import re
 
6910
        if path_list is None:
 
6911
            path_list = ['.']
 
6912
        else:
 
6913
            if from_root:
 
6914
                raise errors.BzrCommandError(
 
6915
                    'cannot specify both --from-root and PATH.')
 
6916
 
 
6917
        if files_with_matches and files_without_match:
 
6918
            raise errors.BzrCommandError(
 
6919
                'cannot specify both '
 
6920
                '-l/--files-with-matches and -L/--files-without-matches.')
 
6921
 
 
6922
        global_config = _mod_config.GlobalConfig()
 
6923
 
 
6924
        if color is None:
 
6925
            color = global_config.get_user_option('grep_color')
 
6926
 
 
6927
        if color is None:
 
6928
            color = 'never'
 
6929
 
 
6930
        if color not in ['always', 'never', 'auto']:
 
6931
            raise errors.BzrCommandError('Valid values for --color are '
 
6932
                                         '"always", "never" or "auto".')
 
6933
 
 
6934
        if levels is None:
 
6935
            levels = 1
 
6936
 
 
6937
        print_revno = False
 
6938
        if revision is not None or levels == 0:
 
6939
            # print revision numbers as we may be showing multiple revisions
 
6940
            print_revno = True
 
6941
 
 
6942
        eol_marker = '\n'
 
6943
        if null:
 
6944
            eol_marker = '\0'
 
6945
 
 
6946
        if not ignore_case and grep.is_fixed_string(pattern):
 
6947
            # if the pattern isalnum, implicitly use to -F for faster grep
 
6948
            fixed_string = True
 
6949
        elif ignore_case and fixed_string:
 
6950
            # GZ 2010-06-02: Fall back to regexp rather than lowercasing
 
6951
            #                pattern and text which will cause pain later
 
6952
            fixed_string = False
 
6953
            pattern = re.escape(pattern)
 
6954
 
 
6955
        patternc = None
 
6956
        re_flags = re.MULTILINE
 
6957
        if ignore_case:
 
6958
            re_flags |= re.IGNORECASE
 
6959
 
 
6960
        if not fixed_string:
 
6961
            patternc = grep.compile_pattern(
 
6962
                pattern.encode(grep._user_encoding), re_flags)
 
6963
 
 
6964
        if color == 'always':
 
6965
            show_color = True
 
6966
        elif color == 'never':
 
6967
            show_color = False
 
6968
        elif color == 'auto':
 
6969
            show_color = _termcolor.allow_color()
 
6970
 
 
6971
        opts = grep.GrepOptions()
 
6972
 
 
6973
        opts.verbose = verbose
 
6974
        opts.ignore_case = ignore_case
 
6975
        opts.no_recursive = no_recursive
 
6976
        opts.from_root = from_root
 
6977
        opts.null = null
 
6978
        opts.levels = levels
 
6979
        opts.line_number = line_number
 
6980
        opts.path_list = path_list
 
6981
        opts.revision = revision
 
6982
        opts.pattern = pattern
 
6983
        opts.include = include
 
6984
        opts.exclude = exclude
 
6985
        opts.fixed_string = fixed_string
 
6986
        opts.files_with_matches = files_with_matches
 
6987
        opts.files_without_match = files_without_match
 
6988
        opts.color = color
 
6989
        opts.diff = False
 
6990
 
 
6991
        opts.eol_marker = eol_marker
 
6992
        opts.print_revno = print_revno
 
6993
        opts.patternc = patternc
 
6994
        opts.recursive = not no_recursive
 
6995
        opts.fixed_string = fixed_string
 
6996
        opts.outf = self.outf
 
6997
        opts.show_color = show_color
 
6998
 
 
6999
        if diff:
 
7000
            # options not used:
 
7001
            # files_with_matches, files_without_match
 
7002
            # levels(?), line_number, from_root
 
7003
            # include, exclude
 
7004
            # These are silently ignored.
 
7005
            grep.grep_diff(opts)
 
7006
        elif revision is None:
 
7007
            grep.workingtree_grep(opts)
 
7008
        else:
 
7009
            grep.versioned_grep(opts)
 
7010
 
 
7011
 
6874
7012
def _register_lazy_builtins():
6875
7013
    # register lazy builtins from other modules; called at startup and should
6876
7014
    # be only called once.
6879
7017
            ('cmd_bundle_info', [], 'breezy.bundle.commands'),
6880
7018
            ('cmd_config', [], 'breezy.config'),
6881
7019
            ('cmd_dump_btree', [], 'breezy.bzr.debug_commands'),
 
7020
            ('cmd_file_id', [], 'breezy.bzr.debug_commands'),
 
7021
            ('cmd_file_path', [], 'breezy.bzr.debug_commands'),
6882
7022
            ('cmd_version_info', [], 'breezy.cmd_version_info'),
6883
7023
            ('cmd_resolve', ['resolved'], 'breezy.conflicts'),
6884
7024
            ('cmd_conflicts', [], 'breezy.conflicts'),