blob: 8e017ed82515872e674a6a94ba91b2af3a985e4d [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 Frysingerb8e09ea2021-05-03 00:51:52 -040042 # Turn this into a warning & remove this someday.
43 p.add_option('-b',
44 dest='current_branch', action='store_true',
45 help=optparse.SUPPRESS_HELP)
Olof Johansson33949c32012-07-10 14:32:23 +020046 p.add_option('-l', '--local-only',
47 dest="local", action="store_true",
48 help="Disable all remote operations")
49
Olof Johansson33949c32012-07-10 14:32:23 +020050 def Execute(self, opt, args):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040051 self.out = _Coloring(self.client.globalConfig)
David Pursehousee5913ae2020-02-12 13:56:59 +090052 self.heading = self.out.printer('heading', attr='bold')
53 self.headtext = self.out.nofmt_printer('headtext', fg='yellow')
54 self.redtext = self.out.printer('redtext', fg='red')
55 self.sha = self.out.printer("sha", fg='yellow')
Olof Johansson75b4c2d2013-02-18 13:18:16 +010056 self.text = self.out.nofmt_printer('text')
David Pursehousee5913ae2020-02-12 13:56:59 +090057 self.dimtext = self.out.printer('dimtext', attr='dim')
Olof Johansson33949c32012-07-10 14:32:23 +020058
59 self.opt = opt
60
Conley Owens61ac9ae2013-03-05 10:35:36 -080061 manifestConfig = self.manifest.manifestProject.config
62 mergeBranch = manifestConfig.GetBranch("default").merge
63 manifestGroups = (manifestConfig.GetString('manifest.groups')
64 or 'all,-notdefault')
Olof Johansson33949c32012-07-10 14:32:23 +020065
66 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070067 if self.manifest.default.revisionExpr:
68 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020069 self.out.nl()
70 self.heading("Manifest merge branch: ")
71 self.headtext(mergeBranch)
72 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080073 self.heading("Manifest groups: ")
74 self.headtext(manifestGroups)
75 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020076
77 self.printSeparator()
78
79 if not opt.overview:
80 self.printDiffInfo(args)
81 else:
82 self.printCommitOverview(args)
83
84 def printSeparator(self):
85 self.text("----------------------------")
86 self.out.nl()
87
88 def printDiffInfo(self, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040089 # We let exceptions bubble up to main as they'll be well structured.
90 projs = self.GetProjects(args)
Olof Johansson33949c32012-07-10 14:32:23 +020091
92 for p in projs:
93 self.heading("Project: ")
94 self.headtext(p.name)
95 self.out.nl()
96
97 self.heading("Mount path: ")
98 self.headtext(p.worktree)
99 self.out.nl()
100
101 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400102 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +0200103 self.out.nl()
104
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400105 currentBranch = p.CurrentBranch
106 if currentBranch:
107 self.heading('Current branch: ')
108 self.headtext(currentBranch)
109 self.out.nl()
110
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100111 self.heading("Manifest revision: ")
112 self.headtext(p.revisionExpr)
113 self.out.nl()
114
Mike Frysinger31067c02019-06-13 02:13:23 -0400115 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200116 self.heading("Local Branches: ")
117 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400118 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200119 self.text(" [")
120 self.text(", ".join(localBranches))
121 self.text("]")
122 self.out.nl()
123
124 if self.opt.all:
125 self.findRemoteLocalDiff(p)
126
127 self.printSeparator()
128
129 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900130 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200131 if not self.opt.local:
132 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
133
Daniel Kutike7082cc2020-05-08 13:46:51 +0800134 branch = self.manifest.manifestProject.config.GetBranch('default').merge
135 if branch.startswith(R_HEADS):
136 branch = branch[len(R_HEADS):]
137 logTarget = R_M + branch
Olof Johansson33949c32012-07-10 14:32:23 +0200138
139 bareTmp = project.bare_git._bare
140 project.bare_git._bare = False
141 localCommits = project.bare_git.rev_list(
142 '--abbrev=8',
143 '--abbrev-commit',
144 '--pretty=oneline',
145 logTarget + "..",
146 '--')
147
148 originCommits = project.bare_git.rev_list(
149 '--abbrev=8',
150 '--abbrev-commit',
151 '--pretty=oneline',
152 ".." + logTarget,
153 '--')
154 project.bare_git._bare = bareTmp
155
156 self.heading("Local Commits: ")
157 self.redtext(str(len(localCommits)))
158 self.dimtext(" (on current branch)")
159 self.out.nl()
160
161 for c in localCommits:
162 split = c.split()
163 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900164 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200165 self.out.nl()
166
167 self.printSeparator()
168
169 self.heading("Remote Commits: ")
170 self.redtext(str(len(originCommits)))
171 self.out.nl()
172
173 for c in originCommits:
174 split = c.split()
175 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900176 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200177 self.out.nl()
178
179 def printCommitOverview(self, args):
180 all_branches = []
181 for project in self.GetProjects(args):
182 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530183 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200184 br = [x for x in br if x]
185 if self.opt.current_branch:
186 br = [x for x in br if x.name == project.CurrentBranch]
187 all_branches.extend(br)
188
189 if not all_branches:
190 return
191
192 self.out.nl()
193 self.heading('Projects Overview')
194 project = None
195
196 for branch in all_branches:
197 if project != branch.project:
198 project = branch.project
199 self.out.nl()
200 self.headtext(project.relpath)
201 self.out.nl()
202
203 commits = branch.commits
204 date = branch.date
205 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900206 branch.name == project.CurrentBranch and '*' or ' ',
207 branch.name,
208 len(commits),
209 len(commits) != 1 and 's' or '',
210 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200211 self.out.nl()
212
213 for commit in commits:
214 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900215 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200216 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900217 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200218 self.out.nl()