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 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 25 | import itertools |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 26 | import os |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 27 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 28 | from color import Coloring |
| 29 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | class Status(PagedCommand): |
| 31 | common = True |
| 32 | helpSummary = "Show the working tree status" |
| 33 | helpUsage = """ |
| 34 | %prog [<project>...] |
| 35 | """ |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 36 | helpDescription = """ |
| 37 | '%prog' compares the working tree to the staging area (aka index), |
| 38 | and the most recent commit on this branch (HEAD), in each project |
| 39 | specified. A summary is displayed, one line per file where there |
| 40 | is a difference between these three states. |
| 41 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 42 | The -j/--jobs option can be used to run multiple status queries |
| 43 | in parallel. |
| 44 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 45 | The -o/--orphans option can be used to show objects that are in |
| 46 | the working directory, but not associated with a repo project. |
| 47 | This includes unmanaged top-level files and directories, but also |
| 48 | includes deeper items. For example, if dir/subdir/proj1 and |
| 49 | dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown |
| 50 | if it is not known to repo. |
| 51 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 52 | # Status Display |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 53 | |
| 54 | The status display is organized into three columns of information, |
| 55 | for example if the file 'subcmds/status.py' is modified in the |
| 56 | project 'repo' on branch 'devwork': |
| 57 | |
| 58 | project repo/ branch devwork |
| 59 | -m subcmds/status.py |
| 60 | |
| 61 | The first column explains how the staging area (index) differs from |
| 62 | the last commit (HEAD). Its values are always displayed in upper |
| 63 | case and have the following meanings: |
| 64 | |
| 65 | -: no difference |
| 66 | A: added (not in HEAD, in index ) |
| 67 | M: modified ( in HEAD, in index, different content ) |
| 68 | D: deleted ( in HEAD, not in index ) |
| 69 | R: renamed (not in HEAD, in index, path changed ) |
| 70 | C: copied (not in HEAD, in index, copied from another) |
| 71 | T: mode changed ( in HEAD, in index, same content ) |
| 72 | U: unmerged; conflict resolution required |
| 73 | |
| 74 | The second column explains how the working directory differs from |
| 75 | the index. Its values are always displayed in lower case and have |
| 76 | the following meanings: |
| 77 | |
| 78 | -: new / unknown (not in index, in work tree ) |
| 79 | m: modified ( in index, in work tree, modified ) |
| 80 | d: deleted ( in index, not in work tree ) |
| 81 | |
| 82 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 84 | def _Options(self, p): |
| 85 | p.add_option('-j', '--jobs', |
| 86 | dest='jobs', action='store', type='int', default=2, |
| 87 | help="number of projects to check simultaneously") |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 88 | p.add_option('-o', '--orphans', |
| 89 | dest='orphans', action='store_true', |
| 90 | help="include objects in working directory outside of repo projects") |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 91 | p.add_option('-q', '--quiet', action='store_true', |
| 92 | help="only print the name of modified projects") |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 93 | |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 94 | def _StatusHelper(self, project, clean_counter, sem, quiet): |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 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: |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 108 | state = project.PrintWorkTreeStatus(quiet=quiet) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 109 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 110 | next(clean_counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 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): |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 119 | outstring.append(''.join([status_header, item])) |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 120 | continue |
| 121 | if item in proj_dirs: |
| 122 | continue |
| 123 | if item in proj_dirs_parents: |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 124 | self._FindOrphans(glob.glob('%s/.*' % item) + |
| 125 | glob.glob('%s/*' % item), |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 126 | proj_dirs, proj_dirs_parents, outstring) |
| 127 | continue |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 128 | outstring.append(''.join([status_header, item, '/'])) |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 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: |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 136 | state = project.PrintWorkTreeStatus(quiet=opt.quiet) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 137 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 138 | next(counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 139 | else: |
| 140 | sem = _threading.Semaphore(opt.jobs) |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 141 | threads = [] |
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 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 145 | t = _threading.Thread(target=self._StatusHelper, |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 146 | args=(project, counter, sem, opt.quiet)) |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 147 | threads.append(t) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 148 | t.daemon = True |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 149 | t.start() |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 150 | for t in threads: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 151 | t.join() |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 152 | if not opt.quiet and len(all_projects) == next(counter): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 153 | print('nothing to commit (working directory clean)') |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 154 | |
| 155 | if opt.orphans: |
| 156 | proj_dirs = set() |
| 157 | proj_dirs_parents = set() |
| 158 | for project in self.GetProjects(None, missing_ok=True): |
| 159 | proj_dirs.add(project.relpath) |
| 160 | (head, _tail) = os.path.split(project.relpath) |
| 161 | while head != "": |
| 162 | proj_dirs_parents.add(head) |
| 163 | (head, _tail) = os.path.split(head) |
| 164 | proj_dirs.add('.repo') |
| 165 | |
| 166 | class StatusColoring(Coloring): |
| 167 | def __init__(self, config): |
| 168 | Coloring.__init__(self, config, 'status') |
| 169 | self.project = self.printer('header', attr = 'bold') |
| 170 | self.untracked = self.printer('untracked', fg = 'red') |
| 171 | |
| 172 | orig_path = os.getcwd() |
| 173 | try: |
| 174 | os.chdir(self.manifest.topdir) |
| 175 | |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 176 | outstring = [] |
| 177 | self._FindOrphans(glob.glob('.*') + |
| 178 | glob.glob('*'), |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 179 | proj_dirs, proj_dirs_parents, outstring) |
| 180 | |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 181 | if outstring: |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 182 | output = StatusColoring(self.manifest.globalConfig) |
| 183 | output.project('Objects not within a project (orphans)') |
| 184 | output.nl() |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 185 | for entry in outstring: |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 186 | output.untracked(entry) |
| 187 | output.nl() |
| 188 | else: |
| 189 | print('No orphan files or directories') |
| 190 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 191 | finally: |
| 192 | # Restore CWD. |
| 193 | os.chdir(orig_path) |