blob: cdc262e639d846545933023c973a96a6dfd76509 [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):
pelyad67872d2012-03-28 14:49:58 +030035 p.add_option('-u', '--absolute',
36 dest='absolute', action='store_true',
37 help='Paths are relative to the repository root')
38
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050039 def _DiffHelper(self, absolute, project):
40 """Obtains the diff for a specific project.
41
42 Args:
43 absolute: Paths are relative to the root.
44 project: Project to get status of.
45
46 Returns:
47 The status of the project.
48 """
49 buf = io.StringIO()
50 ret = project.PrintWorkTreeDiff(absolute, output_redir=buf)
51 return (ret, buf.getvalue())
52
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070053 def Execute(self, opt, args):
Mike Frysinger0a9265e2019-09-30 23:59:27 -040054 ret = 0
Mike Frysinger69b4a9c2021-02-16 18:18:01 -050055 all_projects = self.GetProjects(args)
56
57 # NB: Multiprocessing is heavy, so don't spin it up for one job.
58 if len(all_projects) == 1 or opt.jobs == 1:
59 for project in all_projects:
60 if not project.PrintWorkTreeDiff(opt.absolute):
61 ret = 1
62 else:
63 with multiprocessing.Pool(opt.jobs) as pool:
64 states = pool.imap(functools.partial(self._DiffHelper, opt.absolute),
65 all_projects, WORKER_BATCH_SIZE)
66 for (state, output) in states:
67 if output:
68 print(output, end='')
69 if not state:
70 ret = 1
71
Mike Frysinger0a9265e2019-09-30 23:59:27 -040072 return ret