/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

Merge new dulwich.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""An adapter between a Git control dir and a Bazaar BzrDir"""
18
18
 
 
19
import os
 
20
 
 
21
import bzrlib
 
22
from bzrlib.lazy_import import lazy_import
19
23
from bzrlib import (
20
24
    bzrdir,
21
 
    errors,
22
25
    lockable_files,
23
26
    urlutils,
24
27
    )
25
28
 
26
 
from bzrlib.plugins.git.gitlib import (
27
 
    git_branch,
28
 
    git_repository,
 
29
lazy_import(globals(), """
 
30
from bzrlib.lockable_files import TransportLock
 
31
from bzrlib.plugins.git import (
 
32
    errors,
 
33
    branch,
 
34
    repository,
 
35
    workingtree,
29
36
    )
 
37
""")
 
38
 
30
39
 
31
40
 
32
41
class GitLock(object):
33
42
    """A lock that thunks through to Git."""
34
43
 
35
 
    def lock_write(self):
 
44
    def lock_write(self, token=None):
36
45
        pass
37
46
 
38
47
    def lock_read(self):
41
50
    def unlock(self):
42
51
        pass
43
52
 
 
53
    def peek(self):
 
54
        pass
 
55
 
44
56
 
45
57
class GitLockableFiles(lockable_files.LockableFiles):
46
58
    """Git specific lockable files abstraction."""
55
67
class GitDir(bzrdir.BzrDir):
56
68
    """An adapter to the '.git' dir used by git."""
57
69
 
58
 
    def __init__(self, transport, lockfiles, format):
 
70
    _gitrepository_class = repository.GitRepository
 
71
 
 
72
    def __init__(self, transport, lockfiles, gitrepo, format):
59
73
        self._format = format
60
74
        self.root_transport = transport
61
 
        self.transport = transport.clone('.git')
 
75
        self._git = gitrepo
 
76
        if gitrepo.bare:
 
77
            self.transport = transport
 
78
        else:
 
79
            self.transport = transport.clone('.git')
62
80
        self._lockfiles = lockfiles
63
81
 
64
82
    def get_branch_transport(self, branch_format):
66
84
            return self.transport
67
85
        if isinstance(branch_format, GitBzrDirFormat):
68
86
            return self.transport
69
 
        raise errors.IncompatibleFormat(branch_format, self._format)
 
87
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
70
88
 
71
89
    get_repository_transport = get_branch_transport
72
90
    get_workingtree_transport = get_branch_transport
75
93
        return True
76
94
 
77
95
    def open_branch(self, ignored=None):
78
 
        """'crate' a branch for this dir."""
79
 
        return git_branch.GitBranch(self, self._lockfiles)
 
96
        """'create' a branch for this dir."""
 
97
        repo = self.open_repository()
 
98
        if repo._git.heads == []:
 
99
            head = None
 
100
        else:
 
101
            head = repo._git.head()
 
102
        return branch.GitBranch(self, repo, head, 
 
103
                                    self.root_transport.base, self._lockfiles)
80
104
 
81
105
    def open_repository(self, shared=False):
82
106
        """'open' a repository for this dir."""
83
 
        return git_repository.GitRepository(self, self._lockfiles)
84
 
 
85
 
    def open_workingtree(self):
86
 
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
87
 
        raise errors.NoWorkingTree(loc)
 
107
        return self._gitrepository_class(self, self._lockfiles)
 
108
 
 
109
    def open_workingtree(self, recommend_upgrade=True):
 
110
        if self._git.bare:
 
111
            loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
112
            raise errors.bzr_errors.NoWorkingTree(loc)
 
113
        else:
 
114
            return workingtree.GitWorkingTree(self, self.open_repository(), 
 
115
                                                  self.open_branch())
 
116
 
 
117
    def cloning_metadir(self, stacked=False):
 
118
        if stacked:
 
119
            return bzrlib.bzrdir.format_registry.make_bzrdir("pack-0.92")
 
120
        else:
 
121
            return bzrlib.bzrdir.format_registry.make_bzrdir("1.6")
 
122
 
 
123
    def create_repository(self, shared=False):
 
124
        return self.open_repository()
88
125
 
89
126
 
90
127
class GitBzrDirFormat(bzrdir.BzrDirFormat):
91
128
    """The .git directory control format."""
92
129
 
 
130
    _gitdir_class = GitDir
 
131
    _lock_class = TransportLock
 
132
 
93
133
    @classmethod
94
134
    def _known_formats(self):
95
135
        return set([GitBzrDirFormat()])
96
136
 
97
 
    def open(self, transport, _create=False, _found=None):
 
137
    def open(self, transport, _found=None):
98
138
        """Open this directory.
99
139
 
100
 
        :param _create: create the git dir on the fly. private to GitDirFormat.
101
140
        """
 
141
        from bzrlib.plugins.git import git
102
142
        # we dont grok readonly - git isn't integrated with transport.
103
143
        url = transport.base
104
144
        if url.startswith('readonly+'):
105
145
            url = url[len('readonly+'):]
106
 
        path = urlutils.local_path_from_url(url)
107
 
        if not transport.has('.git'):
108
 
            raise errors.NotBranchError(path=transport.base)
 
146
 
 
147
        try:
 
148
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
149
        except errors.bzr_errors.NotLocalUrl:
 
150
            raise errors.bzr_errors.NotBranchError(path=transport.base)
109
151
        lockfiles = GitLockableFiles(GitLock())
110
 
        return GitDir(transport, lockfiles, self)
 
152
        return self._gitdir_class(transport, lockfiles, gitrepo, self)
111
153
 
112
154
    @classmethod
113
155
    def probe_transport(klass, transport):
122
164
            format.open(transport)
123
165
            return format
124
166
        except Exception, e:
125
 
            raise errors.NotBranchError(path=transport.base)
126
 
        raise errors.NotBranchError(path=transport.base)
 
167
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
168
        raise errors.bzr_errors.NotBranchError(path=transport.base)
 
169
 
 
170
    def get_format_description(self):
 
171
        return "Local Git Repository"
 
172
 
 
173
    def get_format_string(self):
 
174
        return "Local Git Repository"
 
175
 
 
176
    def initialize_on_transport(self, transport):
 
177
        from bzrlib.transport.local import LocalTransport
 
178
        from bzrlib.plugins.git import git
 
179
 
 
180
        if not isinstance(transport, LocalTransport):
 
181
            raise NotImplementedError(self.initialize, 
 
182
                "Can't create Git Repositories/branches on "
 
183
                "non-local transports")
 
184
 
 
185
        git.repo.Repo.create(transport.local_abspath(".")) 
 
186
        return self.open(transport)
127
187
 
128
188
 
129
189
bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)