/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

Cope with files disappearing during commit.

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