/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

  • Committer: Jelmer Vernooij
  • Date: 2018-04-02 14:59:43 UTC
  • mto: (0.200.1913 work)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180402145943-s5jmpbvvf1x42pao
Just don't touch the URL if it's already a valid URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
# Copyright (C) 2012-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""File graph access."""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
import stat
 
23
 
 
24
from dulwich.errors import (
 
25
    NotTreeError,
 
26
    )
 
27
from dulwich.object_store import (
 
28
    tree_lookup_path,
 
29
    )
 
30
 
 
31
from ...revision import (
 
32
    NULL_REVISION,
 
33
    )
 
34
 
 
35
 
 
36
class GitFileLastChangeScanner(object):
 
37
 
 
38
    def __init__(self, repository):
 
39
        self.repository = repository
 
40
        self.store = self.repository._git.object_store
 
41
 
 
42
    def find_last_change_revision(self, path, commit_id):
 
43
        commit = self.store[commit_id]
 
44
        target_mode, target_sha = tree_lookup_path(self.store.__getitem__,
 
45
            commit.tree, path)
 
46
        if path == '':
 
47
            target_mode = stat.S_IFDIR
 
48
        if target_mode is None:
 
49
            raise AssertionError("sha %r for %r in %r" % (target_sha, path, commit_id))
 
50
        while True:
 
51
            parent_commits = []
 
52
            for parent_commit in [self.store[c] for c in commit.parents]:
 
53
                try:
 
54
                    mode, sha = tree_lookup_path(self.store.__getitem__,
 
55
                        parent_commit.tree, path)
 
56
                except (NotTreeError, KeyError):
 
57
                    continue
 
58
                else:
 
59
                    parent_commits.append(parent_commit)
 
60
                if path == '':
 
61
                    mode = stat.S_IFDIR
 
62
                # Candidate found iff, mode or text changed,
 
63
                # or is a directory that didn't previously exist.
 
64
                if mode != target_mode or (
 
65
                    not stat.S_ISDIR(target_mode) and sha != target_sha):
 
66
                        return (path, commit.id)
 
67
            if parent_commits == []:
 
68
                break
 
69
            commit = parent_commits[0]
 
70
        return (path, commit.id)
 
71
 
 
72
 
 
73
class GitFileParentProvider(object):
 
74
 
 
75
    def __init__(self, change_scanner):
 
76
        self.change_scanner = change_scanner
 
77
        self.store = self.change_scanner.repository._git.object_store
 
78
 
 
79
    def _get_parents(self, file_id, text_revision):
 
80
        commit_id, mapping = self.change_scanner.repository.lookup_bzr_revision_id(
 
81
            text_revision)
 
82
        try:
 
83
            path = mapping.parse_file_id(file_id)
 
84
        except ValueError:
 
85
            raise KeyError(file_id)
 
86
        text_parents = []
 
87
        for commit_parent in self.store[commit_id].parents:
 
88
            try:
 
89
                (path, text_parent) = self.change_scanner.find_last_change_revision(path, commit_parent)
 
90
            except KeyError:
 
91
                continue
 
92
            if text_parent not in text_parents:
 
93
                text_parents.append(text_parent)
 
94
        return tuple([(file_id,
 
95
            self.change_scanner.repository.lookup_foreign_revision_id(p)) for p
 
96
            in text_parents])
 
97
 
 
98
    def get_parent_map(self, keys):
 
99
        ret = {}
 
100
        for key in keys:
 
101
            (file_id, text_revision) = key
 
102
            if text_revision == NULL_REVISION:
 
103
                ret[key] = ()
 
104
                continue
 
105
            try:
 
106
                ret[key] = self._get_parents(file_id, text_revision)
 
107
            except KeyError:
 
108
                pass
 
109
        return ret