blob: b3f712ce4511e5a509620a7ef067515c6eef055b [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08002# -*- coding: utf-8 -*-
Kuang-che Wue41e0062017-09-01 19:04:14 +08003# Copyright 2017 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"""Git bisector to bisect a range of git commits.
7
8Example:
9 $ ./bisect_git.py init --old rev1 --new rev2 --git_repo /path/to/git/repo
10 $ ./bisect_git.py config switch ./switch_git.py
11 $ ./bisect_git.py config eval ./eval-manually.sh
12 $ ./bisect_git.py run
13"""
14
15from __future__ import print_function
Kuang-che Wue80bb872018-11-15 19:45:25 +080016import logging
17import subprocess
Kuang-che Wue41e0062017-09-01 19:04:14 +080018
Kuang-che Wu2526a672019-09-10 16:23:59 +080019from bisect_kit import bisector_cli
Kuang-che Wue41e0062017-09-01 19:04:14 +080020from bisect_kit import cli
21from bisect_kit import core
Kuang-che Wue121fae2018-11-09 16:18:39 +080022from bisect_kit import errors
Kuang-che Wue41e0062017-09-01 19:04:14 +080023from bisect_kit import git_util
24
Kuang-che Wue80bb872018-11-15 19:45:25 +080025logger = logging.getLogger(__name__)
26
Kuang-che Wue41e0062017-09-01 19:04:14 +080027
28class GitDomain(core.BisectDomain):
29 """BisectDomain for git revisions."""
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080030 revtype = staticmethod(cli.argtype_notempty)
Kuang-che Wub2376262017-11-20 18:05:24 +080031 help = globals()['__doc__']
Kuang-che Wue41e0062017-09-01 19:04:14 +080032
33 @staticmethod
34 def add_init_arguments(parser):
35 parser.add_argument(
36 '--git_repo',
37 required=True,
38 type=cli.argtype_dir_path,
39 help='Git repository path')
40
41 @staticmethod
42 def init(opts):
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080043 try:
44 opts.old = git_util.get_commit_hash(opts.git_repo, opts.old)
45 except ValueError as e:
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +080046 raise errors.ArgumentError('--old', str(e))
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080047 try:
48 opts.new = git_util.get_commit_hash(opts.git_repo, opts.new)
49 except ValueError as e:
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +080050 raise errors.ArgumentError('--new', str(e))
Kuang-che Wue41e0062017-09-01 19:04:14 +080051
52 config = dict(git_repo=opts.git_repo)
53 revlist = git_util.get_revlist(opts.git_repo, opts.old, opts.new)
54 return config, revlist
55
56 def __init__(self, config):
57 self.config = config
58
59 def setenv(self, env, rev):
60 env['GIT_REPO'] = self.config['git_repo']
61 env['GIT_REV'] = rev
62
Kuang-che Wue80bb872018-11-15 19:45:25 +080063 def fill_candidate_summary(self, summary, interesting_indexes):
64 for i in interesting_indexes:
65 rev_info = summary['rev_info'][i]
66 rev = rev_info['rev']
67 try:
68 commit_summary = git_util.get_commit_log(self.config['git_repo'],
69 rev).splitlines()[0]
70 except subprocess.CalledProcessError:
71 logger.warning('failed to get commit log of %s at %s', rev[:10],
72 self.config['git_repo'])
73 commit_summary = '(unknown)'
74 text = 'commit %s %r' % (rev[:10], commit_summary)
75 rev_info.update({
76 'actions': [{
77 'text': text,
78 }],
79 })
Kuang-che Wue41e0062017-09-01 19:04:14 +080080
81
82if __name__ == '__main__':
Kuang-che Wu2526a672019-09-10 16:23:59 +080083 bisector_cli.BisectorCommandLine(GitDomain).main()