blob: 6381fa8e7e535124ebbfafc5741a066bdc1cb7b8 [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
15from command import PagedCommand
16from color import Coloring
Daniel Kutike7082cc2020-05-08 13:46:51 +080017from git_refs import R_M, R_HEADS
Olof Johansson33949c32012-07-10 14:32:23 +020018
David Pursehouse819827a2020-02-12 15:20:19 +090019
Olof Johansson33949c32012-07-10 14:32:23 +020020class _Coloring(Coloring):
21 def __init__(self, config):
22 Coloring.__init__(self, config, "status")
23
David Pursehouse819827a2020-02-12 15:20:19 +090024
Olof Johansson33949c32012-07-10 14:32:23 +020025class Info(PagedCommand):
26 common = True
27 helpSummary = "Get info on the manifest branch, current branch or unmerged branches"
28 helpUsage = "%prog [-dl] [-o [-b]] [<project>...]"
29
David Pursehouseda45e5d2013-05-15 17:34:45 +090030 def _Options(self, p):
Olof Johansson33949c32012-07-10 14:32:23 +020031 p.add_option('-d', '--diff',
32 dest='all', action='store_true',
33 help="show full info and commit diff including remote branches")
34 p.add_option('-o', '--overview',
35 dest='overview', action='store_true',
36 help='show overview of all local commits')
37 p.add_option('-b', '--current-branch',
38 dest="current_branch", action="store_true",
39 help="consider only checked out branches")
40 p.add_option('-l', '--local-only',
41 dest="local", action="store_true",
42 help="Disable all remote operations")
43
Olof Johansson33949c32012-07-10 14:32:23 +020044 def Execute(self, opt, args):
Mike Frysinger8c1e9cb2020-09-06 14:53:18 -040045 self.out = _Coloring(self.client.globalConfig)
David Pursehousee5913ae2020-02-12 13:56:59 +090046 self.heading = self.out.printer('heading', attr='bold')
47 self.headtext = self.out.nofmt_printer('headtext', fg='yellow')
48 self.redtext = self.out.printer('redtext', fg='red')
49 self.sha = self.out.printer("sha", fg='yellow')
Olof Johansson75b4c2d2013-02-18 13:18:16 +010050 self.text = self.out.nofmt_printer('text')
David Pursehousee5913ae2020-02-12 13:56:59 +090051 self.dimtext = self.out.printer('dimtext', attr='dim')
Olof Johansson33949c32012-07-10 14:32:23 +020052
53 self.opt = opt
54
Conley Owens61ac9ae2013-03-05 10:35:36 -080055 manifestConfig = self.manifest.manifestProject.config
56 mergeBranch = manifestConfig.GetBranch("default").merge
57 manifestGroups = (manifestConfig.GetString('manifest.groups')
58 or 'all,-notdefault')
Olof Johansson33949c32012-07-10 14:32:23 +020059
60 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070061 if self.manifest.default.revisionExpr:
62 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020063 self.out.nl()
64 self.heading("Manifest merge branch: ")
65 self.headtext(mergeBranch)
66 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080067 self.heading("Manifest groups: ")
68 self.headtext(manifestGroups)
69 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020070
71 self.printSeparator()
72
73 if not opt.overview:
74 self.printDiffInfo(args)
75 else:
76 self.printCommitOverview(args)
77
78 def printSeparator(self):
79 self.text("----------------------------")
80 self.out.nl()
81
82 def printDiffInfo(self, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040083 # We let exceptions bubble up to main as they'll be well structured.
84 projs = self.GetProjects(args)
Olof Johansson33949c32012-07-10 14:32:23 +020085
86 for p in projs:
87 self.heading("Project: ")
88 self.headtext(p.name)
89 self.out.nl()
90
91 self.heading("Mount path: ")
92 self.headtext(p.worktree)
93 self.out.nl()
94
95 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -040096 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +020097 self.out.nl()
98
Mike Frysingerf1c5dd82019-10-01 01:17:55 -040099 currentBranch = p.CurrentBranch
100 if currentBranch:
101 self.heading('Current branch: ')
102 self.headtext(currentBranch)
103 self.out.nl()
104
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100105 self.heading("Manifest revision: ")
106 self.headtext(p.revisionExpr)
107 self.out.nl()
108
Mike Frysinger31067c02019-06-13 02:13:23 -0400109 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200110 self.heading("Local Branches: ")
111 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400112 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200113 self.text(" [")
114 self.text(", ".join(localBranches))
115 self.text("]")
116 self.out.nl()
117
118 if self.opt.all:
119 self.findRemoteLocalDiff(p)
120
121 self.printSeparator()
122
123 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900124 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200125 if not self.opt.local:
126 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
127
Daniel Kutike7082cc2020-05-08 13:46:51 +0800128 branch = self.manifest.manifestProject.config.GetBranch('default').merge
129 if branch.startswith(R_HEADS):
130 branch = branch[len(R_HEADS):]
131 logTarget = R_M + branch
Olof Johansson33949c32012-07-10 14:32:23 +0200132
133 bareTmp = project.bare_git._bare
134 project.bare_git._bare = False
135 localCommits = project.bare_git.rev_list(
136 '--abbrev=8',
137 '--abbrev-commit',
138 '--pretty=oneline',
139 logTarget + "..",
140 '--')
141
142 originCommits = project.bare_git.rev_list(
143 '--abbrev=8',
144 '--abbrev-commit',
145 '--pretty=oneline',
146 ".." + logTarget,
147 '--')
148 project.bare_git._bare = bareTmp
149
150 self.heading("Local Commits: ")
151 self.redtext(str(len(localCommits)))
152 self.dimtext(" (on current branch)")
153 self.out.nl()
154
155 for c in localCommits:
156 split = c.split()
157 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900158 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200159 self.out.nl()
160
161 self.printSeparator()
162
163 self.heading("Remote Commits: ")
164 self.redtext(str(len(originCommits)))
165 self.out.nl()
166
167 for c in originCommits:
168 split = c.split()
169 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900170 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200171 self.out.nl()
172
173 def printCommitOverview(self, args):
174 all_branches = []
175 for project in self.GetProjects(args):
176 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530177 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200178 br = [x for x in br if x]
179 if self.opt.current_branch:
180 br = [x for x in br if x.name == project.CurrentBranch]
181 all_branches.extend(br)
182
183 if not all_branches:
184 return
185
186 self.out.nl()
187 self.heading('Projects Overview')
188 project = None
189
190 for branch in all_branches:
191 if project != branch.project:
192 project = branch.project
193 self.out.nl()
194 self.headtext(project.relpath)
195 self.out.nl()
196
197 commits = branch.commits
198 date = branch.date
199 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900200 branch.name == project.CurrentBranch and '*' or ' ',
201 branch.name,
202 len(commits),
203 len(commits) != 1 and 's' or '',
204 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200205 self.out.nl()
206
207 for commit in commits:
208 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900209 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200210 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900211 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200212 self.out.nl()