/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

More work on roundtrip push support.

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,
24
 
    osutils,
25
23
    urlutils,
 
24
    version_info as bzrlib_version,
26
25
    )
27
 
from bzrlib.bzrdir import CreateRepository
28
 
from bzrlib.transport import do_catching_redirections
29
26
 
30
27
LockWarner = getattr(lockable_files, "_LockWarner", None)
31
28
 
32
 
from bzrlib.controldir import (
33
 
    ControlDir,
34
 
    ControlDirFormat,
35
 
    format_registry,
 
29
from bzrlib.plugins.git import (
 
30
    BareLocalGitControlDirFormat,
 
31
    LocalGitControlDirFormat,
36
32
    )
 
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
37
45
 
38
46
 
39
47
class GitLock(object):
40
48
    """A lock that thunks through to Git."""
41
49
 
42
 
    def __init__(self):
43
 
        self.lock_name = "git lock"
44
 
 
45
50
    def lock_write(self, token=None):
46
51
        pass
47
52
 
58
63
        pass
59
64
 
60
65
    def break_lock(self):
61
 
        raise NotImplementedError(self.break_lock)
62
 
 
63
 
    def dont_leave_in_place(self):
64
 
        raise NotImplementedError(self.dont_leave_in_place)
65
 
 
66
 
    def leave_in_place(self):
67
 
        raise NotImplementedError(self.leave_in_place)
 
66
        pass
68
67
 
69
68
 
70
69
class GitLockableFiles(lockable_files.LockableFiles):
91
90
        raise bzr_errors.BzrError("Cannot set configuration")
92
91
 
93
92
 
94
 
class GitControlDirFormat(ControlDirFormat):
95
 
 
96
 
    _lock_class = lockable_files.TransportLock
97
 
 
98
 
    colocated_branches = True
99
 
    fixed_components = True
100
 
 
101
 
    def __eq__(self, other):
102
 
        return type(self) == type(other)
103
 
 
104
 
    def is_supported(self):
105
 
        return True
106
 
 
107
 
    def network_name(self):
108
 
        return "git"
109
 
 
110
 
 
111
93
class GitDir(ControlDir):
112
94
    """An adapter to the '.git' dir used by git."""
113
95
 
123
105
    def cloning_metadir(self, stacked=False):
124
106
        return format_registry.make_bzrdir("default")
125
107
 
126
 
    def checkout_metadir(self, stacked=False):
127
 
        return format_registry.make_bzrdir("default")
128
 
 
129
108
    def _branch_name_to_ref(self, name):
130
109
        raise NotImplementedError(self._branch_name_to_ref)
131
110
 
 
111
    if bzrlib_version >= (2, 2):
 
112
        def open_branch(self, name=None, unsupported=False, 
 
113
            ignore_fallbacks=None):
 
114
            return self._open_branch(name=name,
 
115
                ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
 
116
    else:
 
117
        def open_branch(self, ignore_fallbacks=None, unsupported=False):
 
118
            return self._open_branch(name=None,
 
119
                ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
 
120
 
132
121
    def get_config(self):
133
122
        return GitDirConfig()
134
123
 
135
 
    def _available_backup_name(self, base):
136
 
        return osutils.available_backup_name(base, self.root_transport.has)
137
 
 
138
 
    def sprout(self, url, revision_id=None, force_new_repo=False,
139
 
               recurse='down', possible_transports=None,
140
 
               accelerator_tree=None, hardlink=False, stacked=False,
141
 
               source_branch=None, create_tree_if_local=True):
142
 
        from bzrlib.repository import InterRepository
143
 
        from bzrlib.transport.local import LocalTransport
144
 
        from bzrlib.transport import get_transport
145
 
        target_transport = get_transport(url, possible_transports)
146
 
        target_transport.ensure_base()
147
 
        cloning_format = self.cloning_metadir()
148
 
        # Create/update the result branch
149
 
        result = cloning_format.initialize_on_transport(target_transport)
150
 
        source_branch = self.open_branch()
151
 
        source_repository = self.find_repository()
152
 
        try:
153
 
            result_repo = result.find_repository()
154
 
        except bzr_errors.NoRepositoryPresent:
155
 
            result_repo = result.create_repository()
156
 
            target_is_empty = True
157
 
        else:
158
 
            target_is_empty = None # Unknown
159
 
        if stacked:
160
 
            raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
161
 
        interrepo = InterRepository.get(source_repository, result_repo)
162
 
 
163
 
        if revision_id is not None:
164
 
            determine_wants = interrepo.get_determine_wants_revids(
165
 
                [revision_id], include_tags=True)
166
 
        else:
167
 
            determine_wants = interrepo.determine_wants_all
168
 
        interrepo.fetch_objects(determine_wants=determine_wants,
169
 
            mapping=source_branch.mapping)
170
 
        result_branch = source_branch.sprout(result,
171
 
            revision_id=revision_id, repository=result_repo)
172
 
        if (create_tree_if_local and isinstance(target_transport, LocalTransport)
173
 
            and (result_repo is None or result_repo.make_working_trees())):
174
 
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
175
 
                hardlink=hardlink, from_branch=result_branch)
176
 
            wt.lock_write()
177
 
            try:
178
 
                if wt.path2id('') is None:
179
 
                    try:
180
 
                        wt.set_root_id(self.open_workingtree.get_root_id())
181
 
                    except bzr_errors.NoWorkingTree:
182
 
                        pass
183
 
            finally:
184
 
                wt.unlock()
185
 
        return result
186
 
 
187
 
    def clone_on_transport(self, transport, revision_id=None,
188
 
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
189
 
        create_prefix=False, use_existing_dir=True, no_tree=False):
190
 
        """See ControlDir.clone_on_transport."""
191
 
        from bzrlib.repository import InterRepository
192
 
        from bzrlib.plugins.git.mapping import default_mapping
193
 
        if no_tree:
194
 
            format = BareLocalGitControlDirFormat()
195
 
        else:
196
 
            format = LocalGitControlDirFormat()
197
 
        (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)
198
 
        target_git_repo = target_repo._git
199
 
        source_repo = self.open_repository()
200
 
        source_git_repo = source_repo._git
201
 
        interrepo = InterRepository.get(source_repo, target_repo)
202
 
        if revision_id is not None:
203
 
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
204
 
        else:
205
 
            determine_wants = interrepo.determine_wants_all
206
 
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
207
 
            mapping=default_mapping)
208
 
        for name, val in refs.iteritems():
209
 
            target_git_repo.refs[name] = val
210
 
        lockfiles = GitLockableFiles(transport, GitLock())
211
 
        return self.__class__(transport, lockfiles, target_git_repo, format)
212
 
 
213
 
    def find_repository(self):
214
 
        """Find the repository that should be used.
215
 
 
216
 
        This does not require a branch as we use it to find the repo for
217
 
        new branches as well as to hook existing branches up to their
218
 
        repository.
219
 
        """
220
 
        return self.open_repository()
221
 
 
222
 
 
223
 
class LocalGitControlDirFormat(GitControlDirFormat):
224
 
    """The .git directory control format."""
225
 
 
226
 
    bare = False
227
 
 
228
 
    @classmethod
229
 
    def _known_formats(self):
230
 
        return set([LocalGitControlDirFormat()])
231
 
 
232
 
    @property
233
 
    def repository_format(self):
234
 
        from bzrlib.plugins.git.repository import GitRepositoryFormat
235
 
        return GitRepositoryFormat()
236
 
 
237
 
    def get_branch_format(self):
238
 
        from bzrlib.plugins.git.branch import GitBranchFormat
239
 
        return GitBranchFormat()
240
 
 
241
 
    def open(self, transport, _found=None):
242
 
        """Open this directory.
243
 
 
244
 
        """
245
 
        from bzrlib.plugins.git.transportgit import TransportRepo
246
 
        gitrepo = TransportRepo(transport)
247
 
        lockfiles = GitLockableFiles(transport, GitLock())
248
 
        return LocalGitDir(transport, lockfiles, gitrepo, self)
249
 
 
250
 
    def get_format_description(self):
251
 
        return "Local Git Repository"
252
 
 
253
 
    def initialize_on_transport(self, transport):
254
 
        from bzrlib.plugins.git.transportgit import TransportRepo
255
 
        TransportRepo.init(transport, bare=self.bare)
256
 
        return self.open(transport)
257
 
 
258
 
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
259
 
        create_prefix=False, force_new_repo=False, stacked_on=None,
260
 
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
261
 
        shared_repo=False, vfs_only=False):
262
 
        def make_directory(transport):
263
 
            transport.mkdir('.')
264
 
            return transport
265
 
        def redirected(transport, e, redirection_notice):
266
 
            trace.note(redirection_notice)
267
 
            return transport._redirected_to(e.source, e.target)
268
 
        try:
269
 
            transport = do_catching_redirections(make_directory, transport,
270
 
                redirected)
271
 
        except bzr_errors.FileExists:
272
 
            if not use_existing_dir:
273
 
                raise
274
 
        except bzr_errors.NoSuchFile:
275
 
            if not create_prefix:
276
 
                raise
277
 
            transport.create_prefix()
278
 
        controldir = self.initialize_on_transport(transport)
279
 
        repository = controldir.open_repository()
280
 
        repository.lock_write()
281
 
        return (repository, controldir, False, CreateRepository(controldir))
282
 
 
283
 
    def is_supported(self):
284
 
        return True
285
 
 
286
 
 
287
 
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
288
 
 
289
 
    bare = True
290
 
    supports_workingtrees = False
291
 
 
292
 
    def get_format_description(self):
293
 
        return "Local Git Repository (bare)"
294
 
 
295
124
 
296
125
class LocalGitDir(GitDir):
297
126
    """An adapter to the '.git' dir used by git."""
333
162
        return ref
334
163
 
335
164
    def is_control_filename(self, filename):
336
 
        return (filename == '.git' or filename.startswith('.git/'))
 
165
        return filename == '.git' or filename.startswith('.git/')
337
166
 
338
167
    def get_branch_transport(self, branch_format, name=None):
339
168
        if branch_format is None:
356
185
            return self.transport
357
186
        raise bzr_errors.IncompatibleFormat(format, self._format)
358
187
 
359
 
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
 
188
    def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
360
189
        """'create' a branch for this dir."""
361
190
        repo = self.open_repository()
362
191
        from bzrlib.plugins.git.branch import LocalGitBranch
386
215
                ret.append(self.open_branch(name=name))
387
216
        return ret
388
217
 
389
 
    def open_repository(self):
 
218
    def open_repository(self, shared=False):
390
219
        """'open' a repository for this dir."""
391
220
        return self._gitrepository_class(self, self._lockfiles)
392
221
 
410
239
        raise bzr_errors.NoWorkingTree(loc)
411
240
 
412
241
    def create_repository(self, shared=False):
413
 
        from bzrlib.plugins.git.repository import GitRepositoryFormat
414
 
        if shared:
415
 
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
416
242
        return self.open_repository()
417
243
 
418
 
    def create_branch(self, name=None, repository=None):
 
244
    def create_branch(self, name=None):
419
245
        refname = self._branch_name_to_ref(name)
420
246
        from dulwich.protocol import ZERO_SHA
421
247
        self._git.refs[refname or "HEAD"] = ZERO_SHA
432
258
    def create_workingtree(self, revision_id=None, from_branch=None,
433
259
        accelerator_tree=None, hardlink=False):
434
260
        if self._git.bare:
435
 
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
 
261
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
436
262
        from dulwich.index import write_index
437
263
        from dulwich.pack import SHA1Writer
438
264
        f = open(self.transport.local_abspath("index"), 'w+')
443
269
            f.close()
444
270
        return self.open_workingtree()
445
271
 
446
 
    def _find_or_create_repository(self, force_new_repo=None):
447
 
        return self.create_repository(shared=False)
 
272
    def find_repository(self):
 
273
        """Find the repository that should be used.
 
274
 
 
275
        This does not require a branch as we use it to find the repo for
 
276
        new branches as well as to hook existing branches up to their
 
277
        repository.
 
278
        """
 
279
        return self.open_repository()
448
280
 
449
281
    def _find_creation_modes(self):
450
282
        """Determine the appropriate modes for files and directories.
459
291
        self._mode_check_done = True
460
292
        try:
461
293
            st = self.transport.stat('.')
462
 
        except bzr_errors.TransportNotPossible:
 
294
        except TransportNotPossible:
463
295
            self._dir_mode = None
464
296
            self._file_mode = None
465
297
        else:
490
322
            self._find_creation_modes()
491
323
        return self._dir_mode
492
324
 
 
325