1
# Copyright (C) 2011 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
"""File graph access."""
19
from dulwich.errors import (
22
from dulwich.object_store import (
26
from bzrlib.revision import (
31
class GitFileLastChangeScanner(object):
33
def __init__(self, repository):
34
self.repository = repository
35
self.store = self.repository._git.object_store
37
def find_last_change_revision(self, path, commit_id):
38
commit = self.store[commit_id]
39
target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
42
parent_commits = [self.store[c] for c in commit.parents]
43
for parent_commit in parent_commits:
45
mode, sha = tree_lookup_path(self.store.__getitem__,
46
parent_commit.tree, path)
47
except (NotTreeError, KeyError):
49
if mode != target_mode or sha != target_sha:
50
return (path, commit.id)
51
if parent_commits == []:
53
commit = parent_commits[0]
54
return (path, commit.id)
57
class GitFileParentProvider(object):
59
def __init__(self, change_scanner):
60
self.change_scanner = change_scanner
61
self.store = self.change_scanner.repository._git.object_store
63
def _get_parents(self, file_id, text_revision):
64
commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
66
path = mapping.parse_file_id(file_id)
68
for commit_parent in self.store[commit_id].parents:
69
(_, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
70
if text_parent not in text_parents:
71
text_parents.append(text_parent)
72
return tuple([(file_id,
73
self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
76
def get_parent_map(self, keys):
79
(file_id, text_revision) = key
80
if text_revision == NULL_REVISION:
83
ret[key] = self._get_parents(file_id, text_revision)