git-remote-hg: improve sanitation of local repo urls
[git] / git_remote_helpers / fastimport / head_tracker.py
1
2
3 class HeadTracker(object):
4     """
5     Keep track of the heads in a fastimport stream.
6     """
7     def __init__(self):
8         self.last_ref = None
9
10         # map git ref name (e.g. "refs/heads/master") to id of last
11         # commit with that ref
12         self.last_ids = {}
13
14         # the set of heads seen so far in the stream, as a mapping
15         # from commit id of the head to set of ref names
16         self.heads = {}
17
18     def track_heads(self, cmd):
19         """Track the repository heads given a CommitCommand.
20         
21         :param cmd: the CommitCommand
22         :return: the list of parents in terms of commit-ids
23         """
24         # Get the true set of parents
25         if cmd.from_ is not None:
26             parents = [cmd.from_]
27         else:
28             last_id = self.last_ids.get(cmd.ref)
29             if last_id is not None:
30                 parents = [last_id]
31             else:
32                 parents = []
33         parents.extend(cmd.merges)
34
35         # Track the heads
36         self.track_heads_for_ref(cmd.ref, cmd.id, parents)
37         return parents
38
39     def track_heads_for_ref(self, cmd_ref, cmd_id, parents=None):
40         if parents is not None:
41             for parent in parents:
42                 if parent in self.heads:
43                     del self.heads[parent]
44         self.heads.setdefault(cmd_id, set()).add(cmd_ref)
45         self.last_ids[cmd_ref] = cmd_id
46         self.last_ref = cmd_ref
47