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 __future__ import absolute_import
21
from dulwich.errors import (
24
from dulwich.object_store import (
28
from ...revision import (
33
class GitFileLastChangeScanner(object):
35
def __init__(self, repository):
36
self.repository = repository
37
self.store = self.repository._git.object_store
39
def find_last_change_revision(self, path, commit_id):
40
commit = self.store[commit_id]
41
target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
44
parent_commits = [self.store[c] for c in commit.parents]
45
for parent_commit in parent_commits:
47
mode, sha = tree_lookup_path(self.store.__getitem__,
48
parent_commit.tree, path)
49
except (NotTreeError, KeyError):
51
if mode != target_mode or sha != target_sha:
52
return (path, commit.id)
53
if parent_commits == []:
55
commit = parent_commits[0]
56
return (path, commit.id)
59
class GitFileParentProvider(object):
61
def __init__(self, change_scanner):
62
self.change_scanner = change_scanner
63
self.store = self.change_scanner.repository._git.object_store
65
def _get_parents(self, file_id, text_revision):
66
commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
68
path = mapping.parse_file_id(file_id)
70
for commit_parent in self.store[commit_id].parents:
71
(_, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
72
if text_parent not in text_parents:
73
text_parents.append(text_parent)
74
return tuple([(file_id,
75
self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
78
def get_parent_map(self, keys):
81
(file_id, text_revision) = key
82
if text_revision == NULL_REVISION:
85
ret[key] = self._get_parents(file_id, text_revision)