status.py 6.8 KB

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