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