~bzr-pqm/bzr/bzr.dev

3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
3918.2.2 by Martin Pool
Add import statement
16
17
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
18
"""Foreign branch utilities."""
19
3918.2.2 by Martin Pool
Add import statement
20
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
21
from bzrlib.branch import Branch
22
from bzrlib.commands import Command, Option
3918.2.2 by Martin Pool
Add import statement
23
from bzrlib.repository import Repository
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
24
from bzrlib.revision import Revision
25
from bzrlib.lazy_import import lazy_import
26
lazy_import(globals(), """
27
from bzrlib import (
28
    errors,
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
29
    osutils,
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
30
    registry,
31
    )
32
""")
33
34
class VcsMapping(object):
35
    """Describes the mapping between the semantics of Bazaar and a foreign vcs.
36
37
    """
38
    # Whether this is an experimental mapping that is still open to changes.
39
    experimental = False
40
41
    # Whether this mapping supports exporting and importing all bzr semantics.
42
    roundtripping = False
43
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
44
    # Prefix used when importing native foreign revisions (not roundtripped)
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
45
    # using this mapping.
46
    revid_prefix = None
47
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
48
    def __init__(self, vcs):
49
        """Create a new VcsMapping.
50
51
        :param vcs: VCS that this mapping maps to Bazaar
52
        """
53
        self.vcs = vcs
54
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
55
    def revision_id_bzr_to_foreign(self, bzr_revid):
56
        """Parse a bzr revision id and convert it to a foreign revid.
57
58
        :param bzr_revid: The bzr revision id (a string).
59
        :return: A foreign revision id, can be any sort of object.
60
        """
61
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
62
63
    def revision_id_foreign_to_bzr(self, foreign_revid):
64
        """Parse a foreign revision id and convert it to a bzr revid.
65
66
        :param foreign_revid: Foreign revision id, can be any sort of object.
67
        :return: A bzr revision id.
68
        """
69
        raise NotImplementedError(self.revision_id_foreign_to_bzr)
70
71
72
class VcsMappingRegistry(registry.Registry):
73
    """Registry for Bazaar<->foreign VCS mappings.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
74
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
75
    There should be one instance of this registry for every foreign VCS.
76
    """
77
78
    def register(self, key, factory, help):
79
        """Register a mapping between Bazaar and foreign VCS semantics.
80
81
        The factory must be a callable that takes one parameter: the key.
82
        It must produce an instance of VcsMapping when called.
83
        """
84
        if ":" in key:
85
            raise ValueError("mapping name can not contain colon (:)")
86
        registry.Registry.register(self, key, factory, help)
87
88
    def set_default(self, key):
89
        """Set the 'default' key to be a clone of the supplied key.
90
91
        This method must be called once and only once.
92
        """
93
        self._set_default_key(key)
94
95
    def get_default(self):
96
        """Convenience function for obtaining the default mapping to use."""
97
        return self.get(self._get_default_key())
98
99
    def revision_id_bzr_to_foreign(self, revid):
100
        """Convert a bzr revision id to a foreign revid."""
101
        raise NotImplementedError(self.revision_id_bzr_to_foreign)
102
103
104
class ForeignRevision(Revision):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
105
    """A Revision from a Foreign repository. Remembers
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
106
    information about foreign revision id and mapping.
107
108
    """
109
110
    def __init__(self, foreign_revid, mapping, *args, **kwargs):
3830.4.4 by Jelmer Vernooij
make inventory_sha1 default to an empty string.
111
        if not "inventory_sha1" in kwargs:
112
            kwargs["inventory_sha1"] = ""
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
113
        super(ForeignRevision, self).__init__(*args, **kwargs)
114
        self.foreign_revid = foreign_revid
115
        self.mapping = mapping
116
117
118
def show_foreign_properties(rev):
119
    """Custom log displayer for foreign revision identifiers.
120
121
    :param rev: Revision object.
122
    """
123
    # Revision comes directly from a foreign repository
124
    if isinstance(rev, ForeignRevision):
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
125
        return rev.mapping.vcs.show_foreign_revid(rev.foreign_revid)
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
126
127
    # Revision was once imported from a foreign repository
128
    try:
129
        foreign_revid, mapping = \
130
            foreign_vcs_registry.parse_revision_id(rev.revision_id)
131
    except errors.InvalidRevisionId:
132
        return {}
133
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
134
    return mapping.vcs.show_foreign_revid(foreign_revid)
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
135
136
137
class ForeignVcs(object):
138
    """A foreign version control system."""
139
140
    def __init__(self, mapping_registry):
141
        self.mapping_registry = mapping_registry
142
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
143
    def show_foreign_revid(self, foreign_revid):
144
        """Prepare a foreign revision id for formatting using bzr log.
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
145
3949.5.1 by Jelmer Vernooij
Move ForeignVcsMapping.show_foreign_revid to ForeignVcs.
146
        :param foreign_revid: Foreign revision id.
147
        :return: Dictionary mapping string keys to string values.
148
        """
149
        return { }
150
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
151
152
class ForeignVcsRegistry(registry.Registry):
153
    """Registry for Foreign VCSes.
154
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
155
    There should be one entry per foreign VCS. Example entries would be
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
156
    "git", "svn", "hg", "darcs", etc.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
157
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
158
    """
159
160
    def register(self, key, foreign_vcs, help):
161
        """Register a foreign VCS.
162
163
        :param key: Prefix of the foreign VCS in revision ids
164
        :param foreign_vcs: ForeignVCS instance
165
        :param help: Description of the foreign VCS
166
        """
167
        if ":" in key or "-" in key:
168
            raise ValueError("vcs name can not contain : or -")
169
        registry.Registry.register(self, key, foreign_vcs, help)
170
171
    def parse_revision_id(self, revid):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
172
        """Parse a bzr revision and return the matching mapping and foreign
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
173
        revid.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
174
3830.4.1 by Jelmer Vernooij
Add base classes for foreign branches.
175
        :param revid: The bzr revision id
176
        :return: tuple with foreign revid and vcs mapping
177
        """
178
        if not "-" in revid:
179
            raise errors.InvalidRevisionId(revid, None)
180
        try:
181
            foreign_vcs = self.get(revid.split("-")[0])
182
        except KeyError:
183
            raise errors.InvalidRevisionId(revid, None)
184
        return foreign_vcs.mapping_registry.revision_id_bzr_to_foreign(revid)
185
186
187
foreign_vcs_registry = ForeignVcsRegistry()
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
188
189
190
class ForeignRepository(Repository):
191
    """A Repository that exists in a foreign version control system.
192
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
193
    The data in this repository can not be represented natively using
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
194
    Bazaars internal datastructures, but have to converted using a VcsMapping.
195
    """
196
197
    # This repository's native version control system
198
    vcs = None
199
200
    def has_foreign_revision(self, foreign_revid):
201
        """Check whether the specified foreign revision is present.
202
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
203
        :param foreign_revid: A foreign revision id, in the format used
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
204
                              by this Repository's VCS.
205
        """
206
        raise NotImplementedError(self.has_foreign_revision)
207
208
    def lookup_bzr_revision_id(self, revid):
209
        """Lookup a mapped or roundtripped revision by revision id.
210
211
        :param revid: Bazaar revision id
212
        :return: Tuple with foreign revision id and mapping.
213
        """
214
        raise NotImplementedError(self.lookup_revision_id)
215
216
    def all_revision_ids(self, mapping=None):
217
        """See Repository.all_revision_ids()."""
218
        raise NotImplementedError(self.all_revision_ids)
219
220
    def get_default_mapping(self):
221
        """Get the default mapping for this repository."""
222
        raise NotImplementedError(self.get_default_mapping)
223
224
    def get_inventory_xml(self, revision_id):
225
        """See Repository.get_inventory_xml()."""
226
        return self.serialise_inventory(self.get_inventory(revision_id))
227
228
    def get_inventory_sha1(self, revision_id):
229
        """Get the sha1 for the XML representation of an inventory.
230
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
231
        :param revision_id: Revision id of the inventory for which to return
3878.5.1 by Jelmer Vernooij
Add a ForeignRepository base class.
232
         the SHA1.
233
        :return: XML string
234
        """
235
236
        return osutils.sha_string(self.get_inventory_xml(revision_id))
237
238
    def get_revision_xml(self, revision_id):
239
        """Return the XML representation of a revision.
240
241
        :param revision_id: Revision for which to return the XML.
242
        :return: XML string
243
        """
244
        return self._serializer.write_revision_to_string(
245
            self.get_revision(revision_id))
246
247
3920.2.1 by Jelmer Vernooij
Add ForeignBranch class.
248
class ForeignBranch(Branch):
249
    """Branch that exists in a foreign version control system."""
250
251
    def __init__(self, mapping):
252
        self.mapping = mapping
253
        super(ForeignBranch, self).__init__()
254
255
    def dpull(self, source, stop_revision=None):
256
        """Pull deltas from another branch.
257
258
        :note: This does not, like pull, retain the revision ids from 
259
            the source branch and will, rather than adding bzr-specific 
260
            metadata, push only those semantics of the revision that can be 
261
            natively represented by this branch' VCS.
262
263
        :param source: Source branch
264
        :param stop_revision: Revision to pull, defaults to last revision.
3920.2.30 by Jelmer Vernooij
Review from John.
265
        :return: Dictionary mapping revision ids from the source branch 
266
            to new revision ids in the target branch, for each 
267
            revision that was pull.
3920.2.1 by Jelmer Vernooij
Add ForeignBranch class.
268
        """
269
        raise NotImplementedError(self.dpull)
3920.2.2 by Jelmer Vernooij
Import dpush command.
270
271
3920.2.30 by Jelmer Vernooij
Review from John.
272
def _determine_fileid_renames(old_inv, new_inv):
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
273
    """Determine the file ids based on a old and a new inventory that 
274
    are equal in content.
275
276
    :param old_inv: Old inventory
277
    :param new_inv: New inventory
278
    :return: Dictionary a (old_id, new_id) tuple for each path in the 
279
        inventories.
280
    """
281
    ret = {}
282
    if len(old_inv) != len(new_inv):
283
        raise AssertionError("Inventories are not of the same size")
284
    for old_file_id in old_inv:
3920.2.30 by Jelmer Vernooij
Review from John.
285
        path = old_inv.id2path(old_file_id)
286
        new_file_id = new_inv.path2id(path)
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
287
        if new_file_id is None:
288
            raise AssertionError(
289
                "Unable to find %s in new inventory" % old_file_id)
3920.2.30 by Jelmer Vernooij
Review from John.
290
        ret[path] = (old_file_id, new_file_id)
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
291
    return ret
292
293
294
def update_workinginv_fileids(wt, old_inv, new_inv):
3920.2.2 by Jelmer Vernooij
Import dpush command.
295
    """Update all file ids in wt according to old_tree/new_tree. 
296
297
    old_tree and new_tree should be two RevisionTree's that differ only
298
    in file ids.
299
    """
3920.2.30 by Jelmer Vernooij
Review from John.
300
    fileid_renames = _determine_fileid_renames(old_inv, new_inv)
3920.2.2 by Jelmer Vernooij
Import dpush command.
301
    old_fileids = []
302
    new_fileids = []
303
    new_root_id = None
304
    # Adjust file ids in working tree
3920.2.3 by Jelmer Vernooij
Make determine_fileid_renames() return a dictionary.
305
    # Sorted, so we process parents before children
3920.2.21 by Jelmer Vernooij
Just update all files ids rather than worrying about unchanged children of changed directories.
306
    for path in sorted(fileid_renames.keys()):
3920.2.24 by Jelmer Vernooij
Only unversion top-level entries, as WorkingTree.unversion() works recursively.
307
        (old_fileid, new_fileid) = fileid_renames[path]
3920.2.2 by Jelmer Vernooij
Import dpush command.
308
        if path != "":
3920.2.24 by Jelmer Vernooij
Only unversion top-level entries, as WorkingTree.unversion() works recursively.
309
            new_fileids.append((path, new_fileid))
310
            # unversion() works recursively so we only have to unversion the 
311
            # top-level. Unfortunately unversioning / is not supported yet, 
312
            # so unversion its children instead and use set_root_id() for /
313
            if old_inv[old_fileid].parent_id == old_inv.root.file_id:
314
                old_fileids.append(old_fileid)
3920.2.2 by Jelmer Vernooij
Import dpush command.
315
        else:
3920.2.24 by Jelmer Vernooij
Only unversion top-level entries, as WorkingTree.unversion() works recursively.
316
            new_root_id = new_fileid
3920.2.2 by Jelmer Vernooij
Import dpush command.
317
    new_fileids.reverse()
318
    wt.unversion(old_fileids)
319
    if new_root_id is not None:
320
        wt.set_root_id(new_root_id)
321
    wt.add([x[0] for x in new_fileids], [x[1] for x in new_fileids])
3920.2.4 by Jelmer Vernooij
Add tests for update_workinginv_fileids.
322
    wt.set_last_revision(new_inv.revision_id)
3920.2.2 by Jelmer Vernooij
Import dpush command.
323
324
325
class cmd_dpush(Command):
326
    """Push diffs into a foreign version control system without any 
327
    Bazaar-specific metadata.
328
329
    This will afterwards rebase the local Bazaar branch on the remote
330
    branch unless the --no-rebase option is used, in which case 
331
    the two branches will be out of sync. 
332
    """
3920.2.30 by Jelmer Vernooij
Review from John.
333
    hidden = True
3920.2.2 by Jelmer Vernooij
Import dpush command.
334
    takes_args = ['location?']
335
    takes_options = ['remember', Option('directory',
336
            help='Branch to push from, '
337
                 'rather than the one containing the working directory.',
338
            short_name='d',
339
            type=unicode,
340
            ),
3920.2.14 by Jelmer Vernooij
Fix formatting of dpush help.
341
            Option('no-rebase', help="Do not rebase after push.")]
3920.2.2 by Jelmer Vernooij
Import dpush command.
342
343
    def run(self, location=None, remember=False, directory=None, 
344
            no_rebase=False):
345
        from bzrlib import urlutils
346
        from bzrlib.bzrdir import BzrDir
347
        from bzrlib.errors import BzrCommandError, NoWorkingTree
348
        from bzrlib.trace import info
349
        from bzrlib.workingtree import WorkingTree
350
351
        if directory is None:
352
            directory = "."
353
        try:
354
            source_wt = WorkingTree.open_containing(directory)[0]
355
            source_branch = source_wt.branch
356
        except NoWorkingTree:
3920.2.30 by Jelmer Vernooij
Review from John.
357
            source_branch = Branch.open(directory)
3920.2.2 by Jelmer Vernooij
Import dpush command.
358
            source_wt = None
359
        stored_loc = source_branch.get_push_location()
360
        if location is None:
361
            if stored_loc is None:
362
                raise BzrCommandError("No push location known or specified.")
363
            else:
364
                display_url = urlutils.unescape_for_display(stored_loc,
365
                        self.outf.encoding)
366
                self.outf.write("Using saved location: %s\n" % display_url)
367
                location = stored_loc
368
369
        bzrdir = BzrDir.open(location)
370
        target_branch = bzrdir.open_branch()
3920.2.23 by Jelmer Vernooij
Just require that the target branch has a dpull method, rather than requiring it descends from ForeignBranch.
371
        dpull = getattr(target_branch, "dpull", None)
372
        if dpull is None:
3920.2.18 by Jelmer Vernooij
make sure dpush between native branches fails.
373
            raise BzrCommandError("%r is not a foreign branch, use "
374
                                  "regular push." % target_branch)
3920.2.2 by Jelmer Vernooij
Import dpush command.
375
        target_branch.lock_write()
376
        try:
3920.2.23 by Jelmer Vernooij
Just require that the target branch has a dpull method, rather than requiring it descends from ForeignBranch.
377
            revid_map = dpull(source_branch)
3920.2.2 by Jelmer Vernooij
Import dpush command.
378
            # We successfully created the target, remember it
379
            if source_branch.get_push_location() is None or remember:
380
                source_branch.set_push_location(target_branch.base)
381
            if not no_rebase:
3920.2.36 by Jelmer Vernooij
Fix tests after CommitBuilder changes.
382
                old_last_revid = source_branch.last_revision()
3920.2.30 by Jelmer Vernooij
Review from John.
383
                source_branch.pull(target_branch, overwrite=True)
3920.2.36 by Jelmer Vernooij
Fix tests after CommitBuilder changes.
384
                new_last_revid = source_branch.last_revision()
3920.3.1 by Jelmer Vernooij
Skip tree changing if nothing changed.
385
                if source_wt is not None and old_last_revid != new_last_revid:
3920.2.2 by Jelmer Vernooij
Import dpush command.
386
                    source_wt.lock_write()
387
                    try:
3920.2.10 by Jelmer Vernooij
More work trying to implement a dummy version control system.
388
                        update_workinginv_fileids(source_wt, 
3920.2.20 by Jelmer Vernooij
Fix dpush tests.
389
                            source_wt.branch.repository.get_inventory(
3920.2.18 by Jelmer Vernooij
make sure dpush between native branches fails.
390
                                old_last_revid),
3920.2.20 by Jelmer Vernooij
Fix dpush tests.
391
                            source_wt.branch.repository.get_inventory(
3920.2.18 by Jelmer Vernooij
make sure dpush between native branches fails.
392
                                new_last_revid))
3920.2.2 by Jelmer Vernooij
Import dpush command.
393
                    finally:
394
                        source_wt.unlock()
395
        finally:
396
            target_branch.unlock()
397
398