status.py 6.5 KB

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