/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 basic infrastructure for dpush.

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 git, os
 
19
import os
20
20
 
 
21
import bzrlib
21
22
from bzrlib.lazy_import import lazy_import
22
23
from bzrlib import (
23
24
    bzrdir,
35
36
    )
36
37
""")
37
38
 
 
39
from bzrlib.plugins.git import LocalGitBzrDirFormat
 
40
 
 
41
 
38
42
 
39
43
class GitLock(object):
40
44
    """A lock that thunks through to Git."""
51
55
    def peek(self):
52
56
        pass
53
57
 
 
58
    def validate_token(self, token):
 
59
        pass
 
60
 
54
61
 
55
62
class GitLockableFiles(lockable_files.LockableFiles):
56
63
    """Git specific lockable files abstraction."""
57
64
 
58
 
    def __init__(self, lock):
 
65
    def __init__(self, transport, lock):
59
66
        self._lock = lock
60
67
        self._transaction = None
61
68
        self._lock_mode = None
62
69
        self._lock_count = 0
 
70
        self._transport = transport
63
71
 
64
72
 
65
73
class GitDir(bzrdir.BzrDir):
66
74
    """An adapter to the '.git' dir used by git."""
67
75
 
68
 
    _gitrepository_class = repository.GitRepository
 
76
    def is_supported(self):
 
77
        return True
 
78
 
 
79
    def cloning_metadir(self, stacked=False):
 
80
        return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
81
 
 
82
 
 
83
class LocalGitDir(GitDir):
 
84
    """An adapter to the '.git' dir used by git."""
 
85
 
 
86
    _gitrepository_class = repository.LocalGitRepository
69
87
 
70
88
    def __init__(self, transport, lockfiles, gitrepo, format):
71
89
        self._format = format
80
98
    def get_branch_transport(self, branch_format):
81
99
        if branch_format is None:
82
100
            return self.transport
83
 
        if isinstance(branch_format, GitBzrDirFormat):
 
101
        if isinstance(branch_format, LocalGitBzrDirFormat):
84
102
            return self.transport
85
103
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
86
104
 
87
105
    get_repository_transport = get_branch_transport
88
106
    get_workingtree_transport = get_branch_transport
89
107
 
90
 
    def is_supported(self):
91
 
        return True
92
 
 
93
108
    def open_branch(self, ignored=None):
94
109
        """'create' a branch for this dir."""
95
110
        repo = self.open_repository()
96
 
        if repo._git.heads == []:
97
 
            head = None
98
 
        else:
99
 
            head = repo._git.heads[0].commit.id
100
 
        return branch.GitBranch(self, repo, head, 
101
 
                                    self.root_transport.base, self._lockfiles)
 
111
        return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
102
112
 
103
113
    def open_repository(self, shared=False):
104
114
        """'open' a repository for this dir."""
105
115
        return self._gitrepository_class(self, self._lockfiles)
106
116
 
107
117
    def open_workingtree(self, recommend_upgrade=True):
108
 
        if self._git.bare:
109
 
            loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
110
 
            raise errors.bzr_errors.NoWorkingTree(loc)
111
 
        else:
 
118
        if (not self._git.bare and 
 
119
            os.path.exists(os.path.join(self._git.controldir(), "index"))):
112
120
            return workingtree.GitWorkingTree(self, self.open_repository(), 
113
121
                                                  self.open_branch())
114
 
 
115
 
    def cloning_metadir(self):
116
 
        return bzrdir.BzrDirFormat.get_default_format()
 
122
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
123
        raise errors.bzr_errors.NoWorkingTree(loc)
117
124
 
118
125
    def create_repository(self, shared=False):
119
126
        return self.open_repository()
120
 
 
121
 
 
122
 
class GitBzrDirFormat(bzrdir.BzrDirFormat):
123
 
    """The .git directory control format."""
124
 
 
125
 
    _gitdir_class = GitDir
126
 
    _lock_class = TransportLock
127
 
 
128
 
    @classmethod
129
 
    def _known_formats(self):
130
 
        return set([GitBzrDirFormat()])
131
 
 
132
 
    def open(self, transport, _found=None):
133
 
        """Open this directory.
134
 
 
135
 
        """
136
 
        # we dont grok readonly - git isn't integrated with transport.
137
 
        url = transport.base
138
 
        if url.startswith('readonly+'):
139
 
            url = url[len('readonly+'):]
140
 
 
141
 
        try:
142
 
            gitrepo = git.repo.Repo(transport.local_abspath("."))
143
 
        except errors.bzr_errors.NotLocalUrl:
144
 
            raise errors.bzr_errors.NotBranchError(path=transport.base)
145
 
        lockfiles = GitLockableFiles(GitLock())
146
 
        return self._gitdir_class(transport, lockfiles, gitrepo, self)
147
 
 
148
 
    @classmethod
149
 
    def probe_transport(klass, transport):
150
 
        """Our format is present if the transport ends in '.not/'."""
151
 
        # little ugly, but works
152
 
        format = klass()
153
 
        # delegate to the main opening code. This pays a double rtt cost at the
154
 
        # moment, so perhaps we want probe_transport to return the opened thing
155
 
        # rather than an openener ? or we could return a curried thing with the
156
 
        # dir to open already instantiated ? Needs more thought.
157
 
        try:
158
 
            format.open(transport)
159
 
            return format
160
 
        except Exception, e:
161
 
            raise errors.bzr_errors.NotBranchError(path=transport.base)
162
 
        raise errors.bzr_errors.NotBranchError(path=transport.base)
163
 
 
164
 
    def get_format_description(self):
165
 
        return "Local Git Repository"
166
 
 
167
 
    def get_format_string(self):
168
 
        return "Local Git Repository"
169
 
 
170
 
    def initialize_on_transport(self, transport):
171
 
        from bzrlib.transport.local import LocalTransport
172
 
 
173
 
        if not isinstance(transport, LocalTransport):
174
 
            raise NotImplementedError(self.initialize, 
175
 
                "Can't create Git Repositories/branches on "
176
 
                "non-local transports")
177
 
 
178
 
        git.repo.Repo.create(transport.local_abspath(".")) 
179
 
        return self.open(transport)
180
 
 
181
 
 
182
 
bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)