/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

Implement to_files() for git merge directives.

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