/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 11:31:40 UTC
  • mfrom: (7143.12.3 annotated-tags)
  • Revision ID: breezy.the.bot@gmail.com-20181116113140-618u04763u0dyxnh
Fix fetching of revisions that are referenced by annotated tags.

Merged from https://code.launchpad.net/~jelmer/brz/annotated-tags/+merge/358536

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