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 | |
| 15 | from command import PagedCommand |
| 16 | from color import Coloring |
Daniel Kutik | e7082cc | 2020-05-08 13:46:51 +0800 | [diff] [blame] | 17 | from git_refs import R_M, R_HEADS |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 18 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 19 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 20 | class _Coloring(Coloring): |
| 21 | def __init__(self, config): |
| 22 | Coloring.__init__(self, config, "status") |
| 23 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 24 | |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 25 | class 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 Pursehouse | da45e5d | 2013-05-15 17:34:45 +0900 | [diff] [blame] | 30 | def _Options(self, p): |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 31 | 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 Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 44 | def Execute(self, opt, args): |
Mike Frysinger | 8c1e9cb | 2020-09-06 14:53:18 -0400 | [diff] [blame] | 45 | self.out = _Coloring(self.client.globalConfig) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 46 | 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 Johansson | 75b4c2d | 2013-02-18 13:18:16 +0100 | [diff] [blame] | 50 | self.text = self.out.nofmt_printer('text') |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 51 | self.dimtext = self.out.printer('dimtext', attr='dim') |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 52 | |
| 53 | self.opt = opt |
| 54 | |
Conley Owens | 61ac9ae | 2013-03-05 10:35:36 -0800 | [diff] [blame] | 55 | manifestConfig = self.manifest.manifestProject.config |
| 56 | mergeBranch = manifestConfig.GetBranch("default").merge |
| 57 | manifestGroups = (manifestConfig.GetString('manifest.groups') |
| 58 | or 'all,-notdefault') |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 59 | |
| 60 | self.heading("Manifest branch: ") |
Cassidy Burden | 17af578 | 2015-06-29 14:51:35 -0700 | [diff] [blame] | 61 | if self.manifest.default.revisionExpr: |
| 62 | self.headtext(self.manifest.default.revisionExpr) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 63 | self.out.nl() |
| 64 | self.heading("Manifest merge branch: ") |
| 65 | self.headtext(mergeBranch) |
| 66 | self.out.nl() |
Conley Owens | 61ac9ae | 2013-03-05 10:35:36 -0800 | [diff] [blame] | 67 | self.heading("Manifest groups: ") |
| 68 | self.headtext(manifestGroups) |
| 69 | self.out.nl() |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 70 | |
| 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 Frysinger | 9775a3d | 2019-10-01 01:01:33 -0400 | [diff] [blame] | 83 | # We let exceptions bubble up to main as they'll be well structured. |
| 84 | projs = self.GetProjects(args) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 85 | |
| 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 Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 96 | self.headtext(p.GetRevisionId()) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 97 | self.out.nl() |
| 98 | |
Mike Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 99 | currentBranch = p.CurrentBranch |
| 100 | if currentBranch: |
| 101 | self.heading('Current branch: ') |
| 102 | self.headtext(currentBranch) |
| 103 | self.out.nl() |
| 104 | |
Diogo Ferreira | e4d2037 | 2019-10-14 16:28:46 +0100 | [diff] [blame] | 105 | self.heading("Manifest revision: ") |
| 106 | self.headtext(p.revisionExpr) |
| 107 | self.out.nl() |
| 108 | |
Mike Frysinger | 31067c0 | 2019-06-13 02:13:23 -0400 | [diff] [blame] | 109 | localBranches = list(p.GetBranches().keys()) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 110 | self.heading("Local Branches: ") |
| 111 | self.redtext(str(len(localBranches))) |
Mike Frysinger | f1c5dd8 | 2019-10-01 01:17:55 -0400 | [diff] [blame] | 112 | if localBranches: |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 113 | 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 Pursehouse | 719675b | 2020-02-12 11:46:45 +0900 | [diff] [blame] | 124 | # Fetch all the latest commits. |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 125 | if not self.opt.local: |
| 126 | project.Sync_NetworkHalf(quiet=True, current_branch_only=True) |
| 127 | |
Daniel Kutik | e7082cc | 2020-05-08 13:46:51 +0800 | [diff] [blame] | 128 | 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 Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 132 | |
| 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 Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 158 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 159 | 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 Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 170 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 171 | 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 Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 177 | for x in project.GetBranches()] |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 178 | 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 Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 200 | branch.name == project.CurrentBranch and '*' or ' ', |
| 201 | branch.name, |
| 202 | len(commits), |
| 203 | len(commits) != 1 and 's' or '', |
| 204 | date)) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 205 | self.out.nl() |
| 206 | |
| 207 | for commit in commits: |
| 208 | split = commit.split() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 209 | self.text('{0:38}{1} '.format('', '-')) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 210 | self.sha(split[0] + " ") |
David Pursehouse | e0b6de3 | 2012-11-21 17:36:28 +0900 | [diff] [blame] | 211 | self.text(" ".join(split[1:])) |
Olof Johansson | 33949c3 | 2012-07-10 14:32:23 +0200 | [diff] [blame] | 212 | self.out.nl() |