blob: b895884842dd597cf8164f6ede696e1aede11097 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Shawn O. Pearce27b07322009-04-10 16:02:48 -07002#
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Sarah Owenscecd1d82012-11-01 22:59:27 -070017from __future__ import print_function
Shawn O. Pearce27b07322009-04-10 16:02:48 -070018import sys
19from color import Coloring
20from command import Command
21
22class BranchColoring(Coloring):
23 def __init__(self, config):
24 Coloring.__init__(self, config, 'branch')
25 self.current = self.printer('current', fg='green')
26 self.local = self.printer('local')
27 self.notinproject = self.printer('notinproject', fg='red')
28
29class BranchInfo(object):
30 def __init__(self, name):
31 self.name = name
32 self.current = 0
33 self.published = 0
34 self.published_equal = 0
35 self.projects = []
36
37 def add(self, b):
38 if b.current:
39 self.current += 1
40 if b.published:
41 self.published += 1
42 if b.revision == b.published:
43 self.published_equal += 1
44 self.projects.append(b)
45
46 @property
47 def IsCurrent(self):
48 return self.current > 0
49
50 @property
Etan Cohen588142d2014-07-09 21:33:31 -070051 def IsSplitCurrent(self):
52 return self.current != 0 and self.current != len(self.projects)
53
54 @property
Shawn O. Pearce27b07322009-04-10 16:02:48 -070055 def IsPublished(self):
56 return self.published > 0
57
58 @property
59 def IsPublishedEqual(self):
60 return self.published_equal == len(self.projects)
61
62
63class Branches(Command):
64 common = True
65 helpSummary = "View current topic branches"
66 helpUsage = """
67%prog [<project>...]
68
69Summarizes the currently available topic branches.
Shawn O. Pearce7da73d62009-06-12 17:35:43 -070070
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040071# Branch Display
Shawn O. Pearce7da73d62009-06-12 17:35:43 -070072
73The branch display output by this command is organized into four
74columns of information; for example:
75
76 *P nocolor | in repo
77 repo2 |
78
79The first column contains a * if the branch is the currently
80checked out branch in any of the specified projects, or a blank
81if no project has the branch checked out.
82
83The second column contains either blank, p or P, depending upon
84the upload status of the branch.
85
86 (blank): branch not yet published by repo upload
87 P: all commits were published by repo upload
88 p: only some commits were published by repo upload
89
90The third column contains the branch name.
91
92The fourth column (after the | separator) lists the projects that
93the branch appears in, or does not appear in. If no project list
94is shown, then the branch appears in all projects.
95
Shawn O. Pearce27b07322009-04-10 16:02:48 -070096"""
97
Shawn O. Pearce27b07322009-04-10 16:02:48 -070098 def Execute(self, opt, args):
99 projects = self.GetProjects(args)
100 out = BranchColoring(self.manifest.manifestProject.config)
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900101 all_branches = {}
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700102 project_cnt = len(projects)
103
104 for project in projects:
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530105 for name, b in project.GetBranches().items():
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700106 b.project = project
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900107 if name not in all_branches:
108 all_branches[name] = BranchInfo(name)
109 all_branches[name].add(b)
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700110
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530111 names = list(sorted(all_branches))
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700112
Shawn O. Pearce4e3d6732009-04-18 15:18:35 -0700113 if not names:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700114 print(' (no branches)', file=sys.stderr)
Shawn O. Pearce4e3d6732009-04-18 15:18:35 -0700115 return
116
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700117 width = 25
118 for name in names:
119 if width < len(name):
120 width = len(name)
121
122 for name in names:
David Pursehouse5c6eeac2012-10-11 16:44:48 +0900123 i = all_branches[name]
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700124 in_cnt = len(i.projects)
125
126 if i.IsCurrent:
127 current = '*'
128 hdr = out.current
129 else:
130 current = ' '
131 hdr = out.local
132
133 if i.IsPublishedEqual:
134 published = 'P'
135 elif i.IsPublished:
136 published = 'p'
137 else:
138 published = ' '
139
140 hdr('%c%c %-*s' % (current, published, width, name))
141 out.write(' |')
142
Pär Åsfältff6929d2009-09-05 23:10:56 +0200143 if in_cnt < project_cnt:
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700144 fmt = out.write
145 paths = []
Etan Cohen588142d2014-07-09 21:33:31 -0700146 non_cur_paths = []
147 if i.IsSplitCurrent or (in_cnt < project_cnt - in_cnt):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900148 in_type = 'in'
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700149 for b in i.projects:
Etan Cohen588142d2014-07-09 21:33:31 -0700150 if not i.IsSplitCurrent or b.current:
151 paths.append(b.project.relpath)
152 else:
153 non_cur_paths.append(b.project.relpath)
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700154 else:
155 fmt = out.notinproject
David Pursehouse8a68ff92012-09-24 12:15:13 +0900156 in_type = 'not in'
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700157 have = set()
158 for b in i.projects:
159 have.add(b.project)
160 for p in projects:
David Pursehouseeeff3532020-02-12 11:24:10 +0900161 if p not in have:
Pär Åsfältff6929d2009-09-05 23:10:56 +0200162 paths.append(p.relpath)
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700163
David Pursehouse8a68ff92012-09-24 12:15:13 +0900164 s = ' %s %s' % (in_type, ', '.join(paths))
Etan Cohen588142d2014-07-09 21:33:31 -0700165 if not i.IsSplitCurrent and (width + 7 + len(s) < 80):
166 fmt = out.current if i.IsCurrent else fmt
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700167 fmt(s)
168 else:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900169 fmt(' %s:' % in_type)
Etan Cohen588142d2014-07-09 21:33:31 -0700170 fmt = out.current if i.IsCurrent else out.write
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700171 for p in paths:
172 out.nl()
Pär Åsfältff6929d2009-09-05 23:10:56 +0200173 fmt(width*' ' + ' %s' % p)
Etan Cohen588142d2014-07-09 21:33:31 -0700174 fmt = out.write
175 for p in non_cur_paths:
176 out.nl()
177 fmt(width*' ' + ' %s' % p)
Pär Åsfältff6929d2009-09-05 23:10:56 +0200178 else:
179 out.write(' in all projects')
Shawn O. Pearce27b07322009-04-10 16:02:48 -0700180 out.nl()