blob: 59b50de5d6692be0653e5625683430116291cf3e [file] [log] [blame]
Kuang-che Wu3eb6b502018-06-06 16:15:18 +08001#!/usr/bin/env python2
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08002# -*- coding: utf-8 -*-
Kuang-che Wu3eb6b502018-06-06 16:15:18 +08003# Copyright 2018 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6"""Chrome bisector to bisect a range of chrome commits.
7
8This bisector bisects commits between branched chrome and releases.
9"""
10from __future__ import print_function
11import logging
12import os
13
14from bisect_kit import cli
15from bisect_kit import codechange
16from bisect_kit import configure
17from bisect_kit import core
18from bisect_kit import cros_util
19from bisect_kit import cr_util
Kuang-che Wue121fae2018-11-09 16:18:39 +080020from bisect_kit import errors
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080021from bisect_kit import gclient_util
22from bisect_kit import util
23
24logger = logging.getLogger(__name__)
25
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080026
27def guess_chrome_version(opts, rev):
28 if cros_util.is_cros_version(rev):
29 assert opts.board, 'need to specify BOARD for cros version'
Kuang-che Wu8a28a9d2018-09-11 17:43:36 +080030 chrome_version = cros_util.query_chrome_version(opts.board, rev)
31 assert cr_util.is_chrome_version(chrome_version)
32 logger.info('Converted given CrOS version %s to Chrome version %s', rev,
33 chrome_version)
34 rev = chrome_version
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080035
36 return rev
37
38
39class ChromeSrcDomain(core.BisectDomain):
40 """BisectDomain for Chrome branched tree"""
Kuang-che Wu752228c2018-09-05 13:54:22 +080041 revtype = staticmethod(
42 cli.argtype_multiplexer(cr_util.argtype_chrome_version,
43 cros_util.argtype_cros_version))
44 intra_revtype = staticmethod(
45 codechange.argtype_intra_rev(cr_util.argtype_chrome_version))
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080046 help = globals()['__doc__']
47
48 @staticmethod
49 def add_init_arguments(parser):
50 parser.add_argument(
51 '--chrome_root',
52 required=True,
53 metavar='CHROME_ROOT',
54 type=cli.argtype_dir_path,
55 default=configure.get('CHROME_ROOT'),
56 help='Root of chrome source tree, like ~/chromium')
57 parser.add_argument(
Kuang-che Wud8fc9572018-10-03 21:00:41 +080058 '--chrome_mirror',
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080059 required=True,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080060 metavar='CHROME_MIRROR',
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080061 type=cli.argtype_dir_path,
Kuang-che Wud8fc9572018-10-03 21:00:41 +080062 default=configure.get('CHROME_MIRROR'),
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080063 help='gclient cache dir')
64
65 # Only used for Chrome on ChromeOS.
66 parser.add_argument(
67 '--dut',
68 type=cli.argtype_notempty,
69 metavar='DUT',
70 default=configure.get('DUT'),
71 help='For ChromeOS, address of DUT (Device Under Test)')
72 parser.add_argument(
73 '--board',
74 metavar='BOARD',
75 default=configure.get('BOARD'),
76 help='For ChromeOS, board name')
77
78 @staticmethod
79 def init(opts):
80 chrome_src = os.path.join(opts.chrome_root, 'src')
81 if not os.path.exists(chrome_src):
Kuang-che Wue121fae2018-11-09 16:18:39 +080082 raise errors.ArgumentError('--chrome_root',
83 "chrome src directory doesn't exist")
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080084
85 if opts.dut:
Kuang-che Wue121fae2018-11-09 16:18:39 +080086 if not cros_util.is_dut(opts.dut):
87 raise errors.ArgumentError('--dut', 'invalid DUT or unable to connect')
88
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080089 if not opts.board:
90 opts.board = cros_util.query_dut_board(opts.dut)
91
92 old = guess_chrome_version(opts, opts.old)
93 new = guess_chrome_version(opts, opts.new)
Kuang-che Wue121fae2018-11-09 16:18:39 +080094 if old == new:
95 raise errors.ArgumentError(
96 '--old and --new', 'start and end of chrome versions are identical')
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080097
98 if not util.is_version_lesseq(old, new):
Kuang-che Wue121fae2018-11-09 16:18:39 +080099 raise errors.ArgumentError(
100 '--old and --new',
101 'start chrome version (%s) is newer than end version (%s)' % (old,
102 new))
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800103
104 config = dict(
105 chrome_root=opts.chrome_root,
106 old=old,
107 new=new,
108 board=opts.board,
109 dut=opts.dut,
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800110 chrome_mirror=opts.chrome_mirror)
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800111
112 spec_manager = cr_util.ChromeSpecManager(config)
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800113 cache = gclient_util.GclientCache(opts.chrome_mirror)
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800114
115 # Initial sync to get all buildspecs.
116 release_deps_file = cr_util.ChromeSpecManager.get_release_deps(new)
117 path = os.path.join(opts.chrome_root, 'buildspec', release_deps_file)
118 if not os.path.exists(path):
119 spec_manager.sync_to_release(new)
120
121 for rev in (old, new):
122 release_deps_file = cr_util.ChromeSpecManager.get_release_deps(rev)
123 path = os.path.join(opts.chrome_root, 'buildspec', release_deps_file)
124 if not os.path.exists(path):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800125 raise errors.InternalError("%s doesn't exist" % path)
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800126
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800127 if not util.is_direct_relative_version(old, new):
128 logger.warning('old=%s is not parent of new=%s', old, new)
129 old_ancestor = spec_manager.enumerate_ancestor(old)
130 new_ancestor = spec_manager.enumerate_ancestor(new)
131 for rev in reversed(old_ancestor):
132 if rev in new_ancestor:
133 lowest_common_ancestor = rev
134 break
135 else:
Kuang-che Wue121fae2018-11-09 16:18:39 +0800136 raise errors.InternalError(
137 'Unable to find their common ancestor: %s, %s' % (old, new))
Kuang-che Wu74768d32018-09-07 12:03:24 +0800138 logger.warning(
139 'Assume their lowest common ancestor, %s,'
140 'still have expected old behavior as %s', lowest_common_ancestor, old)
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800141 config['old'] = old = lowest_common_ancestor
142
Kuang-che Wu8a28a9d2018-09-11 17:43:36 +0800143 # TODO(kcwu): verify full git history is available.
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800144 code_manager = codechange.CodeManager(opts.chrome_root, spec_manager, cache)
145 revlist = code_manager.build_revlist(old, new)
146
147 return config, revlist
148
149 def __init__(self, config):
150 self.config = config
151
152 def setenv(self, env, rev):
153 env['CHROME_ROOT'] = self.config['chrome_root']
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800154 env['CHROME_MIRROR'] = self.config['chrome_mirror']
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800155 env['REV'] = rev
156
157 if self.config['board']:
158 env['BOARD'] = self.config['board']
159 if self.config['dut']:
160 env['DUT'] = self.config['dut']
161
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800162 def view(self, revlist, old, new):
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800163 print('old', old)
164 print('new', new)
165
166 old_base, old_next, _ = codechange.parse_intra_rev(old)
167 new_base, new_next, _ = codechange.parse_intra_rev(new)
168 # Only print log url if the range is within two releases.
169 if old_next in (new_base, new_next):
170 log_url = ('https://chromium.googlesource.com/chromium/src/+log/%s..%s'
171 '?pretty=fuller&n=10000')
172 print('Be careful the following url only lists chrome src/ commits. For '
173 'example, commits inside v8 and third party repos are not listed.')
174 if old_base != old_next:
175 print(log_url % (old_base, old_next))
176 if new_base != new_next and old_next != new_next:
177 print(log_url % (new_base, new_next))
178
Kuang-che Wud8fc9572018-10-03 21:00:41 +0800179 cache = gclient_util.GclientCache(self.config['chrome_mirror'])
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800180 spec_manager = cr_util.ChromeSpecManager(self.config)
181 code_manager = codechange.CodeManager(self.config['chrome_root'],
182 spec_manager, cache)
Kuang-che Wu81aecc02018-10-31 19:37:32 +0800183 code_manager.view_rev_diff(revlist, old, new)
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800184
185
186if __name__ == '__main__':
187 cli.BisectorCommandLine(ChromeSrcDomain).main()