/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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
0.200.108 by Jelmer Vernooij
Support bzr init --git.
19
import git, os
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
20
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
21
from bzrlib.lazy_import import lazy_import
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
22
from bzrlib import (
23
    bzrdir,
24
    lockable_files,
25
    urlutils,
26
    )
27
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
28
lazy_import(globals(), """
0.200.108 by Jelmer Vernooij
Support bzr init --git.
29
from bzrlib.lockable_files import TransportLock
0.200.27 by David Allouche
Flat is better than nested, remove the gitlib hierarchy.
30
from bzrlib.plugins.git import (
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
31
    errors,
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
32
    branch,
33
    repository,
34
    workingtree,
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
35
    )
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
36
""")
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
37
38
39
class GitLock(object):
40
    """A lock that thunks through to Git."""
41
0.200.84 by Jelmer Vernooij
Fix lock_write argument.
42
    def lock_write(self, token=None):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
43
        pass
44
45
    def lock_read(self):
46
        pass
47
48
    def unlock(self):
49
        pass
50
0.200.73 by Jelmer Vernooij
Implement GitLock.peek().
51
    def peek(self):
52
        pass
53
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
54
55
class GitLockableFiles(lockable_files.LockableFiles):
56
    """Git specific lockable files abstraction."""
57
58
    def __init__(self, lock):
59
        self._lock = lock
60
        self._transaction = None
61
        self._lock_mode = None
62
        self._lock_count = 0
63
64
65
class GitDir(bzrdir.BzrDir):
66
    """An adapter to the '.git' dir used by git."""
67
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
68
    _gitrepository_class = repository.GitRepository
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
69
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
70
    def __init__(self, transport, lockfiles, gitrepo, format):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
71
        self._format = format
72
        self.root_transport = transport
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
73
        self._git = gitrepo
74
        if gitrepo.bare:
75
            self.transport = transport
76
        else:
77
            self.transport = transport.clone('.git')
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
78
        self._lockfiles = lockfiles
79
80
    def get_branch_transport(self, branch_format):
81
        if branch_format is None:
82
            return self.transport
83
        if isinstance(branch_format, GitBzrDirFormat):
84
            return self.transport
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
85
        raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
86
87
    get_repository_transport = get_branch_transport
88
    get_workingtree_transport = get_branch_transport
89
90
    def is_supported(self):
91
        return True
92
93
    def open_branch(self, ignored=None):
0.200.57 by Jelmer Vernooij
Fix more tests.
94
        """'create' a branch for this dir."""
95
        repo = self.open_repository()
96
        if repo._git.heads == []:
97
            head = None
98
        else:
0.200.58 by Jelmer Vernooij
Fix remaining tests.
99
            head = repo._git.heads[0].commit.id
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
100
        return branch.GitBranch(self, repo, head, 
0.200.57 by Jelmer Vernooij
Fix more tests.
101
                                    self.root_transport.base, self._lockfiles)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
102
103
    def open_repository(self, shared=False):
104
        """'open' a repository for this dir."""
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
105
        return self._gitrepository_class(self, self._lockfiles)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
106
0.203.1 by Aaron Bentley
Make checkouts work
107
    def open_workingtree(self, recommend_upgrade=True):
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
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:
0.200.94 by Jelmer Vernooij
Eliminate (duplicate) git_ prefix.
112
            return workingtree.GitWorkingTree(self, self.open_repository(), 
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
113
                                                  self.open_branch())
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
114
0.203.1 by Aaron Bentley
Make checkouts work
115
    def cloning_metadir(self):
116
        return bzrdir.BzrDirFormat.get_default_format()
117
0.200.108 by Jelmer Vernooij
Support bzr init --git.
118
    def create_repository(self, shared=False):
119
        return self.open_repository()
120
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
121
122
class GitBzrDirFormat(bzrdir.BzrDirFormat):
123
    """The .git directory control format."""
124
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
125
    _gitdir_class = GitDir
0.200.108 by Jelmer Vernooij
Support bzr init --git.
126
    _lock_class = TransportLock
0.202.2 by David Allouche
GitRepository.get_inventory and .revision_tree work for the null revision. Support for testing GitRepository without disk data.
127
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
128
    @classmethod
129
    def _known_formats(self):
130
        return set([GitBzrDirFormat()])
131
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
132
    def open(self, transport, _found=None):
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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+'):]
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
140
141
        try:
142
            gitrepo = git.repo.Repo(transport.local_abspath("."))
143
        except errors.bzr_errors.NotLocalUrl:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
144
            raise errors.bzr_errors.NotBranchError(path=transport.base)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
145
        lockfiles = GitLockableFiles(GitLock())
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
146
        return self._gitdir_class(transport, lockfiles, gitrepo, self)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
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:
0.200.19 by John Arbash Meinel
More refactoring. Add some direct tests for GitModel.
161
            raise errors.bzr_errors.NotBranchError(path=transport.base)
162
        raise errors.bzr_errors.NotBranchError(path=transport.base)
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
163
0.200.69 by Jelmer Vernooij
Implement GitBzrDirFormat.get_format_description.
164
    def get_format_description(self):
165
        return "Local Git Repository"
166
0.200.108 by Jelmer Vernooij
Support bzr init --git.
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
0.200.18 by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc.
181
182
bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)