/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.64.349 by Jelmer Vernooij
Reimport some modules removed from python-fastimport 0.9.2.
1
# Copyright (C) 2009 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, see <http://www.gnu.org/licenses/>.
15
16
17
"""Tracker of refs."""
18
19
20
class RefTracker(object):
21
22
    def __init__(self):
23
        # Head tracking: last ref, last id per ref & map of commit ids to ref*s*
24
        self.last_ref = None
25
        self.last_ids = {}
26
        self.heads = {}
27
28
    def dump_stats(self, note):
29
        self._show_stats_for(self.last_ids, "last-ids", note=note)
30
        self._show_stats_for(self.heads, "heads", note=note)
31
32
    def clear(self):
33
        self.last_ids.clear()
34
        self.heads.clear()
35
36
    def track_heads(self, cmd):
37
        """Track the repository heads given a CommitCommand.
38
39
        :param cmd: the CommitCommand
40
        :return: the list of parents in terms of commit-ids
41
        """
42
        # Get the true set of parents
43
        if cmd.from_ is not None:
44
            parents = [cmd.from_]
45
        else:
46
            last_id = self.last_ids.get(cmd.ref)
47
            if last_id is not None:
48
                parents = [last_id]
49
            else:
50
                parents = []
51
        parents.extend(cmd.merges)
52
53
        # Track the heads
54
        self.track_heads_for_ref(cmd.ref, cmd.id, parents)
55
        return parents
56
57
    def track_heads_for_ref(self, cmd_ref, cmd_id, parents=None):
58
        if parents is not None:
59
            for parent in parents:
60
                if parent in self.heads:
61
                    del self.heads[parent]
62
        self.heads.setdefault(cmd_id, set()).add(cmd_ref)
63
        self.last_ids[cmd_ref] = cmd_id
64
        self.last_ref = cmd_ref
65
66