~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/builtins.py

(vila) Provide a config section matcher respecting the file order. (Vincent
 Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
    )
84
84
 
85
85
 
86
 
def _get_branch_location(control_dir, possible_transports=None):
87
 
    """Return location of branch for this control dir."""
88
 
    try:
89
 
        this_branch = control_dir.open_branch(
90
 
            possible_transports=possible_transports)
91
 
        # This may be a heavy checkout, where we want the master branch
92
 
        master_location = this_branch.get_bound_location()
93
 
        if master_location is not None:
94
 
            return master_location
95
 
        # If not, use a local sibling
96
 
        return this_branch.base
97
 
    except errors.NotBranchError:
98
 
        format = control_dir.find_branch_format()
99
 
        if getattr(format, 'get_reference', None) is not None:
100
 
            return format.get_reference(control_dir)
101
 
        else:
102
 
            return control_dir.root_transport.base
103
 
 
104
 
 
105
 
def _is_colocated(control_dir, possible_transports=None):
106
 
    """Check if the branch in control_dir is colocated.
107
 
 
108
 
    :param control_dir: Control directory
109
 
    :return: Boolean indicating whether 
110
 
    """
111
 
    # This path is meant to be relative to the existing branch
112
 
    this_url = _get_branch_location(control_dir,
113
 
        possible_transports=possible_transports)
114
 
    # Perhaps the target control dir supports colocated branches?
115
 
    try:
116
 
        root = controldir.ControlDir.open(this_url,
117
 
            possible_transports=possible_transports)
118
 
    except errors.NotBranchError:
119
 
        return (False, this_url)
120
 
    else:
121
 
        try:
122
 
            wt = control_dir.open_workingtree()
123
 
        except (errors.NoWorkingTree, errors.NotLocalUrl):
124
 
            return (False, this_url)
125
 
        else:
126
 
            return (
127
 
                root._format.colocated_branches and
128
 
                control_dir.control_url == root.control_url,
129
 
                this_url)
130
 
 
131
 
 
132
 
def lookup_new_sibling_branch(control_dir, location, possible_transports=None):
133
 
    """Lookup the location for a new sibling branch.
134
 
 
135
 
    :param control_dir: Control directory relative to which to look up
136
 
        the name.
137
 
    :param location: Name of the new branch
138
 
    :return: Full location to the new branch
139
 
    """
140
 
    location = directory_service.directories.dereference(location)
141
 
    if '/' not in location and '\\' not in location:
142
 
        (colocated, this_url) = _is_colocated(control_dir, possible_transports)
143
 
 
144
 
        if colocated:
145
 
            return urlutils.join_segment_parameters(this_url,
146
 
                {"branch": urlutils.escape(location)})
147
 
        else:
148
 
            return urlutils.join(this_url, '..', urlutils.escape(location))
149
 
    return location
150
 
 
151
 
 
152
 
def lookup_sibling_branch(control_dir, location, possible_transports=None):
153
 
    """Lookup sibling branch.
154
 
    
155
 
    :param control_dir: Control directory relative to which to lookup the
156
 
        location.
157
 
    :param location: Location to look up
158
 
    :return: branch to open
159
 
    """
160
 
    try:
161
 
        # Perhaps it's a colocated branch?
162
 
        return control_dir.open_branch(location, 
163
 
            possible_transports=possible_transports)
164
 
    except (errors.NotBranchError, errors.NoColocatedBranchSupport):
165
 
        try:
166
 
            return Branch.open(location)
167
 
        except errors.NotBranchError:
168
 
            this_url = _get_branch_location(control_dir)
169
 
            return Branch.open(
170
 
                urlutils.join(
171
 
                    this_url, '..', urlutils.escape(location)))
172
 
 
173
 
 
174
86
@symbol_versioning.deprecated_function(symbol_versioning.deprecated_in((2, 3, 0)))
175
87
def tree_files(file_list, default_branch=u'.', canonicalize=True,
176
88
    apply_view=True):
937
849
            tree = work_tree
938
850
            extra_trees = []
939
851
 
940
 
        self.add_cleanup(tree.lock_read().unlock)
941
852
        if file_list is not None:
942
853
            file_ids = tree.paths2ids(file_list, trees=extra_trees,
943
854
                                      require_versioned=True)
944
855
            # find_ids_across_trees may include some paths that don't
945
856
            # exist in 'tree'.
946
 
            entries = tree.iter_entries_by_dir(specific_file_ids=file_ids)
 
857
            entries = sorted(
 
858
                (tree.id2path(file_id), tree.inventory[file_id])
 
859
                for file_id in file_ids if tree.has_id(file_id))
947
860
        else:
948
 
            entries = tree.iter_entries_by_dir()
 
861
            entries = tree.inventory.entries()
949
862
 
950
 
        for path, entry in sorted(entries):
 
863
        self.cleanup_now()
 
864
        for path, entry in entries:
951
865
            if kind and kind != entry.kind:
952
866
                continue
953
 
            if path == "":
954
 
                continue
955
867
            if show_ids:
956
868
                self.outf.write('%-50s %s\n' % (path, entry.file_id))
957
869
            else:
1029
941
                and rel_names[0].lower() == rel_names[1].lower()):
1030
942
                into_existing = False
1031
943
            else:
 
944
                inv = tree.inventory
1032
945
                # 'fix' the case of a potential 'from'
1033
946
                from_id = tree.path2id(
1034
947
                            tree.get_canonical_inventory_path(rel_names[0]))
1035
948
                if (not osutils.lexists(names_list[0]) and
1036
 
                    from_id and tree.stored_kind(from_id) == "directory"):
 
949
                    from_id and inv.get_file_kind(from_id) == "directory"):
1037
950
                    into_existing = False
1038
951
        # move/rename
1039
952
        if into_existing:
1435
1348
            revision_id = br_from.last_revision()
1436
1349
        if to_location is None:
1437
1350
            to_location = getattr(br_from, "name", None)
1438
 
            if not to_location:
 
1351
            if to_location is None:
1439
1352
                to_location = urlutils.derive_to_location(from_location)
1440
1353
        to_transport = transport.get_transport(to_location)
1441
1354
        try:
1481
1394
                    from_location, revision)
1482
1395
                raise errors.BzrCommandError(msg)
1483
1396
        else:
1484
 
            try:
1485
 
                to_repo = to_dir.open_repository()
1486
 
            except errors.NoRepositoryPresent:
1487
 
                to_repo = to_dir.create_repository()
1488
 
            to_repo.fetch(br_from.repository, revision_id=revision_id)
1489
1397
            branch = br_from.sprout(to_dir, revision_id=revision_id)
1490
1398
        _merge_tags_if_possible(br_from, branch)
1491
1399
        # If the source branch is stacked, the new branch may
1536
1444
        else:
1537
1445
            dir = controldir.ControlDir.open_containing(location)[0]
1538
1446
            try:
1539
 
                active_branch = dir.open_branch(name="")
 
1447
                active_branch = dir.open_branch(name=None)
1540
1448
            except errors.NotBranchError:
1541
1449
                active_branch = None
1542
1450
            branches = dir.get_branches()
1543
1451
            names = {}
1544
1452
            for name, branch in branches.iteritems():
1545
 
                if name == "":
 
1453
                if name is None:
1546
1454
                    continue
1547
1455
                active = (active_branch is not None and
1548
1456
                          active_branch.base == branch.base)
2371
2279
        self.add_cleanup(tree.lock_read().unlock)
2372
2280
        old = tree.basis_tree()
2373
2281
        self.add_cleanup(old.lock_read().unlock)
2374
 
        for path, ie in old.iter_entries_by_dir():
 
2282
        for path, ie in old.inventory.iter_entries():
2375
2283
            if not tree.has_id(ie.file_id):
2376
2284
                self.outf.write(path)
2377
2285
                if show_ids:
2415
2323
        self.add_cleanup(wt.lock_read().unlock)
2416
2324
        basis = wt.basis_tree()
2417
2325
        self.add_cleanup(basis.lock_read().unlock)
2418
 
        root_id = wt.get_root_id()
2419
 
        for file_id in wt.all_file_ids():
2420
 
            if basis.has_id(file_id):
2421
 
                continue
2422
 
            if root_id == file_id:
2423
 
                continue
2424
 
            path = wt.id2path(file_id)
 
2326
        basis_inv = basis.inventory
 
2327
        inv = wt.inventory
 
2328
        for file_id in inv:
 
2329
            if basis_inv.has_id(file_id):
 
2330
                continue
 
2331
            if inv.is_root(file_id) and len(basis_inv) == 0:
 
2332
                continue
 
2333
            path = inv.id2path(file_id)
2425
2334
            if not os.access(osutils.pathjoin(wt.basedir, path), os.F_OK):
2426
2335
                continue
2427
2336
            if null:
4259
4168
    Merge will do its best to combine the changes in two branches, but there
4260
4169
    are some kinds of problems only a human can fix.  When it encounters those,
4261
4170
    it will mark a conflict.  A conflict means that you need to fix something,
4262
 
    before you can commit.
 
4171
    before you should commit.
4263
4172
 
4264
4173
    Use bzr resolve when you have fixed a problem.  See also bzr conflicts.
4265
4174
 
6219
6128
        from bzrlib import switch
6220
6129
        tree_location = directory
6221
6130
        revision = _get_one_revision('switch', revision)
6222
 
        possible_transports = []
6223
 
        control_dir = controldir.ControlDir.open_containing(tree_location,
6224
 
            possible_transports=possible_transports)[0]
 
6131
        control_dir = controldir.ControlDir.open_containing(tree_location)[0]
6225
6132
        if to_location is None:
6226
6133
            if revision is None:
6227
6134
                raise errors.BzrCommandError(gettext('You must supply either a'
6228
6135
                                             ' revision or a location'))
6229
6136
            to_location = tree_location
6230
6137
        try:
6231
 
            branch = control_dir.open_branch(
6232
 
                possible_transports=possible_transports)
 
6138
            branch = control_dir.open_branch()
6233
6139
            had_explicit_nick = branch.get_config().has_explicit_nickname()
6234
6140
        except errors.NotBranchError:
6235
6141
            branch = None
6236
6142
            had_explicit_nick = False
6237
6143
        if create_branch:
6238
6144
            if branch is None:
6239
 
                raise errors.BzrCommandError(
6240
 
                    gettext('cannot create branch without source branch'))
6241
 
            to_location = lookup_new_sibling_branch(control_dir, to_location,
6242
 
                 possible_transports=possible_transports)
 
6145
                raise errors.BzrCommandError(gettext('cannot create branch without'
 
6146
                                             ' source branch'))
 
6147
            to_location = directory_service.directories.dereference(
 
6148
                              to_location)
 
6149
            if '/' not in to_location and '\\' not in to_location:
 
6150
                # This path is meant to be relative to the existing branch
 
6151
                this_url = self._get_branch_location(control_dir)
 
6152
                # Perhaps the target control dir supports colocated branches?
 
6153
                try:
 
6154
                    root = controldir.ControlDir.open(this_url,
 
6155
                        possible_transports=[control_dir.user_transport])
 
6156
                except errors.NotBranchError:
 
6157
                    colocated = False
 
6158
                else:
 
6159
                    colocated = root._format.colocated_branches
 
6160
                if colocated:
 
6161
                    to_location = urlutils.join_segment_parameters(this_url,
 
6162
                        {"branch": urlutils.escape(to_location)})
 
6163
                else:
 
6164
                    to_location = urlutils.join(
 
6165
                        this_url, '..', urlutils.escape(to_location))
6243
6166
            to_branch = branch.bzrdir.sprout(to_location,
6244
 
                 possible_transports=possible_transports,
6245
 
                 source_branch=branch).open_branch()
 
6167
                                 possible_transports=[branch.bzrdir.root_transport],
 
6168
                                 source_branch=branch).open_branch()
6246
6169
        else:
6247
 
            to_branch = lookup_sibling_branch(control_dir, to_location)
 
6170
            # Perhaps it's a colocated branch?
 
6171
            try:
 
6172
                to_branch = control_dir.open_branch(to_location)
 
6173
            except (errors.NotBranchError, errors.NoColocatedBranchSupport):
 
6174
                try:
 
6175
                    to_branch = Branch.open(to_location)
 
6176
                except errors.NotBranchError:
 
6177
                    this_url = self._get_branch_location(control_dir)
 
6178
                    to_branch = Branch.open(
 
6179
                        urlutils.join(
 
6180
                            this_url, '..', urlutils.escape(to_location)))
6248
6181
        if revision is not None:
6249
6182
            revision = revision.as_revision_id(to_branch)
6250
6183
        switch.switch(control_dir, to_branch, force, revision_id=revision)
6254
6187
        note(gettext('Switched to branch: %s'),
6255
6188
            urlutils.unescape_for_display(to_branch.base, 'utf-8'))
6256
6189
 
 
6190
    def _get_branch_location(self, control_dir):
 
6191
        """Return location of branch for this control dir."""
 
6192
        try:
 
6193
            this_branch = control_dir.open_branch()
 
6194
            # This may be a heavy checkout, where we want the master branch
 
6195
            master_location = this_branch.get_bound_location()
 
6196
            if master_location is not None:
 
6197
                return master_location
 
6198
            # If not, use a local sibling
 
6199
            return this_branch.base
 
6200
        except errors.NotBranchError:
 
6201
            format = control_dir.find_branch_format()
 
6202
            if getattr(format, 'get_reference', None) is not None:
 
6203
                return format.get_reference(control_dir)
 
6204
            else:
 
6205
                return control_dir.root_transport.base
6257
6206
 
6258
6207
 
6259
6208
class cmd_view(Command):