status.py 6.8 KB

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