diffmanifests.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #
  2. # Copyright (C) 2014 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 color import Coloring
  16. from command import PagedCommand
  17. from manifest_xml import XmlManifest
  18. class _Coloring(Coloring):
  19. def __init__(self, config):
  20. Coloring.__init__(self, config, "status")
  21. class Diffmanifests(PagedCommand):
  22. """ A command to see logs in projects represented by manifests
  23. This is used to see deeper differences between manifests. Where a simple
  24. diff would only show a diff of sha1s for example, this command will display
  25. the logs of the project between both sha1s, allowing user to see diff at a
  26. deeper level.
  27. """
  28. common = True
  29. helpSummary = "Manifest diff utility"
  30. helpUsage = """%prog manifest1.xml [manifest2.xml] [options]"""
  31. helpDescription = """
  32. The %prog command shows differences between project revisions of manifest1 and
  33. manifest2. if manifest2 is not specified, current manifest.xml will be used
  34. instead. Both absolute and relative paths may be used for manifests. Relative
  35. paths start from project's ".repo/manifests" folder.
  36. The --raw option Displays the diff in a way that facilitates parsing, the
  37. project pattern will be <status> <path> <revision from> [<revision to>] and the
  38. commit pattern will be <status> <onelined log> with status values respectively :
  39. A = Added project
  40. R = Removed project
  41. C = Changed project
  42. U = Project with unreachable revision(s) (revision(s) not found)
  43. for project, and
  44. A = Added commit
  45. R = Removed commit
  46. for a commit.
  47. Only changed projects may contain commits, and commit status always starts with
  48. a space, and are part of last printed project.
  49. Unreachable revisions may occur if project is not up to date or if repo has not
  50. been initialized with all the groups, in which case some projects won't be
  51. synced and their revisions won't be found.
  52. """
  53. def _Options(self, p):
  54. p.add_option('--raw',
  55. dest='raw', action='store_true',
  56. help='Display raw diff.')
  57. p.add_option('--no-color',
  58. dest='color', action='store_false', default=True,
  59. help='does not display the diff in color.')
  60. def _printRawDiff(self, diff):
  61. for project in diff['added']:
  62. self.printText("A %s %s" % (project.relpath, project.revisionExpr))
  63. self.out.nl()
  64. for project in diff['removed']:
  65. self.printText("R %s %s" % (project.relpath, project.revisionExpr))
  66. self.out.nl()
  67. for project, otherProject in diff['changed']:
  68. self.printText("C %s %s %s" % (project.relpath, project.revisionExpr,
  69. otherProject.revisionExpr))
  70. self.out.nl()
  71. self._printLogs(project, otherProject, raw=True, color=False)
  72. for project, otherProject in diff['unreachable']:
  73. self.printText("U %s %s %s" % (project.relpath, project.revisionExpr,
  74. otherProject.revisionExpr))
  75. self.out.nl()
  76. def _printDiff(self, diff, color=True):
  77. if diff['added']:
  78. self.out.nl()
  79. self.printText('added projects : \n')
  80. self.out.nl()
  81. for project in diff['added']:
  82. self.printProject('\t%s' % (project.relpath))
  83. self.printText(' at revision ')
  84. self.printRevision(project.revisionExpr)
  85. self.out.nl()
  86. if diff['removed']:
  87. self.out.nl()
  88. self.printText('removed projects : \n')
  89. self.out.nl()
  90. for project in diff['removed']:
  91. self.printProject('\t%s' % (project.relpath))
  92. self.printText(' at revision ')
  93. self.printRevision(project.revisionExpr)
  94. self.out.nl()
  95. if diff['changed']:
  96. self.out.nl()
  97. self.printText('changed projects : \n')
  98. self.out.nl()
  99. for project, otherProject in diff['changed']:
  100. self.printProject('\t%s' % (project.relpath))
  101. self.printText(' changed from ')
  102. self.printRevision(project.revisionExpr)
  103. self.printText(' to ')
  104. self.printRevision(otherProject.revisionExpr)
  105. self.out.nl()
  106. self._printLogs(project, otherProject, raw=False, color=color)
  107. self.out.nl()
  108. if diff['unreachable']:
  109. self.out.nl()
  110. self.printText('projects with unreachable revisions : \n')
  111. self.out.nl()
  112. for project, otherProject in diff['unreachable']:
  113. self.printProject('\t%s ' % (project.relpath))
  114. self.printRevision(project.revisionExpr)
  115. self.printText(' or ')
  116. self.printRevision(otherProject.revisionExpr)
  117. self.printText(' not found')
  118. self.out.nl()
  119. def _printLogs(self, project, otherProject, raw=False, color=True):
  120. logs = project.getAddedAndRemovedLogs(otherProject, oneline=True,
  121. color=color)
  122. if logs['removed']:
  123. removedLogs = logs['removed'].split('\n')
  124. for log in removedLogs:
  125. if log.strip():
  126. if raw:
  127. self.printText(' R ' + log)
  128. self.out.nl()
  129. else:
  130. self.printRemoved('\t\t[-] ')
  131. self.printText(log)
  132. self.out.nl()
  133. if logs['added']:
  134. addedLogs = logs['added'].split('\n')
  135. for log in addedLogs:
  136. if log.strip():
  137. if raw:
  138. self.printText(' A ' + log)
  139. self.out.nl()
  140. else:
  141. self.printAdded('\t\t[+] ')
  142. self.printText(log)
  143. self.out.nl()
  144. def Execute(self, opt, args):
  145. if not args or len(args) > 2:
  146. self.Usage()
  147. self.out = _Coloring(self.manifest.globalConfig)
  148. self.printText = self.out.nofmt_printer('text')
  149. if opt.color:
  150. self.printProject = self.out.nofmt_printer('project', attr = 'bold')
  151. self.printAdded = self.out.nofmt_printer('green', fg = 'green', attr = 'bold')
  152. self.printRemoved = self.out.nofmt_printer('red', fg = 'red', attr = 'bold')
  153. self.printRevision = self.out.nofmt_printer('revision', fg = 'yellow')
  154. else:
  155. self.printProject = self.printAdded = self.printRemoved = self.printRevision = self.printText
  156. manifest1 = XmlManifest(self.manifest.repodir)
  157. manifest1.Override(args[0])
  158. if len(args) == 1:
  159. manifest2 = self.manifest
  160. else:
  161. manifest2 = XmlManifest(self.manifest.repodir)
  162. manifest2.Override(args[1])
  163. diff = manifest1.projectsDiff(manifest2)
  164. if opt.raw:
  165. self._printRawDiff(diff)
  166. else:
  167. self._printDiff(diff, color=opt.color)