branches.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #
  2. # Copyright (C) 2009 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. import sys
  16. from color import Coloring
  17. from command import Command
  18. class BranchColoring(Coloring):
  19. def __init__(self, config):
  20. Coloring.__init__(self, config, 'branch')
  21. self.current = self.printer('current', fg='green')
  22. self.local = self.printer('local')
  23. self.notinproject = self.printer('notinproject', fg='red')
  24. class BranchInfo(object):
  25. def __init__(self, name):
  26. self.name = name
  27. self.current = 0
  28. self.published = 0
  29. self.published_equal = 0
  30. self.projects = []
  31. def add(self, b):
  32. if b.current:
  33. self.current += 1
  34. if b.published:
  35. self.published += 1
  36. if b.revision == b.published:
  37. self.published_equal += 1
  38. self.projects.append(b)
  39. @property
  40. def IsCurrent(self):
  41. return self.current > 0
  42. @property
  43. def IsPublished(self):
  44. return self.published > 0
  45. @property
  46. def IsPublishedEqual(self):
  47. return self.published_equal == len(self.projects)
  48. class Branches(Command):
  49. common = True
  50. helpSummary = "View current topic branches"
  51. helpUsage = """
  52. %prog [<project>...]
  53. Summarizes the currently available topic branches.
  54. """
  55. def Execute(self, opt, args):
  56. projects = self.GetProjects(args)
  57. out = BranchColoring(self.manifest.manifestProject.config)
  58. all = {}
  59. project_cnt = len(projects)
  60. for project in projects:
  61. for name, b in project.GetBranches().iteritems():
  62. b.project = project
  63. if name not in all:
  64. all[name] = BranchInfo(name)
  65. all[name].add(b)
  66. names = all.keys()
  67. names.sort()
  68. if not names:
  69. print >>sys.stderr, ' (no branches)'
  70. return
  71. width = 25
  72. for name in names:
  73. if width < len(name):
  74. width = len(name)
  75. for name in names:
  76. i = all[name]
  77. in_cnt = len(i.projects)
  78. if i.IsCurrent:
  79. current = '*'
  80. hdr = out.current
  81. else:
  82. current = ' '
  83. hdr = out.local
  84. if i.IsPublishedEqual:
  85. published = 'P'
  86. elif i.IsPublished:
  87. published = 'p'
  88. else:
  89. published = ' '
  90. hdr('%c%c %-*s' % (current, published, width, name))
  91. out.write(' |')
  92. if in_cnt < project_cnt and (in_cnt == 1):
  93. fmt = out.write
  94. paths = []
  95. if in_cnt < project_cnt - in_cnt:
  96. type = 'in'
  97. for b in i.projects:
  98. paths.append(b.project.relpath)
  99. else:
  100. fmt = out.notinproject
  101. type = 'not in'
  102. have = set()
  103. for b in i.projects:
  104. have.add(b.project)
  105. for p in projects:
  106. paths.append(p.relpath)
  107. s = ' %s %s' % (type, ', '.join(paths))
  108. if width + 7 + len(s) < 80:
  109. fmt(s)
  110. else:
  111. out.nl()
  112. fmt(' %s:' % type)
  113. for p in paths:
  114. out.nl()
  115. fmt(' %s' % p)
  116. out.nl()