blob: 773f22d4b4887a281fbb0c9db93f28778f4f8f11 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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
16from command import PagedCommand
17
Terence Haddock4655e812011-03-31 12:33:34 +020018try:
19 import threading as _threading
20except ImportError:
21 import dummy_threading as _threading
22
Will Richey63d356f2012-06-21 09:49:59 -040023import glob
David Pursehouse59bbb582013-05-17 10:49:33 +090024
Terence Haddock4655e812011-03-31 12:33:34 +020025import itertools
Will Richey63d356f2012-06-21 09:49:59 -040026import os
Terence Haddock4655e812011-03-31 12:33:34 +020027
Will Richey63d356f2012-06-21 09:49:59 -040028from color import Coloring
Renaud Paquaybed8b622018-09-27 10:46:58 -070029import platform_utils
Will Richey63d356f2012-06-21 09:49:59 -040030
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031class Status(PagedCommand):
32 common = True
33 helpSummary = "Show the working tree status"
34 helpUsage = """
35%prog [<project>...]
36"""
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070037 helpDescription = """
38'%prog' compares the working tree to the staging area (aka index),
39and the most recent commit on this branch (HEAD), in each project
40specified. A summary is displayed, one line per file where there
41is a difference between these three states.
42
Terence Haddock4655e812011-03-31 12:33:34 +020043The -j/--jobs option can be used to run multiple status queries
44in parallel.
45
Will Richey63d356f2012-06-21 09:49:59 -040046The -o/--orphans option can be used to show objects that are in
47the working directory, but not associated with a repo project.
48This includes unmanaged top-level files and directories, but also
49includes deeper items. For example, if dir/subdir/proj1 and
50dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown
51if it is not known to repo.
52
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040053# Status Display
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070054
55The status display is organized into three columns of information,
56for example if the file 'subcmds/status.py' is modified in the
57project 'repo' on branch 'devwork':
58
59 project repo/ branch devwork
60 -m subcmds/status.py
61
62The first column explains how the staging area (index) differs from
63the last commit (HEAD). Its values are always displayed in upper
64case and have the following meanings:
65
66 -: no difference
67 A: added (not in HEAD, in index )
68 M: modified ( in HEAD, in index, different content )
69 D: deleted ( in HEAD, not in index )
70 R: renamed (not in HEAD, in index, path changed )
71 C: copied (not in HEAD, in index, copied from another)
72 T: mode changed ( in HEAD, in index, same content )
73 U: unmerged; conflict resolution required
74
75The second column explains how the working directory differs from
76the index. Its values are always displayed in lower case and have
77the following meanings:
78
79 -: new / unknown (not in index, in work tree )
80 m: modified ( in index, in work tree, modified )
81 d: deleted ( in index, not in work tree )
82
83"""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084
Terence Haddock4655e812011-03-31 12:33:34 +020085 def _Options(self, p):
86 p.add_option('-j', '--jobs',
87 dest='jobs', action='store', type='int', default=2,
88 help="number of projects to check simultaneously")
Will Richey63d356f2012-06-21 09:49:59 -040089 p.add_option('-o', '--orphans',
90 dest='orphans', action='store_true',
91 help="include objects in working directory outside of repo projects")
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060092 p.add_option('-q', '--quiet', action='store_true',
93 help="only print the name of modified projects")
Terence Haddock4655e812011-03-31 12:33:34 +020094
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060095 def _StatusHelper(self, project, clean_counter, sem, quiet):
Terence Haddock4655e812011-03-31 12:33:34 +020096 """Obtains the status for a specific project.
97
98 Obtains the status for a project, redirecting the output to
99 the specified object. It will release the semaphore
100 when done.
101
102 Args:
103 project: Project to get status of.
104 clean_counter: Counter for clean projects.
105 sem: Semaphore, will call release() when complete.
106 output: Where to output the status.
107 """
108 try:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600109 state = project.PrintWorkTreeStatus(quiet=quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200110 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100111 next(clean_counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200112 finally:
113 sem.release()
114
Will Richey63d356f2012-06-21 09:49:59 -0400115 def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
116 """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
117 status_header = ' --\t'
118 for item in dirs:
Renaud Paquaybed8b622018-09-27 10:46:58 -0700119 if not platform_utils.isdir(item):
Anthony Kingb51f07c2015-04-04 21:18:59 +0100120 outstring.append(''.join([status_header, item]))
Will Richey63d356f2012-06-21 09:49:59 -0400121 continue
122 if item in proj_dirs:
123 continue
124 if item in proj_dirs_parents:
Anthony Kingb51f07c2015-04-04 21:18:59 +0100125 self._FindOrphans(glob.glob('%s/.*' % item) +
126 glob.glob('%s/*' % item),
Will Richey63d356f2012-06-21 09:49:59 -0400127 proj_dirs, proj_dirs_parents, outstring)
128 continue
Anthony Kingb51f07c2015-04-04 21:18:59 +0100129 outstring.append(''.join([status_header, item, '/']))
Will Richey63d356f2012-06-21 09:49:59 -0400130
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900132 all_projects = self.GetProjects(args)
Terence Haddock4655e812011-03-31 12:33:34 +0200133 counter = itertools.count()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700134
Terence Haddock4655e812011-03-31 12:33:34 +0200135 if opt.jobs == 1:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900136 for project in all_projects:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600137 state = project.PrintWorkTreeStatus(quiet=opt.quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200138 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100139 next(counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200140 else:
141 sem = _threading.Semaphore(opt.jobs)
Anthony Kingb51f07c2015-04-04 21:18:59 +0100142 threads = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900143 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200144 sem.acquire()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200145
Terence Haddock4655e812011-03-31 12:33:34 +0200146 t = _threading.Thread(target=self._StatusHelper,
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600147 args=(project, counter, sem, opt.quiet))
Anthony Kingb51f07c2015-04-04 21:18:59 +0100148 threads.append(t)
David 'Digit' Turnere2126652012-09-05 10:35:06 +0200149 t.daemon = True
Terence Haddock4655e812011-03-31 12:33:34 +0200150 t.start()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100151 for t in threads:
Terence Haddock4655e812011-03-31 12:33:34 +0200152 t.join()
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600153 if not opt.quiet and len(all_projects) == next(counter):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700154 print('nothing to commit (working directory clean)')
Will Richey63d356f2012-06-21 09:49:59 -0400155
156 if opt.orphans:
157 proj_dirs = set()
158 proj_dirs_parents = set()
159 for project in self.GetProjects(None, missing_ok=True):
160 proj_dirs.add(project.relpath)
161 (head, _tail) = os.path.split(project.relpath)
162 while head != "":
163 proj_dirs_parents.add(head)
164 (head, _tail) = os.path.split(head)
165 proj_dirs.add('.repo')
166
167 class StatusColoring(Coloring):
168 def __init__(self, config):
169 Coloring.__init__(self, config, 'status')
170 self.project = self.printer('header', attr = 'bold')
171 self.untracked = self.printer('untracked', fg = 'red')
172
173 orig_path = os.getcwd()
174 try:
175 os.chdir(self.manifest.topdir)
176
Anthony Kingb51f07c2015-04-04 21:18:59 +0100177 outstring = []
178 self._FindOrphans(glob.glob('.*') +
179 glob.glob('*'),
Will Richey63d356f2012-06-21 09:49:59 -0400180 proj_dirs, proj_dirs_parents, outstring)
181
Anthony Kingb51f07c2015-04-04 21:18:59 +0100182 if outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400183 output = StatusColoring(self.manifest.globalConfig)
184 output.project('Objects not within a project (orphans)')
185 output.nl()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100186 for entry in outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400187 output.untracked(entry)
188 output.nl()
189 else:
190 print('No orphan files or directories')
191
Will Richey63d356f2012-06-21 09:49:59 -0400192 finally:
193 # Restore CWD.
194 os.chdir(orig_path)