|
86
|
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 from __future__ import absolute_import |
|
|
20 |
|
|
21 |
|
|
22 class RefTracker(object): |
|
|
23 |
|
|
24 def __init__(self): |
|
|
25 # Head tracking: last ref, last id per ref & map of commit ids to ref*s* |
|
|
26 self.last_ref = None |
|
|
27 self.last_ids = {} |
|
|
28 self.heads = {} |
|
|
29 |
|
|
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) |
|
|
33 |
|
|
34 def clear(self): |
|
|
35 self.last_ids.clear() |
|
|
36 self.heads.clear() |
|
|
37 |
|
|
38 def track_heads(self, cmd): |
|
|
39 """Track the repository heads given a CommitCommand. |
|
|
40 |
|
|
41 :param cmd: the CommitCommand |
|
|
42 :return: the list of parents in terms of commit-ids |
|
|
43 """ |
|
|
44 # Get the true set of parents |
|
|
45 if cmd.from_ is not None: |
|
|
46 parents = [cmd.from_] |
|
|
47 else: |
|
|
48 last_id = self.last_ids.get(cmd.ref) |
|
|
49 if last_id is not None: |
|
|
50 parents = [last_id] |
|
|
51 else: |
|
|
52 parents = [] |
|
|
53 parents.extend(cmd.merges) |
|
|
54 |
|
|
55 # Track the heads |
|
|
56 self.track_heads_for_ref(cmd.ref, cmd.id, parents) |
|
|
57 return parents |
|
|
58 |
|
|
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 |
|
|
67 |
|
|
68 |