blob: 57d8c88aa73f8c36e7c5da10b6cee7af6df1c685 [file] [log] [blame]
Shawn O. Pearce27b07322009-04-10 16:02:48 -07001#
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
16import sys
17from color import Coloring
18from command import Command
19
20class BranchColoring(Coloring):
21 def __init__(self, config):
22 Coloring.__init__(self, config, 'branch')
23 self.current = self.printer('current', fg='green')
24 self.local = self.printer('local')
25 self.notinproject = self.printer('notinproject', fg='red')
26
27class BranchInfo(object):
28 def __init__(self, name):
29 self.name = name
30 self.current = 0
31 self.published = 0
32 self.published_equal = 0
33 self.projects = []
34
35 def add(self, b):
36 if b.current:
37 self.current += 1
38 if b.published:
39 self.published += 1
40 if b.revision == b.published:
41 self.published_equal += 1
42 self.projects.append(b)
43
44 @property
45 def IsCurrent(self):
46 return self.current > 0
47
48 @property
49 def IsPublished(self):
50 return self.published > 0
51
52 @property
53 def IsPublishedEqual(self):
54 return self.published_equal == len(self.projects)
55
56
57class Branches(Command):
58 common = True
59 helpSummary = "View current topic branches"
60 helpUsage = """
61%prog [<project>...]
62
63Summarizes the currently available topic branches.
64"""
65
66 def _Options(self, p):
67 p.add_option('-a', '--all',
68 dest='all', action='store_true',
69 help='show all branches, not just the majority')
70
71 def Execute(self, opt, args):
72 projects = self.GetProjects(args)
73 out = BranchColoring(self.manifest.manifestProject.config)
74 all = {}
75 project_cnt = len(projects)
76
77 for project in projects:
78 for name, b in project.GetBranches().iteritems():
79 b.project = project
80 if name not in all:
81 all[name] = BranchInfo(name)
82 all[name].add(b)
83
84 names = all.keys()
85 names.sort()
86
87 if not opt.all and not args:
88 # No -a and no specific projects listed; try to filter the
89 # results down to only the majority of projects.
90 #
91 n = []
92 for name in names:
93 i = all[name]
94 if i.IsCurrent \
95 or 80 <= (100 * len(i.projects)) / project_cnt:
96 n.append(name)
97 names = n
98
Shawn O. Pearce4e3d6732009-04-18 15:18:35 -070099 if not names:
100 print >>sys.stderr, ' (no branches)'
101 return
102
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700103 width = 25
104 for name in names:
105 if width < len(name):
106 width = len(name)
107
108 for name in names:
109 i = all[name]
110 in_cnt = len(i.projects)
111
112 if i.IsCurrent:
113 current = '*'
114 hdr = out.current
115 else:
116 current = ' '
117 hdr = out.local
118
119 if i.IsPublishedEqual:
120 published = 'P'
121 elif i.IsPublished:
122 published = 'p'
123 else:
124 published = ' '
125
126 hdr('%c%c %-*s' % (current, published, width, name))
127 out.write(' |')
128
129 if in_cnt < project_cnt and (in_cnt == 1 or opt.all):
130 fmt = out.write
131 paths = []
132 if in_cnt < project_cnt - in_cnt:
133 type = 'in'
134 for b in i.projects:
135 paths.append(b.project.relpath)
136 else:
137 fmt = out.notinproject
138 type = 'not in'
139 have = set()
140 for b in i.projects:
141 have.add(b.project)
142 for p in projects:
143 paths.append(p.relpath)
144
145 s = ' %s %s' % (type, ', '.join(paths))
146 if width + 7 + len(s) < 80:
147 fmt(s)
148 else:
149 out.nl()
150 fmt(' %s:' % type)
151 for p in paths:
152 out.nl()
153 fmt(' %s' % p)
154 out.nl()