OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Provides a short mapping of all the branches in your local repo, organized | 6 """Provides a short mapping of all the branches in your local repo, organized |
7 by their upstream ('tracking branch') layout. | 7 by their upstream ('tracking branch') layout. |
8 | 8 |
9 Example: | 9 Example: |
10 origin/master | 10 origin/master |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 """A class which constructs output representing the tree's branch structure. | 103 """A class which constructs output representing the tree's branch structure. |
104 | 104 |
105 Attributes: | 105 Attributes: |
106 __tracking_info: a map of branches to their TrackingInfo objects which | 106 __tracking_info: a map of branches to their TrackingInfo objects which |
107 consist of the branch hash, upstream and ahead/behind status. | 107 consist of the branch hash, upstream and ahead/behind status. |
108 __gone_branches: a set of upstreams which are not fetchable by git""" | 108 __gone_branches: a set of upstreams which are not fetchable by git""" |
109 | 109 |
110 def __init__(self): | 110 def __init__(self): |
111 self.verbosity = 0 | 111 self.verbosity = 0 |
112 self.output = OutputManager() | 112 self.output = OutputManager() |
113 self.__tracking_info = get_all_tracking_info() | |
114 self.__gone_branches = set() | 113 self.__gone_branches = set() |
Matt Giuca
2014/09/03 03:21:55
Make sure all the fields are initialized in the co
calamity
2014/09/03 04:38:53
Done.
| |
115 self.__roots = set() | 114 |
115 def start(self): | |
116 self.__tracking_info = get_all_tracking_info( | |
117 include_tracking_status=self.verbosity >= 1) | |
118 roots = set() | |
116 | 119 |
117 # A map of parents to a list of their children. | 120 # A map of parents to a list of their children. |
118 self.parent_map = collections.defaultdict(list) | 121 self.parent_map = collections.defaultdict(list) |
119 for branch, branch_info in self.__tracking_info.iteritems(): | 122 for branch, branch_info in self.__tracking_info.iteritems(): |
120 if not branch_info: | 123 if not branch_info: |
121 continue | 124 continue |
122 | 125 |
123 parent = branch_info.upstream | 126 parent = branch_info.upstream |
124 if parent and not self.__tracking_info[parent]: | 127 if parent and not self.__tracking_info[parent]: |
125 branch_upstream = upstream(branch) | 128 branch_upstream = upstream(branch) |
126 # If git can't find the upstream, mark the upstream as gone. | 129 # If git can't find the upstream, mark the upstream as gone. |
127 if branch_upstream: | 130 if branch_upstream: |
128 parent = branch_upstream | 131 parent = branch_upstream |
129 else: | 132 else: |
130 self.__gone_branches.add(parent) | 133 self.__gone_branches.add(parent) |
131 # A parent that isn't in the tracking info is a root. | 134 # A parent that isn't in the tracking info is a root. |
132 self.__roots.add(parent) | 135 roots.add(parent) |
133 | 136 |
134 self.parent_map[parent].append(branch) | 137 self.parent_map[parent].append(branch) |
135 | 138 |
136 self.__current_branch = current_branch() | 139 self.__current_branch = current_branch() |
137 self.__current_hash = self.__tracking_info[self.__current_branch].hash | 140 self.__current_hash = self.__tracking_info[self.__current_branch].hash |
138 self.__tag_set = tags() | 141 self.__tag_set = tags() |
139 | 142 |
140 def start(self): | 143 for root in sorted(roots): |
141 for root in sorted(self.__roots): | |
142 self.__append_branch(root) | 144 self.__append_branch(root) |
143 | 145 |
144 def __is_invalid_parent(self, parent): | 146 def __is_invalid_parent(self, parent): |
145 return not parent or parent in self.__gone_branches | 147 return not parent or parent in self.__gone_branches |
146 | 148 |
147 def __color_for_branch(self, branch, branch_hash): | 149 def __color_for_branch(self, branch, branch_hash): |
148 if branch.startswith('origin'): | 150 if branch.startswith('origin'): |
149 color = Fore.RED | 151 color = Fore.RED |
150 elif self.__is_invalid_parent(branch) or branch in self.__tag_set: | 152 elif self.__is_invalid_parent(branch) or branch in self.__tag_set: |
151 color = Fore.MAGENTA | 153 color = Fore.MAGENTA |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 opts = parser.parse_args(argv[1:]) | 250 opts = parser.parse_args(argv[1:]) |
249 | 251 |
250 mapper = BranchMapper() | 252 mapper = BranchMapper() |
251 mapper.verbosity = opts.v | 253 mapper.verbosity = opts.v |
252 mapper.output.nocolor = opts.nocolor | 254 mapper.output.nocolor = opts.nocolor |
253 mapper.start() | 255 mapper.start() |
254 print mapper.output.as_formatted_string() | 256 print mapper.output.as_formatted_string() |
255 | 257 |
256 if __name__ == '__main__': | 258 if __name__ == '__main__': |
257 sys.exit(main(sys.argv)) | 259 sys.exit(main(sys.argv)) |
OLD | NEW |