status.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2008 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 __future__ import print_function
  17. import functools
  18. import glob
  19. import multiprocessing
  20. import os
  21. from command import PagedCommand
  22. from color import Coloring
  23. import platform_utils
  24. class Status(PagedCommand):
  25. common = True
  26. helpSummary = "Show the working tree status"
  27. helpUsage = """
  28. %prog [<project>...]
  29. """
  30. helpDescription = """
  31. '%prog' compares the working tree to the staging area (aka index),
  32. and the most recent commit on this branch (HEAD), in each project
  33. specified. A summary is displayed, one line per file where there
  34. is a difference between these three states.
  35. The -j/--jobs option can be used to run multiple status queries
  36. in parallel.
  37. The -o/--orphans option can be used to show objects that are in
  38. the working directory, but not associated with a repo project.
  39. This includes unmanaged top-level files and directories, but also
  40. includes deeper items. For example, if dir/subdir/proj1 and
  41. dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown
  42. if it is not known to repo.
  43. # Status Display
  44. The status display is organized into three columns of information,
  45. for example if the file 'subcmds/status.py' is modified in the
  46. project 'repo' on branch 'devwork':
  47. project repo/ branch devwork
  48. -m subcmds/status.py
  49. The first column explains how the staging area (index) differs from
  50. the last commit (HEAD). Its values are always displayed in upper
  51. case and have the following meanings:
  52. -: no difference
  53. A: added (not in HEAD, in index )
  54. M: modified ( in HEAD, in index, different content )
  55. D: deleted ( in HEAD, not in index )
  56. R: renamed (not in HEAD, in index, path changed )
  57. C: copied (not in HEAD, in index, copied from another)
  58. T: mode changed ( in HEAD, in index, same content )
  59. U: unmerged; conflict resolution required
  60. The second column explains how the working directory differs from
  61. the index. Its values are always displayed in lower case and have
  62. the following meanings:
  63. -: new / unknown (not in index, in work tree )
  64. m: modified ( in index, in work tree, modified )
  65. d: deleted ( in index, not in work tree )
  66. """
  67. def _Options(self, p):
  68. p.add_option('-j', '--jobs',
  69. dest='jobs', action='store', type='int', default=2,
  70. help="number of projects to check simultaneously")
  71. p.add_option('-o', '--orphans',
  72. dest='orphans', action='store_true',
  73. help="include objects in working directory outside of repo projects")
  74. p.add_option('-q', '--quiet', action='store_true',
  75. help="only print the name of modified projects")
  76. def _StatusHelper(self, quiet, project):
  77. """Obtains the status for a specific project.
  78. Obtains the status for a project, redirecting the output to
  79. the specified object.
  80. Args:
  81. quiet: Where to output the status.
  82. project: Project to get status of.
  83. Returns:
  84. The status of the project.
  85. """
  86. return project.PrintWorkTreeStatus(quiet=quiet)
  87. def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
  88. """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
  89. status_header = ' --\t'
  90. for item in dirs:
  91. if not platform_utils.isdir(item):
  92. outstring.append(''.join([status_header, item]))
  93. continue
  94. if item in proj_dirs:
  95. continue
  96. if item in proj_dirs_parents:
  97. self._FindOrphans(glob.glob('%s/.*' % item) +
  98. glob.glob('%s/*' % item),
  99. proj_dirs, proj_dirs_parents, outstring)
  100. continue
  101. outstring.append(''.join([status_header, item, '/']))
  102. def Execute(self, opt, args):
  103. all_projects = self.GetProjects(args)
  104. counter = 0
  105. if opt.jobs == 1:
  106. for project in all_projects:
  107. state = project.PrintWorkTreeStatus(quiet=opt.quiet)
  108. if state == 'CLEAN':
  109. counter += 1
  110. else:
  111. with multiprocessing.Pool(opt.jobs) as pool:
  112. states = pool.map(functools.partial(self._StatusHelper, opt.quiet), all_projects)
  113. counter += states.count('CLEAN')
  114. if not opt.quiet and len(all_projects) == counter:
  115. print('nothing to commit (working directory clean)')
  116. if opt.orphans:
  117. proj_dirs = set()
  118. proj_dirs_parents = set()
  119. for project in self.GetProjects(None, missing_ok=True):
  120. proj_dirs.add(project.relpath)
  121. (head, _tail) = os.path.split(project.relpath)
  122. while head != "":
  123. proj_dirs_parents.add(head)
  124. (head, _tail) = os.path.split(head)
  125. proj_dirs.add('.repo')
  126. class StatusColoring(Coloring):
  127. def __init__(self, config):
  128. Coloring.__init__(self, config, 'status')
  129. self.project = self.printer('header', attr='bold')
  130. self.untracked = self.printer('untracked', fg='red')
  131. orig_path = os.getcwd()
  132. try:
  133. os.chdir(self.manifest.topdir)
  134. outstring = []
  135. self._FindOrphans(glob.glob('.*') +
  136. glob.glob('*'),
  137. proj_dirs, proj_dirs_parents, outstring)
  138. if outstring:
  139. output = StatusColoring(self.manifest.globalConfig)
  140. output.project('Objects not within a project (orphans)')
  141. output.nl()
  142. for entry in outstring:
  143. output.untracked(entry)
  144. output.nl()
  145. else:
  146. print('No orphan files or directories')
  147. finally:
  148. # Restore CWD.
  149. os.chdir(orig_path)