/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

Add tests for revspec.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
 
56
56
 
57
57
def extract_tags(refs):
 
58
    """Extract the tags from a refs dictionary.
 
59
 
 
60
    :param refs: Refs to extract the tags from.
 
61
    :return: Dictionary mapping tag names to SHA1s.
 
62
    """
58
63
    ret = {}
59
64
    for k,v in refs.iteritems():
60
65
        if k.startswith("refs/tags/") and not k.endswith("^{}"):
63
68
    return ret
64
69
 
65
70
 
 
71
def branch_name_to_ref(name):
 
72
    """Map a branch name to a ref.
 
73
 
 
74
    :param name: Branch name
 
75
    :return: ref string
 
76
    """
 
77
    if name is None or name == "HEAD":
 
78
        return "HEAD"
 
79
    if not name.startswith("refs/"):
 
80
        return "refs/heads/%s" % name
 
81
    else:
 
82
        return name
 
83
 
 
84
 
 
85
def ref_to_branch_name(ref):
 
86
    """Map a ref to a branch name
 
87
 
 
88
    :param ref: Ref
 
89
    :return: A branch name
 
90
    """
 
91
    if ref == "HEAD":
 
92
        return "HEAD"
 
93
    if ref.startswith("refs/heads/"):
 
94
        return ref[len("refs/heads/"):]
 
95
    raise ValueError("unable to map ref %s back to branch name")
 
96
 
 
97
 
66
98
class GitPullResult(branch.PullResult):
67
99
 
68
100
    def _lookup_revno(self, revid):
104
136
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
105
137
        return ret
106
138
 
 
139
    def _set_tag_dict(self, to_dict):
 
140
        extra = set(self.repository._git.get_refs().keys())
 
141
        for k, revid in to_dict.iteritems():
 
142
            name = "refs/tags/%s" % k
 
143
            if name in extra:
 
144
                extra.remove(name)
 
145
            self.set_tag(k, revid)
 
146
        for name in extra:
 
147
            if name.startswith("refs/tags/"):
 
148
                del self.repository._git[name]
 
149
        
107
150
    def set_tag(self, name, revid):
108
151
        self.repository._git.refs["refs/tags/%s" % name], _ = \
109
152
            self.branch.mapping.revision_id_bzr_to_foreign(revid)
111
154
 
112
155
class DictTagDict(LocalGitTagDict):
113
156
 
114
 
 
115
157
    def __init__(self, branch, tags):
116
158
        super(DictTagDict, self).__init__(branch)
117
159
        self._tags = tags
120
162
        return self._tags
121
163
 
122
164
 
123
 
 
124
165
class GitBranchFormat(branch.BranchFormat):
125
166
 
126
167
    def get_format_description(self):
147
188
class GitBranch(ForeignBranch):
148
189
    """An adapter to git repositories for bzr Branch objects."""
149
190
 
150
 
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
 
191
    def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
151
192
        self.repository = repository
152
193
        self._format = GitBranchFormat()
153
194
        self.control_files = lockfiles
155
196
        super(GitBranch, self).__init__(repository.get_mapping())
156
197
        if tagsdict is not None:
157
198
            self.tags = DictTagDict(self, tagsdict)
158
 
        self.name = name
 
199
        self.ref = ref
 
200
        self.name = ref_to_branch_name(ref)
159
201
        self._head = None
160
202
        self.base = bzrdir.root_transport.base
161
203
 
185
227
    nick = property(_get_nick, _set_nick)
186
228
 
187
229
    def __repr__(self):
188
 
        return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
 
230
        return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
 
231
            self.ref)
189
232
 
190
233
    def generate_revision_history(self, revid, old_revid=None):
191
234
        # FIXME: Check that old_revid is in the ancestry of revid
235
278
class LocalGitBranch(GitBranch):
236
279
    """A local Git branch."""
237
280
 
 
281
    def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
 
282
        super(LocalGitBranch, self).__init__(bzrdir, repository, name, 
 
283
              lockfiles, tagsdict)
 
284
        if not name in repository._git.get_refs().keys():
 
285
            raise errors.NotBranchError(self.base)
 
286
 
238
287
    def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
288
        accelerator_tree=None, hardlink=False):
240
289
        if lightweight:
279
328
 
280
329
    def _get_head(self):
281
330
        try:
282
 
            return self.repository._git.ref(self.name)
 
331
            return self.repository._git.ref(self.ref)
283
332
        except KeyError:
284
333
            return None
285
334
 
293
342
 
294
343
    def _set_head(self, value):
295
344
        self._head = value
296
 
        self.repository._git.refs[self.name] = self._head
 
345
        self.repository._git.refs[self.ref] = self._head
297
346
        self._clear_cached_state()
298
347
 
299
348
    head = property(_get_head, _set_head)
321
370
        if not is_quiet():
322
371
            if self.old_revid == self.new_revid:
323
372
                to_file.write('No revisions to pull.\n')
324
 
            else:
 
373
            elif self.new_git_head is not None:
325
374
                to_file.write('Now on revision %d (git sha: %s).\n' %
326
375
                        (self.new_revno, self.new_git_head))
 
376
            else:
 
377
                to_file.write('Now on revision %d.\n' % (self.new_revno,))
327
378
        self._show_tag_conficts(to_file)
328
379
 
329
380
 
361
412
                not isinstance(target, GitBranch) and
362
413
                (getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
363
414
 
364
 
    def update_revisions(self, stop_revision=None, overwrite=False,
365
 
        graph=None):
366
 
        """See InterBranch.update_revisions()."""
 
415
    def _update_revisions(self, stop_revision=None, overwrite=False,
 
416
        graph=None, limit=None):
 
417
        """Like InterBranch.update_revisions(), but with additions.
 
418
 
 
419
        Compared to the `update_revisions()` below, this function takes a
 
420
        `limit` argument that limits how many git commits will be converted
 
421
        and returns the new git head.
 
422
        """
367
423
        interrepo = self._get_interrepo(self.source, self.target)
368
 
        self._head = None
369
 
        self._last_revid = None
370
424
        def determine_wants(heads):
371
 
            if not self.source.name in heads:
372
 
                raise NoSuchRef(self.source.name, heads.keys())
 
425
            if not self.source.ref in heads:
 
426
                raise NoSuchRef(self.source.ref, heads.keys())
373
427
            if stop_revision is not None:
374
428
                self._last_revid = stop_revision
375
 
                self._head, mapping = self.source.repository.lookup_bzr_revision_id(
 
429
                head, mapping = self.source.repository.lookup_bzr_revision_id(
376
430
                    stop_revision)
377
431
            else:
378
 
                self._head = heads[self.source.name]
379
 
                self._last_revid = \
380
 
                    self.source.mapping.revision_id_foreign_to_bzr(self._head)
 
432
                head = heads[self.source.ref]
 
433
                self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
 
434
                    head)
381
435
            if self.target.repository.has_revision(self._last_revid):
382
436
                return []
383
 
            return [self._head]
384
 
        interrepo.fetch_objects(determine_wants, self.source.mapping)
 
437
            return [head]
 
438
        pack_hint, head = interrepo.fetch_objects(
 
439
            determine_wants, self.source.mapping, limit=limit)
 
440
        if pack_hint is not None and self.target.repository._format.pack_compresses:
 
441
            self.target.repository.pack(hint=pack_hint)
 
442
        if head is not None:
 
443
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
385
444
        if overwrite:
386
445
            prev_last_revid = None
387
446
        else:
388
447
            prev_last_revid = self.target.last_revision()
389
 
        self.target.generate_revision_history(self._last_revid, prev_last_revid)
 
448
        self.target.generate_revision_history(self._last_revid,
 
449
            prev_last_revid)
 
450
        return head
 
451
 
 
452
    def update_revisions(self, stop_revision=None, overwrite=False,
 
453
                         graph=None):
 
454
        """See InterBranch.update_revisions()."""
 
455
        self._update_revisions(stop_revision, overwrite, graph)
390
456
 
391
457
    def pull(self, overwrite=False, stop_revision=None,
392
458
             possible_transports=None, _hook_master=None, run_hooks=True,
393
 
             _override_hook_target=None, local=False):
 
459
             _override_hook_target=None, local=False, limit=None):
394
460
        """See Branch.pull.
395
461
 
396
462
        :param _hook_master: Private parameter - set the branch to
400
466
            so it should not run its hooks.
401
467
        :param _override_hook_target: Private parameter - set the branch to be
402
468
            supplied as the target_branch to pull hooks.
 
469
        :param limit: Only import this many revisons.  `None`, the default,
 
470
            means import all revisions.
403
471
        """
404
472
        # This type of branch can't be bound.
405
473
        if local:
415
483
            # We assume that during 'pull' the target repository is closer than
416
484
            # the source one.
417
485
            graph = self.target.repository.get_graph(self.source.repository)
418
 
            result.old_revno, result.old_revid = \
 
486
            (result.old_revno, result.old_revid) = \
419
487
                self.target.last_revision_info()
420
 
            self.update_revisions(stop_revision, overwrite=overwrite,
421
 
                graph=graph)
422
 
            result.new_git_head = self._head
 
488
            result.new_git_head = self._update_revisions(
 
489
                stop_revision, overwrite=overwrite, graph=graph, limit=limit)
423
490
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
424
491
                overwrite)
425
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
492
            (result.new_revno, result.new_revid) = \
 
493
                self.target.last_revision_info()
426
494
            if _hook_master:
427
495
                result.master_branch = _hook_master
428
496
                result.local_branch = result.target_branch
442
510
        result.target_branch = self.target
443
511
        graph = self.target.repository.get_graph(self.source.repository)
444
512
        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
 
513
        result.new_git_head = self._update_revisions(
 
514
            stop_revision, overwrite=overwrite, graph=graph)
447
515
        result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
448
516
            overwrite)
449
517
        result.new_revno, result.new_revid = self.target.last_revision_info()
478
546
                refs["refs/tags/%s" % name] = sha
479
547
            return refs
480
548
        self.target.repository.send_pack(get_changed_refs,
481
 
                self.source.repository._git.object_store.generate_pack_contents)
 
549
            self.source.repository._git.object_store.generate_pack_contents)
482
550
        return result
483
551
 
484
552
 
518
586
        return refs, stop_revision
519
587
 
520
588
    def pull(self, stop_revision=None, overwrite=False,
521
 
        possible_transports=None, local=False):
 
589
        possible_transports=None, run_hooks=True,local=False):
522
590
        # This type of branch can't be bound.
523
591
        if local:
524
592
            raise errors.LocalRequiresBoundBranch()