Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 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 | |
| 17 | from command import PagedCommand |
| 18 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 19 | try: |
| 20 | import threading as _threading |
| 21 | except ImportError: |
| 22 | import dummy_threading as _threading |
| 23 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 24 | import glob |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 25 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 26 | import itertools |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 27 | import os |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 28 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 29 | from color import Coloring |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 30 | import platform_utils |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 31 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | class Status(PagedCommand): |
| 33 | common = True |
| 34 | helpSummary = "Show the working tree status" |
| 35 | helpUsage = """ |
| 36 | %prog [<project>...] |
| 37 | """ |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 38 | helpDescription = """ |
| 39 | '%prog' compares the working tree to the staging area (aka index), |
| 40 | and the most recent commit on this branch (HEAD), in each project |
| 41 | specified. A summary is displayed, one line per file where there |
| 42 | is a difference between these three states. |
| 43 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 44 | The -j/--jobs option can be used to run multiple status queries |
| 45 | in parallel. |
| 46 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 47 | The -o/--orphans option can be used to show objects that are in |
| 48 | the working directory, but not associated with a repo project. |
| 49 | This includes unmanaged top-level files and directories, but also |
| 50 | includes deeper items. For example, if dir/subdir/proj1 and |
| 51 | dir/subdir/proj2 are repo projects, dir/subdir/proj3 will be shown |
| 52 | if it is not known to repo. |
| 53 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 54 | # Status Display |
Shawn O. Pearce | 4c5c7aa | 2009-04-13 14:06:10 -0700 | [diff] [blame] | 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") |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 93 | p.add_option('-q', '--quiet', action='store_true', |
| 94 | help="only print the name of modified projects") |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 95 | |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 96 | def _StatusHelper(self, project, clean_counter, sem, quiet): |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 97 | """Obtains the status for a specific project. |
| 98 | |
| 99 | Obtains the status for a project, redirecting the output to |
| 100 | the specified object. It will release the semaphore |
| 101 | when done. |
| 102 | |
| 103 | Args: |
| 104 | project: Project to get status of. |
| 105 | clean_counter: Counter for clean projects. |
| 106 | sem: Semaphore, will call release() when complete. |
| 107 | output: Where to output the status. |
| 108 | """ |
| 109 | try: |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 110 | state = project.PrintWorkTreeStatus(quiet=quiet) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 111 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 112 | next(clean_counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 113 | finally: |
| 114 | sem.release() |
| 115 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 116 | def _FindOrphans(self, dirs, proj_dirs, proj_dirs_parents, outstring): |
| 117 | """find 'dirs' that are present in 'proj_dirs_parents' but not in 'proj_dirs'""" |
| 118 | status_header = ' --\t' |
| 119 | for item in dirs: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 120 | if not platform_utils.isdir(item): |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 121 | outstring.append(''.join([status_header, item])) |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 122 | continue |
| 123 | if item in proj_dirs: |
| 124 | continue |
| 125 | if item in proj_dirs_parents: |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 126 | self._FindOrphans(glob.glob('%s/.*' % item) + |
| 127 | glob.glob('%s/*' % item), |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 128 | proj_dirs, proj_dirs_parents, outstring) |
| 129 | continue |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 130 | outstring.append(''.join([status_header, item, '/'])) |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 131 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | def Execute(self, opt, args): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 133 | all_projects = self.GetProjects(args) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 134 | counter = itertools.count() |
Shawn O. Pearce | 161f445 | 2009-04-10 17:41:44 -0700 | [diff] [blame] | 135 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 136 | if opt.jobs == 1: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 137 | for project in all_projects: |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 138 | state = project.PrintWorkTreeStatus(quiet=opt.quiet) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 139 | if state == 'CLEAN': |
Anthony King | 2cd1f04 | 2014-05-05 21:24:05 +0100 | [diff] [blame] | 140 | next(counter) |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 141 | else: |
| 142 | sem = _threading.Semaphore(opt.jobs) |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 143 | threads = [] |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 144 | for project in all_projects: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 145 | sem.acquire() |
Cezary Baginski | ccf8643 | 2012-04-23 23:55:35 +0200 | [diff] [blame] | 146 | |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 147 | t = _threading.Thread(target=self._StatusHelper, |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 148 | args=(project, counter, sem, opt.quiet)) |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 149 | threads.append(t) |
David 'Digit' Turner | e212665 | 2012-09-05 10:35:06 +0200 | [diff] [blame] | 150 | t.daemon = True |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 151 | t.start() |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 152 | for t in threads: |
Terence Haddock | 4655e81 | 2011-03-31 12:33:34 +0200 | [diff] [blame] | 153 | t.join() |
Andrew Wheeler | 4d5bb68 | 2012-02-27 13:52:22 -0600 | [diff] [blame] | 154 | if not opt.quiet and len(all_projects) == next(counter): |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 155 | print('nothing to commit (working directory clean)') |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 156 | |
| 157 | if opt.orphans: |
| 158 | proj_dirs = set() |
| 159 | proj_dirs_parents = set() |
| 160 | for project in self.GetProjects(None, missing_ok=True): |
| 161 | proj_dirs.add(project.relpath) |
| 162 | (head, _tail) = os.path.split(project.relpath) |
| 163 | while head != "": |
| 164 | proj_dirs_parents.add(head) |
| 165 | (head, _tail) = os.path.split(head) |
| 166 | proj_dirs.add('.repo') |
| 167 | |
| 168 | class StatusColoring(Coloring): |
| 169 | def __init__(self, config): |
| 170 | Coloring.__init__(self, config, 'status') |
| 171 | self.project = self.printer('header', attr = 'bold') |
| 172 | self.untracked = self.printer('untracked', fg = 'red') |
| 173 | |
| 174 | orig_path = os.getcwd() |
| 175 | try: |
| 176 | os.chdir(self.manifest.topdir) |
| 177 | |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 178 | outstring = [] |
| 179 | self._FindOrphans(glob.glob('.*') + |
| 180 | glob.glob('*'), |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 181 | proj_dirs, proj_dirs_parents, outstring) |
| 182 | |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 183 | if outstring: |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 184 | output = StatusColoring(self.manifest.globalConfig) |
| 185 | output.project('Objects not within a project (orphans)') |
| 186 | output.nl() |
Anthony King | b51f07c | 2015-04-04 21:18:59 +0100 | [diff] [blame] | 187 | for entry in outstring: |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 188 | output.untracked(entry) |
| 189 | output.nl() |
| 190 | else: |
| 191 | print('No orphan files or directories') |
| 192 | |
Will Richey | 63d356f | 2012-06-21 09:49:59 -0400 | [diff] [blame] | 193 | finally: |
| 194 | # Restore CWD. |
| 195 | os.chdir(orig_path) |