blob: baa4c5b1c3653b93a8a32eb842b8a1155fec62ab [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):
Mike Frysinger4f210542021-06-14 16:05:19 -040028 COMMON = True
Olof Johansson33949c32012-07-10 14:32:23 +020029 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",
Mike Frysingerc177f942021-05-04 08:06:36 -040051 help="disable all remote operations")
Olof Johansson33949c32012-07-10 14:32:23 +020052
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
LaMont Jonescc879a92021-11-18 22:40:18 +000064 if not opt.this_manifest_only:
65 self.manifest = self.manifest.outer_client
Conley Owens61ac9ae2013-03-05 10:35:36 -080066 manifestConfig = self.manifest.manifestProject.config
67 mergeBranch = manifestConfig.GetBranch("default").merge
LaMont Jones501733c2022-04-20 16:42:32 +000068 manifestGroups = self.manifest.GetGroupsStr()
Olof Johansson33949c32012-07-10 14:32:23 +020069
70 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070071 if self.manifest.default.revisionExpr:
72 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020073 self.out.nl()
74 self.heading("Manifest merge branch: ")
75 self.headtext(mergeBranch)
76 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080077 self.heading("Manifest groups: ")
78 self.headtext(manifestGroups)
79 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020080
81 self.printSeparator()
82
83 if not opt.overview:
LaMont Jonescc879a92021-11-18 22:40:18 +000084 self._printDiffInfo(opt, args)
Olof Johansson33949c32012-07-10 14:32:23 +020085 else:
LaMont Jonescc879a92021-11-18 22:40:18 +000086 self._printCommitOverview(opt, args)
Olof Johansson33949c32012-07-10 14:32:23 +020087
88 def printSeparator(self):
89 self.text("----------------------------")
90 self.out.nl()
91
LaMont Jonescc879a92021-11-18 22:40:18 +000092 def _printDiffInfo(self, opt, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040093 # We let exceptions bubble up to main as they'll be well structured.
LaMont Jonescc879a92021-11-18 22:40:18 +000094 projs = self.GetProjects(args, all_manifests=not opt.this_manifest_only)
Olof Johansson33949c32012-07-10 14:32:23 +020095
96 for p in projs:
97 self.heading("Project: ")
98 self.headtext(p.name)
99 self.out.nl()
100
101 self.heading("Mount path: ")
102 self.headtext(p.worktree)
103 self.out.nl()
104
105 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400106 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +0200107 self.out.nl()
108
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400109 currentBranch = p.CurrentBranch
110 if currentBranch:
111 self.heading('Current branch: ')
112 self.headtext(currentBranch)
113 self.out.nl()
114
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100115 self.heading("Manifest revision: ")
116 self.headtext(p.revisionExpr)
117 self.out.nl()
118
Mike Frysinger31067c02019-06-13 02:13:23 -0400119 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200120 self.heading("Local Branches: ")
121 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400122 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200123 self.text(" [")
124 self.text(", ".join(localBranches))
125 self.text("]")
126 self.out.nl()
127
128 if self.opt.all:
129 self.findRemoteLocalDiff(p)
130
131 self.printSeparator()
132
133 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900134 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200135 if not self.opt.local:
136 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
137
Daniel Kutike7082cc2020-05-08 13:46:51 +0800138 branch = self.manifest.manifestProject.config.GetBranch('default').merge
139 if branch.startswith(R_HEADS):
140 branch = branch[len(R_HEADS):]
141 logTarget = R_M + branch
Olof Johansson33949c32012-07-10 14:32:23 +0200142
143 bareTmp = project.bare_git._bare
144 project.bare_git._bare = False
145 localCommits = project.bare_git.rev_list(
146 '--abbrev=8',
147 '--abbrev-commit',
148 '--pretty=oneline',
149 logTarget + "..",
150 '--')
151
152 originCommits = project.bare_git.rev_list(
153 '--abbrev=8',
154 '--abbrev-commit',
155 '--pretty=oneline',
156 ".." + logTarget,
157 '--')
158 project.bare_git._bare = bareTmp
159
160 self.heading("Local Commits: ")
161 self.redtext(str(len(localCommits)))
162 self.dimtext(" (on current branch)")
163 self.out.nl()
164
165 for c in localCommits:
166 split = c.split()
167 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900168 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200169 self.out.nl()
170
171 self.printSeparator()
172
173 self.heading("Remote Commits: ")
174 self.redtext(str(len(originCommits)))
175 self.out.nl()
176
177 for c in originCommits:
178 split = c.split()
179 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900180 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200181 self.out.nl()
182
LaMont Jonescc879a92021-11-18 22:40:18 +0000183 def _printCommitOverview(self, opt, args):
Olof Johansson33949c32012-07-10 14:32:23 +0200184 all_branches = []
LaMont Jonescc879a92021-11-18 22:40:18 +0000185 for project in self.GetProjects(args, all_manifests=not opt.this_manifest_only):
Olof Johansson33949c32012-07-10 14:32:23 +0200186 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530187 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200188 br = [x for x in br if x]
189 if self.opt.current_branch:
190 br = [x for x in br if x.name == project.CurrentBranch]
191 all_branches.extend(br)
192
193 if not all_branches:
194 return
195
196 self.out.nl()
197 self.heading('Projects Overview')
198 project = None
199
200 for branch in all_branches:
201 if project != branch.project:
202 project = branch.project
203 self.out.nl()
LaMont Jonescc879a92021-11-18 22:40:18 +0000204 self.headtext(project.RelPath(local=opt.this_manifest_only))
Olof Johansson33949c32012-07-10 14:32:23 +0200205 self.out.nl()
206
207 commits = branch.commits
208 date = branch.date
209 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900210 branch.name == project.CurrentBranch and '*' or ' ',
211 branch.name,
212 len(commits),
213 len(commits) != 1 and 's' or '',
214 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200215 self.out.nl()
216
217 for commit in commits:
218 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900219 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200220 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900221 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200222 self.out.nl()