/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/annotate.py

  • Committer: Jelmer Vernooij
  • Date: 2020-01-26 03:56:20 UTC
  • mto: (7463.1.3 cf-size)
  • mto: This revision was merged to the branch mainline in revision 7464.
  • Revision ID: jelmer@jelmer.uk-20200126035620-fax6hvuhu500mvlz
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Annotate."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.object_store import (
 
22
    tree_lookup_path,
 
23
    )
 
24
 
 
25
from .. import osutils
 
26
from ..errors import (
 
27
    NoSuchRevision,
 
28
    UnavailableRepresentation,
 
29
    )
 
30
from ..graph import Graph
 
31
from ..revision import (
 
32
    NULL_REVISION,
 
33
    )
 
34
 
 
35
 
 
36
class GitBlobContentFactory(object):
 
37
    """Static data content factory.
 
38
 
 
39
    This takes a fulltext when created and just returns that during
 
40
    get_bytes_as('fulltext').
 
41
 
 
42
    :ivar sha1: None, or the sha1 of the content fulltext.
 
43
    :ivar storage_kind: The native storage kind of this factory. Always
 
44
        'fulltext'.
 
45
    :ivar key: The key of this content. Each key is a tuple with a single
 
46
        string in it.
 
47
    :ivar parents: A tuple of parent keys for self.key. If the object has
 
48
        no parent information, None (as opposed to () for an empty list of
 
49
        parents).
 
50
     """
 
51
 
 
52
    def __init__(self, store, path, revision, blob_id):
 
53
        """Create a ContentFactory."""
 
54
        self.store = store
 
55
        self.key = (path, revision)
 
56
        self.storage_kind = 'git-blob'
 
57
        self.parents = None
 
58
        self.blob_id = blob_id
 
59
 
 
60
    def get_bytes_as(self, storage_kind):
 
61
        if storage_kind == 'fulltext':
 
62
            return self.store[self.blob_id].as_raw_string()
 
63
        elif storage_kind == 'lines':
 
64
            return osutils.chunks_to_lines(self.store[self.blob_id].as_raw_chunks())
 
65
        elif storage_kind == 'chunked':
 
66
            return self.store[self.blob_id].as_raw_chunks()
 
67
        raise UnavailableRepresentation(self.key, storage_kind,
 
68
                                        self.storage_kind)
 
69
 
 
70
 
 
71
class GitAbsentContentFactory(object):
 
72
    """Absent data content factory.
 
73
 
 
74
    :ivar sha1: None, or the sha1 of the content fulltext.
 
75
    :ivar storage_kind: The native storage kind of this factory. Always
 
76
        'fulltext'.
 
77
    :ivar key: The key of this content. Each key is a tuple with a single
 
78
        string in it.
 
79
    :ivar parents: A tuple of parent keys for self.key. If the object has
 
80
        no parent information, None (as opposed to () for an empty list of
 
81
        parents).
 
82
     """
 
83
 
 
84
    def __init__(self, store, path, revision):
 
85
        """Create a ContentFactory."""
 
86
        self.store = store
 
87
        self.key = (path, revision)
 
88
        self.storage_kind = 'absent'
 
89
        self.parents = None
 
90
 
 
91
    def get_bytes_as(self, storage_kind):
 
92
        raise ValueError
 
93
 
 
94
 
 
95
class AnnotateProvider(object):
 
96
 
 
97
    def __init__(self, change_scanner):
 
98
        self.change_scanner = change_scanner
 
99
        self.store = self.change_scanner.repository._git.object_store
 
100
 
 
101
    def _get_parents(self, path, text_revision):
 
102
        commit_id, mapping = (
 
103
            self.change_scanner.repository.lookup_bzr_revision_id(
 
104
                text_revision))
 
105
        text_parents = []
 
106
        path = path.encode('utf-8')
 
107
        for commit_parent in self.store[commit_id].parents:
 
108
            try:
 
109
                (path, text_parent) = (
 
110
                    self.change_scanner.find_last_change_revision(
 
111
                        path, commit_parent))
 
112
            except KeyError:
 
113
                continue
 
114
            if text_parent not in text_parents:
 
115
                text_parents.append(text_parent)
 
116
        return tuple([
 
117
            (path.decode('utf-8'),
 
118
                self.change_scanner.repository.lookup_foreign_revision_id(p))
 
119
            for p in text_parents])
 
120
 
 
121
    def get_parent_map(self, keys):
 
122
        ret = {}
 
123
        for key in keys:
 
124
            (path, text_revision) = key
 
125
            if text_revision == NULL_REVISION:
 
126
                ret[key] = ()
 
127
                continue
 
128
            try:
 
129
                ret[key] = self._get_parents(path, text_revision)
 
130
            except KeyError:
 
131
                pass
 
132
        return ret
 
133
 
 
134
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
135
        if ordering == 'topological':
 
136
            graph = Graph(self)
 
137
            keys = graph.iter_topo_order(keys)
 
138
        store = self.change_scanner.repository._git.object_store
 
139
        for (path, text_revision) in keys:
 
140
            try:
 
141
                commit_id, mapping = (
 
142
                    self.change_scanner.repository.lookup_bzr_revision_id(
 
143
                        text_revision))
 
144
            except NoSuchRevision:
 
145
                yield GitAbsentContentFactory(store, path, text_revision)
 
146
                continue
 
147
 
 
148
            try:
 
149
                tree_id = store[commit_id].tree
 
150
            except KeyError:
 
151
                yield GitAbsentContentFactory(store, path, text_revision)
 
152
                continue
 
153
            try:
 
154
                (mode, blob_sha) = tree_lookup_path(
 
155
                    store.__getitem__, tree_id, path.encode('utf-8'))
 
156
            except KeyError:
 
157
                yield GitAbsentContentFactory(store, path, text_revision)
 
158
            else:
 
159
                yield GitBlobContentFactory(
 
160
                    store, path, text_revision, blob_sha)