info.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #
  2. # Copyright (C) 2012 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from command import PagedCommand
  16. from color import Coloring
  17. from error import NoSuchProjectError
  18. from git_refs import R_M
  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, show_smart=True):
  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.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.printer('text')
  46. self.dimtext = self.out.printer('dimtext', attr = 'dim')
  47. self.opt = opt
  48. mergeBranch = self.manifest.manifestProject.config.GetBranch("default").merge
  49. self.heading("Manifest branch: ")
  50. self.headtext(self.manifest.default.revisionExpr)
  51. self.out.nl()
  52. self.heading("Manifest merge branch: ")
  53. self.headtext(mergeBranch)
  54. self.out.nl()
  55. self.printSeparator()
  56. if not opt.overview:
  57. self.printDiffInfo(args)
  58. else:
  59. self.printCommitOverview(args)
  60. def printSeparator(self):
  61. self.text("----------------------------")
  62. self.out.nl()
  63. def printDiffInfo(self, args):
  64. try:
  65. projs = self.GetProjects(args)
  66. except NoSuchProjectError:
  67. return
  68. for p in projs:
  69. self.heading("Project: ")
  70. self.headtext(p.name)
  71. self.out.nl()
  72. self.heading("Mount path: ")
  73. self.headtext(p.worktree)
  74. self.out.nl()
  75. self.heading("Current revision: ")
  76. self.headtext(p.revisionExpr)
  77. self.out.nl()
  78. localBranches = p.GetBranches().keys()
  79. self.heading("Local Branches: ")
  80. self.redtext(str(len(localBranches)))
  81. if len(localBranches) > 0:
  82. self.text(" [")
  83. self.text(", ".join(localBranches))
  84. self.text("]")
  85. self.out.nl()
  86. if self.opt.all:
  87. self.findRemoteLocalDiff(p)
  88. self.printSeparator()
  89. def findRemoteLocalDiff(self, project):
  90. #Fetch all the latest commits
  91. if not self.opt.local:
  92. project.Sync_NetworkHalf(quiet=True, current_branch_only=True)
  93. logTarget = R_M + self.manifest.default.revisionExpr
  94. bareTmp = project.bare_git._bare
  95. project.bare_git._bare = False
  96. localCommits = project.bare_git.rev_list(
  97. '--abbrev=8',
  98. '--abbrev-commit',
  99. '--pretty=oneline',
  100. logTarget + "..",
  101. '--')
  102. originCommits = project.bare_git.rev_list(
  103. '--abbrev=8',
  104. '--abbrev-commit',
  105. '--pretty=oneline',
  106. ".." + logTarget,
  107. '--')
  108. project.bare_git._bare = bareTmp
  109. self.heading("Local Commits: ")
  110. self.redtext(str(len(localCommits)))
  111. self.dimtext(" (on current branch)")
  112. self.out.nl()
  113. for c in localCommits:
  114. split = c.split()
  115. self.sha(split[0] + " ")
  116. self.text(" ".join(split[1:]))
  117. self.out.nl()
  118. self.printSeparator()
  119. self.heading("Remote Commits: ")
  120. self.redtext(str(len(originCommits)))
  121. self.out.nl()
  122. for c in originCommits:
  123. split = c.split()
  124. self.sha(split[0] + " ")
  125. self.text(" ".join(split[1:]))
  126. self.out.nl()
  127. def printCommitOverview(self, args):
  128. all_branches = []
  129. for project in self.GetProjects(args):
  130. br = [project.GetUploadableBranch(x)
  131. for x in project.GetBranches().keys()]
  132. br = [x for x in br if x]
  133. if self.opt.current_branch:
  134. br = [x for x in br if x.name == project.CurrentBranch]
  135. all_branches.extend(br)
  136. if not all_branches:
  137. return
  138. self.out.nl()
  139. self.heading('Projects Overview')
  140. project = None
  141. for branch in all_branches:
  142. if project != branch.project:
  143. project = branch.project
  144. self.out.nl()
  145. self.headtext(project.relpath)
  146. self.out.nl()
  147. commits = branch.commits
  148. date = branch.date
  149. self.text('%s %-33s (%2d commit%s, %s)' % (
  150. branch.name == project.CurrentBranch and '*' or ' ',
  151. branch.name,
  152. len(commits),
  153. len(commits) != 1 and 's' or '',
  154. date))
  155. self.out.nl()
  156. for commit in commits:
  157. split = commit.split()
  158. self.text('{0:38}{1} '.format('','-'))
  159. self.sha(split[0] + " ")
  160. self.text(" ".join(split[1:]))
  161. self.out.nl()