blob: 87c4f9b6548fbb3641a9a32d26e2cfb384827860 [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
Shawn O. Pearce27b07322009-04-10 16:02:48 -070066 def Execute(self, opt, args):
67 projects = self.GetProjects(args)
68 out = BranchColoring(self.manifest.manifestProject.config)
69 all = {}
70 project_cnt = len(projects)
71
72 for project in projects:
73 for name, b in project.GetBranches().iteritems():
74 b.project = project
75 if name not in all:
76 all[name] = BranchInfo(name)
77 all[name].add(b)
78
79 names = all.keys()
80 names.sort()
81
Shawn O. Pearce4e3d6732009-04-18 15:18:35 -070082 if not names:
83 print >>sys.stderr, ' (no branches)'
84 return
85
Shawn O. Pearce27b07322009-04-10 16:02:48 -070086 width = 25
87 for name in names:
88 if width < len(name):
89 width = len(name)
90
91 for name in names:
92 i = all[name]
93 in_cnt = len(i.projects)
94
95 if i.IsCurrent:
96 current = '*'
97 hdr = out.current
98 else:
99 current = ' '
100 hdr = out.local
101
102 if i.IsPublishedEqual:
103 published = 'P'
104 elif i.IsPublished:
105 published = 'p'
106 else:
107 published = ' '
108
109 hdr('%c%c %-*s' % (current, published, width, name))
110 out.write(' |')
111
Shawn O. Pearce498a0e82009-05-18 12:28:54 -0700112 if in_cnt < project_cnt and (in_cnt == 1):
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700113 fmt = out.write
114 paths = []
115 if in_cnt < project_cnt - in_cnt:
116 type = 'in'
117 for b in i.projects:
118 paths.append(b.project.relpath)
119 else:
120 fmt = out.notinproject
121 type = 'not in'
122 have = set()
123 for b in i.projects:
124 have.add(b.project)
125 for p in projects:
126 paths.append(p.relpath)
127
128 s = ' %s %s' % (type, ', '.join(paths))
129 if width + 7 + len(s) < 80:
130 fmt(s)
131 else:
132 out.nl()
133 fmt(' %s:' % type)
134 for p in paths:
135 out.nl()
136 fmt(' %s' % p)
137 out.nl()