diffmanifests.py 7.4 KB

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