blob: 8186817603f519df55b6bcb03f95b6f78810f06f [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
17import multiprocessing
18
19from command import DEFAULT_LOCAL_JOBS, PagedCommand, WORKER_BATCH_SIZE
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):
23 common = True
24 helpSummary = "Show changes between commit and working tree"
25 helpUsage = """
26%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"""
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050032 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033
pelyad67872d2012-03-28 14:49:58 +030034 def _Options(self, p):
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050035 super()._Options(p)
pelyad67872d2012-03-28 14:49:58 +030036 p.add_option('-u', '--absolute',
37 dest='absolute', action='store_true',
38 help='Paths are relative to the repository root')
39
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050040 def _DiffHelper(self, absolute, project):
41 """Obtains the diff for a specific project.
42
43 Args:
44 absolute: Paths are relative to the root.
45 project: Project to get status of.
46
47 Returns:
48 The status of the project.
49 """
50 buf = io.StringIO()
51 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf)
52 return (ret, buf.getvalue())
53
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070054 def Execute(self, opt, args):
Mike Frysinger0a9265e2019-09-30 23:59:27 -040055 ret = 0
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050056 all_projects = self.GetProjects(args)
57
58 # NB: Multiprocessing is heavy, so don't spin it up for one job.
59 if len(all_projects) == 1 or opt.jobs == 1:
60 for project in all_projects:
61 if not project.PrintWorkTreeDiff(opt.absolute):
62 ret = 1
63 else:
64 with multiprocessing.Pool(opt.jobs) as pool:
65 states = pool.imap(functools.partial(self._DiffHelper, opt.absolute),
66 all_projects, WORKER_BATCH_SIZE)
67 for (state, output) in states:
68 if output:
69 print(output, end='')
70 if not state:
71 ret = 1
72
Mike Frysinger0a9265e2019-09-30 23:59:27 -040073 return ret