Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 3 | # 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 | |
| 8 | Example: |
| 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 | |
| 15 | from __future__ import print_function |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 16 | import logging |
| 17 | import subprocess |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 18 | |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame^] | 19 | from bisect_kit import bisector_cli |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 20 | from bisect_kit import cli |
| 21 | from bisect_kit import core |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 22 | from bisect_kit import errors |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 23 | from bisect_kit import git_util |
| 24 | |
Kuang-che Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 25 | logger = logging.getLogger(__name__) |
| 26 | |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 27 | |
| 28 | class GitDomain(core.BisectDomain): |
| 29 | """BisectDomain for git revisions.""" |
Kuang-che Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 30 | revtype = staticmethod(cli.argtype_notempty) |
Kuang-che Wu | b237626 | 2017-11-20 18:05:24 +0800 | [diff] [blame] | 31 | help = globals()['__doc__'] |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 32 | |
| 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 Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 43 | try: |
| 44 | opts.old = git_util.get_commit_hash(opts.git_repo, opts.old) |
| 45 | except ValueError as e: |
| 46 | raise errors.ArgumentError('--old', e.message) |
| 47 | try: |
| 48 | opts.new = git_util.get_commit_hash(opts.git_repo, opts.new) |
| 49 | except ValueError as e: |
| 50 | raise errors.ArgumentError('--new', e.message) |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 51 | |
| 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 Wu | e80bb87 | 2018-11-15 19:45:25 +0800 | [diff] [blame] | 63 | 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 Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
Kuang-che Wu | 2526a67 | 2019-09-10 16:23:59 +0800 | [diff] [blame^] | 83 | bisector_cli.BisectorCommandLine(GitDomain).main() |