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