status.py 6.1 KB

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