The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | from command import PagedCommand |
| 17 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 18 | try: |
| 19 | import threading as _threading |
| 20 | except ImportError: |
| 21 | import dummy_threading as _threading |
| 22 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 23 | import glob |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 24 | |
| 25 | from pyversion import is_python3 |
| 26 | if is_python3(): |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 27 | import io |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 28 | else: |
| 29 | import StringIO as io |
| 30 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 31 | import itertools |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 32 | import os |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 33 | import sys |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 34 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 35 | from color import Coloring |
| 36 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | class Status(PagedCommand): |
| 38 | common = True |
| 39 | helpSummary = "Show the working tree status" |
| 40 | helpUsage = """ |
| 41 | %prog [<project>...] |
| 42 | """ |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 43 | helpDescription = """ |
| 44 | '%prog' compares the working tree to the staging area (aka index), |
| 45 | and the most recent commit on this branch (HEAD), in each project |
| 46 | specified. A summary is displayed, one line per file where there |
| 47 | is a difference between these three states. |
| 48 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 49 | The -j/--jobs option can be used to run multiple status queries |
| 50 | in parallel. |
| 51 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 52 | The -o/--orphans option can be used to show objects that are in |
| 53 | the working directory, but not associated with a repo project. |
| 54 | This includes unmanaged top-level files and directories, but also |
| 55 | includes deeper items. For example, if dir/subdir/proj1 and |
| 56 | dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown |
| 57 | if it is not known to repo. |
| 58 | |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 59 | Status Display |
| 60 | -------------- |
| 61 | |
| 62 | The status display is organized into three columns of information, |
| 63 | for example if the file 'subcmds/status.py' is modified in the |
| 64 | project 'repo' on branch 'devwork': |
| 65 | |
| 66 | project repo/ branch devwork |
| 67 | -m subcmds/status.py |
| 68 | |
| 69 | The first column explains how the staging area (index) differs from |
| 70 | the last commit (HEAD). Its values are always displayed in upper |
| 71 | case and have the following meanings: |
| 72 | |
| 73 | -: no difference |
| 74 | A: added (not in HEAD, in index ) |
| 75 | M: modified ( in HEAD, in index, different content ) |
| 76 | D: deleted ( in HEAD, not in index ) |
| 77 | R: renamed (not in HEAD, in index, path changed ) |
| 78 | C: copied (not in HEAD, in index, copied from another) |
| 79 | T: mode changed ( in HEAD, in index, same content ) |
| 80 | U: unmerged; conflict resolution required |
| 81 | |
| 82 | The second column explains how the working directory differs from |
| 83 | the index. Its values are always displayed in lower case and have |
| 84 | the following meanings: |
| 85 | |
| 86 | -: new / unknown (not in index, in work tree ) |
| 87 | m: modified ( in index, in work tree, modified ) |
| 88 | d: deleted ( in index, not in work tree ) |
| 89 | |
| 90 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 91 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 92 | def _Options(self, p): |
| 93 | p.add_option('-j', '--jobs', |
| 94 | dest='jobs', action='store', type='int', default=2, |
| 95 | help="number of projects to check simultaneously") |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 96 | p.add_option('-o', '--orphans', |
| 97 | dest='orphans', action='store_true', |
| 98 | help="include objects in working directory outside of repo projects") |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 99 | |
| 100 | def _StatusHelper(self, project, clean_counter, sem, output): |
| 101 | """Obtains the status for a specific project. |
| 102 | |
| 103 | Obtains the status for a project, redirecting the output to |
| 104 | the specified object. It will release the semaphore |
| 105 | when done. |
| 106 | |
| 107 | Args: |
| 108 | project: Project to get status of. |
| 109 | clean_counter: Counter for clean projects. |
| 110 | sem: Semaphore, will call release() when complete. |
| 111 | output: Where to output the status. |
| 112 | """ |
| 113 | try: |
| 114 | state = project.PrintWorkTreeStatus(output) |
| 115 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame^] | 116 | next(clean_counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 117 | finally: |
| 118 | sem.release() |
| 119 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 120 | def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring): |
| 121 | """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'""" |
| 122 | status_header = ' --\t' |
| 123 | for item in dirs: |
| 124 | if not os.path.isdir(item): |
| 125 | outstring.write(''.join([status_header, item])) |
| 126 | continue |
| 127 | if item in proj_dirs: |
| 128 | continue |
| 129 | if item in proj_dirs_parents: |
| 130 | self._FindOrphans(glob.glob('%s/.*' % item) + \ |
| 131 | glob.glob('%s/*' % item), \ |
| 132 | proj_dirs, proj_dirs_parents, outstring) |
| 133 | continue |
| 134 | outstring.write(''.join([status_header, item, '/'])) |
| 135 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | def Execute(self, opt, args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 137 | all_projects = self.GetProjects(args) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 138 | counter = itertools.count() |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 139 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 140 | if opt.jobs == 1: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 141 | for project in all_projects: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 142 | state = project.PrintWorkTreeStatus() |
| 143 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame^] | 144 | next(counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 145 | else: |
| 146 | sem = _threading.Semaphore(opt.jobs) |
| 147 | threads_and_output = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 148 | for project in all_projects: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 149 | sem.acquire() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 150 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 151 | class BufList(io.StringIO): |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 152 | def dump(self, ostream): |
| 153 | for entry in self.buflist: |
| 154 | ostream.write(entry) |
| 155 | |
| 156 | output = BufList() |
| 157 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 158 | t = _threading.Thread(target=self._StatusHelper, |
| 159 | args=(project, counter, sem, output)) |
| 160 | threads_and_output.append((t, output)) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 161 | t.daemon = True |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 162 | t.start() |
| 163 | for (t, output) in threads_and_output: |
| 164 | t.join() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 165 | output.dump(sys.stdout) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 166 | output.close() |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame^] | 167 | if len(all_projects) == next(counter): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 168 | print('nothing to commit (working directory clean)') |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 169 | |
| 170 | if opt.orphans: |
| 171 | proj_dirs = set() |
| 172 | proj_dirs_parents = set() |
| 173 | for project in self.GetProjects(None, missing_ok=True): |
| 174 | proj_dirs.add(project.relpath) |
| 175 | (head, _tail) = os.path.split(project.relpath) |
| 176 | while head != "": |
| 177 | proj_dirs_parents.add(head) |
| 178 | (head, _tail) = os.path.split(head) |
| 179 | proj_dirs.add('.repo') |
| 180 | |
| 181 | class StatusColoring(Coloring): |
| 182 | def __init__(self, config): |
| 183 | Coloring.__init__(self, config, 'status') |
| 184 | self.project = self.printer('header', attr = 'bold') |
| 185 | self.untracked = self.printer('untracked', fg = 'red') |
| 186 | |
| 187 | orig_path = os.getcwd() |
| 188 | try: |
| 189 | os.chdir(self.manifest.topdir) |
| 190 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 191 | outstring = io.StringIO() |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 192 | self._FindOrphans(glob.glob('.*') + \ |
| 193 | glob.glob('*'), \ |
| 194 | proj_dirs, proj_dirs_parents, outstring) |
| 195 | |
| 196 | if outstring.buflist: |
| 197 | output = StatusColoring(self.manifest.globalConfig) |
| 198 | output.project('Objects not within a project (orphans)') |
| 199 | output.nl() |
| 200 | for entry in outstring.buflist: |
| 201 | output.untracked(entry) |
| 202 | output.nl() |
| 203 | else: |
| 204 | print('No orphan files or directories') |
| 205 | |
| 206 | outstring.close() |
| 207 | |
| 208 | finally: |
| 209 | # Restore CWD. |
| 210 | os.chdir(orig_path) |