blob: cce00c8103014ba1b8f4f773ad26ab9cf3dde32b [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
Terence Haddock4655e812011-03-31 12:33:34 +020024import itertools
Will Richey63d356f2012-06-21 09:49:59 -040025import os
Terence Haddock4655e812011-03-31 12:33:34 +020026import sys
27import StringIO
28
Will Richey63d356f2012-06-21 09:49:59 -040029from color import Coloring
30
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
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070053Status Display
54--------------
55
56The status display is organized into three columns of information,
57for example if the file 'subcmds/status.py' is modified in the
58project 'repo' on branch 'devwork':
59
60 project repo/ branch devwork
61 -m subcmds/status.py
62
63The first column explains how the staging area (index) differs from
64the last commit (HEAD). Its values are always displayed in upper
65case and have the following meanings:
66
67 -: no difference
68 A: added (not in HEAD, in index )
69 M: modified ( in HEAD, in index, different content )
70 D: deleted ( in HEAD, not in index )
71 R: renamed (not in HEAD, in index, path changed )
72 C: copied (not in HEAD, in index, copied from another)
73 T: mode changed ( in HEAD, in index, same content )
74 U: unmerged; conflict resolution required
75
76The second column explains how the working directory differs from
77the index. Its values are always displayed in lower case and have
78the following meanings:
79
80 -: new / unknown (not in index, in work tree )
81 m: modified ( in index, in work tree, modified )
82 d: deleted ( in index, not in work tree )
83
84"""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070085
Terence Haddock4655e812011-03-31 12:33:34 +020086 def _Options(self, p):
87 p.add_option('-j', '--jobs',
88 dest='jobs', action='store', type='int', default=2,
89 help="number of projects to check simultaneously")
Will Richey63d356f2012-06-21 09:49:59 -040090 p.add_option('-o', '--orphans',
91 dest='orphans', action='store_true',
92 help="include objects in working directory outside of repo projects")
Terence Haddock4655e812011-03-31 12:33:34 +020093
94 def _StatusHelper(self, project, clean_counter, sem, output):
95 """Obtains the status for a specific project.
96
97 Obtains the status for a project, redirecting the output to
98 the specified object. It will release the semaphore
99 when done.
100
101 Args:
102 project: Project to get status of.
103 clean_counter: Counter for clean projects.
104 sem: Semaphore, will call release() when complete.
105 output: Where to output the status.
106 """
107 try:
108 state = project.PrintWorkTreeStatus(output)
109 if state == 'CLEAN':
110 clean_counter.next()
111 finally:
112 sem.release()
113
Will Richey63d356f2012-06-21 09:49:59 -0400114 def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
115 """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
116 status_header = ' --\t'
117 for item in dirs:
118 if not os.path.isdir(item):
119 outstring.write(''.join([status_header, item]))
120 continue
121 if item in proj_dirs:
122 continue
123 if item in proj_dirs_parents:
124 self._FindOrphans(glob.glob('%s/.*' % item) + \
125 glob.glob('%s/*' % item), \
126 proj_dirs, proj_dirs_parents, outstring)
127 continue
128 outstring.write(''.join([status_header, item, '/']))
129
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900131 all_projects = self.GetProjects(args)
Terence Haddock4655e812011-03-31 12:33:34 +0200132 counter = itertools.count()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700133
Terence Haddock4655e812011-03-31 12:33:34 +0200134 if opt.jobs == 1:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900135 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200136 state = project.PrintWorkTreeStatus()
137 if state == 'CLEAN':
138 counter.next()
139 else:
140 sem = _threading.Semaphore(opt.jobs)
141 threads_and_output = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900142 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200143 sem.acquire()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200144
145 class BufList(StringIO.StringIO):
146 def dump(self, ostream):
147 for entry in self.buflist:
148 ostream.write(entry)
149
150 output = BufList()
151
Terence Haddock4655e812011-03-31 12:33:34 +0200152 t = _threading.Thread(target=self._StatusHelper,
153 args=(project, counter, sem, output))
154 threads_and_output.append((t, output))
David 'Digit' Turnere2126652012-09-05 10:35:06 +0200155 t.daemon = True
Terence Haddock4655e812011-03-31 12:33:34 +0200156 t.start()
157 for (t, output) in threads_and_output:
158 t.join()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200159 output.dump(sys.stdout)
Terence Haddock4655e812011-03-31 12:33:34 +0200160 output.close()
David Pursehouse8a68ff92012-09-24 12:15:13 +0900161 if len(all_projects) == counter.next():
Sarah Owenscecd1d82012-11-01 22:59:27 -0700162 print('nothing to commit (working directory clean)')
Will Richey63d356f2012-06-21 09:49:59 -0400163
164 if opt.orphans:
165 proj_dirs = set()
166 proj_dirs_parents = set()
167 for project in self.GetProjects(None, missing_ok=True):
168 proj_dirs.add(project.relpath)
169 (head, _tail) = os.path.split(project.relpath)
170 while head != "":
171 proj_dirs_parents.add(head)
172 (head, _tail) = os.path.split(head)
173 proj_dirs.add('.repo')
174
175 class StatusColoring(Coloring):
176 def __init__(self, config):
177 Coloring.__init__(self, config, 'status')
178 self.project = self.printer('header', attr = 'bold')
179 self.untracked = self.printer('untracked', fg = 'red')
180
181 orig_path = os.getcwd()
182 try:
183 os.chdir(self.manifest.topdir)
184
185 outstring = StringIO.StringIO()
186 self._FindOrphans(glob.glob('.*') + \
187 glob.glob('*'), \
188 proj_dirs, proj_dirs_parents, outstring)
189
190 if outstring.buflist:
191 output = StatusColoring(self.manifest.globalConfig)
192 output.project('Objects not within a project (orphans)')
193 output.nl()
194 for entry in outstring.buflist:
195 output.untracked(entry)
196 output.nl()
197 else:
198 print('No orphan files or directories')
199
200 outstring.close()
201
202 finally:
203 # Restore CWD.
204 os.chdir(orig_path)