blob: 5c627c0cdfc9d9e29690fcf68629ec383604810c [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 Frysingerb5d075d2021-03-01 00:56:38 -050018from command import DEFAULT_LOCAL_JOBS, PagedCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070019
David Pursehouse819827a2020-02-12 15:20:19 +090020
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021class Diff(PagedCommand):
Gavin Makea2e3302023-03-11 06:46:20 +000022 COMMON = True
23 helpSummary = "Show changes between commit and working tree"
24 helpUsage = """
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025%prog [<project>...]
pelyad67872d2012-03-28 14:49:58 +030026
27The -u option causes '%prog' to generate diff output with file paths
28relative to the repository root, so the output can be applied
29to the Unix 'patch' command.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030"""
Gavin Makea2e3302023-03-11 06:46:20 +000031 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Gavin Makea2e3302023-03-11 06:46:20 +000033 def _Options(self, p):
34 p.add_option(
35 "-u",
36 "--absolute",
37 dest="absolute",
38 action="store_true",
39 help="paths are relative to the repository root",
40 )
pelyad67872d2012-03-28 14:49:58 +030041
Gavin Makea2e3302023-03-11 06:46:20 +000042 def _ExecuteOne(self, absolute, local, project):
43 """Obtains the diff for a specific project.
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050044
Gavin Makea2e3302023-03-11 06:46:20 +000045 Args:
46 absolute: Paths are relative to the root.
47 local: a boolean, if True, the path is relative to the local
48 (sub)manifest. If false, the path is relative to the outermost
49 manifest.
50 project: Project to get status of.
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050051
Gavin Makea2e3302023-03-11 06:46:20 +000052 Returns:
53 The status of the project.
54 """
55 buf = io.StringIO()
56 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf, local=local)
57 return (ret, buf.getvalue())
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050058
Gavin Makea2e3302023-03-11 06:46:20 +000059 def Execute(self, opt, args):
60 all_projects = self.GetProjects(
61 args, all_manifests=not opt.this_manifest_only
62 )
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050063
Gavin Makea2e3302023-03-11 06:46:20 +000064 def _ProcessResults(_pool, _output, results):
65 ret = 0
66 for state, output in results:
67 if output:
68 print(output, end="")
69 if not state:
70 ret = 1
71 return ret
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050072
Gavin Makea2e3302023-03-11 06:46:20 +000073 return self.ExecuteInParallel(
74 opt.jobs,
75 functools.partial(
76 self._ExecuteOne, opt.absolute, opt.this_manifest_only
77 ),
78 all_projects,
79 callback=_ProcessResults,
80 ordered=True,
81 )