blob: 9810337f1329735833c30f7d4331b387871f9912 [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
Chirayu Desai217ea7d2013-03-01 19:14:38 +053024try:
25 # For python2
26 import StringIO as io
27except ImportError:
28 # For python3
29 import io
Terence Haddock4655e812011-03-31 12:33:34 +020030import itertools
Will Richey63d356f2012-06-21 09:49:59 -040031import os
Terence Haddock4655e812011-03-31 12:33:34 +020032import sys
Terence Haddock4655e812011-03-31 12:33:34 +020033
Will Richey63d356f2012-06-21 09:49:59 -040034from color import Coloring
35
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070036class Status(PagedCommand):
37 common = True
38 helpSummary = "Show the working tree status"
39 helpUsage = """
40%prog [<project>...]
41"""
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070042 helpDescription = """
43'%prog' compares the working tree to the staging area (aka index),
44and the most recent commit on this branch (HEAD), in each project
45specified. A summary is displayed, one line per file where there
46is a difference between these three states.
47
Terence Haddock4655e812011-03-31 12:33:34 +020048The -j/--jobs option can be used to run multiple status queries
49in parallel.
50
Will Richey63d356f2012-06-21 09:49:59 -040051The -o/--orphans option can be used to show objects that are in
52the working directory, but not associated with a repo project.
53This includes unmanaged top-level files and directories, but also
54includes deeper items. For example, if dir/subdir/proj1 and
55dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown
56if it is not known to repo.
57
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070058Status Display
59--------------
60
61The status display is organized into three columns of information,
62for example if the file 'subcmds/status.py' is modified in the
63project 'repo' on branch 'devwork':
64
65 project repo/ branch devwork
66 -m subcmds/status.py
67
68The first column explains how the staging area (index) differs from
69the last commit (HEAD). Its values are always displayed in upper
70case and have the following meanings:
71
72 -: no difference
73 A: added (not in HEAD, in index )
74 M: modified ( in HEAD, in index, different content )
75 D: deleted ( in HEAD, not in index )
76 R: renamed (not in HEAD, in index, path changed )
77 C: copied (not in HEAD, in index, copied from another)
78 T: mode changed ( in HEAD, in index, same content )
79 U: unmerged; conflict resolution required
80
81The second column explains how the working directory differs from
82the index. Its values are always displayed in lower case and have
83the following meanings:
84
85 -: new / unknown (not in index, in work tree )
86 m: modified ( in index, in work tree, modified )
87 d: deleted ( in index, not in work tree )
88
89"""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070090
Terence Haddock4655e812011-03-31 12:33:34 +020091 def _Options(self, p):
92 p.add_option('-j', '--jobs',
93 dest='jobs', action='store', type='int', default=2,
94 help="number of projects to check simultaneously")
Will Richey63d356f2012-06-21 09:49:59 -040095 p.add_option('-o', '--orphans',
96 dest='orphans', action='store_true',
97 help="include objects in working directory outside of repo projects")
Terence Haddock4655e812011-03-31 12:33:34 +020098
99 def _StatusHelper(self, project, clean_counter, sem, output):
100 """Obtains the status for a specific project.
101
102 Obtains the status for a project, redirecting the output to
103 the specified object. It will release the semaphore
104 when done.
105
106 Args:
107 project: Project to get status of.
108 clean_counter: Counter for clean projects.
109 sem: Semaphore, will call release() when complete.
110 output: Where to output the status.
111 """
112 try:
113 state = project.PrintWorkTreeStatus(output)
114 if state == 'CLEAN':
115 clean_counter.next()
116 finally:
117 sem.release()
118
Will Richey63d356f2012-06-21 09:49:59 -0400119 def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
120 """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
121 status_header = ' --\t'
122 for item in dirs:
123 if not os.path.isdir(item):
124 outstring.write(''.join([status_header, item]))
125 continue
126 if item in proj_dirs:
127 continue
128 if item in proj_dirs_parents:
129 self._FindOrphans(glob.glob('%s/.*' % item) + \
130 glob.glob('%s/*' % item), \
131 proj_dirs, proj_dirs_parents, outstring)
132 continue
133 outstring.write(''.join([status_header, item, '/']))
134
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700135 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900136 all_projects = self.GetProjects(args)
Terence Haddock4655e812011-03-31 12:33:34 +0200137 counter = itertools.count()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700138
Terence Haddock4655e812011-03-31 12:33:34 +0200139 if opt.jobs == 1:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900140 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200141 state = project.PrintWorkTreeStatus()
142 if state == 'CLEAN':
143 counter.next()
144 else:
145 sem = _threading.Semaphore(opt.jobs)
146 threads_and_output = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900147 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200148 sem.acquire()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200149
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530150 class BufList(io.StringIO):
Cezary Baginskiccf86432012-04-23 23:55:35 +0200151 def dump(self, ostream):
152 for entry in self.buflist:
153 ostream.write(entry)
154
155 output = BufList()
156
Terence Haddock4655e812011-03-31 12:33:34 +0200157 t = _threading.Thread(target=self._StatusHelper,
158 args=(project, counter, sem, output))
159 threads_and_output.append((t, output))
David 'Digit' Turnere2126652012-09-05 10:35:06 +0200160 t.daemon = True
Terence Haddock4655e812011-03-31 12:33:34 +0200161 t.start()
162 for (t, output) in threads_and_output:
163 t.join()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200164 output.dump(sys.stdout)
Terence Haddock4655e812011-03-31 12:33:34 +0200165 output.close()
David Pursehouse8a68ff92012-09-24 12:15:13 +0900166 if len(all_projects) == counter.next():
Sarah Owenscecd1d82012-11-01 22:59:27 -0700167 print('nothing to commit (working directory clean)')
Will Richey63d356f2012-06-21 09:49:59 -0400168
169 if opt.orphans:
170 proj_dirs = set()
171 proj_dirs_parents = set()
172 for project in self.GetProjects(None, missing_ok=True):
173 proj_dirs.add(project.relpath)
174 (head, _tail) = os.path.split(project.relpath)
175 while head != "":
176 proj_dirs_parents.add(head)
177 (head, _tail) = os.path.split(head)
178 proj_dirs.add('.repo')
179
180 class StatusColoring(Coloring):
181 def __init__(self, config):
182 Coloring.__init__(self, config, 'status')
183 self.project = self.printer('header', attr = 'bold')
184 self.untracked = self.printer('untracked', fg = 'red')
185
186 orig_path = os.getcwd()
187 try:
188 os.chdir(self.manifest.topdir)
189
Chirayu Desai217ea7d2013-03-01 19:14:38 +0530190 outstring = io.StringIO()
Will Richey63d356f2012-06-21 09:49:59 -0400191 self._FindOrphans(glob.glob('.*') + \
192 glob.glob('*'), \
193 proj_dirs, proj_dirs_parents, outstring)
194
195 if outstring.buflist:
196 output = StatusColoring(self.manifest.globalConfig)
197 output.project('Objects not within a project (orphans)')
198 output.nl()
199 for entry in outstring.buflist:
200 output.untracked(entry)
201 output.nl()
202 else:
203 print('No orphan files or directories')
204
205 outstring.close()
206
207 finally:
208 # Restore CWD.
209 os.chdir(orig_path)