blob: 4bedf9d5048b5df2ba72c5e76cd3e97e1fcef5aa [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
68 manifestGroups = (manifestConfig.GetString('manifest.groups')
69 or 'all,-notdefault')
Olof Johansson33949c32012-07-10 14:32:23 +020070
71 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070072 if self.manifest.default.revisionExpr:
73 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020074 self.out.nl()
75 self.heading("Manifest merge branch: ")
76 self.headtext(mergeBranch)
77 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080078 self.heading("Manifest groups: ")
79 self.headtext(manifestGroups)
80 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020081
82 self.printSeparator()
83
84 if not opt.overview:
LaMont Jonescc879a92021-11-18 22:40:18 +000085 self._printDiffInfo(opt, args)
Olof Johansson33949c32012-07-10 14:32:23 +020086 else:
LaMont Jonescc879a92021-11-18 22:40:18 +000087 self._printCommitOverview(opt, args)
Olof Johansson33949c32012-07-10 14:32:23 +020088
89 def printSeparator(self):
90 self.text("----------------------------")
91 self.out.nl()
92
LaMont Jonescc879a92021-11-18 22:40:18 +000093 def _printDiffInfo(self, opt, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040094 # We let exceptions bubble up to main as they'll be well structured.
LaMont Jonescc879a92021-11-18 22:40:18 +000095 projs = self.GetProjects(args, all_manifests=not opt.this_manifest_only)
Olof Johansson33949c32012-07-10 14:32:23 +020096
97 for p in projs:
98 self.heading("Project: ")
99 self.headtext(p.name)
100 self.out.nl()
101
102 self.heading("Mount path: ")
103 self.headtext(p.worktree)
104 self.out.nl()
105
106 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400107 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +0200108 self.out.nl()
109
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400110 currentBranch = p.CurrentBranch
111 if currentBranch:
112 self.heading('Current branch: ')
113 self.headtext(currentBranch)
114 self.out.nl()
115
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100116 self.heading("Manifest revision: ")
117 self.headtext(p.revisionExpr)
118 self.out.nl()
119
Mike Frysinger31067c02019-06-13 02:13:23 -0400120 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200121 self.heading("Local Branches: ")
122 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400123 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200124 self.text(" [")
125 self.text(", ".join(localBranches))
126 self.text("]")
127 self.out.nl()
128
129 if self.opt.all:
130 self.findRemoteLocalDiff(p)
131
132 self.printSeparator()
133
134 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900135 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200136 if not self.opt.local:
137 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
138
Daniel Kutike7082cc2020-05-08 13:46:51 +0800139 branch = self.manifest.manifestProject.config.GetBranch('default').merge
140 if branch.startswith(R_HEADS):
141 branch = branch[len(R_HEADS):]
142 logTarget = R_M + branch
Olof Johansson33949c32012-07-10 14:32:23 +0200143
144 bareTmp = project.bare_git._bare
145 project.bare_git._bare = False
146 localCommits = project.bare_git.rev_list(
147 '--abbrev=8',
148 '--abbrev-commit',
149 '--pretty=oneline',
150 logTarget + "..",
151 '--')
152
153 originCommits = project.bare_git.rev_list(
154 '--abbrev=8',
155 '--abbrev-commit',
156 '--pretty=oneline',
157 ".." + logTarget,
158 '--')
159 project.bare_git._bare = bareTmp
160
161 self.heading("Local Commits: ")
162 self.redtext(str(len(localCommits)))
163 self.dimtext(" (on current branch)")
164 self.out.nl()
165
166 for c in localCommits:
167 split = c.split()
168 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900169 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200170 self.out.nl()
171
172 self.printSeparator()
173
174 self.heading("Remote Commits: ")
175 self.redtext(str(len(originCommits)))
176 self.out.nl()
177
178 for c in originCommits:
179 split = c.split()
180 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900181 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200182 self.out.nl()
183
LaMont Jonescc879a92021-11-18 22:40:18 +0000184 def _printCommitOverview(self, opt, args):
Olof Johansson33949c32012-07-10 14:32:23 +0200185 all_branches = []
LaMont Jonescc879a92021-11-18 22:40:18 +0000186 for project in self.GetProjects(args, all_manifests=not opt.this_manifest_only):
Olof Johansson33949c32012-07-10 14:32:23 +0200187 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530188 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200189 br = [x for x in br if x]
190 if self.opt.current_branch:
191 br = [x for x in br if x.name == project.CurrentBranch]
192 all_branches.extend(br)
193
194 if not all_branches:
195 return
196
197 self.out.nl()
198 self.heading('Projects Overview')
199 project = None
200
201 for branch in all_branches:
202 if project != branch.project:
203 project = branch.project
204 self.out.nl()
LaMont Jonescc879a92021-11-18 22:40:18 +0000205 self.headtext(project.RelPath(local=opt.this_manifest_only))
Olof Johansson33949c32012-07-10 14:32:23 +0200206 self.out.nl()
207
208 commits = branch.commits
209 date = branch.date
210 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900211 branch.name == project.CurrentBranch and '*' or ' ',
212 branch.name,
213 len(commits),
214 len(commits) != 1 and 's' or '',
215 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200216 self.out.nl()
217
218 for commit in commits:
219 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900220 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200221 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900222 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200223 self.out.nl()