Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 15 | import itertools |
| 16 | import multiprocessing |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 17 | import sys |
| 18 | from color import Coloring |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame^] | 19 | from command import Command, DEFAULT_LOCAL_JOBS |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 20 | |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 21 | # Number of projects to submit to a single worker process at a time. |
| 22 | # This number represents a tradeoff between the overhead of IPC and finer |
| 23 | # grained opportunity for parallelism. This particular value was chosen by |
| 24 | # iterating through powers of two until the overall performance no longer |
| 25 | # improved. The performance of this batch size is not a function of the |
| 26 | # number of cores on the system. |
| 27 | WORKER_BATCH_SIZE = 32 |
| 28 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 29 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 30 | class BranchColoring(Coloring): |
| 31 | def __init__(self, config): |
| 32 | Coloring.__init__(self, config, 'branch') |
| 33 | self.current = self.printer('current', fg='green') |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 34 | self.local = self.printer('local') |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 35 | self.notinproject = self.printer('notinproject', fg='red') |
| 36 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 37 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 38 | class BranchInfo(object): |
| 39 | def __init__(self, name): |
| 40 | self.name = name |
| 41 | self.current = 0 |
| 42 | self.published = 0 |
| 43 | self.published_equal = 0 |
| 44 | self.projects = [] |
| 45 | |
| 46 | def add(self, b): |
| 47 | if b.current: |
| 48 | self.current += 1 |
| 49 | if b.published: |
| 50 | self.published += 1 |
| 51 | if b.revision == b.published: |
| 52 | self.published_equal += 1 |
| 53 | self.projects.append(b) |
| 54 | |
| 55 | @property |
| 56 | def IsCurrent(self): |
| 57 | return self.current > 0 |
| 58 | |
| 59 | @property |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 60 | def IsSplitCurrent(self): |
| 61 | return self.current != 0 and self.current != len(self.projects) |
| 62 | |
| 63 | @property |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 64 | def IsPublished(self): |
| 65 | return self.published > 0 |
| 66 | |
| 67 | @property |
| 68 | def IsPublishedEqual(self): |
| 69 | return self.published_equal == len(self.projects) |
| 70 | |
| 71 | |
| 72 | class Branches(Command): |
| 73 | common = True |
| 74 | helpSummary = "View current topic branches" |
| 75 | helpUsage = """ |
| 76 | %prog [<project>...] |
| 77 | |
| 78 | Summarizes the currently available topic branches. |
Shawn O. Pearce | 7da73d6 | 2009-06-12 17:35:43 -0700 | [diff] [blame] | 79 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 80 | # Branch Display |
Shawn O. Pearce | 7da73d6 | 2009-06-12 17:35:43 -0700 | [diff] [blame] | 81 | |
| 82 | The branch display output by this command is organized into four |
| 83 | columns of information; for example: |
| 84 | |
| 85 | *P nocolor | in repo |
| 86 | repo2 | |
| 87 | |
| 88 | The first column contains a * if the branch is the currently |
| 89 | checked out branch in any of the specified projects, or a blank |
| 90 | if no project has the branch checked out. |
| 91 | |
| 92 | The second column contains either blank, p or P, depending upon |
| 93 | the upload status of the branch. |
| 94 | |
| 95 | (blank): branch not yet published by repo upload |
| 96 | P: all commits were published by repo upload |
| 97 | p: only some commits were published by repo upload |
| 98 | |
| 99 | The third column contains the branch name. |
| 100 | |
| 101 | The fourth column (after the | separator) lists the projects that |
| 102 | the branch appears in, or does not appear in. If no project list |
| 103 | is shown, then the branch appears in all projects. |
| 104 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 105 | """ |
Mike Frysinger | 6a2400a | 2021-02-16 01:43:31 -0500 | [diff] [blame^] | 106 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 107 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 108 | def Execute(self, opt, args): |
| 109 | projects = self.GetProjects(args) |
| 110 | out = BranchColoring(self.manifest.manifestProject.config) |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 111 | all_branches = {} |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 112 | project_cnt = len(projects) |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 113 | with multiprocessing.Pool(processes=opt.jobs) as pool: |
| 114 | project_branches = pool.imap_unordered( |
| 115 | expand_project_to_branches, projects, chunksize=WORKER_BATCH_SIZE) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 116 | |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 117 | for name, b in itertools.chain.from_iterable(project_branches): |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 118 | if name not in all_branches: |
| 119 | all_branches[name] = BranchInfo(name) |
| 120 | all_branches[name].add(b) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 121 | |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 122 | names = sorted(all_branches) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 123 | |
Shawn O. Pearce | 4e3d673 | 2009-04-18 15:18:35 -0700 | [diff] [blame] | 124 | if not names: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 125 | print(' (no branches)', file=sys.stderr) |
Shawn O. Pearce | 4e3d673 | 2009-04-18 15:18:35 -0700 | [diff] [blame] | 126 | return |
| 127 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 128 | width = 25 |
| 129 | for name in names: |
| 130 | if width < len(name): |
| 131 | width = len(name) |
| 132 | |
| 133 | for name in names: |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 134 | i = all_branches[name] |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 135 | in_cnt = len(i.projects) |
| 136 | |
| 137 | if i.IsCurrent: |
| 138 | current = '*' |
| 139 | hdr = out.current |
| 140 | else: |
| 141 | current = ' ' |
| 142 | hdr = out.local |
| 143 | |
| 144 | if i.IsPublishedEqual: |
| 145 | published = 'P' |
| 146 | elif i.IsPublished: |
| 147 | published = 'p' |
| 148 | else: |
| 149 | published = ' ' |
| 150 | |
| 151 | hdr('%c%c %-*s' % (current, published, width, name)) |
| 152 | out.write(' |') |
| 153 | |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 154 | if in_cnt < project_cnt: |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 155 | fmt = out.write |
| 156 | paths = [] |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 157 | non_cur_paths = [] |
| 158 | if i.IsSplitCurrent or (in_cnt < project_cnt - in_cnt): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 159 | in_type = 'in' |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 160 | for b in i.projects: |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 161 | if not i.IsSplitCurrent or b.current: |
| 162 | paths.append(b.project.relpath) |
| 163 | else: |
| 164 | non_cur_paths.append(b.project.relpath) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 165 | else: |
| 166 | fmt = out.notinproject |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 167 | in_type = 'not in' |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 168 | have = set() |
| 169 | for b in i.projects: |
| 170 | have.add(b.project) |
| 171 | for p in projects: |
David Pursehouse | eeff353 | 2020-02-12 11:24:10 +0900 | [diff] [blame] | 172 | if p not in have: |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 173 | paths.append(p.relpath) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 174 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 175 | s = ' %s %s' % (in_type, ', '.join(paths)) |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 176 | if not i.IsSplitCurrent and (width + 7 + len(s) < 80): |
| 177 | fmt = out.current if i.IsCurrent else fmt |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 178 | fmt(s) |
| 179 | else: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 180 | fmt(' %s:' % in_type) |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 181 | fmt = out.current if i.IsCurrent else out.write |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 182 | for p in paths: |
| 183 | out.nl() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 184 | fmt(width * ' ' + ' %s' % p) |
Etan Cohen | 588142d | 2014-07-09 21:33:31 -0700 | [diff] [blame] | 185 | fmt = out.write |
| 186 | for p in non_cur_paths: |
| 187 | out.nl() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 188 | fmt(width * ' ' + ' %s' % p) |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 189 | else: |
| 190 | out.write(' in all projects') |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 191 | out.nl() |
Chris McDonald | 8add623 | 2020-12-09 14:27:59 -0700 | [diff] [blame] | 192 | |
| 193 | |
| 194 | def expand_project_to_branches(project): |
| 195 | """Expands a project into a list of branch names & associated information. |
| 196 | |
| 197 | Args: |
| 198 | project: project.Project |
| 199 | |
| 200 | Returns: |
| 201 | List[Tuple[str, git_config.Branch]] |
| 202 | """ |
| 203 | branches = [] |
| 204 | for name, b in project.GetBranches().items(): |
| 205 | b.project = project |
| 206 | branches.append((name, b)) |
| 207 | return branches |