/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-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

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