1
# Copyright (C) 2011 Canonical Ltd
2
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""File graph access."""
22
from dulwich.errors import (
25
from dulwich.object_store import (
29
from ..revision import (
33
from .mapping import (
38
class GitFileLastChangeScanner(object):
40
def __init__(self, repository):
41
self.repository = repository
42
self.store = self.repository._git.object_store
44
def find_last_change_revision(self, path, commit_id):
45
if not isinstance(path, bytes):
47
commit = self.store[commit_id]
48
target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
51
target_mode = stat.S_IFDIR
52
if target_mode is None:
53
raise AssertionError("sha %r for %r in %r" %
54
(target_sha, path, commit_id))
57
for parent_commit in [self.store[c] for c in commit.parents]:
59
mode, sha = tree_lookup_path(self.store.__getitem__,
60
parent_commit.tree, path)
61
except (NotTreeError, KeyError):
64
parent_commits.append(parent_commit)
67
# Candidate found iff, mode or text changed,
68
# or is a directory that didn't previously exist.
69
if mode != target_mode or (
70
not stat.S_ISDIR(target_mode) and sha != target_sha):
71
return (path, commit.id)
72
if parent_commits == []:
74
commit = parent_commits[0]
75
return (path, commit.id)
78
class GitFileParentProvider(object):
80
def __init__(self, change_scanner):
81
self.change_scanner = change_scanner
82
self.store = self.change_scanner.repository._git.object_store
84
def _get_parents(self, file_id, text_revision):
85
commit_id, mapping = (
86
self.change_scanner.repository.lookup_bzr_revision_id(
89
path = encode_git_path(mapping.parse_file_id(file_id))
91
raise KeyError(file_id)
93
for commit_parent in self.store[commit_id].parents:
95
(path, text_parent) = (
96
self.change_scanner.find_last_change_revision(
100
if text_parent not in text_parents:
101
text_parents.append(text_parent)
104
self.change_scanner.repository.lookup_foreign_revision_id(p))
105
for p in text_parents])
107
def get_parent_map(self, keys):
110
(file_id, text_revision) = key
111
if text_revision == NULL_REVISION:
114
if not isinstance(file_id, bytes):
115
raise TypeError(file_id)
116
if not isinstance(text_revision, bytes):
117
raise TypeError(text_revision)
119
ret[key] = self._get_parents(file_id, text_revision)