status.py 6.6 KB

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