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

Add some basic documentation in 'bzr help git'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2010 Jelmer Vernooij
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
14
15
# along with this program; if not, write to the Free Software
15
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
 
17
 
"""An adapter between a Git control dir and a Bazaar BzrDir"""
18
 
 
19
 
import os
20
 
 
21
 
import bzrlib
22
 
from bzrlib.lazy_import import lazy_import
 
18
"""An adapter between a Git control dir and a Bazaar BzrDir."""
 
19
 
23
20
from bzrlib import (
24
21
    bzrdir,
 
22
    errors as bzr_errors,
25
23
    lockable_files,
26
24
    urlutils,
 
25
    version_info as bzrlib_version,
27
26
    )
28
27
 
29
 
lazy_import(globals(), """
30
 
from bzrlib.lockable_files import TransportLock
 
28
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
29
 
31
30
from bzrlib.plugins.git import (
32
 
    errors,
33
 
    branch,
34
 
    repository,
35
 
    workingtree,
 
31
    LocalGitBzrDirFormat,
36
32
    )
37
 
""")
38
 
 
39
33
 
40
34
 
41
35
class GitLock(object):
56
50
    def validate_token(self, token):
57
51
        pass
58
52
 
 
53
    def break_lock(self):
 
54
        pass
 
55
 
59
56
 
60
57
class GitLockableFiles(lockable_files.LockableFiles):
61
58
    """Git specific lockable files abstraction."""
64
61
        self._lock = lock
65
62
        self._transaction = None
66
63
        self._lock_mode = None
67
 
        self._lock_count = 0
68
64
        self._transport = transport
 
65
        if LockWarner is None:
 
66
            # Bzr 1.13
 
67
            self._lock_count = 0
 
68
        else:
 
69
            self._lock_warner = LockWarner(repr(self))
69
70
 
70
71
 
71
72
class GitDir(bzrdir.BzrDir):
74
75
    def is_supported(self):
75
76
        return True
76
77
 
 
78
    def can_convert_format(self):
 
79
        return False
 
80
 
77
81
    def cloning_metadir(self, stacked=False):
78
 
        return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
82
        return bzrdir.format_registry.make_bzrdir("default")
 
83
 
 
84
    def _branch_name_to_ref(self, name):
 
85
        raise NotImplementedError(self._branch_name_to_ref)
 
86
 
 
87
    if bzrlib_version >= (2, 2):
 
88
        def open_branch(self, name=None, unsupported=False, 
 
89
            ignore_fallbacks=None):
 
90
            return self._open_branch(name=name,
 
91
                ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
 
92
    else:
 
93
        def open_branch(self, ignore_fallbacks=None, unsupported=False):
 
94
            return self._open_branch(name=None,
 
95
                ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
79
96
 
80
97
 
81
98
class LocalGitDir(GitDir):
82
99
    """An adapter to the '.git' dir used by git."""
83
100
 
84
 
    _gitrepository_class = repository.LocalGitRepository
 
101
    def _get_gitrepository_class(self):
 
102
        from bzrlib.plugins.git.repository import LocalGitRepository
 
103
        return LocalGitRepository
 
104
 
 
105
    _gitrepository_class = property(_get_gitrepository_class)
85
106
 
86
107
    def __init__(self, transport, lockfiles, gitrepo, format):
87
108
        self._format = format
92
113
        else:
93
114
            self.transport = transport.clone('.git')
94
115
        self._lockfiles = lockfiles
95
 
 
96
 
    def get_branch_transport(self, branch_format):
 
116
        self._mode_check_done = None
 
117
 
 
118
    def _branch_name_to_ref(self, name):
 
119
        from bzrlib.plugins.git.refs import branch_name_to_ref
 
120
        ref = branch_name_to_ref(name, None)
 
121
        if ref == "HEAD":
 
122
            from dulwich.repo import SYMREF
 
123
            refcontents = self._git.refs.read_ref(ref)
 
124
            if refcontents.startswith(SYMREF):
 
125
                ref = refcontents[len(SYMREF):]
 
126
        return ref
 
127
 
 
128
    def is_control_filename(self, filename):
 
129
        return filename == '.git' or filename.startswith('.git/')
 
130
 
 
131
    def get_branch_transport(self, branch_format, name=None):
97
132
        if branch_format is None:
98
133
            return self.transport
99
134
        if isinstance(branch_format, LocalGitBzrDirFormat):
100
135
            return self.transport
101
 
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
102
 
 
103
 
    get_repository_transport = get_branch_transport
104
 
    get_workingtree_transport = get_branch_transport
105
 
 
106
 
    def open_branch(self, ignored=None):
 
136
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
137
 
 
138
    def get_repository_transport(self, format):
 
139
        if format is None:
 
140
            return self.transport
 
141
        if isinstance(format, LocalGitBzrDirFormat):
 
142
            return self.transport
 
143
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
144
 
 
145
    def get_workingtree_transport(self, format):
 
146
        if format is None:
 
147
            return self.transport
 
148
        if isinstance(format, LocalGitBzrDirFormat):
 
149
            return self.transport
 
150
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
151
 
 
152
    def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
107
153
        """'create' a branch for this dir."""
108
154
        repo = self.open_repository()
109
 
        return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
 
155
        from bzrlib.plugins.git.branch import LocalGitBranch
 
156
        return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
 
157
            self._lockfiles)
 
158
 
 
159
    def destroy_branch(self, name=None):
 
160
        refname = self._branch_name_to_ref(name)
 
161
        if not refname in self._git.refs:
 
162
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
163
                    bzrdir=self)
 
164
        del self._git.refs[refname]
 
165
 
 
166
    def destroy_repository(self):
 
167
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
 
168
 
 
169
    def destroy_workingtree(self):
 
170
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
171
 
 
172
    def needs_format_conversion(self, format=None):
 
173
        return not isinstance(self._format, format.__class__)
 
174
 
 
175
    def list_branches(self):
 
176
        ret = []
 
177
        for name in self._git.get_refs():
 
178
            if name.startswith("refs/heads/"):
 
179
                ret.append(self.open_branch(name=name))
 
180
        return ret
110
181
 
111
182
    def open_repository(self, shared=False):
112
183
        """'open' a repository for this dir."""
113
184
        return self._gitrepository_class(self, self._lockfiles)
114
185
 
115
186
    def open_workingtree(self, recommend_upgrade=True):
116
 
        if self._git.bare:
117
 
            loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
118
 
            raise errors.bzr_errors.NoWorkingTree(loc)
119
 
        else:
120
 
            return workingtree.GitWorkingTree(self, self.open_repository(), 
121
 
                                                  self.open_branch())
 
187
        if not self._git.bare:
 
188
            from dulwich.errors import NoIndexPresent
 
189
            repo = self.open_repository()
 
190
            try:
 
191
                index = repo._git.open_index()
 
192
            except NoIndexPresent:
 
193
                pass
 
194
            else:
 
195
                from bzrlib.plugins.git.workingtree import GitWorkingTree
 
196
                try:
 
197
                    branch = self.open_branch()
 
198
                except bzr_errors.NotBranchError:
 
199
                    pass
 
200
                else:
 
201
                    return GitWorkingTree(self, repo, branch, index)
 
202
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
203
        raise bzr_errors.NoWorkingTree(loc)
122
204
 
123
205
    def create_repository(self, shared=False):
124
206
        return self.open_repository()
125
207
 
126
 
 
127
 
class GitBzrDirFormat(bzrdir.BzrDirFormat):
128
 
    _lock_class = TransportLock
129
 
 
130
 
    def is_supported(self):
131
 
        return True
132
 
 
133
 
 
134
 
class LocalGitBzrDirFormat(GitBzrDirFormat):
135
 
    """The .git directory control format."""
136
 
 
137
 
    _gitdir_class = LocalGitDir
138
 
 
139
 
    @classmethod
140
 
    def _known_formats(self):
141
 
        return set([LocalGitBzrDirFormat()])
142
 
 
143
 
    def open(self, transport, _found=None):
144
 
        """Open this directory.
145
 
 
146
 
        """
147
 
        from bzrlib.plugins.git import git
148
 
        # we dont grok readonly - git isn't integrated with transport.
149
 
        url = transport.base
150
 
        if url.startswith('readonly+'):
151
 
            url = url[len('readonly+'):]
152
 
 
153
 
        try:
154
 
            gitrepo = git.repo.Repo(transport.local_abspath("."))
155
 
        except errors.bzr_errors.NotLocalUrl:
156
 
            raise errors.bzr_errors.NotBranchError(path=transport.base)
157
 
        lockfiles = GitLockableFiles(transport, GitLock())
158
 
        return self._gitdir_class(transport, lockfiles, gitrepo, self)
159
 
 
160
 
    @classmethod
161
 
    def probe_transport(klass, transport):
162
 
        """Our format is present if the transport ends in '.not/'."""
163
 
        from bzrlib.plugins.git import git
164
 
        # little ugly, but works
165
 
        format = klass()
166
 
        # delegate to the main opening code. This pays a double rtt cost at the
167
 
        # moment, so perhaps we want probe_transport to return the opened thing
168
 
        # rather than an openener ? or we could return a curried thing with the
169
 
        # dir to open already instantiated ? Needs more thought.
170
 
        try:
171
 
            format.open(transport)
172
 
            return format
173
 
        except git.errors.NotGitRepository, e:
174
 
            raise errors.bzr_errors.NotBranchError(path=transport.base)
175
 
        raise errors.bzr_errors.NotBranchError(path=transport.base)
176
 
 
177
 
    def get_format_description(self):
178
 
        return "Local Git Repository"
179
 
 
180
 
    def get_format_string(self):
181
 
        return "Local Git Repository"
182
 
 
183
 
    def initialize_on_transport(self, transport):
184
 
        from bzrlib.transport.local import LocalTransport
185
 
        from bzrlib.plugins.git import git
186
 
 
187
 
        if not isinstance(transport, LocalTransport):
188
 
            raise NotImplementedError(self.initialize, 
189
 
                "Can't create Git Repositories/branches on "
190
 
                "non-local transports")
191
 
 
192
 
        git.repo.Repo.create(transport.local_abspath(".")) 
193
 
        return self.open(transport)
194
 
 
195
 
    def is_supported(self):
196
 
        return True
197
 
 
198
 
 
199
 
class RemoteGitBzrDirFormat(GitBzrDirFormat):
200
 
    """The .git directory control format."""
201
 
 
202
 
    @classmethod
203
 
    def _known_formats(self):
204
 
        return set([RemoteGitBzrDirFormat()])
205
 
 
206
 
    def open(self, transport, _found=None):
207
 
        """Open this directory.
208
 
 
209
 
        """
210
 
        from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
211
 
        if not isinstance(transport, GitSmartTransport):
212
 
            raise errors.bzr_errors.NotBranchError(transport.base)
213
 
        # we dont grok readonly - git isn't integrated with transport.
214
 
        url = transport.base
215
 
        if url.startswith('readonly+'):
216
 
            url = url[len('readonly+'):]
217
 
 
218
 
        lockfiles = GitLockableFiles(transport, GitLock())
219
 
        return RemoteGitDir(transport, lockfiles, self)
220
 
 
221
 
    @classmethod
222
 
    def probe_transport(klass, transport):
223
 
        """Our format is present if the transport ends in '.not/'."""
224
 
        # little ugly, but works
225
 
        format = klass()
226
 
        from bzrlib.plugins.git.remote import GitSmartTransport
227
 
        if not isinstance(transport, GitSmartTransport):
228
 
            raise errors.bzr_errors.NotBranchError(transport.base)
229
 
        # The only way to know a path exists and contains a valid repository 
230
 
        # is to do a request against it:
231
 
        try:
232
 
            transport.fetch_pack(lambda x: [], None, lambda x: None, 
233
 
                                 lambda x: mutter("git: %s" % x))
234
 
        except errors.git_errors.GitProtocolError:
235
 
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
208
    def create_branch(self, name=None):
 
209
        refname = self._branch_name_to_ref(name)
 
210
        from dulwich.protocol import ZERO_SHA
 
211
        self._git.refs[refname or "HEAD"] = ZERO_SHA
 
212
        return self.open_branch(name)
 
213
 
 
214
    def backup_bzrdir(self):
 
215
        if self._git.bare:
 
216
            self.root_transport.copy_tree(".git", ".git.backup")
 
217
            return (self.root_transport.abspath(".git"),
 
218
                    self.root_transport.abspath(".git.backup"))
236
219
        else:
237
 
            return format
238
 
        raise errors.bzr_errors.NotBranchError(path=transport.base)
239
 
 
240
 
    def get_format_description(self):
241
 
        return "Remote Git Repository"
242
 
 
243
 
    def get_format_string(self):
244
 
        return "Remote Git Repository"
245
 
 
246
 
    def initialize_on_transport(self, transport):
247
 
        raise errors.bzr_errors.UninitializableFormat(self)
248
 
 
 
220
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
221
 
 
222
    def create_workingtree(self, revision_id=None, from_branch=None,
 
223
        accelerator_tree=None, hardlink=False):
 
224
        if self._git.bare:
 
225
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
 
226
        from dulwich.index import write_index
 
227
        from dulwich.pack import SHA1Writer
 
228
        f = open(self.transport.local_abspath("index"), 'w+')
 
229
        try:
 
230
            f = SHA1Writer(f)
 
231
            write_index(f, [])
 
232
        finally:
 
233
            f.close()
 
234
        return self.open_workingtree()