/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

Add docstring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
18
18
 
19
 
import os
20
 
 
21
 
import bzrlib
22
19
from bzrlib import (
23
20
    bzrdir,
 
21
    errors as bzr_errors,
24
22
    lockable_files,
25
23
    urlutils,
 
24
    version_info as bzrlib_version,
26
25
    )
27
26
 
28
27
LockWarner = getattr(lockable_files, "_LockWarner", None)
29
28
 
30
29
from bzrlib.plugins.git import (
31
30
    LocalGitBzrDirFormat,
32
 
    branch,
33
 
    errors,
34
31
    get_rich_root_format,
35
 
    repository,
36
 
    workingtree,
37
32
    )
38
33
 
39
34
 
55
50
    def validate_token(self, token):
56
51
        pass
57
52
 
 
53
    def break_lock(self):
 
54
        pass
 
55
 
58
56
 
59
57
class GitLockableFiles(lockable_files.LockableFiles):
60
58
    """Git specific lockable files abstraction."""
80
78
    def cloning_metadir(self, stacked=False):
81
79
        return get_rich_root_format(stacked)
82
80
 
 
81
    def _branch_name_to_ref(self, name):
 
82
        from bzrlib.plugins.git.branch import branch_name_to_ref
 
83
        return branch_name_to_ref(name)
 
84
 
83
85
 
84
86
class LocalGitDir(GitDir):
85
87
    """An adapter to the '.git' dir used by git."""
86
88
 
87
 
    _gitrepository_class = repository.LocalGitRepository
 
89
    def _get_gitrepository_class(self):
 
90
        from bzrlib.plugins.git.repository import LocalGitRepository
 
91
        return LocalGitRepository
 
92
 
 
93
    _gitrepository_class = property(_get_gitrepository_class)
88
94
 
89
95
    def __init__(self, transport, lockfiles, gitrepo, format):
90
96
        self._format = format
105
111
            return self.transport
106
112
        if isinstance(branch_format, LocalGitBzrDirFormat):
107
113
            return self.transport
108
 
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
 
114
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
109
115
 
110
116
    get_repository_transport = get_branch_transport
111
117
    get_workingtree_transport = get_branch_transport
112
118
 
113
 
    def open_branch(self, ignore_fallbacks=None):
 
119
    def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
114
120
        """'create' a branch for this dir."""
115
121
        repo = self.open_repository()
116
 
        return branch.LocalGitBranch(self, repo, "HEAD", self._lockfiles)
 
122
        from bzrlib.plugins.git.branch import LocalGitBranch
 
123
        return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
 
124
            self._lockfiles)
 
125
 
 
126
    if bzrlib_version >= (2, 2):
 
127
        open_branch = _open_branch
 
128
    else:
 
129
        def open_branch(self, ignore_fallbacks=None, unsupported=False):
 
130
            return self._open_branch(name=None,
 
131
                ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
 
132
 
 
133
    def destroy_branch(self, name=None):
 
134
        del self._git.refs[self._branch_name_to_ref(name)]
 
135
 
 
136
    def list_branches(self):
 
137
        ret = []
 
138
        for name in self._git.get_refs():
 
139
            if name.startswith("refs/heads/") or name == "HEAD":
 
140
                ret.append(self.open_branch(name=name))
 
141
        return ret
117
142
 
118
143
    def open_repository(self, shared=False):
119
144
        """'open' a repository for this dir."""
120
145
        return self._gitrepository_class(self, self._lockfiles)
121
146
 
122
147
    def open_workingtree(self, recommend_upgrade=True):
123
 
        if not self._git.bare and self._git.has_index():
124
 
            return workingtree.GitWorkingTree(self, self.open_repository(), 
125
 
                                                  self.open_branch())
 
148
        if not self._git.bare:
 
149
            from dulwich.errors import NoIndexPresent
 
150
            try:
 
151
                from bzrlib.plugins.git.workingtree import GitWorkingTree
 
152
                return GitWorkingTree(self, self.open_repository(),
 
153
                                                      self.open_branch())
 
154
            except NoIndexPresent:
 
155
                pass
126
156
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
127
 
        raise errors.bzr_errors.NoWorkingTree(loc)
 
157
        raise bzr_errors.NoWorkingTree(loc)
128
158
 
129
159
    def create_repository(self, shared=False):
130
160
        return self.open_repository()
131
161
 
132
 
    def create_branch(self):
133
 
        return self.open_branch()
 
162
    def create_branch(self, name=None):
 
163
        refname = self._branch_name_to_ref(name)
 
164
        self._git.refs[refname] = "0" * 40
 
165
        return self.open_branch(name)
134
166
 
135
167
    def backup_bzrdir(self):
136
168
        if self._git.bare:
138
170
            return (self.root_transport.abspath(".git"),
139
171
                    self.root_transport.abspath(".git.backup"))
140
172
        else:
141
 
            raise errors.bzr_errors.BzrError("Unable to backup bare repositories")
 
173
            raise bzr_errors.BzrError("Unable to backup bare repositories")
142
174
 
143
175
    def create_workingtree(self, revision_id=None, from_branch=None,
144
176
        accelerator_tree=None, hardlink=False):
145
177
        if self._git.bare:
146
 
            raise errors.bzr_errors.BzrError("Can't create working tree in a bare repo")
 
178
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
147
179
        from dulwich.index import write_index
148
 
        write_index(self.root_transport.abspath(".git/index"), [])
 
180
        from dulwich.pack import SHA1Writer
 
181
        f = open(self.transport.local_abspath("index"), 'w+')
 
182
        try:
 
183
            f = SHA1Writer(f)
 
184
            write_index(f, [])
 
185
        finally:
 
186
            f.close()
149
187
        return self.open_workingtree()