/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

Fix handling of empty trees.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
 
18
 
 
19
from bzrlib import (
 
20
    bzrdir,
 
21
    lockable_files,
 
22
    urlutils,
 
23
    )
 
24
 
 
25
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
26
 
 
27
from bzrlib.plugins.git import (
 
28
    LocalGitBzrDirFormat,
 
29
    branch,
 
30
    errors,
 
31
    get_rich_root_format,
 
32
    repository,
 
33
    workingtree,
 
34
    )
 
35
 
 
36
 
 
37
class GitLock(object):
 
38
    """A lock that thunks through to Git."""
 
39
 
 
40
    def lock_write(self, token=None):
 
41
        pass
 
42
 
 
43
    def lock_read(self):
 
44
        pass
 
45
 
 
46
    def unlock(self):
 
47
        pass
 
48
 
 
49
    def peek(self):
 
50
        pass
 
51
 
 
52
    def validate_token(self, token):
 
53
        pass
 
54
 
 
55
 
 
56
class GitLockableFiles(lockable_files.LockableFiles):
 
57
    """Git specific lockable files abstraction."""
 
58
 
 
59
    def __init__(self, transport, lock):
 
60
        self._lock = lock
 
61
        self._transaction = None
 
62
        self._lock_mode = None
 
63
        self._transport = transport
 
64
        if LockWarner is None:
 
65
            # Bzr 1.13
 
66
            self._lock_count = 0
 
67
        else:
 
68
            self._lock_warner = LockWarner(repr(self))
 
69
 
 
70
 
 
71
class GitDir(bzrdir.BzrDir):
 
72
    """An adapter to the '.git' dir used by git."""
 
73
 
 
74
    def is_supported(self):
 
75
        return True
 
76
 
 
77
    def cloning_metadir(self, stacked=False):
 
78
        return get_rich_root_format(stacked)
 
79
 
 
80
 
 
81
class LocalGitDir(GitDir):
 
82
    """An adapter to the '.git' dir used by git."""
 
83
 
 
84
    _gitrepository_class = repository.LocalGitRepository
 
85
 
 
86
    def __init__(self, transport, lockfiles, gitrepo, format):
 
87
        self._format = format
 
88
        self.root_transport = transport
 
89
        self._git = gitrepo
 
90
        if gitrepo.bare:
 
91
            self.transport = transport
 
92
        else:
 
93
            self.transport = transport.clone('.git')
 
94
        self._lockfiles = lockfiles
 
95
        self._mode_check_done = None
 
96
 
 
97
    def is_control_filename(self, filename):
 
98
        return filename == '.git' or filename.startswith('.git/')
 
99
 
 
100
    def get_branch_transport(self, branch_format):
 
101
        if branch_format is None:
 
102
            return self.transport
 
103
        if isinstance(branch_format, LocalGitBzrDirFormat):
 
104
            return self.transport
 
105
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
 
106
 
 
107
    get_repository_transport = get_branch_transport
 
108
    get_workingtree_transport = get_branch_transport
 
109
 
 
110
    def open_branch(self, ignore_fallbacks=None):
 
111
        """'create' a branch for this dir."""
 
112
        repo = self.open_repository()
 
113
        return branch.LocalGitBranch(self, repo, "HEAD", self._lockfiles)
 
114
 
 
115
    def open_repository(self, shared=False):
 
116
        """'open' a repository for this dir."""
 
117
        return self._gitrepository_class(self, self._lockfiles)
 
118
 
 
119
    def open_workingtree(self, recommend_upgrade=True):
 
120
        if not self._git.bare and self._git.has_index():
 
121
            return workingtree.GitWorkingTree(self, self.open_repository(), 
 
122
                                                  self.open_branch())
 
123
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
124
        raise errors.bzr_errors.NoWorkingTree(loc)
 
125
 
 
126
    def create_repository(self, shared=False):
 
127
        return self.open_repository()
 
128
 
 
129
    def create_branch(self):
 
130
        return self.open_branch()
 
131
 
 
132
    def backup_bzrdir(self):
 
133
        if self._git.bare:
 
134
            self.root_transport.copy_tree(".git", ".git.backup")
 
135
            return (self.root_transport.abspath(".git"),
 
136
                    self.root_transport.abspath(".git.backup"))
 
137
        else:
 
138
            raise errors.bzr_errors.BzrError("Unable to backup bare repositories")
 
139
 
 
140
    def create_workingtree(self, revision_id=None, from_branch=None,
 
141
        accelerator_tree=None, hardlink=False):
 
142
        if self._git.bare:
 
143
            raise errors.bzr_errors.BzrError("Can't create working tree in a bare repo")
 
144
        from dulwich.index import write_index
 
145
        write_index(self.root_transport.abspath(".git/index"), [])
 
146
        return self.open_workingtree()