/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:
 
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
    errors as bzr_errors,
 
22
    lockable_files,
 
23
    urlutils,
 
24
    )
 
25
 
 
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
27
 
 
28
from bzrlib.plugins.git import (
 
29
    LocalGitBzrDirFormat,
 
30
    get_rich_root_format,
 
31
    )
 
32
 
 
33
 
 
34
class GitLock(object):
 
35
    """A lock that thunks through to Git."""
 
36
 
 
37
    def lock_write(self, token=None):
 
38
        pass
 
39
 
 
40
    def lock_read(self):
 
41
        pass
 
42
 
 
43
    def unlock(self):
 
44
        pass
 
45
 
 
46
    def peek(self):
 
47
        pass
 
48
 
 
49
    def validate_token(self, token):
 
50
        pass
 
51
 
 
52
    def break_lock(self):
 
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
    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
 
 
88
 
 
89
class LocalGitDir(GitDir):
 
90
    """An adapter to the '.git' dir used by git."""
 
91
 
 
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)
 
97
 
 
98
    def __init__(self, transport, lockfiles, gitrepo, format):
 
99
        self._format = format
 
100
        self.root_transport = transport
 
101
        self._git = gitrepo
 
102
        if gitrepo.bare:
 
103
            self.transport = transport
 
104
        else:
 
105
            self.transport = transport.clone('.git')
 
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/')
 
111
 
 
112
    def get_branch_transport(self, branch_format):
 
113
        if branch_format is None:
 
114
            return self.transport
 
115
        if isinstance(branch_format, LocalGitBzrDirFormat):
 
116
            return self.transport
 
117
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
118
 
 
119
    get_repository_transport = get_branch_transport
 
120
    get_workingtree_transport = get_branch_transport
 
121
 
 
122
 
 
123
    def open_branch(self, ignore_fallbacks=None, name=None, unsupported=False):
 
124
        """'create' a branch for this dir."""
 
125
        repo = self.open_repository()
 
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
 
138
 
 
139
    def open_repository(self, shared=False):
 
140
        """'open' a repository for this dir."""
 
141
        return self._gitrepository_class(self, self._lockfiles)
 
142
 
 
143
    def open_workingtree(self, recommend_upgrade=True):
 
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
 
152
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
153
        raise bzr_errors.NoWorkingTree(loc)
 
154
 
 
155
    def create_repository(self, shared=False):
 
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()