1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# Copyright (C) 2011 Canonical Ltd
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""File graph access."""
import stat
from dulwich.errors import (
NotTreeError,
)
from dulwich.object_store import (
tree_lookup_path,
)
from ..revision import (
NULL_REVISION,
)
from .mapping import (
encode_git_path,
)
class GitFileLastChangeScanner(object):
def __init__(self, repository):
self.repository = repository
self.store = self.repository._git.object_store
def find_last_change_revision(self, path, commit_id):
if not isinstance(path, bytes):
raise TypeError(path)
commit = self.store[commit_id]
target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
commit.tree, path)
if path == b'':
target_mode = stat.S_IFDIR
if target_mode is None:
raise AssertionError("sha %r for %r in %r" %
(target_sha, path, commit_id))
while True:
parent_commits = []
for parent_commit in [self.store[c] for c in commit.parents]:
try:
mode, sha = tree_lookup_path(self.store.__getitem__,
parent_commit.tree, path)
except (NotTreeError, KeyError):
continue
else:
parent_commits.append(parent_commit)
if path == b'':
mode = stat.S_IFDIR
# Candidate found iff, mode or text changed,
# or is a directory that didn't previously exist.
if mode != target_mode or (
not stat.S_ISDIR(target_mode) and sha != target_sha):
return (path, commit.id)
if parent_commits == []:
break
commit = parent_commits[0]
return (path, commit.id)
class GitFileParentProvider(object):
def __init__(self, change_scanner):
self.change_scanner = change_scanner
self.store = self.change_scanner.repository._git.object_store
def _get_parents(self, file_id, text_revision):
commit_id, mapping = (
self.change_scanner.repository.lookup_bzr_revision_id(
text_revision))
try:
path = encode_git_path(mapping.parse_file_id(file_id))
except ValueError:
raise KeyError(file_id)
text_parents = []
for commit_parent in self.store[commit_id].parents:
try:
(path, text_parent) = (
self.change_scanner.find_last_change_revision(
path, commit_parent))
except KeyError:
continue
if text_parent not in text_parents:
text_parents.append(text_parent)
return tuple([
(file_id,
self.change_scanner.repository.lookup_foreign_revision_id(p))
for p in text_parents])
def get_parent_map(self, keys):
ret = {}
for key in keys:
(file_id, text_revision) = key
if text_revision == NULL_REVISION:
ret[key] = ()
continue
if not isinstance(file_id, bytes):
raise TypeError(file_id)
if not isinstance(text_revision, bytes):
raise TypeError(text_revision)
try:
ret[key] = self._get_parents(file_id, text_revision)
except KeyError:
pass
return ret
|