blob: 85710feede2b1160015a7266ab432e98a72e86c3 [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."""
29 revtype = staticmethod(git_util.argtype_git_rev)
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 Wue121fae2018-11-09 16:18:39 +080042 if not git_util.is_containing_commit(opts.git_repo, opts.old):
43 raise errors.ArgumentError(
44 '--old', '%s is not in git_repo=%r' % (opts.old, opts.git_repo))
45 if not git_util.is_containing_commit(opts.git_repo, opts.new):
46 raise errors.ArgumentError(
47 '--new', '%s is not in git_repo=%r' % (opts.new, opts.git_repo))
Kuang-che Wue41e0062017-09-01 19:04:14 +080048
49 config = dict(git_repo=opts.git_repo)
50 revlist = git_util.get_revlist(opts.git_repo, opts.old, opts.new)
51 return config, revlist
52
53 def __init__(self, config):
54 self.config = config
55
56 def setenv(self, env, rev):
57 env['GIT_REPO'] = self.config['git_repo']
58 env['GIT_REV'] = rev
59
Kuang-che Wue80bb872018-11-15 19:45:25 +080060 def fill_candidate_summary(self, summary, interesting_indexes):
61 for i in interesting_indexes:
62 rev_info = summary['rev_info'][i]
63 rev = rev_info['rev']
64 try:
65 commit_summary = git_util.get_commit_log(self.config['git_repo'],
66 rev).splitlines()[0]
67 except subprocess.CalledProcessError:
68 logger.warning('failed to get commit log of %s at %s', rev[:10],
69 self.config['git_repo'])
70 commit_summary = '(unknown)'
71 text = 'commit %s %r' % (rev[:10], commit_summary)
72 rev_info.update({
73 'actions': [{
74 'text': text,
75 }],
76 })
Kuang-che Wue41e0062017-09-01 19:04:14 +080077
78
79if __name__ == '__main__':
Kuang-che Wu68db08a2018-03-30 11:50:34 +080080 cli.BisectorCommandLine(GitDomain).main()