info.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding:utf-8 -*-
  2. #
  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. from command import PagedCommand
  17. from color import Coloring
  18. from git_refs import R_M, R_HEADS
  19. class _Coloring(Coloring):
  20. def __init__(self, config):
  21. Coloring.__init__(self, config, "status")
  22. class Info(PagedCommand):
  23. common = True
  24. helpSummary = "Get info on the manifest branch, current branch or unmerged branches"
  25. helpUsage = "%prog [-dl] [-o [-b]] [<project>...]"
  26. def _Options(self, p):
  27. p.add_option('-d', '--diff',
  28. dest='all', action='store_true',
  29. help="show full info and commit diff including remote branches")
  30. p.add_option('-o', '--overview',
  31. dest='overview', action='store_true',
  32. help='show overview of all local commits')
  33. p.add_option('-b', '--current-branch',
  34. dest="current_branch", action="store_true",
  35. help="consider only checked out branches")
  36. p.add_option('-l', '--local-only',
  37. dest="local", action="store_true",
  38. help="Disable all remote operations")
  39. def Execute(self, opt, args):
  40. self.out = _Coloring(self.manifest.globalConfig)
  41. self.heading = self.out.printer('heading', attr='bold')
  42. self.headtext = self.out.nofmt_printer('headtext', fg='yellow')
  43. self.redtext = self.out.printer('redtext', fg='red')
  44. self.sha = self.out.printer("sha", fg='yellow')
  45. self.text = self.out.nofmt_printer('text')
  46. self.dimtext = self.out.printer('dimtext', attr='dim')
  47. self.opt = opt
  48. manifestConfig = self.manifest.manifestProject.config
  49. mergeBranch = manifestConfig.GetBranch("default").merge
  50. manifestGroups = (manifestConfig.GetString('manifest.groups')
  51. or 'all,-notdefault')
  52. self.heading("Manifest branch: ")
  53. if self.manifest.default.revisionExpr:
  54. self.headtext(self.manifest.default.revisionExpr)
  55. self.out.nl()
  56. self.heading("Manifest merge branch: ")
  57. self.headtext(mergeBranch)
  58. self.out.nl()
  59. self.heading("Manifest groups: ")
  60. self.headtext(manifestGroups)
  61. self.out.nl()
  62. self.printSeparator()
  63. if not opt.overview:
  64. self.printDiffInfo(args)
  65. else:
  66. self.printCommitOverview(args)
  67. def printSeparator(self):
  68. self.text("----------------------------")
  69. self.out.nl()
  70. def printDiffInfo(self, args):
  71. # We let exceptions bubble up to main as they'll be well structured.
  72. projs = self.GetProjects(args)
  73. for p in projs:
  74. self.heading("Project: ")
  75. self.headtext(p.name)
  76. self.out.nl()
  77. self.heading("Mount path: ")
  78. self.headtext(p.worktree)
  79. self.out.nl()
  80. self.heading("Current revision: ")
  81. self.headtext(p.GetRevisionId())
  82. self.out.nl()
  83. currentBranch = p.CurrentBranch
  84. if currentBranch:
  85. self.heading('Current branch: ')
  86. self.headtext(currentBranch)
  87. self.out.nl()
  88. self.heading("Manifest revision: ")
  89. self.headtext(p.revisionExpr)
  90. self.out.nl()
  91. localBranches = list(p.GetBranches().keys())
  92. self.heading("Local Branches: ")
  93. self.redtext(str(len(localBranches)))
  94. if localBranches:
  95. self.text(" [")
  96. self.text(", ".join(localBranches))
  97. self.text("]")
  98. self.out.nl()
  99. if self.opt.all:
  100. self.findRemoteLocalDiff(p)
  101. self.printSeparator()
  102. def findRemoteLocalDiff(self, project):
  103. # Fetch all the latest commits.
  104. if not self.opt.local:
  105. project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
  106. branch = self.manifest.manifestProject.config.GetBranch('default').merge
  107. if branch.startswith(R_HEADS):
  108. branch = branch[len(R_HEADS):]
  109. logTarget = R_M + branch
  110. bareTmp = project.bare_git._bare
  111. project.bare_git._bare = False
  112. localCommits = project.bare_git.rev_list(
  113. '--abbrev=8',
  114. '--abbrev-commit',
  115. '--pretty=oneline',
  116. logTarget + "..",
  117. '--')
  118. originCommits = project.bare_git.rev_list(
  119. '--abbrev=8',
  120. '--abbrev-commit',
  121. '--pretty=oneline',
  122. ".." + logTarget,
  123. '--')
  124. project.bare_git._bare = bareTmp
  125. self.heading("Local Commits: ")
  126. self.redtext(str(len(localCommits)))
  127. self.dimtext(" (on current branch)")
  128. self.out.nl()
  129. for c in localCommits:
  130. split = c.split()
  131. self.sha(split[0] + " ")
  132. self.text(" ".join(split[1:]))
  133. self.out.nl()
  134. self.printSeparator()
  135. self.heading("Remote Commits: ")
  136. self.redtext(str(len(originCommits)))
  137. self.out.nl()
  138. for c in originCommits:
  139. split = c.split()
  140. self.sha(split[0] + " ")
  141. self.text(" ".join(split[1:]))
  142. self.out.nl()
  143. def printCommitOverview(self, args):
  144. all_branches = []
  145. for project in self.GetProjects(args):
  146. br = [project.GetUploadableBranch(x)
  147. for x in project.GetBranches()]
  148. br = [x for x in br if x]
  149. if self.opt.current_branch:
  150. br = [x for x in br if x.name == project.CurrentBranch]
  151. all_branches.extend(br)
  152. if not all_branches:
  153. return
  154. self.out.nl()
  155. self.heading('Projects Overview')
  156. project = None
  157. for branch in all_branches:
  158. if project != branch.project:
  159. project = branch.project
  160. self.out.nl()
  161. self.headtext(project.relpath)
  162. self.out.nl()
  163. commits = branch.commits
  164. date = branch.date
  165. self.text('%s %-33s (%2d commit%s, %s)' % (
  166. branch.name == project.CurrentBranch and '*' or ' ',
  167. branch.name,
  168. len(commits),
  169. len(commits) != 1 and 's' or '',
  170. date))
  171. self.out.nl()
  172. for commit in commits:
  173. split = commit.split()
  174. self.text('{0:38}{1} '.format('', '-'))
  175. self.sha(split[0] + " ")
  176. self.text(" ".join(split[1:]))
  177. self.out.nl()