/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 branch.py

More tests for sha maps, fix cache misses in tdb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
from bzrlib import (
26
26
    branch,
27
 
    bzrdir,
28
27
    config,
29
28
    errors,
 
29
    foreign,
30
30
    repository,
31
31
    revision,
32
32
    tag,
40
40
    mutter,
41
41
    )
42
42
 
43
 
from bzrlib.plugins.git import (
44
 
    get_rich_root_format,
45
 
    )
46
43
from bzrlib.plugins.git.config import (
47
44
    GitBranchConfig,
48
45
    )
51
48
    NoSuchRef,
52
49
    )
53
50
 
54
 
from bzrlib.foreign import ForeignBranch
55
 
 
56
 
 
57
 
def extract_tags(refs):
 
51
try:
 
52
    from bzrlib.foreign import ForeignBranch
 
53
except ImportError:
 
54
    class ForeignBranch(branch.Branch):
 
55
        def __init__(self, mapping):
 
56
            self.mapping = mapping
 
57
            super(ForeignBranch, self).__init__()
 
58
 
 
59
 
 
60
def extract_tags(refs, mapping):
58
61
    ret = {}
59
62
    for k,v in refs.iteritems():
60
63
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
64
            v = refs.get(k+"^{}", v)
62
 
            ret[k[len("refs/tags/"):]] = v
 
65
            ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
63
66
    return ret
64
67
 
65
68
 
88
91
 
89
92
    def get_tag_dict(self):
90
93
        ret = {}
91
 
        for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
92
 
            try:
93
 
                obj = self.repository._git[v]
94
 
            except KeyError:
95
 
                mutter("Tag %s points at unknown object %s, ignoring", v, obj)
96
 
                continue
 
94
        for k,v in self.repository._git.tags.iteritems():
 
95
            obj = self.repository._git.get_object(v)
97
96
            while isinstance(obj, Tag):
98
97
                v = obj.object[1]
99
 
                obj = self.repository._git[v]
 
98
                obj = self.repository._git.get_object(v)
100
99
            if not isinstance(obj, Commit):
101
100
                mutter("Tag %s points at object %r that is not a commit, "
102
101
                       "ignoring", k, obj)
105
104
        return ret
106
105
 
107
106
    def set_tag(self, name, revid):
108
 
        self.repository._git.refs["refs/tags/%s" % name], _ = \
 
107
        self.repository._git.tags[name], _ = \
109
108
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
110
109
 
111
110
 
112
 
class DictTagDict(LocalGitTagDict):
113
 
 
114
 
 
115
 
    def __init__(self, branch, tags):
116
 
        super(DictTagDict, self).__init__(branch)
117
 
        self._tags = tags
118
 
 
119
 
    def get_tag_dict(self):
120
 
        return self._tags
121
 
 
122
 
 
123
 
 
124
111
class GitBranchFormat(branch.BranchFormat):
125
112
 
126
113
    def get_format_description(self):
127
114
        return 'Git Branch'
128
115
 
129
 
    def network_name(self):
130
 
        return "git"
131
 
 
132
116
    def supports_tags(self):
133
117
        return True
134
118
 
135
 
    def get_foreign_tests_branch_factory(self):
136
 
        from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
137
 
        return ForeignTestsBranchFactory()
138
 
 
139
119
    def make_tags(self, branch):
140
120
        if getattr(branch.repository, "get_refs", None) is not None:
141
121
            from bzrlib.plugins.git.remote import RemoteGitTagDict
147
127
class GitBranch(ForeignBranch):
148
128
    """An adapter to git repositories for bzr Branch objects."""
149
129
 
150
 
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
 
130
    def __init__(self, bzrdir, repository, name, lockfiles):
151
131
        self.repository = repository
152
132
        self._format = GitBranchFormat()
153
133
        self.control_files = lockfiles
154
134
        self.bzrdir = bzrdir
155
135
        super(GitBranch, self).__init__(repository.get_mapping())
156
 
        if tagsdict is not None:
157
 
            self.tags = DictTagDict(self, tagsdict)
158
136
        self.name = name
159
137
        self._head = None
160
 
        self.base = bzrdir.root_transport.base
161
 
 
162
 
    def _get_checkout_format(self):
163
 
        """Return the most suitable metadir for a checkout of this branch.
164
 
        Weaves are used if this branch's repository uses weaves.
165
 
        """
166
 
        return get_rich_root_format()
167
 
 
168
 
    def get_child_submit_format(self):
169
 
        """Return the preferred format of submissions to this branch."""
170
 
        ret = self.get_config().get_user_option("child_submit_format")
171
 
        if ret is not None:
172
 
            return ret
173
 
        return "git"
 
138
        self.base = bzrdir.transport.base
174
139
 
175
140
    def _get_nick(self, local=False, possible_master_transports=None):
176
141
        """Find the nick name for this branch.
187
152
    def __repr__(self):
188
153
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
189
154
 
 
155
    def dpull(self, source, stop_revision=None):
 
156
        return branch.InterBranch.get(source, self).lossy_push()
 
157
 
190
158
    def generate_revision_history(self, revid, old_revid=None):
191
159
        # FIXME: Check that old_revid is in the ancestry of revid
192
160
        newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
197
165
 
198
166
    def get_stacked_on_url(self):
199
167
        # Git doesn't do stacking (yet...)
200
 
        raise errors.UnstackableBranchFormat(self._format, self.base)
 
168
        return None
201
169
 
202
170
    def get_parent(self):
203
171
        """See Branch.get_parent()."""
227
195
            return revision.NULL_REVISION
228
196
        return self.mapping.revision_id_foreign_to_bzr(self.head)
229
197
 
230
 
    def _basic_push(self, target, overwrite=False, stop_revision=None):
231
 
        return branch.InterBranch.get(self, target)._basic_push(
232
 
            overwrite, stop_revision)
233
 
 
234
 
 
 
198
 
235
199
class LocalGitBranch(GitBranch):
236
200
    """A local Git branch."""
237
201
 
 
202
    def _get_checkout_format(self):
 
203
        """Return the most suitable metadir for a checkout of this branch.
 
204
        Weaves are used if this branch's repository uses weaves.
 
205
        """
 
206
        format = self.repository.bzrdir.checkout_metadir()
 
207
        format.set_branch_format(self._format)
 
208
        return format
 
209
 
238
210
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
211
        accelerator_tree=None, hardlink=False):
240
212
        if lightweight:
242
214
            t.ensure_base()
243
215
            format = self._get_checkout_format()
244
216
            checkout = format.initialize_on_transport(t)
245
 
            from_branch = branch.BranchReferenceFormat().initialize(checkout,
 
217
            from_branch = branch.BranchReferenceFormat().initialize(checkout, 
246
218
                self)
247
219
            tree = checkout.create_workingtree(revision_id,
248
220
                from_branch=from_branch, hardlink=hardlink)
251
223
            return self._create_heavyweight_checkout(to_location, revision_id,
252
224
            hardlink)
253
225
 
254
 
    def _create_heavyweight_checkout(self, to_location, revision_id=None,
 
226
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
255
227
                                     hardlink=False):
256
228
        """Create a new heavyweight checkout of this branch.
257
229
 
260
232
        :param hardlink: Whether to hardlink
261
233
        :return: WorkingTree object of checkout.
262
234
        """
263
 
        checkout_branch = bzrdir.BzrDir.create_branch_convenience(
 
235
        checkout_branch = BzrDir.create_branch_convenience(
264
236
            to_location, force_new_tree=False, format=get_rich_root_format())
265
237
        checkout = checkout_branch.bzrdir
266
238
        checkout_branch.bind(self)
267
 
        # pull up to the specified revision_id to set the initial
 
239
        # pull up to the specified revision_id to set the initial 
268
240
        # branch tip correctly, and seed it with history.
269
241
        checkout_branch.pull(self, stop_revision=revision_id)
270
242
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
278
250
        return ret
279
251
 
280
252
    def _get_head(self):
281
 
        try:
282
 
            return self.repository._git.ref(self.name)
283
 
        except KeyError:
284
 
            return None
285
 
 
286
 
    def set_last_revision_info(self, revno, revid):
287
 
        self.set_last_revision(revid)
288
 
 
289
 
    def set_last_revision(self, revid):
290
 
        (newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
291
 
                revid)
292
 
        self.head = newhead
 
253
        return self.repository._git.ref(self.name)
293
254
 
294
255
    def _set_head(self, value):
295
256
        self._head = value
296
 
        self.repository._git.refs[self.name] = self._head
 
257
        self.repository._git.set_ref(self.name, self._head)
297
258
        self._clear_cached_state()
298
259
 
299
260
    head = property(_get_head, _set_head)
322
283
            if self.old_revid == self.new_revid:
323
284
                to_file.write('No revisions to pull.\n')
324
285
            else:
325
 
                to_file.write('Now on revision %d (git sha: %s).\n' %
 
286
                to_file.write('Now on revision %d (git sha: %s).\n' % 
326
287
                        (self.new_revno, self.new_git_head))
327
288
        self._show_tag_conficts(to_file)
328
289
 
329
290
 
330
 
class GitBranchPushResult(branch.BranchPushResult):
331
 
 
332
 
    def _lookup_revno(self, revid):
333
 
        assert isinstance(revid, str), "was %r" % revid
334
 
        # Try in source branch first, it'll be faster
335
 
        try:
336
 
            return self.source_branch.revision_id_to_revno(revid)
337
 
        except errors.NoSuchRevision:
338
 
            # FIXME: Check using graph.find_distance_to_null() ?
339
 
            return self.target_branch.revision_id_to_revno(revid)
340
 
 
341
 
    @property
342
 
    def old_revno(self):
343
 
        return self._lookup_revno(self.old_revid)
344
 
 
345
 
    @property
346
 
    def new_revno(self):
347
 
        return self._lookup_revno(self.new_revid)
348
 
 
349
 
 
350
 
class InterFromGitBranch(branch.GenericInterBranch):
 
291
class InterFromGitBranch(branch.InterBranch):
351
292
    """InterBranch implementation that pulls from Git into bzr."""
352
293
 
353
294
    @classmethod
354
 
    def _get_interrepo(self, source, target):
355
 
        return repository.InterRepository.get(source.repository,
356
 
            target.repository)
357
 
 
358
 
    @classmethod
359
 
    def is_compatible(cls, source, target):
360
 
        return (isinstance(source, GitBranch) and
361
 
                not isinstance(target, GitBranch) and
362
 
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
 
295
    def is_compatible(self, source, target):
 
296
        return (isinstance(source, GitBranch) and 
 
297
                not isinstance(target, GitBranch))
363
298
 
364
299
    def update_revisions(self, stop_revision=None, overwrite=False,
365
300
        graph=None):
366
301
        """See InterBranch.update_revisions()."""
367
 
        interrepo = self._get_interrepo(self.source, self.target)
 
302
        interrepo = repository.InterRepository.get(self.source.repository, 
 
303
            self.target.repository)
368
304
        self._head = None
369
305
        self._last_revid = None
370
306
        def determine_wants(heads):
372
308
                raise NoSuchRef(self.source.name, heads.keys())
373
309
            if stop_revision is not None:
374
310
                self._last_revid = stop_revision
375
 
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
 
311
                self._head, mapping = self.source.repository.lookup_git_revid(
376
312
                    stop_revision)
377
313
            else:
378
314
                self._head = heads[self.source.name]
417
353
            graph = self.target.repository.get_graph(self.source.repository)
418
354
            result.old_revno, result.old_revid = \
419
355
                self.target.last_revision_info()
420
 
            self.update_revisions(stop_revision, overwrite=overwrite,
 
356
            self.update_revisions(stop_revision, overwrite=overwrite, 
421
357
                graph=graph)
422
358
            result.new_git_head = self._head
423
359
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
436
372
            self.source.unlock()
437
373
        return result
438
374
 
439
 
    def _basic_push(self, overwrite=False, stop_revision=None):
440
 
        result = branch.BranchPushResult()
441
 
        result.source_branch = self.source
442
 
        result.target_branch = self.target
443
 
        graph = self.target.repository.get_graph(self.source.repository)
444
 
        result.old_revno, result.old_revid = self.target.last_revision_info()
445
 
        self.update_revisions(stop_revision, overwrite=overwrite, graph=graph)
446
 
        result.new_git_head = self._head
447
 
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
448
 
            overwrite)
449
 
        result.new_revno, result.new_revid = self.target.last_revision_info()
450
 
        return result
451
 
 
452
 
 
453
 
class InterGitBranch(branch.GenericInterBranch):
 
375
 
 
376
class InterGitRemoteLocalBranch(branch.InterBranch):
454
377
    """InterBranch implementation that pulls between Git branches."""
455
378
 
456
 
 
457
 
class InterGitLocalRemoteBranch(InterGitBranch):
458
 
    """InterBranch that copies from a local to a remote git branch."""
459
 
 
460
 
    @classmethod
461
 
    def is_compatible(self, source, target):
462
 
        from bzrlib.plugins.git.remote import RemoteGitBranch
463
 
        return (isinstance(source, LocalGitBranch) and
464
 
                isinstance(target, RemoteGitBranch))
465
 
 
466
 
    def _basic_push(self, overwrite=False, stop_revision=None):
467
 
        result = GitBranchPushResult()
468
 
        result.source_branch = self.source
469
 
        result.target_branch = self.target
470
 
        if stop_revision is None:
471
 
            stop_revision = self.source.last_revision()
472
 
        # FIXME: Check for diverged branches
473
 
        def get_changed_refs(old_refs):
474
 
            result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
475
 
            refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
476
 
            result.new_revid = stop_revision
477
 
            for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
478
 
                refs["refs/tags/%s" % name] = sha
479
 
            return refs
480
 
        self.target.repository.send_pack(get_changed_refs,
481
 
                self.source.repository._git.object_store.generate_pack_contents)
482
 
        return result
483
 
 
484
 
 
485
 
class InterGitRemoteLocalBranch(InterGitBranch):
486
 
    """InterBranch that copies from a remote to a local git branch."""
487
 
 
488
 
    @classmethod
489
 
    def is_compatible(self, source, target):
490
 
        from bzrlib.plugins.git.remote import RemoteGitBranch
491
 
        return (isinstance(source, RemoteGitBranch) and
 
379
    @classmethod
 
380
    def is_compatible(self, source, target):
 
381
        from bzrlib.plugins.git.remote import RemoteGitBranch
 
382
        return (isinstance(source, RemoteGitBranch) and 
492
383
                isinstance(target, LocalGitBranch))
493
384
 
494
 
    def _basic_push(self, overwrite=False, stop_revision=None):
495
 
        result = branch.BranchPushResult()
 
385
    def pull(self, stop_revision=None, overwrite=False, 
 
386
        possible_transports=None, local=False):
 
387
        # This type of branch can't be bound.
 
388
        if local:
 
389
            raise errors.LocalRequiresBoundBranch()
 
390
        result = GitPullResult()
496
391
        result.source_branch = self.source
497
392
        result.target_branch = self.target
 
393
        interrepo = repository.InterRepository.get(self.source.repository, 
 
394
            self.target.repository)
498
395
        result.old_revid = self.target.last_revision()
499
 
        refs, stop_revision = self.update_refs(stop_revision)
500
 
        self.target.generate_revision_history(stop_revision, result.old_revid)
501
 
        self.update_tags(refs)
502
 
        result.new_revid = self.target.last_revision()
503
 
        return result
504
 
 
505
 
    def update_tags(self, refs):
506
 
        for name, v in extract_tags(refs).iteritems():
507
 
            revid = self.target.mapping.revision_id_foreign_to_bzr(v)
508
 
            self.target.tags.set_tag(name, revid)
509
 
 
510
 
    def update_refs(self, stop_revision=None):
511
 
        interrepo = repository.InterRepository.get(self.source.repository,
512
 
            self.target.repository)
513
396
        if stop_revision is None:
514
397
            refs = interrepo.fetch_refs(branches=["HEAD"])
515
398
            stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
516
399
        else:
517
400
            refs = interrepo.fetch_refs(revision_id=stop_revision)
518
 
        return refs, stop_revision
519
 
 
520
 
    def pull(self, stop_revision=None, overwrite=False,
521
 
        possible_transports=None, local=False):
522
 
        # This type of branch can't be bound.
523
 
        if local:
524
 
            raise errors.LocalRequiresBoundBranch()
525
 
        result = GitPullResult()
526
 
        result.source_branch = self.source
527
 
        result.target_branch = self.target
528
 
        result.old_revid = self.target.last_revision()
529
 
        refs, stop_revision = self.update_refs(stop_revision)
530
401
        self.target.generate_revision_history(stop_revision, result.old_revid)
531
 
        self.update_tags(refs)
 
402
        for name, revid in extract_tags(refs, self.target.mapping).iteritems():
 
403
            self.target.tags.set_tag(name, revid)
532
404
        result.new_revid = self.target.last_revision()
533
405
        return result
534
406
 
535
 
 
 
407
    
536
408
class InterToGitBranch(branch.InterBranch):
537
409
    """InterBranch implementation that pulls from Git into bzr."""
538
410
 
539
 
    @staticmethod
540
 
    def _get_branch_formats_to_test():
541
 
        return None, None
542
 
 
543
411
    @classmethod
544
412
    def is_compatible(self, source, target):
545
 
        return (not isinstance(source, GitBranch) and
 
413
        return (not isinstance(source, GitBranch) and 
546
414
                isinstance(target, GitBranch))
547
415
 
548
 
    def update_revisions(self, *args, **kwargs):
549
 
        raise NoPushSupport()
550
 
 
551
 
    def push(self, overwrite=True, stop_revision=None,
 
416
    def push(self, overwrite=True, stop_revision=None, 
552
417
             _override_hook_source_branch=None):
553
418
        raise NoPushSupport()
554
419
 
555
420
    def lossy_push(self, stop_revision=None):
556
 
        result = GitBranchPushResult()
557
 
        result.source_branch = self.source
558
 
        result.target_branch = self.target
559
 
        try:
560
 
            result.old_revid = self.target.last_revision()
561
 
        except NoSuchRef:
562
 
            result.old_revid = revision.NULL_REVISION
563
421
        if stop_revision is None:
564
422
            stop_revision = self.source.last_revision()
565
423
        # FIXME: Check for diverged branches
571
429
            self.source.repository, refs)
572
430
        if revidmap != {}:
573
431
            self.target.generate_revision_history(revidmap[stop_revision])
574
 
            result.new_revid = revidmap[stop_revision]
575
 
        else:
576
 
            result.new_revid = result.old_revid
577
 
        result.revidmap = revidmap
578
 
        return result
 
432
        return revidmap
579
433
 
580
434
 
581
435
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
582
436
branch.InterBranch.register_optimiser(InterFromGitBranch)
583
437
branch.InterBranch.register_optimiser(InterToGitBranch)
584
 
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)