blob: 76f5d1d68de2628925f6d3bab58c9177bca30607 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Olof Johansson33949c32012-07-10 14:32:23 +02002#
3# Copyright (C) 2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from command import PagedCommand
18from color import Coloring
Olof Johansson33949c32012-07-10 14:32:23 +020019from git_refs import R_M
20
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"
30 helpUsage = "%prog [-dl] [-o [-b]] [<project>...]"
31
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')
39 p.add_option('-b', '--current-branch',
40 dest="current_branch", action="store_true",
41 help="consider only checked out branches")
42 p.add_option('-l', '--local-only',
43 dest="local", action="store_true",
44 help="Disable all remote operations")
45
Olof Johansson33949c32012-07-10 14:32:23 +020046 def Execute(self, opt, args):
47 self.out = _Coloring(self.manifest.globalConfig)
David Pursehousee5913ae2020-02-12 13:56:59 +090048 self.heading = self.out.printer('heading', attr='bold')
49 self.headtext = self.out.nofmt_printer('headtext', fg='yellow')
50 self.redtext = self.out.printer('redtext', fg='red')
51 self.sha = self.out.printer("sha", fg='yellow')
Olof Johansson75b4c2d2013-02-18 13:18:16 +010052 self.text = self.out.nofmt_printer('text')
David Pursehousee5913ae2020-02-12 13:56:59 +090053 self.dimtext = self.out.printer('dimtext', attr='dim')
Olof Johansson33949c32012-07-10 14:32:23 +020054
55 self.opt = opt
56
Conley Owens61ac9ae2013-03-05 10:35:36 -080057 manifestConfig = self.manifest.manifestProject.config
58 mergeBranch = manifestConfig.GetBranch("default").merge
59 manifestGroups = (manifestConfig.GetString('manifest.groups')
60 or 'all,-notdefault')
Olof Johansson33949c32012-07-10 14:32:23 +020061
62 self.heading("Manifest branch: ")
Cassidy Burden17af5782015-06-29 14:51:35 -070063 if self.manifest.default.revisionExpr:
64 self.headtext(self.manifest.default.revisionExpr)
Olof Johansson33949c32012-07-10 14:32:23 +020065 self.out.nl()
66 self.heading("Manifest merge branch: ")
67 self.headtext(mergeBranch)
68 self.out.nl()
Conley Owens61ac9ae2013-03-05 10:35:36 -080069 self.heading("Manifest groups: ")
70 self.headtext(manifestGroups)
71 self.out.nl()
Olof Johansson33949c32012-07-10 14:32:23 +020072
73 self.printSeparator()
74
75 if not opt.overview:
76 self.printDiffInfo(args)
77 else:
78 self.printCommitOverview(args)
79
80 def printSeparator(self):
81 self.text("----------------------------")
82 self.out.nl()
83
84 def printDiffInfo(self, args):
Mike Frysinger9775a3d2019-10-01 01:01:33 -040085 # We let exceptions bubble up to main as they'll be well structured.
86 projs = self.GetProjects(args)
Olof Johansson33949c32012-07-10 14:32:23 +020087
88 for p in projs:
89 self.heading("Project: ")
90 self.headtext(p.name)
91 self.out.nl()
92
93 self.heading("Mount path: ")
94 self.headtext(p.worktree)
95 self.out.nl()
96
97 self.heading("Current revision: ")
Mike Frysingerf1c5dd82019-10-01 01:17:55 -040098 self.headtext(p.GetRevisionId())
Olof Johansson33949c32012-07-10 14:32:23 +020099 self.out.nl()
100
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400101 currentBranch = p.CurrentBranch
102 if currentBranch:
103 self.heading('Current branch: ')
104 self.headtext(currentBranch)
105 self.out.nl()
106
Diogo Ferreirae4d20372019-10-14 16:28:46 +0100107 self.heading("Manifest revision: ")
108 self.headtext(p.revisionExpr)
109 self.out.nl()
110
Mike Frysinger31067c02019-06-13 02:13:23 -0400111 localBranches = list(p.GetBranches().keys())
Olof Johansson33949c32012-07-10 14:32:23 +0200112 self.heading("Local Branches: ")
113 self.redtext(str(len(localBranches)))
Mike Frysingerf1c5dd82019-10-01 01:17:55 -0400114 if localBranches:
Olof Johansson33949c32012-07-10 14:32:23 +0200115 self.text(" [")
116 self.text(", ".join(localBranches))
117 self.text("]")
118 self.out.nl()
119
120 if self.opt.all:
121 self.findRemoteLocalDiff(p)
122
123 self.printSeparator()
124
125 def findRemoteLocalDiff(self, project):
David Pursehouse719675b2020-02-12 11:46:45 +0900126 # Fetch all the latest commits.
Olof Johansson33949c32012-07-10 14:32:23 +0200127 if not self.opt.local:
128 project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
129
Olof Johansson57bd7b72013-01-29 08:22:05 +0100130 logTarget = R_M + self.manifest.manifestProject.config.GetBranch("default").merge
Olof Johansson33949c32012-07-10 14:32:23 +0200131
132 bareTmp = project.bare_git._bare
133 project.bare_git._bare = False
134 localCommits = project.bare_git.rev_list(
135 '--abbrev=8',
136 '--abbrev-commit',
137 '--pretty=oneline',
138 logTarget + "..",
139 '--')
140
141 originCommits = project.bare_git.rev_list(
142 '--abbrev=8',
143 '--abbrev-commit',
144 '--pretty=oneline',
145 ".." + logTarget,
146 '--')
147 project.bare_git._bare = bareTmp
148
149 self.heading("Local Commits: ")
150 self.redtext(str(len(localCommits)))
151 self.dimtext(" (on current branch)")
152 self.out.nl()
153
154 for c in localCommits:
155 split = c.split()
156 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900157 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200158 self.out.nl()
159
160 self.printSeparator()
161
162 self.heading("Remote Commits: ")
163 self.redtext(str(len(originCommits)))
164 self.out.nl()
165
166 for c in originCommits:
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 def printCommitOverview(self, args):
173 all_branches = []
174 for project in self.GetProjects(args):
175 br = [project.GetUploadableBranch(x)
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530176 for x in project.GetBranches()]
Olof Johansson33949c32012-07-10 14:32:23 +0200177 br = [x for x in br if x]
178 if self.opt.current_branch:
179 br = [x for x in br if x.name == project.CurrentBranch]
180 all_branches.extend(br)
181
182 if not all_branches:
183 return
184
185 self.out.nl()
186 self.heading('Projects Overview')
187 project = None
188
189 for branch in all_branches:
190 if project != branch.project:
191 project = branch.project
192 self.out.nl()
193 self.headtext(project.relpath)
194 self.out.nl()
195
196 commits = branch.commits
197 date = branch.date
198 self.text('%s %-33s (%2d commit%s, %s)' % (
David Pursehouseabdf7502020-02-12 14:58:39 +0900199 branch.name == project.CurrentBranch and '*' or ' ',
200 branch.name,
201 len(commits),
202 len(commits) != 1 and 's' or '',
203 date))
Olof Johansson33949c32012-07-10 14:32:23 +0200204 self.out.nl()
205
206 for commit in commits:
207 split = commit.split()
David Pursehouse54a4e602020-02-12 14:31:05 +0900208 self.text('{0:38}{1} '.format('', '-'))
Olof Johansson33949c32012-07-10 14:32:23 +0200209 self.sha(split[0] + " ")
David Pursehousee0b6de32012-11-21 17:36:28 +0900210 self.text(" ".join(split[1:]))
Olof Johansson33949c32012-07-10 14:32:23 +0200211 self.out.nl()