/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

  • Committer: Jelmer Vernooij
  • Date: 2009-03-16 13:56:04 UTC
  • mto: (0.200.259 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090316135604-9nkd7d2ztiygybiy
Strip ref directory name from tag names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
3
#
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
19
20
from bzrlib import (
20
21
    branch,
21
22
    config,
 
23
    repository,
22
24
    revision,
 
25
    tag,
23
26
    )
24
27
from bzrlib.decorators import needs_read_lock
25
 
 
26
 
from bzrlib.plugins.git import ids
 
28
from bzrlib.trace import mutter
 
29
 
 
30
from bzrlib.plugins.git.foreign import ForeignBranch
 
31
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
 
32
 
 
33
from dulwich.objects import (
 
34
        Commit,
 
35
        Tag,
 
36
        )
 
37
 
 
38
class GitTagDict(tag.BasicTags):
 
39
 
 
40
    def __init__(self, branch):
 
41
        self.branch = branch
 
42
        self.repository = branch.repository
 
43
 
 
44
    def get_tag_dict(self):
 
45
        ret = {}
 
46
        for k,v in self.repository._git.tags.iteritems():
 
47
            obj = self.repository._git.get_object(v)
 
48
            while isinstance(obj, Tag):
 
49
                v = obj.object[1]
 
50
                obj = self.repository._git.get_object(v)
 
51
            if not isinstance(obj, Commit):
 
52
                mutter("Tag %s points at object %r that is not a commit, ignoring", k, obj)
 
53
                continue
 
54
            ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
 
55
        return ret
 
56
 
 
57
    def set_tag(self, name, revid):
 
58
        self.repository._git.tags[name] = revid
27
59
 
28
60
 
29
61
class GitBranchConfig(config.BranchConfig):
34
66
        # do not provide a BranchDataConfig
35
67
        self.option_sources = self.option_sources[0], self.option_sources[2]
36
68
 
37
 
    def set_user_option(self, name, value, local=False):
 
69
    def set_user_option(self, name, value, store=config.STORE_BRANCH, warn_masked=False):
38
70
        """Force local to True"""
39
 
        config.BranchConfig.set_user_option(self, name, value, local=True)
 
71
        config.BranchConfig.set_user_option(self, name, value, store=config.STORE_LOCATION, warn_masked=warn_masked)
40
72
 
41
73
 
42
74
class GitBranchFormat(branch.BranchFormat):
44
76
    def get_format_description(self):
45
77
        return 'Git Branch'
46
78
 
47
 
 
48
 
class GitBranch(branch.Branch):
 
79
    def supports_tags(self):
 
80
        return True
 
81
 
 
82
    def make_tags(self, branch):
 
83
        if getattr(branch.repository, "get_refs", None) is not None:
 
84
            from bzrlib.plugins.git.remote import RemoteGitTagDict
 
85
            return RemoteGitTagDict(branch)
 
86
        return GitTagDict(branch)
 
87
 
 
88
 
 
89
class GitBranch(ForeignBranch):
49
90
    """An adapter to git repositories for bzr Branch objects."""
50
91
 
51
 
    def __init__(self, bzrdir, repository, head, base, lockfiles):
52
 
        super(GitBranch, self).__init__()
 
92
    def __init__(self, bzrdir, repository, name, head, lockfiles):
 
93
        self.repository = repository
 
94
        self._format = GitBranchFormat()
 
95
        super(GitBranch, self).__init__(repository.get_mapping())
53
96
        self.control_files = lockfiles
54
97
        self.bzrdir = bzrdir
55
 
        self.repository = repository
 
98
        self.name = name
56
99
        self.head = head
57
 
        self.base = base
58
 
        self._format = GitBranchFormat()
 
100
        self.base = bzrdir.transport.base
 
101
 
 
102
    def dpull(self, source, stop_revision=None):
 
103
        if stop_revision is None:
 
104
            stop_revision = source.last_revision()
 
105
        # FIXME: Check for diverged branches
 
106
        revidmap = self.repository.dfetch(source.repository, stop_revision)
 
107
        self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
 
108
        return revidmap
59
109
 
60
110
    def lock_write(self):
61
111
        self.control_files.lock_write()
62
112
 
 
113
    def get_stacked_on_url(self):
 
114
        # Git doesn't do stacking (yet...)
 
115
        return None
 
116
 
 
117
    def get_parent(self):
 
118
        """See Branch.get_parent()."""
 
119
        return None
 
120
 
 
121
    def set_parent(self, url):
 
122
        pass
 
123
 
 
124
    def lock_read(self):
 
125
        self.control_files.lock_read()
 
126
 
 
127
    def unlock(self):
 
128
        self.control_files.unlock()
 
129
 
 
130
    def get_physical_lock_status(self):
 
131
        return False
 
132
 
 
133
 
 
134
class LocalGitBranch(GitBranch):
 
135
 
63
136
    @needs_read_lock
64
137
    def last_revision(self):
65
138
        # perhaps should escape this ?
66
139
        if self.head is None:
67
140
            return revision.NULL_REVISION
68
 
        return ids.convert_revision_id_git_to_bzr(self.head)
69
 
 
70
 
    def get_parent(self):
71
 
        """See Branch.get_parent()."""
72
 
        return None
73
 
 
74
 
    def get_stacked_on_url(self):
75
 
        return None
 
141
        return self.mapping.revision_id_foreign_to_bzr(self.head)
 
142
 
 
143
    def create_checkout(self, to_location, revision_id=None, 
 
144
                        lightweight=False, accelerator_tree=None, hardlink=False):
 
145
        if lightweight:
 
146
            raise LightWeightCheckoutsNotSupported()
 
147
        return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
 
148
 
 
149
    def _create_heavyweight_checkout(self, to_location, revision_id=None, 
 
150
                                     hardlink=False):
 
151
        """Create a new heavyweight checkout of this branch.
 
152
 
 
153
        :param to_location: URL of location to create the new checkout in.
 
154
        :param revision_id: Revision that should be the tip of the checkout.
 
155
        :param hardlink: Whether to hardlink
 
156
        :return: WorkingTree object of checkout.
 
157
        """
 
158
        checkout_branch = BzrDir.create_branch_convenience(
 
159
            to_location, force_new_tree=False, format=get_rich_root_format())
 
160
        checkout = checkout_branch.bzrdir
 
161
        checkout_branch.bind(self)
 
162
        # pull up to the specified revision_id to set the initial 
 
163
        # branch tip correctly, and seed it with history.
 
164
        checkout_branch.pull(self, stop_revision=revision_id)
 
165
        return checkout.create_workingtree(revision_id, hardlink=hardlink)
76
166
 
77
167
    def _gen_revision_history(self):
78
168
        if self.head is None:
79
169
            return []
80
 
        skip = 0
81
 
        cms = None
82
 
        ret = []
83
 
        max_count = 1000
84
 
        nextid = self.head
85
 
        while cms != []:
86
 
            cms = self.repository._git.commits(self.head, max_count=max_count, skip=skip)
87
 
            skip += max_count
88
 
            for cm in cms:
89
 
                if cm.id == nextid:
90
 
                    ret.append(ids.convert_revision_id_git_to_bzr(cm.id))
91
 
                    if cm.parents == []:
92
 
                        nextid = None
93
 
                    else:
94
 
                        nextid = cm.parents[0].id
 
170
        ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
95
171
        ret.reverse()
96
172
        return ret
97
173
 
98
174
    def get_config(self):
99
175
        return GitBranchConfig(self)
100
176
 
101
 
    def lock_read(self):
102
 
        self.control_files.lock_read()
103
 
 
104
 
    def unlock(self):
105
 
        self.control_files.unlock()
106
 
 
107
 
    def get_physical_lock_status(self):
108
 
        return False
109
 
 
110
177
    def get_push_location(self):
111
178
        """See Branch.get_push_location."""
112
179
        push_loc = self.get_config().get_user_option('push_location')
115
182
    def set_push_location(self, location):
116
183
        """See Branch.set_push_location."""
117
184
        self.get_config().set_user_option('push_location', location,
118
 
                                          local=True)
 
185
                                          store=config.STORE_LOCATION)
119
186
 
120
187
    def supports_tags(self):
121
 
        return False
 
188
        return True
 
189
 
 
190
 
 
191
class InterGitGenericBranch(branch.InterBranch):
 
192
 
 
193
    @classmethod
 
194
    def is_compatible(self, source, target):
 
195
        return isinstance(source, GitBranch)
 
196
 
 
197
    def update_revisions(self, stop_revision=None, overwrite=False,
 
198
        graph=None):
 
199
        """See InterBranch.update_revisions()."""
 
200
        # TODO: stop_revision, overwrite
 
201
        interrepo = repository.InterRepository.get(self.source.repository, 
 
202
            self.target.repository)
 
203
        self._last_revid = None
 
204
        def determine_wants(heads):
 
205
            if not self.source.name in heads:
 
206
                raise BzrError("No such remote branch '%s', found: %r" % (
 
207
                    self.source.name, heads.keys()))
 
208
            head = heads[self.source.name]
 
209
            self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
 
210
            if self.target.repository.has_revision(self._last_revid):
 
211
                return []
 
212
            return [head]
 
213
        interrepo.fetch_objects(determine_wants, self.source.mapping)
 
214
        self.target.generate_revision_history(self._last_revid)
 
215
 
 
216
 
 
217
branch.InterBranch.register_optimiser(InterGitGenericBranch)