/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/filegraph.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
"""File graph access."""
 
19
 
 
20
import stat
 
21
 
 
22
from dulwich.errors import (
 
23
    NotTreeError,
 
24
    )
 
25
from dulwich.object_store import (
 
26
    tree_lookup_path,
 
27
    )
 
28
 
 
29
from ..revision import (
 
30
    NULL_REVISION,
 
31
    )
 
32
 
 
33
 
 
34
class GitFileLastChangeScanner(object):
 
35
 
 
36
    def __init__(self, repository):
 
37
        self.repository = repository
 
38
        self.store = self.repository._git.object_store
 
39
 
 
40
    def find_last_change_revision(self, path, commit_id):
 
41
        if not isinstance(path, bytes):
 
42
            raise TypeError(path)
 
43
        commit = self.store[commit_id]
 
44
        target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
 
45
                                                   commit.tree, path)
 
46
        if path == b'':
 
47
            target_mode = stat.S_IFDIR
 
48
        if target_mode is None:
 
49
            raise AssertionError("sha %r for %r in %r" %
 
50
                                 (target_sha, path, commit_id))
 
51
        while True:
 
52
            parent_commits = []
 
53
            for parent_commit in [self.store[c] for c in commit.parents]:
 
54
                try:
 
55
                    mode, sha = tree_lookup_path(self.store.__getitem__,
 
56
                                                 parent_commit.tree, path)
 
57
                except (NotTreeError, KeyError):
 
58
                    continue
 
59
                else:
 
60
                    parent_commits.append(parent_commit)
 
61
                if path == b'':
 
62
                    mode = stat.S_IFDIR
 
63
                # Candidate found iff, mode or text changed,
 
64
                # or is a directory that didn't previously exist.
 
65
                if mode != target_mode or (
 
66
                        not stat.S_ISDIR(target_mode) and sha != target_sha):
 
67
                    return (path, commit.id)
 
68
            if parent_commits == []:
 
69
                break
 
70
            commit = parent_commits[0]
 
71
        return (path, commit.id)
 
72
 
 
73
 
 
74
class GitFileParentProvider(object):
 
75
 
 
76
    def __init__(self, change_scanner):
 
77
        self.change_scanner = change_scanner
 
78
        self.store = self.change_scanner.repository._git.object_store
 
79
 
 
80
    def _get_parents(self, file_id, text_revision):
 
81
        commit_id, mapping = (
 
82
            self.change_scanner.repository.lookup_bzr_revision_id(
 
83
                text_revision))
 
84
        try:
 
85
            path = mapping.parse_file_id(file_id).encode('utf-8')
 
86
        except ValueError:
 
87
            raise KeyError(file_id)
 
88
        text_parents = []
 
89
        for commit_parent in self.store[commit_id].parents:
 
90
            try:
 
91
                (path, text_parent) = (
 
92
                    self.change_scanner.find_last_change_revision(
 
93
                        path, commit_parent))
 
94
            except KeyError:
 
95
                continue
 
96
            if text_parent not in text_parents:
 
97
                text_parents.append(text_parent)
 
98
        return tuple([
 
99
            (file_id,
 
100
                self.change_scanner.repository.lookup_foreign_revision_id(p))
 
101
            for p in text_parents])
 
102
 
 
103
    def get_parent_map(self, keys):
 
104
        ret = {}
 
105
        for key in keys:
 
106
            (file_id, text_revision) = key
 
107
            if text_revision == NULL_REVISION:
 
108
                ret[key] = ()
 
109
                continue
 
110
            if not isinstance(file_id, bytes):
 
111
                raise TypeError(file_id)
 
112
            if not isinstance(text_revision, bytes):
 
113
                raise TypeError(text_revision)
 
114
            try:
 
115
                ret[key] = self._get_parents(file_id, text_revision)
 
116
            except KeyError:
 
117
                pass
 
118
        return ret