status.py 6.7 KB

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