blob: 63972d724d98d5b19632f5f775282dfc2b9f4f47 [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
Mike Frysinger9e718422019-06-13 12:41:32 -040017from __future__ import print_function
18
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019from command import PagedCommand
20
Terence Haddock4655e812011-03-31 12:33:34 +020021try:
22 import threading as _threading
23except ImportError:
24 import dummy_threading as _threading
25
Will Richey63d356f2012-06-21 09:49:59 -040026import glob
David Pursehouse59bbb582013-05-17 10:49:33 +090027
Terence Haddock4655e812011-03-31 12:33:34 +020028import itertools
Will Richey63d356f2012-06-21 09:49:59 -040029import os
Terence Haddock4655e812011-03-31 12:33:34 +020030
Will Richey63d356f2012-06-21 09:49:59 -040031from color import Coloring
Renaud Paquaybed8b622018-09-27 10:46:58 -070032import platform_utils
Will Richey63d356f2012-06-21 09:49:59 -040033
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034class Status(PagedCommand):
35 common = True
36 helpSummary = "Show the working tree status"
37 helpUsage = """
38%prog [<project>...]
39"""
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070040 helpDescription = """
41'%prog' compares the working tree to the staging area (aka index),
42and the most recent commit on this branch (HEAD), in each project
43specified. A summary is displayed, one line per file where there
44is a difference between these three states.
45
Terence Haddock4655e812011-03-31 12:33:34 +020046The -j/--jobs option can be used to run multiple status queries
47in parallel.
48
Will Richey63d356f2012-06-21 09:49:59 -040049The -o/--orphans option can be used to show objects that are in
50the working directory, but not associated with a repo project.
51This includes unmanaged top-level files and directories, but also
52includes deeper items. For example, if dir/subdir/proj1 and
53dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown
54if it is not known to repo.
55
Mike Frysingerb8f7bb02018-10-10 01:05:11 -040056# Status Display
Shawn O. Pearce4c5c7aa2009-04-13 14:06:10 -070057
58The status display is organized into three columns of information,
59for example if the file 'subcmds/status.py' is modified in the
60project 'repo' on branch 'devwork':
61
62 project repo/ branch devwork
63 -m subcmds/status.py
64
65The first column explains how the staging area (index) differs from
66the last commit (HEAD). Its values are always displayed in upper
67case and have the following meanings:
68
69 -: no difference
70 A: added (not in HEAD, in index )
71 M: modified ( in HEAD, in index, different content )
72 D: deleted ( in HEAD, not in index )
73 R: renamed (not in HEAD, in index, path changed )
74 C: copied (not in HEAD, in index, copied from another)
75 T: mode changed ( in HEAD, in index, same content )
76 U: unmerged; conflict resolution required
77
78The second column explains how the working directory differs from
79the index. Its values are always displayed in lower case and have
80the following meanings:
81
82 -: new / unknown (not in index, in work tree )
83 m: modified ( in index, in work tree, modified )
84 d: deleted ( in index, not in work tree )
85
86"""
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070087
Terence Haddock4655e812011-03-31 12:33:34 +020088 def _Options(self, p):
89 p.add_option('-j', '--jobs',
90 dest='jobs', action='store', type='int', default=2,
91 help="number of projects to check simultaneously")
Will Richey63d356f2012-06-21 09:49:59 -040092 p.add_option('-o', '--orphans',
93 dest='orphans', action='store_true',
94 help="include objects in working directory outside of repo projects")
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060095 p.add_option('-q', '--quiet', action='store_true',
96 help="only print the name of modified projects")
Terence Haddock4655e812011-03-31 12:33:34 +020097
Andrew Wheeler4d5bb682012-02-27 13:52:22 -060098 def _StatusHelper(self, project, clean_counter, sem, quiet):
Terence Haddock4655e812011-03-31 12:33:34 +020099 """Obtains the status for a specific project.
100
101 Obtains the status for a project, redirecting the output to
102 the specified object. It will release the semaphore
103 when done.
104
105 Args:
106 project: Project to get status of.
107 clean_counter: Counter for clean projects.
108 sem: Semaphore, will call release() when complete.
109 output: Where to output the status.
110 """
111 try:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600112 state = project.PrintWorkTreeStatus(quiet=quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200113 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100114 next(clean_counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200115 finally:
116 sem.release()
117
Will Richey63d356f2012-06-21 09:49:59 -0400118 def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring):
119 """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'"""
120 status_header = ' --\t'
121 for item in dirs:
Renaud Paquaybed8b622018-09-27 10:46:58 -0700122 if not platform_utils.isdir(item):
Anthony Kingb51f07c2015-04-04 21:18:59 +0100123 outstring.append(''.join([status_header, item]))
Will Richey63d356f2012-06-21 09:49:59 -0400124 continue
125 if item in proj_dirs:
126 continue
127 if item in proj_dirs_parents:
Anthony Kingb51f07c2015-04-04 21:18:59 +0100128 self._FindOrphans(glob.glob('%s/.*' % item) +
129 glob.glob('%s/*' % item),
Will Richey63d356f2012-06-21 09:49:59 -0400130 proj_dirs, proj_dirs_parents, outstring)
131 continue
Anthony Kingb51f07c2015-04-04 21:18:59 +0100132 outstring.append(''.join([status_header, item, '/']))
Will Richey63d356f2012-06-21 09:49:59 -0400133
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700134 def Execute(self, opt, args):
David Pursehouse8a68ff92012-09-24 12:15:13 +0900135 all_projects = self.GetProjects(args)
Terence Haddock4655e812011-03-31 12:33:34 +0200136 counter = itertools.count()
Shawn O. Pearce161f4452009-04-10 17:41:44 -0700137
Terence Haddock4655e812011-03-31 12:33:34 +0200138 if opt.jobs == 1:
David Pursehouse8a68ff92012-09-24 12:15:13 +0900139 for project in all_projects:
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600140 state = project.PrintWorkTreeStatus(quiet=opt.quiet)
Terence Haddock4655e812011-03-31 12:33:34 +0200141 if state == 'CLEAN':
Anthony King2cd1f042014-05-05 21:24:05 +0100142 next(counter)
Terence Haddock4655e812011-03-31 12:33:34 +0200143 else:
144 sem = _threading.Semaphore(opt.jobs)
Anthony Kingb51f07c2015-04-04 21:18:59 +0100145 threads = []
David Pursehouse8a68ff92012-09-24 12:15:13 +0900146 for project in all_projects:
Terence Haddock4655e812011-03-31 12:33:34 +0200147 sem.acquire()
Cezary Baginskiccf86432012-04-23 23:55:35 +0200148
Terence Haddock4655e812011-03-31 12:33:34 +0200149 t = _threading.Thread(target=self._StatusHelper,
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600150 args=(project, counter, sem, opt.quiet))
Anthony Kingb51f07c2015-04-04 21:18:59 +0100151 threads.append(t)
David 'Digit' Turnere2126652012-09-05 10:35:06 +0200152 t.daemon = True
Terence Haddock4655e812011-03-31 12:33:34 +0200153 t.start()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100154 for t in threads:
Terence Haddock4655e812011-03-31 12:33:34 +0200155 t.join()
Andrew Wheeler4d5bb682012-02-27 13:52:22 -0600156 if not opt.quiet and len(all_projects) == next(counter):
Sarah Owenscecd1d82012-11-01 22:59:27 -0700157 print('nothing to commit (working directory clean)')
Will Richey63d356f2012-06-21 09:49:59 -0400158
159 if opt.orphans:
160 proj_dirs = set()
161 proj_dirs_parents = set()
162 for project in self.GetProjects(None, missing_ok=True):
163 proj_dirs.add(project.relpath)
164 (head, _tail) = os.path.split(project.relpath)
165 while head != "":
166 proj_dirs_parents.add(head)
167 (head, _tail) = os.path.split(head)
168 proj_dirs.add('.repo')
169
170 class StatusColoring(Coloring):
171 def __init__(self, config):
172 Coloring.__init__(self, config, 'status')
173 self.project = self.printer('header', attr = 'bold')
174 self.untracked = self.printer('untracked', fg = 'red')
175
176 orig_path = os.getcwd()
177 try:
178 os.chdir(self.manifest.topdir)
179
Anthony Kingb51f07c2015-04-04 21:18:59 +0100180 outstring = []
181 self._FindOrphans(glob.glob('.*') +
182 glob.glob('*'),
Will Richey63d356f2012-06-21 09:49:59 -0400183 proj_dirs, proj_dirs_parents, outstring)
184
Anthony Kingb51f07c2015-04-04 21:18:59 +0100185 if outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400186 output = StatusColoring(self.manifest.globalConfig)
187 output.project('Objects not within a project (orphans)')
188 output.nl()
Anthony Kingb51f07c2015-04-04 21:18:59 +0100189 for entry in outstring:
Will Richey63d356f2012-06-21 09:49:59 -0400190 output.untracked(entry)
191 output.nl()
192 else:
193 print('No orphan files or directories')
194
Will Richey63d356f2012-06-21 09:49:59 -0400195 finally:
196 # Restore CWD.
197 os.chdir(orig_path)