/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

Require dulwich 0.7.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2010 Jelmer Vernooij
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
 
19
 
 
20
from bzrlib import (
 
21
    errors as bzr_errors,
 
22
    lockable_files,
 
23
    trace,
 
24
    urlutils,
 
25
    version_info as bzrlib_version,
 
26
    )
 
27
from bzrlib.bzrdir import CreateRepository
 
28
from bzrlib.transport import do_catching_redirections
 
29
 
 
30
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
31
 
 
32
from bzrlib.controldir import (
 
33
    ControlDir,
 
34
    ControlDirFormat,
 
35
    format_registry,
 
36
    )
 
37
 
 
38
 
 
39
class GitLock(object):
 
40
    """A lock that thunks through to Git."""
 
41
 
 
42
    def __init__(self):
 
43
        self.lock_name = "git lock"
 
44
 
 
45
    def lock_write(self, token=None):
 
46
        pass
 
47
 
 
48
    def lock_read(self):
 
49
        pass
 
50
 
 
51
    def unlock(self):
 
52
        pass
 
53
 
 
54
    def peek(self):
 
55
        pass
 
56
 
 
57
    def validate_token(self, token):
 
58
        pass
 
59
 
 
60
    def break_lock(self):
 
61
        pass
 
62
 
 
63
 
 
64
class GitLockableFiles(lockable_files.LockableFiles):
 
65
    """Git specific lockable files abstraction."""
 
66
 
 
67
    def __init__(self, transport, lock):
 
68
        self._lock = lock
 
69
        self._transaction = None
 
70
        self._lock_mode = None
 
71
        self._transport = transport
 
72
        if LockWarner is None:
 
73
            # Bzr 1.13
 
74
            self._lock_count = 0
 
75
        else:
 
76
            self._lock_warner = LockWarner(repr(self))
 
77
 
 
78
 
 
79
class GitDirConfig(object):
 
80
 
 
81
    def get_default_stack_on(self):
 
82
        return None
 
83
 
 
84
    def set_default_stack_on(self, value):
 
85
        raise bzr_errors.BzrError("Cannot set configuration")
 
86
 
 
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
 
 
105
class GitDir(ControlDir):
 
106
    """An adapter to the '.git' dir used by git."""
 
107
 
 
108
    def is_supported(self):
 
109
        return True
 
110
 
 
111
    def can_convert_format(self):
 
112
        return False
 
113
 
 
114
    def break_lock(self):
 
115
        pass
 
116
 
 
117
    def cloning_metadir(self, stacked=False):
 
118
        return format_registry.make_bzrdir("default")
 
119
 
 
120
    def _branch_name_to_ref(self, name):
 
121
        raise NotImplementedError(self._branch_name_to_ref)
 
122
 
 
123
    def get_config(self):
 
124
        return GitDirConfig()
 
125
 
 
126
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
127
               recurse='down', possible_transports=None,
 
128
               accelerator_tree=None, hardlink=False, stacked=False,
 
129
               source_branch=None, create_tree_if_local=True):
 
130
        from bzrlib.repository import InterRepository
 
131
        from bzrlib.transport.local import LocalTransport
 
132
        from bzrlib.transport import get_transport
 
133
        target_transport = get_transport(url, possible_transports)
 
134
        target_transport.ensure_base()
 
135
        cloning_format = self.cloning_metadir()
 
136
        # Create/update the result branch
 
137
        result = cloning_format.initialize_on_transport(target_transport)
 
138
        source_branch = self.open_branch()
 
139
        source_repository = self.find_repository()
 
140
        try:
 
141
            result_repo = result.find_repository()
 
142
        except bzr_errors.NoRepositoryPresent:
 
143
            result_repo = result.create_repository()
 
144
            target_is_empty = True
 
145
        else:
 
146
            target_is_empty = None # Unknown
 
147
        if stacked:
 
148
            raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
 
149
        interrepo = InterRepository.get(source_repository, result_repo)
 
150
 
 
151
        if revision_id is not None:
 
152
            determine_wants = interrepo.get_determine_wants_revids(
 
153
                [revision_id], include_tags=True)
 
154
        else:
 
155
            determine_wants = interrepo.determine_wants_all
 
156
        interrepo.fetch_objects(determine_wants=determine_wants,
 
157
            mapping=source_branch.mapping)
 
158
        result_branch = source_branch.sprout(result,
 
159
            revision_id=revision_id, repository=result_repo)
 
160
        if (create_tree_if_local and isinstance(target_transport, LocalTransport)
 
161
            and (result_repo is None or result_repo.make_working_trees())):
 
162
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
 
163
                hardlink=hardlink, from_branch=result_branch)
 
164
            wt.lock_write()
 
165
            try:
 
166
                if wt.path2id('') is None:
 
167
                    try:
 
168
                        wt.set_root_id(self.open_workingtree.get_root_id())
 
169
                    except bzr_errors.NoWorkingTree:
 
170
                        pass
 
171
            finally:
 
172
                wt.unlock()
 
173
        return result
 
174
 
 
175
    def clone_on_transport(self, transport, revision_id=None,
 
176
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
177
        create_prefix=False, use_existing_dir=True, no_tree=False):
 
178
        from dulwich.protocol import ZERO_SHA
 
179
        """See ControlDir.clone_on_transport."""
 
180
        if no_tree:
 
181
            format = BareLocalGitControlDirFormat()
 
182
        else:
 
183
            format = LocalGitControlDirFormat()
 
184
        (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)
 
185
        target_git_repo = target_repo._git
 
186
        source_repo = self.open_repository()
 
187
        source_git_repo = source_repo._git
 
188
        if revision_id is not None:
 
189
            determine_wants = source_repo.determine_wants_revid_and_tags(revision_id)
 
190
        else:
 
191
            determine_wants = target_git_repo.object_store.determine_wants_all
 
192
        refs = source_git_repo.fetch(target_git_repo, determine_wants)
 
193
        for name, val in refs.iteritems():
 
194
            target_git_repo.refs[name] = val
 
195
        lockfiles = GitLockableFiles(transport, GitLock())
 
196
        return self.__class__(transport, lockfiles, target_git_repo, format)
 
197
 
 
198
    def find_repository(self):
 
199
        """Find the repository that should be used.
 
200
 
 
201
        This does not require a branch as we use it to find the repo for
 
202
        new branches as well as to hook existing branches up to their
 
203
        repository.
 
204
        """
 
205
        return self.open_repository()
 
206
 
 
207
 
 
208
class LocalGitControlDirFormat(GitControlDirFormat):
 
209
    """The .git directory control format."""
 
210
 
 
211
    bare = False
 
212
 
 
213
    @classmethod
 
214
    def _known_formats(self):
 
215
        return set([LocalGitControlDirFormat()])
 
216
 
 
217
    @property
 
218
    def repository_format(self):
 
219
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
220
        return GitRepositoryFormat()
 
221
 
 
222
    def get_branch_format(self):
 
223
        from bzrlib.plugins.git.branch import GitBranchFormat
 
224
        return GitBranchFormat()
 
225
 
 
226
    def open(self, transport, _found=None):
 
227
        """Open this directory.
 
228
 
 
229
        """
 
230
        from bzrlib.plugins.git.transportgit import TransportRepo
 
231
        gitrepo = TransportRepo(transport)
 
232
        lockfiles = GitLockableFiles(transport, GitLock())
 
233
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
234
 
 
235
    def get_format_description(self):
 
236
        return "Local Git Repository"
 
237
 
 
238
    def initialize_on_transport(self, transport):
 
239
        from bzrlib.plugins.git.transportgit import TransportRepo
 
240
        TransportRepo.init(transport, bare=self.bare)
 
241
        return self.open(transport)
 
242
 
 
243
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
244
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
245
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
246
        shared_repo=False, vfs_only=False):
 
247
        def make_directory(transport):
 
248
            transport.mkdir('.')
 
249
            return transport
 
250
        def redirected(transport, e, redirection_notice):
 
251
            trace.note(redirection_notice)
 
252
            return transport._redirected_to(e.source, e.target)
 
253
        try:
 
254
            transport = do_catching_redirections(make_directory, transport,
 
255
                redirected)
 
256
        except bzr_errors.FileExists:
 
257
            if not use_existing_dir:
 
258
                raise
 
259
        except bzr_errors.NoSuchFile:
 
260
            if not create_prefix:
 
261
                raise
 
262
            transport.create_prefix()
 
263
        controldir = self.initialize_on_transport(transport)
 
264
        repository = controldir.open_repository()
 
265
        repository.lock_write()
 
266
        return (repository, controldir, False, CreateRepository(controldir))
 
267
 
 
268
    def is_supported(self):
 
269
        return True
 
270
 
 
271
 
 
272
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
273
 
 
274
    bare = True
 
275
    supports_workingtrees = False
 
276
 
 
277
    def get_format_description(self):
 
278
        return "Local Git Repository (bare)"
 
279
 
 
280
 
 
281
class LocalGitDir(GitDir):
 
282
    """An adapter to the '.git' dir used by git."""
 
283
 
 
284
    def _get_gitrepository_class(self):
 
285
        from bzrlib.plugins.git.repository import LocalGitRepository
 
286
        return LocalGitRepository
 
287
 
 
288
    _gitrepository_class = property(_get_gitrepository_class)
 
289
 
 
290
    @property
 
291
    def user_transport(self):
 
292
        return self.root_transport
 
293
 
 
294
    @property
 
295
    def control_transport(self):
 
296
        return self.transport
 
297
 
 
298
    def __init__(self, transport, lockfiles, gitrepo, format):
 
299
        self._format = format
 
300
        self.root_transport = transport
 
301
        self._mode_check_done = False
 
302
        self._git = gitrepo
 
303
        if gitrepo.bare:
 
304
            self.transport = transport
 
305
        else:
 
306
            self.transport = transport.clone('.git')
 
307
        self._lockfiles = lockfiles
 
308
        self._mode_check_done = None
 
309
 
 
310
    def _branch_name_to_ref(self, name):
 
311
        from bzrlib.plugins.git.refs import branch_name_to_ref
 
312
        ref = branch_name_to_ref(name, None)
 
313
        if ref == "HEAD":
 
314
            from dulwich.repo import SYMREF
 
315
            refcontents = self._git.refs.read_ref(ref)
 
316
            if refcontents.startswith(SYMREF):
 
317
                ref = refcontents[len(SYMREF):]
 
318
        return ref
 
319
 
 
320
    def is_control_filename(self, filename):
 
321
        return (filename == '.git' or filename.startswith('.git/'))
 
322
 
 
323
    def get_branch_transport(self, branch_format, name=None):
 
324
        if branch_format is None:
 
325
            return self.transport
 
326
        if isinstance(branch_format, LocalGitControlDirFormat):
 
327
            return self.transport
 
328
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
329
 
 
330
    def get_repository_transport(self, format):
 
331
        if format is None:
 
332
            return self.transport
 
333
        if isinstance(format, LocalGitControlDirFormat):
 
334
            return self.transport
 
335
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
336
 
 
337
    def get_workingtree_transport(self, format):
 
338
        if format is None:
 
339
            return self.transport
 
340
        if isinstance(format, LocalGitControlDirFormat):
 
341
            return self.transport
 
342
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
343
 
 
344
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
 
345
        """'create' a branch for this dir."""
 
346
        repo = self.open_repository()
 
347
        from bzrlib.plugins.git.branch import LocalGitBranch
 
348
        return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
 
349
            self._lockfiles)
 
350
 
 
351
    def destroy_branch(self, name=None):
 
352
        refname = self._branch_name_to_ref(name)
 
353
        if not refname in self._git.refs:
 
354
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
355
                    bzrdir=self)
 
356
        del self._git.refs[refname]
 
357
 
 
358
    def destroy_repository(self):
 
359
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
 
360
 
 
361
    def destroy_workingtree(self):
 
362
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
363
 
 
364
    def needs_format_conversion(self, format=None):
 
365
        return not isinstance(self._format, format.__class__)
 
366
 
 
367
    def list_branches(self):
 
368
        ret = []
 
369
        for name in self._git.get_refs():
 
370
            if name.startswith("refs/heads/"):
 
371
                ret.append(self.open_branch(name=name))
 
372
        return ret
 
373
 
 
374
    def open_repository(self):
 
375
        """'open' a repository for this dir."""
 
376
        return self._gitrepository_class(self, self._lockfiles)
 
377
 
 
378
    def open_workingtree(self, recommend_upgrade=True):
 
379
        if not self._git.bare:
 
380
            from dulwich.errors import NoIndexPresent
 
381
            repo = self.open_repository()
 
382
            try:
 
383
                index = repo._git.open_index()
 
384
            except NoIndexPresent:
 
385
                pass
 
386
            else:
 
387
                from bzrlib.plugins.git.workingtree import GitWorkingTree
 
388
                try:
 
389
                    branch = self.open_branch()
 
390
                except bzr_errors.NotBranchError:
 
391
                    pass
 
392
                else:
 
393
                    return GitWorkingTree(self, repo, branch, index)
 
394
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
395
        raise bzr_errors.NoWorkingTree(loc)
 
396
 
 
397
    def create_repository(self, shared=False):
 
398
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
399
        if shared:
 
400
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
 
401
        return self.open_repository()
 
402
 
 
403
    def create_branch(self, name=None, repository=None):
 
404
        refname = self._branch_name_to_ref(name)
 
405
        from dulwich.protocol import ZERO_SHA
 
406
        self._git.refs[refname or "HEAD"] = ZERO_SHA
 
407
        return self.open_branch(name)
 
408
 
 
409
    def backup_bzrdir(self):
 
410
        if self._git.bare:
 
411
            self.root_transport.copy_tree(".git", ".git.backup")
 
412
            return (self.root_transport.abspath(".git"),
 
413
                    self.root_transport.abspath(".git.backup"))
 
414
        else:
 
415
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
416
 
 
417
    def create_workingtree(self, revision_id=None, from_branch=None,
 
418
        accelerator_tree=None, hardlink=False):
 
419
        if self._git.bare:
 
420
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
 
421
        from dulwich.index import write_index
 
422
        from dulwich.pack import SHA1Writer
 
423
        f = open(self.transport.local_abspath("index"), 'w+')
 
424
        try:
 
425
            f = SHA1Writer(f)
 
426
            write_index(f, [])
 
427
        finally:
 
428
            f.close()
 
429
        return self.open_workingtree()
 
430
 
 
431
    def _find_or_create_repository(self, force_new_repo=None):
 
432
        return self.create_repository(shared=False)
 
433
 
 
434
    def _find_creation_modes(self):
 
435
        """Determine the appropriate modes for files and directories.
 
436
 
 
437
        They're always set to be consistent with the base directory,
 
438
        assuming that this transport allows setting modes.
 
439
        """
 
440
        # TODO: Do we need or want an option (maybe a config setting) to turn
 
441
        # this off or override it for particular locations? -- mbp 20080512
 
442
        if self._mode_check_done:
 
443
            return
 
444
        self._mode_check_done = True
 
445
        try:
 
446
            st = self.transport.stat('.')
 
447
        except bzr_errors.TransportNotPossible:
 
448
            self._dir_mode = None
 
449
            self._file_mode = None
 
450
        else:
 
451
            # Check the directory mode, but also make sure the created
 
452
            # directories and files are read-write for this user. This is
 
453
            # mostly a workaround for filesystems which lie about being able to
 
454
            # write to a directory (cygwin & win32)
 
455
            if (st.st_mode & 07777 == 00000):
 
456
                # FTP allows stat but does not return dir/file modes
 
457
                self._dir_mode = None
 
458
                self._file_mode = None
 
459
            else:
 
460
                self._dir_mode = (st.st_mode & 07777) | 00700
 
461
                # Remove the sticky and execute bits for files
 
462
                self._file_mode = self._dir_mode & ~07111
 
463
 
 
464
    def _get_file_mode(self):
 
465
        """Return Unix mode for newly created files, or None.
 
466
        """
 
467
        if not self._mode_check_done:
 
468
            self._find_creation_modes()
 
469
        return self._file_mode
 
470
 
 
471
    def _get_dir_mode(self):
 
472
        """Return Unix mode for newly created directories, or None.
 
473
        """
 
474
        if not self._mode_check_done:
 
475
            self._find_creation_modes()
 
476
        return self._dir_mode
 
477