Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 1 | # 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 Frysinger | b8e09ea | 2021-05-03 00:51:52 -0400 | [diff] [blame] | 15 | import optparse |
| 16 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 17 | from command import PagedCommand |
| 18 | from color import Coloring |
Daniel Kutik | e7082cc | 2020-05-08 13:46:51 +0800 | [diff] [blame] | 19 | from git_refs import R_M, R_HEADS |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 20 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 21 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 22 | class _Coloring(Coloring): |
| 23 | def __init__(self, config): |
| 24 | Coloring.__init__(self, config, "status") |
| 25 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 26 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 27 | class Info(PagedCommand): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 28 | COMMON = True |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 29 | helpSummary = "Get info on the manifest branch, current branch or unmerged branches" |
Mike Frysinger | b8e09ea | 2021-05-03 00:51:52 -0400 | [diff] [blame] | 30 | helpUsage = "%prog [-dl] [-o [-c]] [<project>...]" |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 31 | |
David Pursehouse | da45e5d | 2013-05-15 17:34:45 +0900 | [diff] [blame] | 32 | def _Options(self, p): |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 33 | 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 Frysinger | b8e09ea | 2021-05-03 00:51:52 -0400 | [diff] [blame] | 39 | p.add_option('-c', '--current-branch', |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 40 | dest="current_branch", action="store_true", |
| 41 | help="consider only checked out branches") |
Mike Frysinger | 7356114 | 2021-05-03 01:10:09 -0400 | [diff] [blame] | 42 | p.add_option('--no-current-branch', |
| 43 | dest='current_branch', action='store_false', |
| 44 | help='consider all local branches') |
Mike Frysinger | b8e09ea | 2021-05-03 00:51:52 -0400 | [diff] [blame] | 45 | # 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 Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 49 | p.add_option('-l', '--local-only', |
| 50 | dest="local", action="store_true", |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 51 | help="disable all remote operations") |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 52 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 53 | def Execute(self, opt, args): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 54 | self.out = _Coloring(self.client.globalConfig) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 55 | 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 Johansson | 75b4c2d | 2013-02-18 13:18:16 +0100 | [diff] [blame] | 59 | self.text = self.out.nofmt_printer('text') |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 60 | self.dimtext = self.out.printer('dimtext', attr='dim') |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 61 | |
| 62 | self.opt = opt |
| 63 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 64 | if not opt.this_manifest_only: |
| 65 | self.manifest = self.manifest.outer_client |
Conley Owens | 61ac9ae | 2013-03-05 10:35:36 -0800 | [diff] [blame] | 66 | manifestConfig = self.manifest.manifestProject.config |
| 67 | mergeBranch = manifestConfig.GetBranch("default").merge |
LaMont Jones | 501733c | 2022-04-20 16:42:32 +0000 | [diff] [blame] | 68 | manifestGroups = self.manifest.GetGroupsStr() |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 69 | |
| 70 | self.heading("Manifest branch: ") |
Cassidy Burden | 17af578 | 2015-06-29 14:51:35 -0700 | [diff] [blame] | 71 | if self.manifest.default.revisionExpr: |
| 72 | self.headtext(self.manifest.default.revisionExpr) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 73 | self.out.nl() |
| 74 | self.heading("Manifest merge branch: ") |
| 75 | self.headtext(mergeBranch) |
| 76 | self.out.nl() |
Conley Owens | 61ac9ae | 2013-03-05 10:35:36 -0800 | [diff] [blame] | 77 | self.heading("Manifest groups: ") |
| 78 | self.headtext(manifestGroups) |
| 79 | self.out.nl() |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 80 | |
| 81 | self.printSeparator() |
| 82 | |
| 83 | if not opt.overview: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 84 | self._printDiffInfo(opt, args) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 85 | else: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 86 | self._printCommitOverview(opt, args) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 87 | |
| 88 | def printSeparator(self): |
| 89 | self.text("----------------------------") |
| 90 | self.out.nl() |
| 91 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 92 | def _printDiffInfo(self, opt, args): |
Mike Frysinger | 9775a3d | 2019-10-01 01:01:33 -0400 | [diff] [blame] | 93 | # We let exceptions bubble up to main as they'll be well structured. |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 94 | projs = self.GetProjects(args, all_manifests=not opt.this_manifest_only) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 95 | |
| 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 Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 106 | self.headtext(p.GetRevisionId()) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 107 | self.out.nl() |
| 108 | |
Mike Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 109 | currentBranch = p.CurrentBranch |
| 110 | if currentBranch: |
| 111 | self.heading('Current branch: ') |
| 112 | self.headtext(currentBranch) |
| 113 | self.out.nl() |
| 114 | |
Diogo Ferreira | e4d2037 | 2019-10-14 16:28:46 +0100 | [diff] [blame] | 115 | self.heading("Manifest revision: ") |
| 116 | self.headtext(p.revisionExpr) |
| 117 | self.out.nl() |
| 118 | |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 119 | localBranches = list(p.GetBranches().keys()) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 120 | self.heading("Local Branches: ") |
| 121 | self.redtext(str(len(localBranches))) |
Mike Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 122 | if localBranches: |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 123 | 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 Pursehouse | 719675b | 2020-02-12 11:46:45 +0900 | [diff] [blame] | 134 | # Fetch all the latest commits. |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 135 | if not self.opt.local: |
| 136 | project.Sync_NetworkHalf(quiet=True, current_branch_only=True) |
| 137 | |
Daniel Kutik | e7082cc | 2020-05-08 13:46:51 +0800 | [diff] [blame] | 138 | 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 Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 142 | |
| 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 Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 168 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 169 | 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 Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 180 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 181 | self.out.nl() |
| 182 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 183 | def _printCommitOverview(self, opt, args): |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 184 | all_branches = [] |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 185 | for project in self.GetProjects(args, all_manifests=not opt.this_manifest_only): |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 186 | br = [project.GetUploadableBranch(x) |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 187 | for x in project.GetBranches()] |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 188 | 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 Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 204 | self.headtext(project.RelPath(local=opt.this_manifest_only)) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 205 | self.out.nl() |
| 206 | |
| 207 | commits = branch.commits |
| 208 | date = branch.date |
| 209 | self.text('%s %-33s (%2d commit%s, %s)' % ( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 210 | branch.name == project.CurrentBranch and '*' or ' ', |
| 211 | branch.name, |
| 212 | len(commits), |
| 213 | len(commits) != 1 and 's' or '', |
| 214 | date)) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 215 | self.out.nl() |
| 216 | |
| 217 | for commit in commits: |
| 218 | split = commit.split() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 219 | self.text('{0:38}{1} '.format('', '-')) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 220 | self.sha(split[0] + " ") |
David Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 221 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 222 | self.out.nl() |