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 |
| 50 | def IsPublished(self): |
| 51 | return self.published > 0 |
| 52 | |
| 53 | @property |
| 54 | def IsPublishedEqual(self): |
| 55 | return self.published_equal == len(self.projects) |
| 56 | |
| 57 | |
| 58 | class Branches(Command): |
| 59 | common = True |
| 60 | helpSummary = "View current topic branches" |
| 61 | helpUsage = """ |
| 62 | %prog [<project>...] |
| 63 | |
| 64 | Summarizes the currently available topic branches. |
Shawn O. Pearce | 7da73d6 | 2009-06-12 17:35:43 -0700 | [diff] [blame] | 65 | |
| 66 | Branch Display |
| 67 | -------------- |
| 68 | |
| 69 | The branch display output by this command is organized into four |
| 70 | columns of information; for example: |
| 71 | |
| 72 | *P nocolor | in repo |
| 73 | repo2 | |
| 74 | |
| 75 | The first column contains a * if the branch is the currently |
| 76 | checked out branch in any of the specified projects, or a blank |
| 77 | if no project has the branch checked out. |
| 78 | |
| 79 | The second column contains either blank, p or P, depending upon |
| 80 | the upload status of the branch. |
| 81 | |
| 82 | (blank): branch not yet published by repo upload |
| 83 | P: all commits were published by repo upload |
| 84 | p: only some commits were published by repo upload |
| 85 | |
| 86 | The third column contains the branch name. |
| 87 | |
| 88 | The fourth column (after the | separator) lists the projects that |
| 89 | the branch appears in, or does not appear in. If no project list |
| 90 | is shown, then the branch appears in all projects. |
| 91 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 92 | """ |
| 93 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 94 | def Execute(self, opt, args): |
| 95 | projects = self.GetProjects(args) |
| 96 | out = BranchColoring(self.manifest.manifestProject.config) |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 97 | all_branches = {} |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 98 | project_cnt = len(projects) |
| 99 | |
| 100 | for project in projects: |
| 101 | for name, b in project.GetBranches().iteritems(): |
| 102 | b.project = project |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 103 | if name not in all_branches: |
| 104 | all_branches[name] = BranchInfo(name) |
| 105 | all_branches[name].add(b) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 106 | |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 107 | names = all_branches.keys() |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 108 | names.sort() |
| 109 | |
Shawn O. Pearce | 4e3d673 | 2009-04-18 15:18:35 -0700 | [diff] [blame] | 110 | if not names: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 111 | print(' (no branches)', file=sys.stderr) |
Shawn O. Pearce | 4e3d673 | 2009-04-18 15:18:35 -0700 | [diff] [blame] | 112 | return |
| 113 | |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 114 | width = 25 |
| 115 | for name in names: |
| 116 | if width < len(name): |
| 117 | width = len(name) |
| 118 | |
| 119 | for name in names: |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 120 | i = all_branches[name] |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 121 | in_cnt = len(i.projects) |
| 122 | |
| 123 | if i.IsCurrent: |
| 124 | current = '*' |
| 125 | hdr = out.current |
| 126 | else: |
| 127 | current = ' ' |
| 128 | hdr = out.local |
| 129 | |
| 130 | if i.IsPublishedEqual: |
| 131 | published = 'P' |
| 132 | elif i.IsPublished: |
| 133 | published = 'p' |
| 134 | else: |
| 135 | published = ' ' |
| 136 | |
| 137 | hdr('%c%c %-*s' % (current, published, width, name)) |
| 138 | out.write(' |') |
| 139 | |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 140 | if in_cnt < project_cnt: |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 141 | fmt = out.write |
| 142 | paths = [] |
| 143 | if in_cnt < project_cnt - in_cnt: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 144 | in_type = 'in' |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 145 | for b in i.projects: |
| 146 | paths.append(b.project.relpath) |
| 147 | else: |
| 148 | fmt = out.notinproject |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 149 | in_type = 'not in' |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 150 | have = set() |
| 151 | for b in i.projects: |
| 152 | have.add(b.project) |
| 153 | for p in projects: |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 154 | if not p in have: |
| 155 | paths.append(p.relpath) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 156 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 157 | s = ' %s %s' % (in_type, ', '.join(paths)) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 158 | if width + 7 + len(s) < 80: |
| 159 | fmt(s) |
| 160 | else: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 161 | fmt(' %s:' % in_type) |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 162 | for p in paths: |
| 163 | out.nl() |
Pär Åsfält | ff6929d | 2009-09-05 23:10:56 +0200 | [diff] [blame] | 164 | fmt(width*' ' + ' %s' % p) |
| 165 | else: |
| 166 | out.write(' in all projects') |
Shawn O. Pearce | 27b0732 | 2009-04-10 16:02:48 -0700 | [diff] [blame] | 167 | out.nl() |