1
# Copyright (C) 2007 Canonical Ltd
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.
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.
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
17
"""An adapter between a Git Repository and a Bazaar Branch"""
25
from bzrlib.plugins.git import GitModel
28
class GitRepository(repository.Repository):
29
"""An adapter to git repositories for bzr."""
31
def __init__(self, gitdir, lockfiles):
33
self.control_files = lockfiles
34
gitdirectory = urlutils.local_path_from_url(gitdir.transport.base)
35
self.git = GitModel(gitdirectory)
36
self._revision_cache = {}
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]
43
for lines in self.git.ancestor_lines(git_revisions):
44
yield self.parse_rev(lines)
49
def get_revision_graph(self, revision_id=None):
50
if revision_id is None:
53
revisions = [revision_id]
54
return self.get_revision_graph_with_ghosts(revisions).get_ancestors()
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
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,
68
return self.parse_rev(raw)
70
def has_revision(self, revision_id):
72
self.get_revision(revision_id)
73
except NoSuchRevision:
78
def get_revisions(self, revisions):
79
return [self.get_revision(r) for r in revisions]
81
def parse_rev(self, raw):
82
# first field is the rev itself.
83
# then its 'field value'
89
revision_id = bzrrevid_from_git(raw[0][:-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()
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]
104
log.append(field[4:])
109
result = Revision(revision_id)
110
result.parent_ids = parents
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
119
def revision_trees(self, revids):
121
yield self.revision_tree(revid)
123
def revision_tree(self, revision_id):
124
return GitRevisionTree(self, revision_id)
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
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
143
get_inventory(obj_id, full_path+'/')
144
get_inventory(tree_id, '')
148
class GitRevisionTree(object):
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)
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)
160
def is_executable(self, file_id):
161
return self.inventory[file_id].executable