/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 helper function, always set encoding to utf-8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""An adapter between a Git control dir and a Bazaar BzrDir"""
18
 
 
19
 
import os
20
 
 
21
 
import bzrlib
22
 
from bzrlib.lazy_import import lazy_import
 
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
 
18
 
23
19
from bzrlib import (
24
20
    bzrdir,
 
21
    errors as bzr_errors,
25
22
    lockable_files,
26
23
    urlutils,
27
24
    )
28
25
 
29
 
lazy_import(globals(), """
30
 
from bzrlib.lockable_files import TransportLock
 
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
27
 
31
28
from bzrlib.plugins.git import (
32
 
    errors,
33
 
    branch,
34
 
    repository,
35
 
    workingtree,
 
29
    LocalGitBzrDirFormat,
 
30
    get_rich_root_format,
36
31
    )
37
 
""")
38
 
 
39
 
from bzrlib.plugins.git import LocalGitBzrDirFormat
40
 
 
41
32
 
42
33
 
43
34
class GitLock(object):
58
49
    def validate_token(self, token):
59
50
        pass
60
51
 
 
52
    def break_lock(self):
 
53
        pass
 
54
 
61
55
 
62
56
class GitLockableFiles(lockable_files.LockableFiles):
63
57
    """Git specific lockable files abstraction."""
66
60
        self._lock = lock
67
61
        self._transaction = None
68
62
        self._lock_mode = None
69
 
        self._lock_count = 0
70
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))
71
69
 
72
70
 
73
71
class GitDir(bzrdir.BzrDir):
77
75
        return True
78
76
 
79
77
    def cloning_metadir(self, stacked=False):
80
 
        return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
 
78
        return get_rich_root_format(stacked)
 
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
81
87
 
82
88
 
83
89
class LocalGitDir(GitDir):
84
90
    """An adapter to the '.git' dir used by git."""
85
91
 
86
 
    _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)
87
97
 
88
98
    def __init__(self, transport, lockfiles, gitrepo, format):
89
99
        self._format = format
94
104
        else:
95
105
            self.transport = transport.clone('.git')
96
106
        self._lockfiles = lockfiles
 
107
        self._mode_check_done = None
 
108
 
 
109
    def is_control_filename(self, filename):
 
110
        return filename == '.git' or filename.startswith('.git/')
97
111
 
98
112
    def get_branch_transport(self, branch_format):
99
113
        if branch_format is None:
100
114
            return self.transport
101
115
        if isinstance(branch_format, LocalGitBzrDirFormat):
102
116
            return self.transport
103
 
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
 
117
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
104
118
 
105
119
    get_repository_transport = get_branch_transport
106
120
    get_workingtree_transport = get_branch_transport
107
121
 
108
 
    def open_branch(self, ignored=None):
 
122
 
 
123
    def open_branch(self, ignore_fallbacks=None, name=None, unsupported=False):
109
124
        """'create' a branch for this dir."""
110
125
        repo = self.open_repository()
111
 
        return branch.LocalGitBranch(self, repo, "HEAD", repo._git.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
112
138
 
113
139
    def open_repository(self, shared=False):
114
140
        """'open' a repository for this dir."""
115
141
        return self._gitrepository_class(self, self._lockfiles)
116
142
 
117
143
    def open_workingtree(self, recommend_upgrade=True):
118
 
        if (not self._git.bare and 
119
 
            os.path.exists(os.path.join(self._git.controldir(), "index"))):
120
 
            return workingtree.GitWorkingTree(self, self.open_repository(), 
121
 
                                                  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
122
152
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
 
        raise errors.bzr_errors.NoWorkingTree(loc)
 
153
        raise bzr_errors.NoWorkingTree(loc)
124
154
 
125
155
    def create_repository(self, shared=False):
126
156
        return self.open_repository()
 
157
 
 
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)
 
162
 
 
163
    def backup_bzrdir(self):
 
164
        if self._git.bare:
 
165
            self.root_transport.copy_tree(".git", ".git.backup")
 
166
            return (self.root_transport.abspath(".git"),
 
167
                    self.root_transport.abspath(".git.backup"))
 
168
        else:
 
169
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
170
 
 
171
    def create_workingtree(self, revision_id=None, from_branch=None,
 
172
        accelerator_tree=None, hardlink=False):
 
173
        if self._git.bare:
 
174
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
 
175
        from dulwich.index import write_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()
 
183
        return self.open_workingtree()