1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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/>.
17
"""Tracker of refs."""
19
from __future__ import absolute_import
22
class RefTracker(object):
25
# Head tracking: last ref, last id per ref & map of commit ids to ref*s*
30
def dump_stats(self, note):
31
self._show_stats_for(self.last_ids, "last-ids", note=note)
32
self._show_stats_for(self.heads, "heads", note=note)
38
def track_heads(self, cmd):
39
"""Track the repository heads given a CommitCommand.
41
:param cmd: the CommitCommand
42
:return: the list of parents in terms of commit-ids
44
# Get the true set of parents
45
if cmd.from_ is not None:
48
last_id = self.last_ids.get(cmd.ref)
49
if last_id is not None:
53
parents.extend(cmd.merges)
56
self.track_heads_for_ref(cmd.ref, cmd.id, parents)
59
def track_heads_for_ref(self, cmd_ref, cmd_id, parents=None):
60
if parents is not None:
61
for parent in parents:
62
if parent in self.heads:
63
del self.heads[parent]
64
self.heads.setdefault(cmd_id, set()).add(cmd_ref)
65
self.last_ids[cmd_ref] = cmd_id
66
self.last_ref = cmd_ref