| 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 | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 18 | from command import DEFAULT_LOCAL_JOBS |
| 19 | from command import PagedCommand |
| The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | |
| David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 21 | |
| The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | class Diff(PagedCommand): |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 23 | COMMON = True |
| 24 | helpSummary = "Show changes between commit and working tree" |
| 25 | helpUsage = """ |
| The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | %prog [<project>...] |
| pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 27 | |
| 28 | The -u option causes '%prog' to generate diff output with file paths |
| 29 | relative to the repository root, so the output can be applied |
| 30 | to the Unix 'patch' command. |
| The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | """ |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 32 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
| The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 34 | def _Options(self, p): |
| 35 | p.add_option( |
| 36 | "-u", |
| 37 | "--absolute", |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 38 | action="store_true", |
| 39 | help="paths are relative to the repository root", |
| 40 | ) |
| pelya | d67872d | 2012-03-28 14:49:58 +0300 | [diff] [blame] | 41 | |
| Kuang-che Wu | 8da4861 | 2024-10-22 21:04:41 +0800 | [diff] [blame] | 42 | @classmethod |
| 43 | def _ExecuteOne(cls, absolute, local, project_idx): |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 44 | """Obtains the diff for a specific project. |
| Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 45 | |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 46 | Args: |
| 47 | absolute: Paths are relative to the root. |
| 48 | local: a boolean, if True, the path is relative to the local |
| 49 | (sub)manifest. If false, the path is relative to the outermost |
| 50 | manifest. |
| Kuang-che Wu | 8da4861 | 2024-10-22 21:04:41 +0800 | [diff] [blame] | 51 | project_idx: Project index to get status of. |
| Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 52 | |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 53 | Returns: |
| 54 | The status of the project. |
| 55 | """ |
| 56 | buf = io.StringIO() |
| Kuang-che Wu | 8da4861 | 2024-10-22 21:04:41 +0800 | [diff] [blame] | 57 | project = cls.get_parallel_context()["projects"][project_idx] |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 58 | ret = project.PrintWorkTreeDiff(absolute, output_redir=buf, local=local) |
| 59 | return (ret, buf.getvalue()) |
| Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 60 | |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 61 | def Execute(self, opt, args): |
| 62 | all_projects = self.GetProjects( |
| 63 | args, all_manifests=not opt.this_manifest_only |
| 64 | ) |
| Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 65 | |
| Gavin Mak | ea2e330 | 2023-03-11 06:46:20 +0000 | [diff] [blame] | 66 | def _ProcessResults(_pool, _output, results): |
| 67 | ret = 0 |
| 68 | for state, output in results: |
| 69 | if output: |
| 70 | print(output, end="") |
| 71 | if not state: |
| 72 | ret = 1 |
| 73 | return ret |
| Mike Frysinger | 69b4a9c | 2021-02-16 18:18:01 -0500 | [diff] [blame] | 74 | |
| Kuang-che Wu | 8da4861 | 2024-10-22 21:04:41 +0800 | [diff] [blame] | 75 | with self.ParallelContext(): |
| 76 | self.get_parallel_context()["projects"] = all_projects |
| 77 | return self.ExecuteInParallel( |
| 78 | opt.jobs, |
| 79 | functools.partial( |
| 80 | self._ExecuteOne, opt.absolute, opt.this_manifest_only |
| 81 | ), |
| 82 | range(len(all_projects)), |
| 83 | callback=_ProcessResults, |
| 84 | ordered=True, |
| 85 | chunksize=1, |
| 86 | ) |