/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
1
# Copyright (C) 2011 Canonical Ltd
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""File graph access."""
18
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
19
from __future__ import absolute_import
20
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
21
from dulwich.errors import (
22
    NotTreeError,
23
    )
24
from dulwich.object_store import (
25
    tree_lookup_path,
26
    )
27
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
28
from ...revision import (
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
29
    NULL_REVISION,
30
    )
31
32
33
class GitFileLastChangeScanner(object):
34
35
    def __init__(self, repository):
36
        self.repository = repository
37
        self.store = self.repository._git.object_store
38
39
    def find_last_change_revision(self, path, commit_id):
40
        commit = self.store[commit_id]
0.200.1286 by Jelmer Vernooij
Don't set vf-specific properties.
41
        target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
42
            commit.tree, path)
43
        while True:
0.200.1757 by Jelmer Vernooij
Fix root tests.
44
            parent_commits = []
45
            for parent_commit in [self.store[c] for c in commit.parents]:
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
46
                try:
47
                    mode, sha = tree_lookup_path(self.store.__getitem__,
48
                        parent_commit.tree, path)
49
                except (NotTreeError, KeyError):
50
                    continue
0.200.1757 by Jelmer Vernooij
Fix root tests.
51
                else:
52
                    parent_commits.append(parent_commit)
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
53
                if mode != target_mode or sha != target_sha:
54
                    return (path, commit.id)
55
            if parent_commits == []:
56
                break
57
            commit = parent_commits[0]
0.200.1757 by Jelmer Vernooij
Fix root tests.
58
        return (path, commit.id)
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
59
60
61
class GitFileParentProvider(object):
62
63
    def __init__(self, change_scanner):
64
        self.change_scanner = change_scanner
65
        self.store = self.change_scanner.repository._git.object_store
66
67
    def _get_parents(self, file_id, text_revision):
68
        commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
69
            text_revision)
70
        path = mapping.parse_file_id(file_id)
71
        text_parents = []
72
        for commit_parent in self.store[commit_id].parents:
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
73
            try:
74
                (path, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
75
            except KeyError:
76
                continue
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
77
            if text_parent not in text_parents:
78
                text_parents.append(text_parent)
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
79
        return tuple([(file_id,
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
80
            self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
81
            in text_parents])
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
82
83
    def get_parent_map(self, keys):
84
        ret = {}
85
        for key in keys:
86
            (file_id, text_revision) = key
87
            if text_revision == NULL_REVISION:
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
88
                ret[key] = ()
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
89
                continue
90
            try:
91
                ret[key] = self._get_parents(file_id, text_revision)
92
            except KeyError:
93
                pass
94
        return ret