diffmanifests.py 7.4 KB

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