/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

Support bzr init --git.

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
 
17
"""An adapter between a Git control dir and a Bazaar BzrDir"""
 
18
 
 
19
import git, os
 
20
 
 
21
from bzrlib.lazy_import import lazy_import
22
22
from bzrlib import (
23
23
    bzrdir,
24
24
    lockable_files,
25
25
    urlutils,
26
26
    )
27
27
 
28
 
LockWarner = getattr(lockable_files, "_LockWarner", None)
29
 
 
 
28
lazy_import(globals(), """
 
29
from bzrlib.lockable_files import TransportLock
30
30
from bzrlib.plugins.git import (
31
 
    LocalGitBzrDirFormat,
 
31
    errors,
32
32
    branch,
33
 
    errors,
34
 
    get_rich_root_format,
35
33
    repository,
36
34
    workingtree,
37
35
    )
 
36
""")
38
37
 
39
38
 
40
39
class GitLock(object):
52
51
    def peek(self):
53
52
        pass
54
53
 
55
 
    def validate_token(self, token):
56
 
        pass
57
 
 
58
54
 
59
55
class GitLockableFiles(lockable_files.LockableFiles):
60
56
    """Git specific lockable files abstraction."""
61
57
 
62
 
    def __init__(self, transport, lock):
 
58
    def __init__(self, lock):
63
59
        self._lock = lock
64
60
        self._transaction = None
65
61
        self._lock_mode = None
66
 
        self._transport = transport
67
 
        if LockWarner is None:
68
 
            # Bzr 1.13
69
 
            self._lock_count = 0
70
 
        else:
71
 
            self._lock_warner = LockWarner(repr(self))
 
62
        self._lock_count = 0
72
63
 
73
64
 
74
65
class GitDir(bzrdir.BzrDir):
75
66
    """An adapter to the '.git' dir used by git."""
76
67
 
77
 
    def is_supported(self):
78
 
        return True
79
 
 
80
 
    def cloning_metadir(self, stacked=False):
81
 
        return get_rich_root_format(stacked)
82
 
 
83
 
 
84
 
class LocalGitDir(GitDir):
85
 
    """An adapter to the '.git' dir used by git."""
86
 
 
87
 
    _gitrepository_class = repository.LocalGitRepository
 
68
    _gitrepository_class = repository.GitRepository
88
69
 
89
70
    def __init__(self, transport, lockfiles, gitrepo, format):
90
71
        self._format = format
95
76
        else:
96
77
            self.transport = transport.clone('.git')
97
78
        self._lockfiles = lockfiles
98
 
        self._mode_check_done = None
99
 
 
100
 
    def is_control_filename(self, filename):
101
 
        return filename == '.git' or filename.startswith('.git/')
102
79
 
103
80
    def get_branch_transport(self, branch_format):
104
81
        if branch_format is None:
105
82
            return self.transport
106
 
        if isinstance(branch_format, LocalGitBzrDirFormat):
 
83
        if isinstance(branch_format, GitBzrDirFormat):
107
84
            return self.transport
108
85
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
109
86
 
110
87
    get_repository_transport = get_branch_transport
111
88
    get_workingtree_transport = get_branch_transport
112
89
 
113
 
    def open_branch(self, ignore_fallbacks=None):
 
90
    def is_supported(self):
 
91
        return True
 
92
 
 
93
    def open_branch(self, ignored=None):
114
94
        """'create' a branch for this dir."""
115
95
        repo = self.open_repository()
116
 
        return branch.LocalGitBranch(self, repo, "HEAD", self._lockfiles)
 
96
        if repo._git.heads == []:
 
97
            head = None
 
98
        else:
 
99
            head = repo._git.heads[0].commit.id
 
100
        return branch.GitBranch(self, repo, head, 
 
101
                                    self.root_transport.base, self._lockfiles)
117
102
 
118
103
    def open_repository(self, shared=False):
119
104
        """'open' a repository for this dir."""
120
105
        return self._gitrepository_class(self, self._lockfiles)
121
106
 
122
107
    def open_workingtree(self, recommend_upgrade=True):
123
 
        if not self._git.bare and self._git.has_index():
 
108
        if self._git.bare:
 
109
            loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
110
            raise errors.bzr_errors.NoWorkingTree(loc)
 
111
        else:
124
112
            return workingtree.GitWorkingTree(self, self.open_repository(), 
125
113
                                                  self.open_branch())
126
 
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
127
 
        raise errors.bzr_errors.NoWorkingTree(loc)
 
114
 
 
115
    def cloning_metadir(self):
 
116
        return bzrdir.BzrDirFormat.get_default_format()
128
117
 
129
118
    def create_repository(self, shared=False):
130
119
        return self.open_repository()
131
120
 
132
 
    def create_branch(self):
133
 
        return self.open_branch()
134
 
 
135
 
    def backup_bzrdir(self):
136
 
        if self._git.bare:
137
 
            self.root_transport.copy_tree(".git", ".git.backup")
138
 
            return (self.root_transport.abspath(".git"),
139
 
                    self.root_transport.abspath(".git.backup"))
140
 
        else:
141
 
            raise errors.bzr_errors.BzrError("Unable to backup bare repositories")
142
 
 
143
 
    def create_workingtree(self, revision_id=None, from_branch=None,
144
 
        accelerator_tree=None, hardlink=False):
145
 
        if self._git.bare:
146
 
            raise errors.bzr_errors.BzrError("Can't create working tree in a bare repo")
147
 
        from dulwich.index import write_index
148
 
        write_index(self.root_transport.abspath(".git/index"), [])
149
 
        return self.open_workingtree()
 
121
 
 
122
class GitBzrDirFormat(bzrdir.BzrDirFormat):
 
123
    """The .git directory control format."""
 
124
 
 
125
    _gitdir_class = GitDir
 
126
    _lock_class = TransportLock
 
127
 
 
128
    @classmethod
 
129
    def _known_formats(self):
 
130
        return set([GitBzrDirFormat()])
 
131
 
 
132
    def open(self, transport, _found=None):
 
133
        """Open this directory.
 
134
 
 
135
        """
 
136
        # we dont grok readonly - git isn't integrated with transport.
 
137
        url = transport.base
 
138
        if url.startswith('readonly+'):
 
139
            url = url[len('readonly+'):]
 
140
 
 
141
        try:
 
142
            gitrepo = git.repo.Repo(transport.local_abspath("."))
 
143
        except errors.bzr_errors.NotLocalUrl:
 
144
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
145
        lockfiles = GitLockableFiles(GitLock())
 
146
        return self._gitdir_class(transport, lockfiles, gitrepo, self)
 
147
 
 
148
    @classmethod
 
149
    def probe_transport(klass, transport):
 
150
        """Our format is present if the transport ends in '.not/'."""
 
151
        # little ugly, but works
 
152
        format = klass()
 
153
        # delegate to the main opening code. This pays a double rtt cost at the
 
154
        # moment, so perhaps we want probe_transport to return the opened thing
 
155
        # rather than an openener ? or we could return a curried thing with the
 
156
        # dir to open already instantiated ? Needs more thought.
 
157
        try:
 
158
            format.open(transport)
 
159
            return format
 
160
        except Exception, e:
 
161
            raise errors.bzr_errors.NotBranchError(path=transport.base)
 
162
        raise errors.bzr_errors.NotBranchError(path=transport.base)
 
163
 
 
164
    def get_format_description(self):
 
165
        return "Local Git Repository"
 
166
 
 
167
    def get_format_string(self):
 
168
        return "Local Git Repository"
 
169
 
 
170
    def initialize_on_transport(self, transport):
 
171
        from bzrlib.transport.local import LocalTransport
 
172
 
 
173
        if not isinstance(transport, LocalTransport):
 
174
            raise NotImplementedError(self.initialize, 
 
175
                "Can't create Git Repositories/branches on "
 
176
                "non-local transports")
 
177
 
 
178
        git.repo.Repo.create(transport.local_abspath(".")) 
 
179
        return self.open(transport)
 
180
 
 
181
 
 
182
bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)