/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.338.1 by Jelmer Vernooij
Directories don't change when their contents do.
21
import stat
22
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
23
from dulwich.errors import (
24
    NotTreeError,
25
    )
26
from dulwich.object_store import (
27
    tree_lookup_path,
28
    )
29
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
30
from ...revision import (
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
31
    NULL_REVISION,
32
    )
33
34
35
class GitFileLastChangeScanner(object):
36
37
    def __init__(self, repository):
38
        self.repository = repository
39
        self.store = self.repository._git.object_store
40
41
    def find_last_change_revision(self, path, commit_id):
42
        commit = self.store[commit_id]
0.200.1286 by Jelmer Vernooij
Don't set vf-specific properties.
43
        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().
44
            commit.tree, path)
0.338.1 by Jelmer Vernooij
Directories don't change when their contents do.
45
        if path == '':
46
            target_mode = stat.S_IFDIR | 0644
47
        assert target_mode is not None, "sha %r for %r in %r" % (target_sha, path, commit_id)
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
48
        while True:
0.200.1757 by Jelmer Vernooij
Fix root tests.
49
            parent_commits = []
50
            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().
51
                try:
52
                    mode, sha = tree_lookup_path(self.store.__getitem__,
53
                        parent_commit.tree, path)
54
                except (NotTreeError, KeyError):
55
                    continue
0.200.1757 by Jelmer Vernooij
Fix root tests.
56
                else:
57
                    parent_commits.append(parent_commit)
0.338.1 by Jelmer Vernooij
Directories don't change when their contents do.
58
                if path == '':
59
                    mode = stat.S_IFDIR | 0644
60
                # Candidate found iff, mode or text changed,
61
                # or is a directory that didn't previously exist.
62
                if mode != target_mode or (
63
                    not stat.S_ISDIR(target_mode) and sha != target_sha):
64
                        return (path, commit.id)
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
65
            if parent_commits == []:
66
                break
67
            commit = parent_commits[0]
0.200.1757 by Jelmer Vernooij
Fix root tests.
68
        return (path, commit.id)
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
69
70
71
class GitFileParentProvider(object):
72
73
    def __init__(self, change_scanner):
74
        self.change_scanner = change_scanner
75
        self.store = self.change_scanner.repository._git.object_store
76
77
    def _get_parents(self, file_id, text_revision):
78
        commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
79
            text_revision)
80
        path = mapping.parse_file_id(file_id)
81
        text_parents = []
82
        for commit_parent in self.store[commit_id].parents:
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
83
            try:
84
                (path, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
85
            except KeyError:
86
                continue
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
87
            if text_parent not in text_parents:
88
                text_parents.append(text_parent)
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
89
        return tuple([(file_id,
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
90
            self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
91
            in text_parents])
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
92
93
    def get_parent_map(self, keys):
94
        ret = {}
95
        for key in keys:
96
            (file_id, text_revision) = key
97
            if text_revision == NULL_REVISION:
0.200.1756 by Jelmer Vernooij
Initial work on annotate support.
98
                ret[key] = ()
0.200.1283 by Jelmer Vernooij
Provide Repository.get_file_graph() and Tree.get_file_revision().
99
                continue
100
            try:
101
                ret[key] = self._get_parents(file_id, text_revision)
102
            except KeyError:
103
                pass
104
        return ret