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."""
20 class RefTracker(object):
23 # Head tracking: last ref, last id per ref & map of commit ids to ref*s*
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
)
36 def track_heads(self
, cmd
):
37 """Track the repository heads given a CommitCommand.
39 :param cmd: the CommitCommand
40 :return: the list of parents in terms of commit-ids
42 # Get the true set of parents
43 if cmd
.from_
is not None:
46 last_id
= self
.last_ids
.get(cmd
.ref
)
47 if last_id
is not None:
51 parents
.extend(cmd
.merges
)
54 self
.track_heads_for_ref(cmd
.ref
, cmd
.id, parents
)
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