blob: 4544a3278a8e2b77104bc39fc3d9879473e79967 [file] [log] [blame]
Kuang-che Wue41e0062017-09-01 19:04:14 +08001#!/usr/bin/env python2
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
19from bisect_kit import cli
20from bisect_kit import core
Kuang-che Wue121fae2018-11-09 16:18:39 +080021from bisect_kit import errors
Kuang-che Wue41e0062017-09-01 19:04:14 +080022from bisect_kit import git_util
23
Kuang-che Wue80bb872018-11-15 19:45:25 +080024logger = logging.getLogger(__name__)
25
Kuang-che Wue41e0062017-09-01 19:04:14 +080026
27class GitDomain(core.BisectDomain):
28 """BisectDomain for git revisions."""
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080029 revtype = staticmethod(cli.argtype_notempty)
Kuang-che Wub2376262017-11-20 18:05:24 +080030 help = globals()['__doc__']
Kuang-che Wue41e0062017-09-01 19:04:14 +080031
32 @staticmethod
33 def add_init_arguments(parser):
34 parser.add_argument(
35 '--git_repo',
36 required=True,
37 type=cli.argtype_dir_path,
38 help='Git repository path')
39
40 @staticmethod
41 def init(opts):
Kuang-che Wu5e7c9b02019-01-03 21:16:01 +080042 try:
43 opts.old = git_util.get_commit_hash(opts.git_repo, opts.old)
44 except ValueError as e:
45 raise errors.ArgumentError('--old', e.message)
46 try:
47 opts.new = git_util.get_commit_hash(opts.git_repo, opts.new)
48 except ValueError as e:
49 raise errors.ArgumentError('--new', e.message)
Kuang-che Wue41e0062017-09-01 19:04:14 +080050
51 config = dict(git_repo=opts.git_repo)
52 revlist = git_util.get_revlist(opts.git_repo, opts.old, opts.new)
53 return config, revlist
54
55 def __init__(self, config):
56 self.config = config
57
58 def setenv(self, env, rev):
59 env['GIT_REPO'] = self.config['git_repo']
60 env['GIT_REV'] = rev
61
Kuang-che Wue80bb872018-11-15 19:45:25 +080062 def fill_candidate_summary(self, summary, interesting_indexes):
63 for i in interesting_indexes:
64 rev_info = summary['rev_info'][i]
65 rev = rev_info['rev']
66 try:
67 commit_summary = git_util.get_commit_log(self.config['git_repo'],
68 rev).splitlines()[0]
69 except subprocess.CalledProcessError:
70 logger.warning('failed to get commit log of %s at %s', rev[:10],
71 self.config['git_repo'])
72 commit_summary = '(unknown)'
73 text = 'commit %s %r' % (rev[:10], commit_summary)
74 rev_info.update({
75 'actions': [{
76 'text': text,
77 }],
78 })
Kuang-che Wue41e0062017-09-01 19:04:14 +080079
80
81if __name__ == '__main__':
Kuang-che Wu68db08a2018-03-30 11:50:34 +080082 cli.BisectorCommandLine(GitDomain).main()