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 |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 24 | try: |
| 25 | # For python2 |
| 26 | import StringIO as io |
| 27 | except ImportError: |
| 28 | # For python3 |
| 29 | import io |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 30 | import itertools |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 31 | import os |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 32 | import sys |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 33 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 34 | from color import Coloring |
| 35 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | class Status(PagedCommand): |
| 37 | common = True |
| 38 | helpSummary = "Show the working tree status" |
| 39 | helpUsage = """ |
| 40 | %prog [<project>...] |
| 41 | """ |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 42 | helpDescription = """ |
| 43 | '%prog' compares the working tree to the staging area (aka index), |
| 44 | and the most recent commit on this branch (HEAD), in each project |
| 45 | specified. A summary is displayed, one line per file where there |
| 46 | is a difference between these three states. |
| 47 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 48 | The -j/--jobs option can be used to run multiple status queries |
| 49 | in parallel. |
| 50 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 51 | The -o/--orphans option can be used to show objects that are in |
| 52 | the working directory, but not associated with a repo project. |
| 53 | This includes unmanaged top-level files and directories, but also |
| 54 | includes deeper items. For example, if dir/subdir/proj1 and |
| 55 | dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown |
| 56 | if it is not known to repo. |
| 57 | |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 58 | Status Display |
| 59 | -------------- |
| 60 | |
| 61 | The status display is organized into three columns of information, |
| 62 | for example if the file 'subcmds/status.py' is modified in the |
| 63 | project 'repo' on branch 'devwork': |
| 64 | |
| 65 | project repo/ branch devwork |
| 66 | -m subcmds/status.py |
| 67 | |
| 68 | The first column explains how the staging area (index) differs from |
| 69 | the last commit (HEAD). Its values are always displayed in upper |
| 70 | case 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 | |
| 81 | The second column explains how the working directory differs from |
| 82 | the index. Its values are always displayed in lower case and have |
| 83 | the 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 91 | 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 Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 95 | p.add_option('-o', '--orphans', |
| 96 | dest='orphans', action='store_true', |
| 97 | help="include objects in working directory outside of repo projects") |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 98 | |
| 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 Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 119 | 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 Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 135 | def Execute(self, opt, args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 136 | all_projects = self.GetProjects(args) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 137 | counter = itertools.count() |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 138 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 139 | if opt.jobs == 1: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 140 | for project in all_projects: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 141 | state = project.PrintWorkTreeStatus() |
| 142 | if state == 'CLEAN': |
| 143 | counter.next() |
| 144 | else: |
| 145 | sem = _threading.Semaphore(opt.jobs) |
| 146 | threads_and_output = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 147 | for project in all_projects: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 148 | sem.acquire() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 149 | |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 150 | class BufList(io.StringIO): |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 151 | def dump(self, ostream): |
| 152 | for entry in self.buflist: |
| 153 | ostream.write(entry) |
| 154 | |
| 155 | output = BufList() |
| 156 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 157 | t = _threading.Thread(target=self._StatusHelper, |
| 158 | args=(project, counter, sem, output)) |
| 159 | threads_and_output.append((t, output)) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 160 | t.daemon = True |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 161 | t.start() |
| 162 | for (t, output) in threads_and_output: |
| 163 | t.join() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 164 | output.dump(sys.stdout) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 165 | output.close() |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 166 | if len(all_projects) == counter.next(): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 167 | print('nothing to commit (working directory clean)') |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 168 | |
| 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 Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 190 | outstring = io.StringIO() |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 191 | 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) |