The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 15 | import functools |
| 16 | import io |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 17 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 18 | from command import DEFAULT_LOCAL_JOBS, PagedCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 20 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | class Diff(PagedCommand): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 22 | COMMON = True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | helpSummary = "Show changes between commit and working tree" |
| 24 | helpUsage = """ |
| 25 | %prog [<project>...] |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 26 | |
| 27 | The -u option causes '%prog' to generate diff output with file paths |
| 28 | relative to the repository root, so the output can be applied |
| 29 | to the Unix 'patch' command. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | """ |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 31 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 33 | def _Options(self, p): |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 34 | p.add_option('-u', '--absolute', |
| 35 | dest='absolute', action='store_true', |
Mike Frysinger | c177f94 | 2021-05-04 08:06:36 -0400 | [diff] [blame] | 36 | help='paths are relative to the repository root') |
pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 37 | |
LaMont Jones | 8501d46 | 2022-06-22 19:21:15 +0000 | [diff] [blame] | 38 | def _ExecuteOne(self, absolute, local, project): |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 39 | """Obtains the diff for a specific project. |
| 40 | |
| 41 | Args: |
| 42 | absolute: Paths are relative to the root. |
LaMont Jones | 8501d46 | 2022-06-22 19:21:15 +0000 | [diff] [blame] | 43 | local: a boolean, if True, the path is relative to the local |
| 44 | (sub)manifest. If false, the path is relative to the |
| 45 | outermost manifest. |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 46 | project: Project to get status of. |
| 47 | |
| 48 | Returns: |
| 49 | The status of the project. |
| 50 | """ |
| 51 | buf = io.StringIO() |
LaMont Jones | 8501d46 | 2022-06-22 19:21:15 +0000 | [diff] [blame] | 52 | ret = project.PrintWorkTreeDiff(absolute, output_redir=buf, local=local) |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 53 | return (ret, buf.getvalue()) |
| 54 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 55 | def Execute(self, opt, args): |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 56 | all_projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only) |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 57 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 58 | def _ProcessResults(_pool, _output, results): |
| 59 | ret = 0 |
| 60 | for (state, output) in results: |
| 61 | if output: |
| 62 | print(output, end='') |
| 63 | if not state: |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 64 | ret = 1 |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 65 | return ret |
Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 66 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 67 | return self.ExecuteInParallel( |
| 68 | opt.jobs, |
LaMont Jones | 8501d46 | 2022-06-22 19:21:15 +0000 | [diff] [blame] | 69 | functools.partial(self._ExecuteOne, opt.absolute, opt.this_manifest_only), |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 70 | all_projects, |
| 71 | callback=_ProcessResults, |
| 72 | ordered=True) |