blob: 00a7ec298ab5458529416e152c5dc7a447e3aab1 [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
Mike Frysingerb5d075d2021-03-01 00:56:38 -050038 def _ExecuteOne(self, absolute, 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.
43 project: Project to get status of.
44
45 Returns:
46 The status of the project.
47 """
48 buf = io.StringIO()
49 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf)
50 return (ret, buf.getvalue())
51
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052 def Execute(self, opt, args):
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050053 all_projects = self.GetProjects(args)
54
Mike Frysingerb5d075d2021-03-01 00:56:38 -050055 def _ProcessResults(_pool, _output, results):
56 ret = 0
57 for (state, output) in results:
58 if output:
59 print(output, end='')
60 if not state:
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050061 ret = 1
Mike Frysingerb5d075d2021-03-01 00:56:38 -050062 return ret
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050063
Mike Frysingerb5d075d2021-03-01 00:56:38 -050064 return self.ExecuteInParallel(
65 opt.jobs,
66 functools.partial(self._ExecuteOne, opt.absolute),
67 all_projects,
68 callback=_ProcessResults,
69 ordered=True)