blob: 3939c45b8de47a2c3a95ac6280f7b2e387733511 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07002#
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
17from command import PagedCommand
18
Terence Haddock4655e812011-03-31 12:33:34 +020019try:
20 import threading as _threading
21except ImportError:
22 import dummy_threading as _threading
23
Will Richey63d356f2012-06-21 09:49:59 -040024import glob
David Pursehouse59bbb582013-05-17 10:49:33 +090025
Terence Haddock4655e812011-03-31 12:33:34 +020026import itertools
Will Richey63d356f2012-06-21 09:49:59 -040027import os
Terence Haddock4655e812011-03-31 12:33:34 +020028
Will Richey63d356f2012-06-21 09:49:59 -040029from color import Coloring
Renaud Paquaybed8b622018-09-27 10:46:58 -070030import platform_utils
Will Richey63d356f2012-06-21 09:49:59 -040031
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032class Status(PagedCommand):
33 common = True
34 helpSummary = "Show the working tree status"
35 helpUsage = """
36%prog [<project>...]
37"""
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070038 helpDescription = """
39'%prog' compares the working tree to the staging area (aka index),
40and the most recent commit on this branch (HEAD), in each project
41specified. A summary is displayed, one line per file where there
42is a difference between these three states.
43
Terence Haddock4655e812011-03-31 12:33:34 +020044The -j/--jobs option can be used to run multiple status queries
45in parallel.
46
Will Richey63d356f2012-06-21 09:49:59 -040047The -o/--orphans option can be used to show objects that are in
48the working directory, but not associated with a repo project.
49This includes unmanaged top-level files and directories, but also
50includes deeper items. For example, if dir/subdir/proj1 and
51dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown
52if it is not known to repo.
53
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040054# Status Display
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070055
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")
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060093 p.add_option('-q', '--quiet', action='store_true',
94 help="only print the name of modified projects")
Terence Haddock4655e812011-03-31 12:33:34 +020095
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060096 def _StatusHelper(self, project, clean_counter, sem, quiet):
Terence Haddock4655e812011-03-31 12:33:34 +020097 """Obtains the status for a specific project.
98
99 Obtains the status for a project, redirecting the output to
100 the specified object. It will release the semaphore
101 when done.
102
103 Args:
104 project: Project to get status of.
105 clean_counter: Counter for clean projects.
106 sem: Semaphore, will call release() when complete.
107 output: Where to output the status.
108 """
109 try:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600110 state = project.PrintWorkTreeStatus(quiet=quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200111 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100112 next(clean_counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200113 finally:
114 sem.release()
115
Will Richey63d356f2012-06-21 09:49:59 -0400116 def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
117 """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
118 status_header = ' --\t'
119 for item in dirs:
Renaud Paquaybed8b622018-09-27 10:46:58 -0700120 if not platform_utils.isdir(item):
Anthony Kingb51f07c2015-04-04 21:18:59 +0100121 outstring.append(''.join([status_header, item]))
Will Richey63d356f2012-06-21 09:49:59 -0400122 continue
123 if item in proj_dirs:
124 continue
125 if item in proj_dirs_parents:
Anthony Kingb51f07c2015-04-04 21:18:59 +0100126 self._FindOrphans(glob.glob('%s/.*' % item) +
127 glob.glob('%s/*' % item),
Will Richey63d356f2012-06-21 09:49:59 -0400128 proj_dirs, proj_dirs_parents, outstring)
129 continue
Anthony Kingb51f07c2015-04-04 21:18:59 +0100130 outstring.append(''.join([status_header, item, '/']))
Will Richey63d356f2012-06-21 09:49:59 -0400131
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700132 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900133 all_projects = self.GetProjects(args)
Terence Haddock4655e812011-03-31 12:33:34 +0200134 counter = itertools.count()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700135
Terence Haddock4655e812011-03-31 12:33:34 +0200136 if opt.jobs == 1:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900137 for project in all_projects:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600138 state = project.PrintWorkTreeStatus(quiet=opt.quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200139 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100140 next(counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200141 else:
142 sem = _threading.Semaphore(opt.jobs)
Anthony Kingb51f07c2015-04-04 21:18:59 +0100143 threads = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900144 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200145 sem.acquire()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200146
Terence Haddock4655e812011-03-31 12:33:34 +0200147 t = _threading.Thread(target=self._StatusHelper,
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600148 args=(project, counter, sem, opt.quiet))
Anthony Kingb51f07c2015-04-04 21:18:59 +0100149 threads.append(t)
David 'Digit' Turnere2126652012-09-05 10:35:06 +0200150 t.daemon = True
Terence Haddock4655e812011-03-31 12:33:34 +0200151 t.start()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100152 for t in threads:
Terence Haddock4655e812011-03-31 12:33:34 +0200153 t.join()
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600154 if not opt.quiet and len(all_projects) == next(counter):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700155 print('nothing to commit (working directory clean)')
Will Richey63d356f2012-06-21 09:49:59 -0400156
157 if opt.orphans:
158 proj_dirs = set()
159 proj_dirs_parents = set()
160 for project in self.GetProjects(None, missing_ok=True):
161 proj_dirs.add(project.relpath)
162 (head, _tail) = os.path.split(project.relpath)
163 while head != "":
164 proj_dirs_parents.add(head)
165 (head, _tail) = os.path.split(head)
166 proj_dirs.add('.repo')
167
168 class StatusColoring(Coloring):
169 def __init__(self, config):
170 Coloring.__init__(self, config, 'status')
171 self.project = self.printer('header', attr = 'bold')
172 self.untracked = self.printer('untracked', fg = 'red')
173
174 orig_path = os.getcwd()
175 try:
176 os.chdir(self.manifest.topdir)
177
Anthony Kingb51f07c2015-04-04 21:18:59 +0100178 outstring = []
179 self._FindOrphans(glob.glob('.*') +
180 glob.glob('*'),
Will Richey63d356f2012-06-21 09:49:59 -0400181 proj_dirs, proj_dirs_parents, outstring)
182
Anthony Kingb51f07c2015-04-04 21:18:59 +0100183 if outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400184 output = StatusColoring(self.manifest.globalConfig)
185 output.project('Objects not within a project (orphans)')
186 output.nl()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100187 for entry in outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400188 output.untracked(entry)
189 output.nl()
190 else:
191 print('No orphan files or directories')
192
Will Richey63d356f2012-06-21 09:49:59 -0400193 finally:
194 # Restore CWD.
195 os.chdir(orig_path)