Fix bzr-fastimport when used with newer versions of python-fastimport.(Jelmer Vernooij)
[bzr-fastimport.git] / reftracker.py
blob44136c7faf764ee2748cbcdc0e968e38d59fa770
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):
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 = {}
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)
32 def clear(self):
33 self.last_ids.clear()
34 self.heads.clear()
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
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)
53 # Track the heads
54 self.track_heads_for_ref(cmd.ref, cmd.id, parents)
55 return 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