blob: a606ee9aaa1cdad9f8b2c6d50b13036b704a156e [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):
Mike Frysinger4f210542021-06-14 16:05:19 -040022 COMMON = True
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023 helpSummary = "Show changes between commit and working tree"
24 helpUsage = """
25%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"""
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050031 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
pelyad67872d2012-03-28 14:49:58 +030033 def _Options(self, p):
pelyad67872d2012-03-28 14:49:58 +030034 p.add_option('-u', '--absolute',
35 dest='absolute', action='store_true',
Mike Frysingerc177f942021-05-04 08:06:36 -040036 help='paths are relative to the repository root')
pelyad67872d2012-03-28 14:49:58 +030037
LaMont Jones8501d462022-06-22 19:21:15 +000038 def _ExecuteOne(self, absolute, local, project):
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050039 """Obtains the diff for a specific project.
40
41 Args:
42 absolute: Paths are relative to the root.
LaMont Jones8501d462022-06-22 19:21:15 +000043 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 Frysinger69b4a9c2021-02-16 18:18:01 -050046 project: Project to get status of.
47
48 Returns:
49 The status of the project.
50 """
51 buf = io.StringIO()
LaMont Jones8501d462022-06-22 19:21:15 +000052 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf, local=local)
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050053 return (ret, buf.getvalue())
54
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070055 def Execute(self, opt, args):
LaMont Jonescc879a92021-11-18 22:40:18 +000056 all_projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only)
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050057
Mike Frysingerb5d075d2021-03-01 00:56:38 -050058 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 Frysinger69b4a9c2021-02-16 18:18:01 -050064 ret = 1
Mike Frysingerb5d075d2021-03-01 00:56:38 -050065 return ret
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050066
Mike Frysingerb5d075d2021-03-01 00:56:38 -050067 return self.ExecuteInParallel(
68 opt.jobs,
LaMont Jones8501d462022-06-22 19:21:15 +000069 functools.partial(self._ExecuteOne, opt.absolute, opt.this_manifest_only),
Mike Frysingerb5d075d2021-03-01 00:56:38 -050070 all_projects,
71 callback=_ProcessResults,
72 ordered=True)