blob: d9d72b407d25243bfd1f91416e2ba6738e707866 [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# 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 Frysinger69b4a9c2021-02-16 18:18:01 -050015import functools
16import io
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050017
Mike Frysinger64477332023-08-21 21:20:32 -040018from command import DEFAULT_LOCAL_JOBS
19from command import PagedCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020
David Pursehouse819827a2020-02-12 15:20:19 +090021
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022class Diff(PagedCommand):
Gavin Makea2e3302023-03-11 06:46:20 +000023 COMMON = True
24 helpSummary = "Show changes between commit and working tree"
25 helpUsage = """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026%prog [<project>...]
pelyad67872d2012-03-28 14:49:58 +030027
28The -u option causes '%prog' to generate diff output with file paths
29relative to the repository root, so the output can be applied
30to the Unix 'patch' command.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031"""
Gavin Makea2e3302023-03-11 06:46:20 +000032 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033
Gavin Makea2e3302023-03-11 06:46:20 +000034 def _Options(self, p):
35 p.add_option(
36 "-u",
37 "--absolute",
38 dest="absolute",
39 action="store_true",
40 help="paths are relative to the repository root",
41 )
pelyad67872d2012-03-28 14:49:58 +030042
Gavin Makea2e3302023-03-11 06:46:20 +000043 def _ExecuteOne(self, absolute, local, project):
44 """Obtains the diff for a specific project.
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050045
Gavin Makea2e3302023-03-11 06:46:20 +000046 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.
51 project: Project to get status of.
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050052
Gavin Makea2e3302023-03-11 06:46:20 +000053 Returns:
54 The status of the project.
55 """
56 buf = io.StringIO()
57 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf, local=local)
58 return (ret, buf.getvalue())
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050059
Gavin Makea2e3302023-03-11 06:46:20 +000060 def Execute(self, opt, args):
61 all_projects = self.GetProjects(
62 args, all_manifests=not opt.this_manifest_only
63 )
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050064
Gavin Makea2e3302023-03-11 06:46:20 +000065 def _ProcessResults(_pool, _output, results):
66 ret = 0
67 for state, output in results:
68 if output:
69 print(output, end="")
70 if not state:
71 ret = 1
72 return ret
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050073
Gavin Makea2e3302023-03-11 06:46:20 +000074 return self.ExecuteInParallel(
75 opt.jobs,
76 functools.partial(
77 self._ExecuteOne, opt.absolute, opt.this_manifest_only
78 ),
79 all_projects,
80 callback=_ProcessResults,
81 ordered=True,
82 )