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

Raise SettingFileIdUnsupported

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from dulwich.errors import (
 
22
    NotTreeError,
 
23
    )
 
24
from dulwich.object_store import (
 
25
    tree_lookup_path,
 
26
    )
 
27
 
 
28
from ...revision import (
 
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]
 
41
        target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
 
42
            commit.tree, path)
 
43
        while True:
 
44
            parent_commits = [self.store[c] for c in commit.parents]
 
45
            for parent_commit in parent_commits:
 
46
                try:
 
47
                    mode, sha = tree_lookup_path(self.store.__getitem__,
 
48
                        parent_commit.tree, path)
 
49
                except (NotTreeError, KeyError):
 
50
                    continue
 
51
                if mode != target_mode or sha != target_sha:
 
52
                    return (path, commit.id)
 
53
            if parent_commits == []:
 
54
                break
 
55
            commit = parent_commits[0]
 
56
        return (path, commit.id)
 
57
 
 
58
 
 
59
class GitFileParentProvider(object):
 
60
 
 
61
    def __init__(self, change_scanner):
 
62
        self.change_scanner = change_scanner
 
63
        self.store = self.change_scanner.repository._git.object_store
 
64
 
 
65
    def _get_parents(self, file_id, text_revision):
 
66
        commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
 
67
            text_revision)
 
68
        path = mapping.parse_file_id(file_id)
 
69
        text_parents = []
 
70
        for commit_parent in self.store[commit_id].parents:
 
71
            (_, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
 
72
            if text_parent not in text_parents:
 
73
                text_parents.append(text_parent)
 
74
        return tuple([(file_id,
 
75
            self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
 
76
            in text_parents])
 
77
 
 
78
    def get_parent_map(self, keys):
 
79
        ret = {}
 
80
        for key in keys:
 
81
            (file_id, text_revision) = key
 
82
            if text_revision == NULL_REVISION:
 
83
                continue
 
84
            try:
 
85
                ret[key] = self._get_parents(file_id, text_revision)
 
86
            except KeyError:
 
87
                pass
 
88
        return ret