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 Repository and a Bazaar Branch"""
|
|
18 |
||
19 |
from bzrlib import ( |
|
20 |
repository, |
|
21 |
urlutils, |
|
22 |
)
|
|
23 |
||
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
24 |
from bzrlib.plugins.git.gitlib import model |
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
25 |
|
26 |
||
27 |
class GitRepository(repository.Repository): |
|
28 |
"""An adapter to git repositories for bzr.""" |
|
29 |
||
30 |
def __init__(self, gitdir, lockfiles): |
|
31 |
self.bzrdir = gitdir |
|
32 |
self.control_files = lockfiles |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
33 |
gitdirectory = gitdir.transport.local_abspath('.') |
34 |
self._git = model.GitModel(gitdirectory) |
|
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
35 |
self._revision_cache = {} |
36 |
||
37 |
def _ancestor_revisions(self, revision_ids): |
|
38 |
if revision_ids is not None: |
|
39 |
git_revisions = [gitrevid_from_bzr(r) for r in revision_ids] |
|
40 |
else: |
|
41 |
git_revisions = None |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
42 |
for lines in self._git.ancestor_lines(git_revisions): |
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
43 |
yield self.parse_rev(lines) |
44 |
||
45 |
def is_shared(self): |
|
46 |
return True |
|
47 |
||
48 |
def get_revision_graph(self, revision_id=None): |
|
49 |
if revision_id is None: |
|
50 |
revisions = None |
|
51 |
else: |
|
52 |
revisions = [revision_id] |
|
53 |
return self.get_revision_graph_with_ghosts(revisions).get_ancestors() |
|
54 |
||
55 |
def get_revision_graph_with_ghosts(self, revision_ids=None): |
|
56 |
result = deprecated_graph.Graph() |
|
57 |
for revision in self._ancestor_revisions(revision_ids): |
|
58 |
result.add_node(revision.revision_id, revision.parent_ids) |
|
59 |
self._revision_cache[revision.revision_id] = revision |
|
60 |
return result |
|
61 |
||
62 |
def get_revision(self, revision_id): |
|
63 |
if revision_id in self._revision_cache: |
|
64 |
return self._revision_cache[revision_id] |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
65 |
raw = self._git.rev_list([gitrevid_from_bzr(revision_id)], max_count=1, |
66 |
header=True) |
|
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
67 |
return self.parse_rev(raw) |
68 |
||
69 |
def has_revision(self, revision_id): |
|
70 |
try: |
|
71 |
self.get_revision(revision_id) |
|
72 |
except NoSuchRevision: |
|
73 |
return False |
|
74 |
else: |
|
75 |
return True |
|
76 |
||
77 |
def get_revisions(self, revisions): |
|
78 |
return [self.get_revision(r) for r in revisions] |
|
79 |
||
80 |
def parse_rev(self, raw): |
|
81 |
# first field is the rev itself.
|
|
82 |
# then its 'field value'
|
|
83 |
# until the EOF??
|
|
84 |
parents = [] |
|
85 |
log = [] |
|
86 |
in_log = False |
|
87 |
committer = None |
|
88 |
revision_id = bzrrevid_from_git(raw[0][:-1]) |
|
89 |
for field in raw[1:]: |
|
90 |
#if field.startswith('author '):
|
|
91 |
# committer = field[7:]
|
|
92 |
if field.startswith('parent '): |
|
93 |
parents.append(bzrrevid_from_git(field.split()[1])) |
|
94 |
elif field.startswith('committer '): |
|
95 |
commit_fields = field.split() |
|
96 |
if committer is None: |
|
97 |
committer = ' '.join(commit_fields[1:-3]) |
|
98 |
timestamp = commit_fields[-2] |
|
99 |
timezone = commit_fields[-1] |
|
100 |
elif field.startswith('tree '): |
|
101 |
tree_id = field.split()[1] |
|
102 |
elif in_log: |
|
103 |
log.append(field[4:]) |
|
104 |
elif field == '\n': |
|
105 |
in_log = True |
|
106 |
||
107 |
log = ''.join(log) |
|
108 |
result = Revision(revision_id) |
|
109 |
result.parent_ids = parents |
|
110 |
result.message = log |
|
111 |
result.inventory_sha1 = "" |
|
112 |
result.timezone = timezone and int(timezone) |
|
113 |
result.timestamp = float(timestamp) |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
114 |
result.committer = committer |
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
115 |
result.properties['git-tree-id'] = tree_id |
116 |
return result |
|
117 |
||
118 |
def revision_trees(self, revids): |
|
119 |
for revid in revids: |
|
120 |
yield self.revision_tree(revid) |
|
121 |
||
122 |
def revision_tree(self, revision_id): |
|
123 |
return GitRevisionTree(self, revision_id) |
|
124 |
||
125 |
def get_inventory(self, revision_id): |
|
126 |
revision = self.get_revision(revision_id) |
|
127 |
inventory = GitInventory(revision_id) |
|
128 |
tree_id = revision.properties['git-tree-id'] |
|
129 |
type_map = {'blob': 'file', 'tree': 'directory' } |
|
130 |
def get_inventory(tree_id, prefix): |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
131 |
for perms, type, obj_id, name in self._git.get_inventory(tree_id): |
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
132 |
full_path = prefix + name |
133 |
if type == 'blob': |
|
134 |
text_sha1 = obj_id |
|
135 |
else: |
|
136 |
text_sha1 = None |
|
137 |
executable = (perms[-3] in ('1', '3', '5', '7')) |
|
138 |
entry = GitEntry(full_path, type_map[type], revision_id, |
|
139 |
text_sha1, executable) |
|
140 |
inventory.entries[full_path] = entry |
|
141 |
if type == 'tree': |
|
142 |
get_inventory(obj_id, full_path+'/') |
|
143 |
get_inventory(tree_id, '') |
|
144 |
return inventory |
|
145 |
||
146 |
||
147 |
class GitRevisionTree(object): |
|
148 |
||
149 |
def __init__(self, repository, revision_id): |
|
150 |
self.repository = repository |
|
151 |
self.revision_id = revision_id |
|
152 |
self.inventory = repository.get_inventory(revision_id) |
|
153 |
||
154 |
def get_file(self, file_id): |
|
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
155 |
return iterablefile.IterableFile(self.get_file_lines(file_id)) |
156 |
||
157 |
def get_file_lines(self, file_id): |
|
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
158 |
obj_id = self.inventory[file_id].text_sha1 |
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
159 |
return self.repository._git.cat_file('blob', obj_id) |
0.200.18
by John Arbash Meinel
Start splitting up the Git{Branch,Dir,Repository} into separate modules, etc. |
160 |
|
161 |
def is_executable(self, file_id): |
|
162 |
return self.inventory[file_id].executable |
|
163 |
||
164 |
||
0.200.19
by John Arbash Meinel
More refactoring. Add some direct tests for GitModel. |
165 |
class GitInventory(object): |
166 |
||
167 |
def __init__(self, revision_id): |
|
168 |
self.entries = {} |
|
169 |
self.root = GitEntry('', 'directory', revision_id) |
|
170 |
self.entries[''] = self.root |
|
171 |
||
172 |
def __getitem__(self, key): |
|
173 |
return self.entries[key] |
|
174 |
||
175 |
def iter_entries(self): |
|
176 |
return iter(sorted(self.entries.items())) |
|
177 |
||
178 |
def iter_entries_by_dir(self): |
|
179 |
return self.iter_entries() |
|
180 |
||
181 |
def __len__(self): |
|
182 |
return len(self.entries) |
|
183 |
||
184 |
||
185 |
class GitEntry(object): |
|
186 |
||
187 |
def __init__(self, path, kind, revision, text_sha1=None, executable=False, |
|
188 |
text_size=None): |
|
189 |
self.path = path |
|
190 |
self.file_id = path |
|
191 |
self.kind = kind |
|
192 |
self.executable = executable |
|
193 |
self.name = osutils.basename(path) |
|
194 |
if path == '': |
|
195 |
self.parent_id = None |
|
196 |
else: |
|
197 |
self.parent_id = osutils.dirname(path) |
|
198 |
self.revision = revision |
|
199 |
self.symlink_target = None |
|
200 |
self.text_sha1 = text_sha1 |
|
201 |
self.text_size = None |
|
202 |
||
203 |
def __repr__(self): |
|
204 |
return "GitEntry(%r, %r, %r, %r)" % (self.path, self.kind, |
|
205 |
self.revision, self.parent_id) |
|
206 |
||
207 |