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