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 |
errors, |
|
21 |
repository, |
|
22 |
urlutils, |
|
23 |
)
|
|
24 |
||
25 |
from bzrlib.plugins.git import GitModel |
|
26 |
||
27 |
||
28 |
class GitRepository(repository.Repository): |
|
29 |
"""An adapter to git repositories for bzr.""" |
|
30 |
||
31 |
def __init__(self, gitdir, lockfiles): |
|
32 |
self.bzrdir = gitdir |
|
33 |
self.control_files = lockfiles |
|
34 |
gitdirectory = urlutils.local_path_from_url(gitdir.transport.base) |
|
35 |
self.git = GitModel(gitdirectory) |
|
36 |
self._revision_cache = {} |
|
37 |
||
38 |
def _ancestor_revisions(self, revision_ids): |
|
39 |
if revision_ids is not None: |
|
40 |
git_revisions = [gitrevid_from_bzr(r) for r in revision_ids] |
|
41 |
else: |
|
42 |
git_revisions = None |
|
43 |
for lines in self.git.ancestor_lines(git_revisions): |
|
44 |
yield self.parse_rev(lines) |
|
45 |
||
46 |
def is_shared(self): |
|
47 |
return True |
|
48 |
||
49 |
def get_revision_graph(self, revision_id=None): |
|
50 |
if revision_id is None: |
|
51 |
revisions = None |
|
52 |
else: |
|
53 |
revisions = [revision_id] |
|
54 |
return self.get_revision_graph_with_ghosts(revisions).get_ancestors() |
|
55 |
||
56 |
def get_revision_graph_with_ghosts(self, revision_ids=None): |
|
57 |
result = deprecated_graph.Graph() |
|
58 |
for revision in self._ancestor_revisions(revision_ids): |
|
59 |
result.add_node(revision.revision_id, revision.parent_ids) |
|
60 |
self._revision_cache[revision.revision_id] = revision |
|
61 |
return result |
|
62 |
||
63 |
def get_revision(self, revision_id): |
|
64 |
if revision_id in self._revision_cache: |
|
65 |
return self._revision_cache[revision_id] |
|
66 |
raw = self.git.rev_list([gitrevid_from_bzr(revision_id)], max_count=1, |
|
67 |
header=True) |
|
68 |
return self.parse_rev(raw) |
|
69 |
||
70 |
def has_revision(self, revision_id): |
|
71 |
try: |
|
72 |
self.get_revision(revision_id) |
|
73 |
except NoSuchRevision: |
|
74 |
return False |
|
75 |
else: |
|
76 |
return True |
|
77 |
||
78 |
def get_revisions(self, revisions): |
|
79 |
return [self.get_revision(r) for r in revisions] |
|
80 |
||
81 |
def parse_rev(self, raw): |
|
82 |
# first field is the rev itself.
|
|
83 |
# then its 'field value'
|
|
84 |
# until the EOF??
|
|
85 |
parents = [] |
|
86 |
log = [] |
|
87 |
in_log = False |
|
88 |
committer = None |
|
89 |
revision_id = bzrrevid_from_git(raw[0][:-1]) |
|
90 |
for field in raw[1:]: |
|
91 |
#if field.startswith('author '):
|
|
92 |
# committer = field[7:]
|
|
93 |
if field.startswith('parent '): |
|
94 |
parents.append(bzrrevid_from_git(field.split()[1])) |
|
95 |
elif field.startswith('committer '): |
|
96 |
commit_fields = field.split() |
|
97 |
if committer is None: |
|
98 |
committer = ' '.join(commit_fields[1:-3]) |
|
99 |
timestamp = commit_fields[-2] |
|
100 |
timezone = commit_fields[-1] |
|
101 |
elif field.startswith('tree '): |
|
102 |
tree_id = field.split()[1] |
|
103 |
elif in_log: |
|
104 |
log.append(field[4:]) |
|
105 |
elif field == '\n': |
|
106 |
in_log = True |
|
107 |
||
108 |
log = ''.join(log) |
|
109 |
result = Revision(revision_id) |
|
110 |
result.parent_ids = parents |
|
111 |
result.message = log |
|
112 |
result.inventory_sha1 = "" |
|
113 |
result.timezone = timezone and int(timezone) |
|
114 |
result.timestamp = float(timestamp) |
|
115 |
result.committer = committer |
|
116 |
result.properties['git-tree-id'] = tree_id |
|
117 |
return result |
|
118 |
||
119 |
def revision_trees(self, revids): |
|
120 |
for revid in revids: |
|
121 |
yield self.revision_tree(revid) |
|
122 |
||
123 |
def revision_tree(self, revision_id): |
|
124 |
return GitRevisionTree(self, revision_id) |
|
125 |
||
126 |
def get_inventory(self, revision_id): |
|
127 |
revision = self.get_revision(revision_id) |
|
128 |
inventory = GitInventory(revision_id) |
|
129 |
tree_id = revision.properties['git-tree-id'] |
|
130 |
type_map = {'blob': 'file', 'tree': 'directory' } |
|
131 |
def get_inventory(tree_id, prefix): |
|
132 |
for perms, type, obj_id, name in self.git.get_inventory(tree_id): |
|
133 |
full_path = prefix + name |
|
134 |
if type == 'blob': |
|
135 |
text_sha1 = obj_id |
|
136 |
else: |
|
137 |
text_sha1 = None |
|
138 |
executable = (perms[-3] in ('1', '3', '5', '7')) |
|
139 |
entry = GitEntry(full_path, type_map[type], revision_id, |
|
140 |
text_sha1, executable) |
|
141 |
inventory.entries[full_path] = entry |
|
142 |
if type == 'tree': |
|
143 |
get_inventory(obj_id, full_path+'/') |
|
144 |
get_inventory(tree_id, '') |
|
145 |
return inventory |
|
146 |
||
147 |
||
148 |
class GitRevisionTree(object): |
|
149 |
||
150 |
def __init__(self, repository, revision_id): |
|
151 |
self.repository = repository |
|
152 |
self.revision_id = revision_id |
|
153 |
self.inventory = repository.get_inventory(revision_id) |
|
154 |
||
155 |
def get_file(self, file_id): |
|
156 |
obj_id = self.inventory[file_id].text_sha1 |
|
157 |
lines = self.repository.git.cat_file('blob', obj_id) |
|
158 |
return iterablefile.IterableFile(lines) |
|
159 |
||
160 |
def is_executable(self, file_id): |
|
161 |
return self.inventory[file_id].executable |
|
162 |
||
163 |