blob: 2be56109714151b164d6d579d2fc1a74d0fe2896 [file] [log] [blame]
Olof Johansson33949c32012-07-10 14:32:23 +02001# Copyright (C) 2012 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Mike Frysingerb8e09ea2021-05-03 00:51:52 -040015import optparse
16
Olof Johansson33949c32012-07-10 14:32:23 +020017from command import PagedCommand
18from color import Coloring
Daniel Kutike7082cc2020-05-08 13:46:51 +080019from git_refs import R_M, R_HEADS
Olof Johansson33949c32012-07-10 14:32:23 +020020
David Pursehouse819827a2020-02-12 15:20:19 +090021
Olof Johansson33949c32012-07-10 14:32:23 +020022class _Coloring(Coloring):
23 def __init__(self, config):
24 Coloring.__init__(self, config, "status")
25
David Pursehouse819827a2020-02-12 15:20:19 +090026
Olof Johansson33949c32012-07-10 14:32:23 +020027class Info(PagedCommand):
28 common = True
29 helpSummary = "Get info on the manifest branch, current branch or unmerged branches"
Mike Frysingerb8e09ea2021-05-03 00:51:52 -040030 helpUsage = "%prog [-dl] [-o [-c]] [<project>...]"
Olof Johansson33949c32012-07-10 14:32:23 +020031
David Pursehouseda45e5d2013-05-15 17:34:45 +090032 def _Options(self, p):
Olof Johansson33949c32012-07-10 14:32:23 +020033 p.add_option('-d', '--diff',
34 dest='all', action='store_true',
35 help="show full info and commit diff including remote branches")
36 p.add_option('-o', '--overview',
37 dest='overview', action='store_true',
38 help='show overview of all local commits')
Mike Frysingerb8e09ea2021-05-03 00:51:52 -040039 p.add_option('-c', '--current-branch',
Olof Johansson33949c32012-07-10 14:32:23 +020040 dest="current_branch", action="store_true",
41 help="consider only checked out branches")
Mike Frysinger73561142021-05-03 01:10:09 -040042 p.add_option('--no-current-branch',
43 dest='current_branch', action='store_false',
44 help='consider all local branches')
Mike Frysingerb8e09ea2021-05-03 00:51:52 -040045 # Turn this into a warning & remove this someday.
46 p.add_option('-b',
47 dest='current_branch', action='store_true',
48 help=optparse.SUPPRESS_HELP)
Olof Johansson33949c32012-07-10 14:32:23 +020049 p.add_option('-l', '--local-only',
50 dest="local", action="store_true",
51 help="Disable all remote operations")
52
Olof Johansson33949c32012-07-10 14:32:23 +020053 def Execute(self, opt, args):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040054 self.out = _Coloring(self.client.globalConfig)
David Pursehousee5913ae2020-02-12 13:56:59 +090055 self.heading = self.out.printer('heading', attr='bold')
56 self.headtext = self.out.nofmt_printer('headtext', fg='yellow')
57 self.redtext = self.out.printer('redtext', fg='red')
58 self.sha = self.out.printer("sha", fg='yellow')
Olof Johansson75b4c2d2013-02-18 13:18:16 +010059 self.text = self.out.nofmt_printer('text')
David Pursehousee5913ae2020-02-12 13:56:59 +090060 self.dimtext = self.out.printer('dimtext', attr='dim')
Olof Johansson33949c32012-07-10 14:32:23 +020061
62 self.opt = opt
63
Conley Owens61ac9ae2013-03-05 10:35:36 -080064 manifestConfig = self.manifest.manifestProject.config
65 mergeBranch = manifestConfig.GetBranch("default").merge
66 manifestGroups = (manifestConfig.GetString('manifest.groups')
67 or 'all,-notdefault')
Olof Johansson33949c32012-07-10 14:32:23 +020068
69 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070070 if self.manifest.default.revisionExpr:
71 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020072 self.out.nl()
73 self.heading("Manifest merge branch: ")
74 self.headtext(mergeBranch)
75 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080076 self.heading("Manifest groups: ")
77 self.headtext(manifestGroups)
78 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020079
80 self.printSeparator()
81
82 if not opt.overview:
83 self.printDiffInfo(args)
84 else:
85 self.printCommitOverview(args)
86
87 def printSeparator(self):
88 self.text("----------------------------")
89 self.out.nl()
90
91 def printDiffInfo(self, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040092 # We let exceptions bubble up to main as they'll be well structured.
93 projs = self.GetProjects(args)
Olof Johansson33949c32012-07-10 14:32:23 +020094
95 for p in projs:
96 self.heading("Project: ")
97 self.headtext(p.name)
98 self.out.nl()
99
100 self.heading("Mount path: ")
101 self.headtext(p.worktree)
102 self.out.nl()
103
104 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400105 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +0200106 self.out.nl()
107
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400108 currentBranch = p.CurrentBranch
109 if currentBranch:
110 self.heading('Current branch: ')
111 self.headtext(currentBranch)
112 self.out.nl()
113
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100114 self.heading("Manifest revision: ")
115 self.headtext(p.revisionExpr)
116 self.out.nl()
117
Mike Frysinger31067c02019-06-13 02:13:23 -0400118 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200119 self.heading("Local Branches: ")
120 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400121 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200122 self.text(" [")
123 self.text(", ".join(localBranches))
124 self.text("]")
125 self.out.nl()
126
127 if self.opt.all:
128 self.findRemoteLocalDiff(p)
129
130 self.printSeparator()
131
132 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900133 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200134 if not self.opt.local:
135 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
136
Daniel Kutike7082cc2020-05-08 13:46:51 +0800137 branch = self.manifest.manifestProject.config.GetBranch('default').merge
138 if branch.startswith(R_HEADS):
139 branch = branch[len(R_HEADS):]
140 logTarget = R_M + branch
Olof Johansson33949c32012-07-10 14:32:23 +0200141
142 bareTmp = project.bare_git._bare
143 project.bare_git._bare = False
144 localCommits = project.bare_git.rev_list(
145 '--abbrev=8',
146 '--abbrev-commit',
147 '--pretty=oneline',
148 logTarget + "..",
149 '--')
150
151 originCommits = project.bare_git.rev_list(
152 '--abbrev=8',
153 '--abbrev-commit',
154 '--pretty=oneline',
155 ".." + logTarget,
156 '--')
157 project.bare_git._bare = bareTmp
158
159 self.heading("Local Commits: ")
160 self.redtext(str(len(localCommits)))
161 self.dimtext(" (on current branch)")
162 self.out.nl()
163
164 for c in localCommits:
165 split = c.split()
166 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900167 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200168 self.out.nl()
169
170 self.printSeparator()
171
172 self.heading("Remote Commits: ")
173 self.redtext(str(len(originCommits)))
174 self.out.nl()
175
176 for c in originCommits:
177 split = c.split()
178 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900179 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200180 self.out.nl()
181
182 def printCommitOverview(self, args):
183 all_branches = []
184 for project in self.GetProjects(args):
185 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530186 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200187 br = [x for x in br if x]
188 if self.opt.current_branch:
189 br = [x for x in br if x.name == project.CurrentBranch]
190 all_branches.extend(br)
191
192 if not all_branches:
193 return
194
195 self.out.nl()
196 self.heading('Projects Overview')
197 project = None
198
199 for branch in all_branches:
200 if project != branch.project:
201 project = branch.project
202 self.out.nl()
203 self.headtext(project.relpath)
204 self.out.nl()
205
206 commits = branch.commits
207 date = branch.date
208 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900209 branch.name == project.CurrentBranch and '*' or ' ',
210 branch.name,
211 len(commits),
212 len(commits) != 1 and 's' or '',
213 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200214 self.out.nl()
215
216 for commit in commits:
217 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900218 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200219 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900220 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200221 self.out.nl()