1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""An adapter between a Git control dir and a Bazaar BzrDir"""
import git
from bzrlib.lazy_import import lazy_import
from bzrlib import (
bzrdir,
lockable_files,
urlutils,
)
lazy_import(globals(), """
from bzrlib.plugins.git import (
errors,
git_branch,
git_repository,
git_workingtree,
)
""")
class GitLock(object):
"""A lock that thunks through to Git."""
def lock_write(self, token=None):
pass
def lock_read(self):
pass
def unlock(self):
pass
def peek(self):
pass
class GitLockableFiles(lockable_files.LockableFiles):
"""Git specific lockable files abstraction."""
def __init__(self, lock):
self._lock = lock
self._transaction = None
self._lock_mode = None
self._lock_count = 0
class GitDir(bzrdir.BzrDir):
"""An adapter to the '.git' dir used by git."""
_gitrepository_class = git_repository.GitRepository
def __init__(self, transport, lockfiles, gitrepo, format):
self._format = format
self.root_transport = transport
self._git = gitrepo
if gitrepo.bare:
self.transport = transport
else:
self.transport = transport.clone('.git')
self._lockfiles = lockfiles
def get_branch_transport(self, branch_format):
if branch_format is None:
return self.transport
if isinstance(branch_format, GitBzrDirFormat):
return self.transport
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
get_repository_transport = get_branch_transport
get_workingtree_transport = get_branch_transport
def is_supported(self):
return True
def open_branch(self, ignored=None):
"""'create' a branch for this dir."""
repo = self.open_repository()
if repo._git.heads == []:
head = None
else:
head = repo._git.heads[0].commit.id
return git_branch.GitBranch(self, repo, head,
self.root_transport.base, self._lockfiles)
def open_repository(self, shared=False):
"""'open' a repository for this dir."""
return self._gitrepository_class(self, self._lockfiles)
def open_workingtree(self, recommend_upgrade=True):
if self._git.bare:
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
raise errors.bzr_errors.NoWorkingTree(loc)
else:
return git_workingtree.GitWorkingTree(self, self.open_repository(),
self.open_branch())
def cloning_metadir(self):
return bzrdir.BzrDirFormat.get_default_format()
class GitBzrDirFormat(bzrdir.BzrDirFormat):
"""The .git directory control format."""
_gitdir_class = GitDir
@classmethod
def _known_formats(self):
return set([GitBzrDirFormat()])
def open(self, transport, _found=None):
"""Open this directory.
"""
# we dont grok readonly - git isn't integrated with transport.
url = transport.base
if url.startswith('readonly+'):
url = url[len('readonly+'):]
try:
gitrepo = git.repo.Repo(transport.local_abspath("."))
except errors.bzr_errors.NotLocalUrl:
raise errors.bzr_errors.NotBranchError(path=transport.base)
lockfiles = GitLockableFiles(GitLock())
return self._gitdir_class(transport, lockfiles, gitrepo, self)
@classmethod
def probe_transport(klass, transport):
"""Our format is present if the transport ends in '.not/'."""
# little ugly, but works
format = klass()
# delegate to the main opening code. This pays a double rtt cost at the
# moment, so perhaps we want probe_transport to return the opened thing
# rather than an openener ? or we could return a curried thing with the
# dir to open already instantiated ? Needs more thought.
try:
format.open(transport)
return format
except Exception, e:
raise errors.bzr_errors.NotBranchError(path=transport.base)
raise errors.bzr_errors.NotBranchError(path=transport.base)
def get_format_description(self):
return "Local Git Repository"
bzrdir.BzrDirFormat.register_control_format(GitBzrDirFormat)
|