-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge_bisect.py
executable file
·205 lines (161 loc) · 5.26 KB
/
merge_bisect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals,
)
import argparse
import datetime
import subprocess
import sys
from collections import OrderedDict
from contextlib import contextmanager
class Call(object):
def __init__(self, cmd):
self._p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.stdout, self.stderr = self._p.communicate()
self.returncode = self._p.returncode
def __bool__(self):
return self.returncode == 0
__nonzero__ = __bool__
class Commit(object):
def __init__(self, date, sha1, author, description):
self.datetime = datetime.datetime.fromtimestamp(int(date))
self.sha1 = sha1
self.author = author
self.description = description
@classmethod
def from_log(cls, s, delimiter='\t'):
return cls(*s.split(delimiter))
def __repr__(self):
return (
'<{} "{datetime}" {sha1} "{author}" "{description}">'
''.format(self.__class__.__name__, **vars(self))
)
def __eq__(self, other):
return self.sha1 == other.sha1
def commits_for_n_days(days):
since = datetime.datetime.utcnow() - datetime.timedelta(days=days)
cmd = (
'git log '
'--first-parent '
'--pretty="format:%at\t%H\t%an\t%s" '
'--since={since}'
).format(since=since.strftime('%Y-%m-%d'))
return [
Commit.from_log(i)
for i in Call(cmd).stdout.splitlines()
if i
]
def current_branch():
branch = Call('git rev-parse --abbrev-ref HEAD').stdout
# already checkout to particular commit
if branch == 'HEAD':
return Call('git rev-parse HEAD').stdout
return branch
def checkout(sha1):
Call('git checkout {}'.format(sha1))
@contextmanager
def stay_on_branch():
branch = current_branch()
try:
yield
finally:
checkout(branch)
def call_on_commit(cmd, commit, verbose=False):
checkout(commit.sha1)
c = Call(cmd)
if verbose:
print('\n' * 2)
if c:
print('PASSED: {!r}'.format(commit))
else:
print('FAILED: {!r}'.format(commit))
if verbose:
print('=' * 150)
print(c.stdout)
print(c.stderr)
print('=' * 150)
print('\n' * 3)
return c
parser = argparse.ArgumentParser(
description='Like git bisect, but on merge commits.'
)
parser.add_argument(
'cmd',
help='Command to run in order to find whether the commit is good or bad. ',
)
parser.add_argument(
'--days',
type=int,
default=30,
help='Check merge commits only going this many days '
'in the past against the given command.',
)
parser.add_argument(
'-v', '--verbose',
dest='verbose',
action='store_true',
default=False,
help='Print stdout while running each command.'
)
def main():
args = parser.parse_args()
with stay_on_branch():
all_commits = OrderedDict((i, None) for i in reversed(commits_for_n_days(args.days)))
commits = all_commits.keys()
print('Found {} commits'.format(len(commits)))
print('')
if len(commits) < 2:
print('At least 2 merge commits must be present in order to bisect on merges', file=sys.stderr)
return 1
commit = commits[0]
commits.remove(commit)
commit_call = call_on_commit(args.cmd, commit, args.verbose)
all_commits[commit] = bool(commit_call)
if not commit_call:
print(
'Earliest commit {!r} already fails running "{}". '
'At least one passing commit should be succeeding in the resultset to do bisect.'
''.format(commit, args.cmd)
)
return 1
commit = commits[-1]
commits.remove(commit)
commit_call = call_on_commit(args.cmd, commit, args.verbose)
all_commits[commit] = bool(commit_call)
if commit_call:
print(
'Latest commit {!r} already succeeds running "{}". '
'At least one passing commit should be failing in the resultset to do bisect.'
''.format(commit, args.cmd)
)
return 1
while commits:
middle = len(commits) // 2
commit = commits[middle]
commit_call = call_on_commit(args.cmd, commit, args.verbose)
all_commits[commit] = bool(commit_call)
if commit_call:
for c in commits[:middle]:
all_commits[c] = bool(commit_call)
commits = commits[middle + 1:]
else:
for c in commits[middle + 1:]:
all_commits[c] = bool(commit_call)
commits = commits[:middle]
bad_commit = next(commit for commit, is_good in all_commits.items() if not is_good)
print('')
print('Done')
print('')
print('Commit log (last commit first):')
for commit, is_good in reversed(all_commits.items()):
t = 'SUCCESS' if is_good else 'FAILURE'
print('{}: {!r}'.format(t, commit))
print()
print('BAD COMMIT: {!r}'.format(bad_commit))
return 0
if __name__ == '__main__':
exit(main())