/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

Support branches where the ref can't be mapped back to a branch name.

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
import urllib
 
21
 
 
22
from bzrlib import (
 
23
    errors as bzr_errors,
 
24
    lockable_files,
 
25
    trace,
 
26
    osutils,
 
27
    urlutils,
 
28
    )
 
29
from bzrlib.bzrdir import CreateRepository
 
30
from bzrlib.transport import do_catching_redirections
 
31
 
 
32
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
33
 
 
34
from bzrlib.controldir import (
 
35
    ControlDir,
 
36
    ControlDirFormat,
 
37
    format_registry,
 
38
    )
 
39
 
 
40
 
 
41
class GitLock(object):
 
42
    """A lock that thunks through to Git."""
 
43
 
 
44
    def __init__(self):
 
45
        self.lock_name = "git lock"
 
46
 
 
47
    def lock_write(self, token=None):
 
48
        pass
 
49
 
 
50
    def lock_read(self):
 
51
        pass
 
52
 
 
53
    def unlock(self):
 
54
        pass
 
55
 
 
56
    def peek(self):
 
57
        pass
 
58
 
 
59
    def validate_token(self, token):
 
60
        pass
 
61
 
 
62
    def break_lock(self):
 
63
        raise NotImplementedError(self.break_lock)
 
64
 
 
65
    def dont_leave_in_place(self):
 
66
        raise NotImplementedError(self.dont_leave_in_place)
 
67
 
 
68
    def leave_in_place(self):
 
69
        raise NotImplementedError(self.leave_in_place)
 
70
 
 
71
 
 
72
class GitLockableFiles(lockable_files.LockableFiles):
 
73
    """Git specific lockable files abstraction."""
 
74
 
 
75
    def __init__(self, transport, lock):
 
76
        self._lock = lock
 
77
        self._transaction = None
 
78
        self._lock_mode = None
 
79
        self._transport = transport
 
80
        if LockWarner is None:
 
81
            # Bzr 1.13
 
82
            self._lock_count = 0
 
83
        else:
 
84
            self._lock_warner = LockWarner(repr(self))
 
85
 
 
86
    def __str__(self):
 
87
        return 'GitLockableFiles(%s)' % (self._transport.base)
 
88
 
 
89
 
 
90
class GitDirConfig(object):
 
91
 
 
92
    def get_default_stack_on(self):
 
93
        return None
 
94
 
 
95
    def set_default_stack_on(self, value):
 
96
        raise bzr_errors.BzrError("Cannot set configuration")
 
97
 
 
98
 
 
99
class GitControlDirFormat(ControlDirFormat):
 
100
 
 
101
    _lock_class = lockable_files.TransportLock
 
102
 
 
103
    colocated_branches = True
 
104
    fixed_components = True
 
105
 
 
106
    def __eq__(self, other):
 
107
        return type(self) == type(other)
 
108
 
 
109
    def is_supported(self):
 
110
        return True
 
111
 
 
112
    def network_name(self):
 
113
        return "git"
 
114
 
 
115
 
 
116
class GitDir(ControlDir):
 
117
    """An adapter to the '.git' dir used by git."""
 
118
 
 
119
    def is_supported(self):
 
120
        return True
 
121
 
 
122
    def can_convert_format(self):
 
123
        return False
 
124
 
 
125
    def break_lock(self):
 
126
        pass
 
127
 
 
128
    def cloning_metadir(self, stacked=False):
 
129
        return format_registry.make_bzrdir("default")
 
130
 
 
131
    def checkout_metadir(self, stacked=False):
 
132
        return format_registry.make_bzrdir("default")
 
133
 
 
134
    def _get_selected_ref(self, branch):
 
135
        if branch is None and getattr(self, "_get_selected_branch", False):
 
136
            branch = self._get_selected_branch()
 
137
        if branch is not None:
 
138
            from bzrlib.plugins.git.refs import branch_name_to_ref
 
139
            return branch_name_to_ref(branch, None)
 
140
        segment_parameters = getattr(
 
141
            self.user_transport, "get_segment_parameters", lambda: {})()
 
142
        ref = segment_parameters.get("ref")
 
143
        if ref is not None:
 
144
            ref = urlutils.unescape(ref)
 
145
        return ref
 
146
 
 
147
    def get_config(self):
 
148
        return GitDirConfig()
 
149
 
 
150
    def _available_backup_name(self, base):
 
151
        return osutils.available_backup_name(base, self.root_transport.has)
 
152
 
 
153
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
154
               recurse='down', possible_transports=None,
 
155
               accelerator_tree=None, hardlink=False, stacked=False,
 
156
               source_branch=None, create_tree_if_local=True):
 
157
        from bzrlib.repository import InterRepository
 
158
        from bzrlib.transport.local import LocalTransport
 
159
        from bzrlib.transport import get_transport
 
160
        target_transport = get_transport(url, possible_transports)
 
161
        target_transport.ensure_base()
 
162
        cloning_format = self.cloning_metadir()
 
163
        # Create/update the result branch
 
164
        result = cloning_format.initialize_on_transport(target_transport)
 
165
        source_branch = self.open_branch()
 
166
        source_repository = self.find_repository()
 
167
        try:
 
168
            result_repo = result.find_repository()
 
169
        except bzr_errors.NoRepositoryPresent:
 
170
            result_repo = result.create_repository()
 
171
            target_is_empty = True
 
172
        else:
 
173
            target_is_empty = None # Unknown
 
174
        if stacked:
 
175
            raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
 
176
        interrepo = InterRepository.get(source_repository, result_repo)
 
177
 
 
178
        if revision_id is not None:
 
179
            determine_wants = interrepo.get_determine_wants_revids(
 
180
                [revision_id], include_tags=True)
 
181
        else:
 
182
            determine_wants = interrepo.determine_wants_all
 
183
        interrepo.fetch_objects(determine_wants=determine_wants,
 
184
            mapping=source_branch.mapping)
 
185
        result_branch = source_branch.sprout(result,
 
186
            revision_id=revision_id, repository=result_repo)
 
187
        if (create_tree_if_local and isinstance(target_transport, LocalTransport)
 
188
            and (result_repo is None or result_repo.make_working_trees())):
 
189
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
 
190
                hardlink=hardlink, from_branch=result_branch)
 
191
            wt.lock_write()
 
192
            try:
 
193
                if wt.path2id('') is None:
 
194
                    try:
 
195
                        wt.set_root_id(self.open_workingtree.get_root_id())
 
196
                    except bzr_errors.NoWorkingTree:
 
197
                        pass
 
198
            finally:
 
199
                wt.unlock()
 
200
        return result
 
201
 
 
202
    def clone_on_transport(self, transport, revision_id=None,
 
203
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
204
        create_prefix=False, use_existing_dir=True, no_tree=False):
 
205
        """See ControlDir.clone_on_transport."""
 
206
        from bzrlib.repository import InterRepository
 
207
        from bzrlib.plugins.git.mapping import default_mapping
 
208
        if no_tree:
 
209
            format = BareLocalGitControlDirFormat()
 
210
        else:
 
211
            format = LocalGitControlDirFormat()
 
212
        (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)
 
213
        target_git_repo = target_repo._git
 
214
        source_repo = self.open_repository()
 
215
        source_git_repo = source_repo._git
 
216
        interrepo = InterRepository.get(source_repo, target_repo)
 
217
        if revision_id is not None:
 
218
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
 
219
        else:
 
220
            determine_wants = interrepo.determine_wants_all
 
221
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
 
222
            mapping=default_mapping)
 
223
        for name, val in refs.iteritems():
 
224
            target_git_repo.refs[name] = val
 
225
        lockfiles = GitLockableFiles(transport, GitLock())
 
226
        return self.__class__(transport, lockfiles, target_git_repo, format)
 
227
 
 
228
    def find_repository(self):
 
229
        """Find the repository that should be used.
 
230
 
 
231
        This does not require a branch as we use it to find the repo for
 
232
        new branches as well as to hook existing branches up to their
 
233
        repository.
 
234
        """
 
235
        return self.open_repository()
 
236
 
 
237
 
 
238
class LocalGitControlDirFormat(GitControlDirFormat):
 
239
    """The .git directory control format."""
 
240
 
 
241
    bare = False
 
242
 
 
243
    @classmethod
 
244
    def _known_formats(self):
 
245
        return set([LocalGitControlDirFormat()])
 
246
 
 
247
    @property
 
248
    def repository_format(self):
 
249
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
250
        return GitRepositoryFormat()
 
251
 
 
252
    def get_branch_format(self):
 
253
        from bzrlib.plugins.git.branch import GitBranchFormat
 
254
        return GitBranchFormat()
 
255
 
 
256
    def open(self, transport, _found=None):
 
257
        """Open this directory.
 
258
 
 
259
        """
 
260
        from bzrlib.plugins.git.transportgit import TransportRepo
 
261
        gitrepo = TransportRepo(transport)
 
262
        lockfiles = GitLockableFiles(transport, GitLock())
 
263
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
264
 
 
265
    def get_format_description(self):
 
266
        return "Local Git Repository"
 
267
 
 
268
    def initialize_on_transport(self, transport):
 
269
        from bzrlib.plugins.git.transportgit import TransportRepo
 
270
        TransportRepo.init(transport, bare=self.bare)
 
271
        return self.open(transport)
 
272
 
 
273
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
274
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
275
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
276
        shared_repo=False, vfs_only=False):
 
277
        def make_directory(transport):
 
278
            transport.mkdir('.')
 
279
            return transport
 
280
        def redirected(transport, e, redirection_notice):
 
281
            trace.note(redirection_notice)
 
282
            return transport._redirected_to(e.source, e.target)
 
283
        try:
 
284
            transport = do_catching_redirections(make_directory, transport,
 
285
                redirected)
 
286
        except bzr_errors.FileExists:
 
287
            if not use_existing_dir:
 
288
                raise
 
289
        except bzr_errors.NoSuchFile:
 
290
            if not create_prefix:
 
291
                raise
 
292
            transport.create_prefix()
 
293
        controldir = self.initialize_on_transport(transport)
 
294
        repository = controldir.open_repository()
 
295
        repository.lock_write()
 
296
        return (repository, controldir, False, CreateRepository(controldir))
 
297
 
 
298
    def is_supported(self):
 
299
        return True
 
300
 
 
301
 
 
302
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
303
 
 
304
    bare = True
 
305
    supports_workingtrees = False
 
306
 
 
307
    def get_format_description(self):
 
308
        return "Local Git Repository (bare)"
 
309
 
 
310
 
 
311
class LocalGitDir(GitDir):
 
312
    """An adapter to the '.git' dir used by git."""
 
313
 
 
314
    def _get_gitrepository_class(self):
 
315
        from bzrlib.plugins.git.repository import LocalGitRepository
 
316
        return LocalGitRepository
 
317
 
 
318
    def __repr__(self):
 
319
        return "<%s at %r>" % (
 
320
            self.__class__.__name__, self.root_transport.base)
 
321
 
 
322
    _gitrepository_class = property(_get_gitrepository_class)
 
323
 
 
324
    @property
 
325
    def user_transport(self):
 
326
        return self.root_transport
 
327
 
 
328
    @property
 
329
    def control_transport(self):
 
330
        return self.transport
 
331
 
 
332
    def __init__(self, transport, lockfiles, gitrepo, format):
 
333
        self._format = format
 
334
        self.root_transport = transport
 
335
        self._mode_check_done = False
 
336
        self._git = gitrepo
 
337
        if gitrepo.bare:
 
338
            self.transport = transport
 
339
        else:
 
340
            self.transport = transport.clone('.git')
 
341
        self._lockfiles = lockfiles
 
342
        self._mode_check_done = None
 
343
 
 
344
    def is_control_filename(self, filename):
 
345
        return (filename == '.git' or filename.startswith('.git/'))
 
346
 
 
347
    def _get_symref(self, ref):
 
348
        from dulwich.repo import SYMREF
 
349
        refcontents = self._git.refs.read_ref(ref)
 
350
        if refcontents is None: # no such ref
 
351
            return None
 
352
        if refcontents.startswith(SYMREF):
 
353
            return refcontents[len(SYMREF):].rstrip("\n")
 
354
        return None
 
355
 
 
356
    def set_branch_reference(self, name, target):
 
357
        ref = self._get_selected_ref(name)
 
358
        if ref is None:
 
359
            ref = "HEAD"
 
360
        if not getattr(target, "ref", None):
 
361
            raise bzr_errors.BzrError("Can only set symrefs to Git refs")
 
362
        self._git.refs.set_symbolic_ref(ref, target.ref)
 
363
 
 
364
    def get_branch_reference(self, name=None):
 
365
        ref = self._get_selected_ref(name)
 
366
        if ref is None:
 
367
            ref = "HEAD"
 
368
        target_ref = self._get_symref(ref)
 
369
        if target_ref is not None:
 
370
            return ",ref=%s" % urllib.quote(target_ref)
 
371
        return None
 
372
 
 
373
    def find_branch_format(self, name=None):
 
374
        from bzrlib.plugins.git.branch import (
 
375
            GitBranchFormat,
 
376
            GitSymrefBranchFormat,
 
377
            )
 
378
        ref = self._get_selected_ref(name)
 
379
        if ref is None:
 
380
            ref = "HEAD"
 
381
        if self._get_symref(ref) is not None:
 
382
            return GitSymrefBranchFormat()
 
383
        else:
 
384
            return GitBranchFormat()
 
385
 
 
386
    def get_branch_transport(self, branch_format, name=None):
 
387
        if branch_format is None:
 
388
            return self.transport
 
389
        if isinstance(branch_format, LocalGitControlDirFormat):
 
390
            return self.transport
 
391
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
392
 
 
393
    def get_repository_transport(self, format):
 
394
        if format is None:
 
395
            return self.transport
 
396
        if isinstance(format, LocalGitControlDirFormat):
 
397
            return self.transport
 
398
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
399
 
 
400
    def get_workingtree_transport(self, format):
 
401
        if format is None:
 
402
            return self.transport
 
403
        if isinstance(format, LocalGitControlDirFormat):
 
404
            return self.transport
 
405
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
406
 
 
407
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
 
408
        """'create' a branch for this dir."""
 
409
        repo = self.open_repository()
 
410
        from bzrlib.plugins.git.branch import LocalGitBranch
 
411
        ref = self._get_selected_ref(name)
 
412
        if ref is None:
 
413
            ref = "HEAD"
 
414
        ref, sha = self._git.refs._follow(ref)
 
415
        if not ref in self._git.refs:
 
416
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
417
                    bzrdir=self)
 
418
        return LocalGitBranch(self, repo, ref, self._lockfiles)
 
419
 
 
420
    def destroy_branch(self, name=None):
 
421
        refname = self._get_selected_ref(name)
 
422
        if not refname in self._git.refs:
 
423
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
424
                    bzrdir=self)
 
425
        del self._git.refs[refname]
 
426
 
 
427
    def destroy_repository(self):
 
428
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
 
429
 
 
430
    def destroy_workingtree(self):
 
431
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
432
 
 
433
    def needs_format_conversion(self, format=None):
 
434
        return not isinstance(self._format, format.__class__)
 
435
 
 
436
    def list_branches(self):
 
437
        ret = []
 
438
        for name in self._git.refs.keys():
 
439
            if name.startswith("refs/heads/"):
 
440
                ret.append(self.open_branch(name=name))
 
441
        return ret
 
442
 
 
443
    def open_repository(self):
 
444
        """'open' a repository for this dir."""
 
445
        return self._gitrepository_class(self, self._lockfiles)
 
446
 
 
447
    def open_workingtree(self, recommend_upgrade=True):
 
448
        if not self._git.bare:
 
449
            from dulwich.errors import NoIndexPresent
 
450
            repo = self.open_repository()
 
451
            try:
 
452
                index = repo._git.open_index()
 
453
            except NoIndexPresent:
 
454
                pass
 
455
            else:
 
456
                from bzrlib.plugins.git.workingtree import GitWorkingTree
 
457
                try:
 
458
                    branch = self.open_branch()
 
459
                except bzr_errors.NotBranchError:
 
460
                    pass
 
461
                else:
 
462
                    return GitWorkingTree(self, repo, branch, index)
 
463
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
464
        raise bzr_errors.NoWorkingTree(loc)
 
465
 
 
466
    def create_repository(self, shared=False):
 
467
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
468
        if shared:
 
469
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
 
470
        return self.open_repository()
 
471
 
 
472
    def create_branch(self, name=None, repository=None):
 
473
        refname = self._get_selected_ref(name)
 
474
        from dulwich.protocol import ZERO_SHA
 
475
        # FIXME: This is a bit awkward. Perhaps we should have a
 
476
        # a separate method for changing the default branch?
 
477
        if refname is None:
 
478
            refname = "refs/heads/master"
 
479
            self._git.refs.set_symbolic_ref("HEAD", refname)
 
480
        self._git.refs[refname] = ZERO_SHA
 
481
        return self.open_branch(name)
 
482
 
 
483
    def backup_bzrdir(self):
 
484
        if self._git.bare:
 
485
            self.root_transport.copy_tree(".git", ".git.backup")
 
486
            return (self.root_transport.abspath(".git"),
 
487
                    self.root_transport.abspath(".git.backup"))
 
488
        else:
 
489
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
490
 
 
491
    def create_workingtree(self, revision_id=None, from_branch=None,
 
492
        accelerator_tree=None, hardlink=False):
 
493
        if self._git.bare:
 
494
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
 
495
        from dulwich.index import write_index
 
496
        from dulwich.pack import SHA1Writer
 
497
        f = open(self.transport.local_abspath("index"), 'w+')
 
498
        try:
 
499
            f = SHA1Writer(f)
 
500
            write_index(f, [])
 
501
        finally:
 
502
            f.close()
 
503
        return self.open_workingtree()
 
504
 
 
505
    def _find_or_create_repository(self, force_new_repo=None):
 
506
        return self.create_repository(shared=False)
 
507
 
 
508
    def _find_creation_modes(self):
 
509
        """Determine the appropriate modes for files and directories.
 
510
 
 
511
        They're always set to be consistent with the base directory,
 
512
        assuming that this transport allows setting modes.
 
513
        """
 
514
        # TODO: Do we need or want an option (maybe a config setting) to turn
 
515
        # this off or override it for particular locations? -- mbp 20080512
 
516
        if self._mode_check_done:
 
517
            return
 
518
        self._mode_check_done = True
 
519
        try:
 
520
            st = self.transport.stat('.')
 
521
        except bzr_errors.TransportNotPossible:
 
522
            self._dir_mode = None
 
523
            self._file_mode = None
 
524
        else:
 
525
            # Check the directory mode, but also make sure the created
 
526
            # directories and files are read-write for this user. This is
 
527
            # mostly a workaround for filesystems which lie about being able to
 
528
            # write to a directory (cygwin & win32)
 
529
            if (st.st_mode & 07777 == 00000):
 
530
                # FTP allows stat but does not return dir/file modes
 
531
                self._dir_mode = None
 
532
                self._file_mode = None
 
533
            else:
 
534
                self._dir_mode = (st.st_mode & 07777) | 00700
 
535
                # Remove the sticky and execute bits for files
 
536
                self._file_mode = self._dir_mode & ~07111
 
537
 
 
538
    def _get_file_mode(self):
 
539
        """Return Unix mode for newly created files, or None.
 
540
        """
 
541
        if not self._mode_check_done:
 
542
            self._find_creation_modes()
 
543
        return self._file_mode
 
544
 
 
545
    def _get_dir_mode(self):
 
546
        """Return Unix mode for newly created directories, or None.
 
547
        """
 
548
        if not self._mode_check_done:
 
549
            self._find_creation_modes()
 
550
        return self._dir_mode
 
551