/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

Use transports in git-import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
from bzrlib import (
21
21
    errors as bzr_errors,
22
22
    lockable_files,
 
23
    trace,
23
24
    urlutils,
24
25
    version_info as bzrlib_version,
25
26
    )
 
27
from bzrlib.bzrdir import CreateRepository
 
28
from bzrlib.transport import do_catching_redirections
26
29
 
27
30
LockWarner = getattr(lockable_files, "_LockWarner", None)
28
31
 
29
 
from bzrlib.plugins.git import (
30
 
    BareLocalGitControlDirFormat,
31
 
    LocalGitControlDirFormat,
 
32
from bzrlib.controldir import (
 
33
    ControlDir,
 
34
    ControlDirFormat,
 
35
    format_registry,
32
36
    )
33
 
try:
34
 
    from bzrlib.controldir import (
35
 
        ControlDir,
36
 
        format_registry,
37
 
        )
38
 
except ImportError:
39
 
    # bzr < 2.3
40
 
    from bzrlib.bzrdir import (
41
 
        BzrDir,
42
 
        format_registry,
43
 
        )
44
 
    ControlDir = BzrDir
45
37
 
46
38
 
47
39
class GitLock(object):
48
40
    """A lock that thunks through to Git."""
49
41
 
 
42
    def __init__(self):
 
43
        self.lock_name = "git lock"
 
44
 
50
45
    def lock_write(self, token=None):
51
46
        pass
52
47
 
90
85
        raise bzr_errors.BzrError("Cannot set configuration")
91
86
 
92
87
 
 
88
class GitControlDirFormat(ControlDirFormat):
 
89
 
 
90
    _lock_class = lockable_files.TransportLock
 
91
 
 
92
    colocated_branches = True
 
93
    fixed_components = True
 
94
 
 
95
    def __eq__(self, other):
 
96
        return type(self) == type(other)
 
97
 
 
98
    def is_supported(self):
 
99
        return True
 
100
 
 
101
    def network_name(self):
 
102
        return "git"
 
103
 
 
104
 
93
105
class GitDir(ControlDir):
94
106
    """An adapter to the '.git' dir used by git."""
95
107
 
121
133
    def get_config(self):
122
134
        return GitDirConfig()
123
135
 
 
136
    def clone_on_transport(self, transport, revision_id=None,
 
137
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
138
        create_prefix=False, use_existing_dir=True, no_tree=False):
 
139
        from dulwich.protocol import ZERO_SHA
 
140
        """See ControlDir.clone_on_transport."""
 
141
        if no_tree:
 
142
            format = BareLocalGitControlDirFormat()
 
143
        else:
 
144
            format = LocalGitControlDirFormat()
 
145
        (target_repo, target_controldir, stacking, repo_policy) = format.initialize_on_transport_ex(transport, use_existing_dir=use_existing_dir, create_prefix=create_prefix, force_new_repo=force_new_repo)
 
146
        target_git_repo = target_repo._git
 
147
        source_repo = self.open_repository()
 
148
        source_git_repo = source_repo._git
 
149
        if revision_id is not None:
 
150
            git_sha, mapping = source_repo.lookup_bzr_revision_id(revision_id)
 
151
            if git_sha == ZERO_SHA:
 
152
                wants = []
 
153
            else:
 
154
                wants = [git_sha]
 
155
            determine_wants = lambda heads: wants
 
156
        else:
 
157
            determine_wants = target_git_repo.object_store.determine_wants_all
 
158
        refs = source_git_repo.fetch(target_git_repo, determine_wants)
 
159
        for name, val in refs.iteritems():
 
160
            target_git_repo.refs[name] = val
 
161
        lockfiles = GitLockableFiles(transport, GitLock())
 
162
        return self.__class__(transport, lockfiles, target_git_repo, format)
 
163
 
 
164
 
 
165
class LocalGitControlDirFormat(GitControlDirFormat):
 
166
    """The .git directory control format."""
 
167
 
 
168
    bare = False
 
169
 
 
170
    @classmethod
 
171
    def _known_formats(self):
 
172
        return set([LocalGitControlDirFormat()])
 
173
 
 
174
    @property
 
175
    def repository_format(self):
 
176
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
177
        return GitRepositoryFormat()
 
178
 
 
179
    def get_branch_format(self):
 
180
        from bzrlib.plugins.git.branch import GitBranchFormat
 
181
        return GitBranchFormat()
 
182
 
 
183
    def open(self, transport, _found=None):
 
184
        """Open this directory.
 
185
 
 
186
        """
 
187
        from bzrlib.plugins.git.transportgit import TransportRepo
 
188
        gitrepo = TransportRepo(transport)
 
189
        lockfiles = GitLockableFiles(transport, GitLock())
 
190
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
191
 
 
192
    def get_format_description(self):
 
193
        return "Local Git Repository"
 
194
 
 
195
    def initialize_on_transport(self, transport):
 
196
        from bzrlib.plugins.git.transportgit import TransportRepo
 
197
        TransportRepo.init(transport, bare=self.bare)
 
198
        return self.open(transport)
 
199
 
 
200
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
201
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
202
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
203
        shared_repo=False, vfs_only=False):
 
204
        def make_directory(transport):
 
205
            transport.mkdir('.')
 
206
            return transport
 
207
        def redirected(transport, e, redirection_notice):
 
208
            trace.note(redirection_notice)
 
209
            return transport._redirected_to(e.source, e.target)
 
210
        try:
 
211
            transport = do_catching_redirections(make_directory, transport,
 
212
                redirected)
 
213
        except bzr_errors.FileExists:
 
214
            if not use_existing_dir:
 
215
                raise
 
216
        except bzr_errors.NoSuchFile:
 
217
            if not create_prefix:
 
218
                raise
 
219
            transport.create_prefix()
 
220
        controldir = self.initialize_on_transport(transport)
 
221
        repository = controldir.open_repository()
 
222
        repository.lock_write()
 
223
        return (repository, controldir, False, CreateRepository(controldir))
 
224
 
 
225
    def is_supported(self):
 
226
        return True
 
227
 
 
228
 
 
229
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
230
 
 
231
    bare = True
 
232
    supports_workingtrees = False
 
233
 
 
234
    def get_format_description(self):
 
235
        return "Local Git Repository (bare)"
 
236
 
124
237
 
125
238
class LocalGitDir(GitDir):
126
239
    """An adapter to the '.git' dir used by git."""
162
275
        return ref
163
276
 
164
277
    def is_control_filename(self, filename):
165
 
        return filename == '.git' or filename.startswith('.git/')
 
278
        return (filename == '.git' or filename.startswith('.git/'))
166
279
 
167
280
    def get_branch_transport(self, branch_format, name=None):
168
281
        if branch_format is None:
215
328
                ret.append(self.open_branch(name=name))
216
329
        return ret
217
330
 
218
 
    def open_repository(self, shared=False):
 
331
    def open_repository(self):
219
332
        """'open' a repository for this dir."""
220
333
        return self._gitrepository_class(self, self._lockfiles)
221
334
 
239
352
        raise bzr_errors.NoWorkingTree(loc)
240
353
 
241
354
    def create_repository(self, shared=False):
 
355
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
356
        if shared:
 
357
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
242
358
        return self.open_repository()
243
359
 
244
 
    def create_branch(self, name=None):
 
360
    def create_branch(self, name=None, repository=None):
245
361
        refname = self._branch_name_to_ref(name)
246
362
        from dulwich.protocol import ZERO_SHA
247
363
        self._git.refs[refname or "HEAD"] = ZERO_SHA
258
374
    def create_workingtree(self, revision_id=None, from_branch=None,
259
375
        accelerator_tree=None, hardlink=False):
260
376
        if self._git.bare:
261
 
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
 
377
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
262
378
        from dulwich.index import write_index
263
379
        from dulwich.pack import SHA1Writer
264
380
        f = open(self.transport.local_abspath("index"), 'w+')
278
394
        """
279
395
        return self.open_repository()
280
396
 
 
397
    def _find_or_create_repository(self, force_new_repo=None):
 
398
        return self.create_repository(shared=False)
 
399
 
281
400
    def _find_creation_modes(self):
282
401
        """Determine the appropriate modes for files and directories.
283
402
 
291
410
        self._mode_check_done = True
292
411
        try:
293
412
            st = self.transport.stat('.')
294
 
        except TransportNotPossible:
 
413
        except bzr_errors.TransportNotPossible:
295
414
            self._dir_mode = None
296
415
            self._file_mode = None
297
416
        else:
322
441
            self._find_creation_modes()
323
442
        return self._dir_mode
324
443
 
325