info.py 6.0 KB

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