forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathrevert_checker.py
executable file
·264 lines (208 loc) · 8.53 KB
/
revert_checker.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#===----------------------------------------------------------------------===##
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#===----------------------------------------------------------------------===##
"""Checks for reverts of commits across a given git commit.
To clarify the meaning of 'across' with an example, if we had the following
commit history (where `a -> b` notes that `b` is a direct child of `a`):
123abc -> 223abc -> 323abc -> 423abc -> 523abc
And where 423abc is a revert of 223abc, this revert is considered to be 'across'
323abc. More generally, a revert A of a parent commit B is considered to be
'across' a commit C if C is a parent of A and B is a parent of C.
Please note that revert detection in general is really difficult, since merge
conflicts/etc always introduce _some_ amount of fuzziness. This script just
uses a bundle of heuristics, and is bound to ignore / incorrectly flag some
reverts. The hope is that it'll easily catch the vast majority (>90%) of them,
though.
This is designed to be used in one of two ways: an import in Python, or run
directly from a shell. If you want to import this, the `find_reverts`
function is the thing to look at. If you'd rather use this from a shell, have a
usage example:
```
./revert_checker.py c47f97169 origin/main origin/release/12.x
```
This checks for all reverts from the tip of origin/main to c47f97169, which are
across the latter. It then does the same for origin/release/12.x to c47f97169.
Duplicate reverts discovered when walking both roots (origin/main and
origin/release/12.x) are deduplicated in output.
"""
importargparse
importcollections
importlogging
importre
importsubprocess
importsys
fromtypingimportGenerator, List, NamedTuple, Iterable
assertsys.version_info>= (3, 6), 'Only Python 3.6+ is supported.'
# People are creative with their reverts, and heuristics are a bit difficult.
# Like 90% of of reverts have "This reverts commit ${full_sha}".
# Some lack that entirely, while others have many of them specified in ad-hoc
# ways, while others use short SHAs and whatever.
#
# The 90% case is trivial to handle (and 100% free + automatic). The extra 10%
# starts involving human intervention, which is probably not worth it for now.
def_try_parse_reverts_from_commit_message(commit_message: str) ->List[str]:
ifnotcommit_message:
return []
results=re.findall(r'This reverts commit ([a-f0-9]{40})\b', commit_message)
first_line=commit_message.splitlines()[0]
initial_revert=re.match(r'Revert ([a-f0-9]{6,}) "', first_line)
ifinitial_revert:
results.append(initial_revert.group(1))
returnresults
def_stream_stdout(command: List[str]) ->Generator[str, None, None]:
withsubprocess.Popen(
command, stdout=subprocess.PIPE, encoding='utf-8', errors='replace') asp:
assertp.stdoutisnotNone# for mypy's happiness.
yieldfromp.stdout
def_resolve_sha(git_dir: str, sha: str) ->str:
iflen(sha) ==40:
returnsha
returnsubprocess.check_output(
['git', '-C', git_dir, 'rev-parse', sha],
encoding='utf-8',
stderr=subprocess.DEVNULL,
).strip()
_LogEntry=NamedTuple('_LogEntry', [
('sha', str),
('commit_message', str),
])
def_log_stream(git_dir: str, root_sha: str,
end_at_sha: str) ->Iterable[_LogEntry]:
sep=50*'<>'
log_command= [
'git',
'-C',
git_dir,
'log',
'^'+end_at_sha,
root_sha,
'--format='+sep+'%n%H%n%B%n',
]
stdout_stream=iter(_stream_stdout(log_command))
# Find the next separator line. If there's nothing to log, it may not exist.
# It might not be the first line if git feels complainy.
found_commit_header=False
forlineinstdout_stream:
ifline.rstrip() ==sep:
found_commit_header=True
break
whilefound_commit_header:
sha=next(stdout_stream, None)
assertshaisnotNone, 'git died?'
sha=sha.rstrip()
commit_message= []
found_commit_header=False
forlineinstdout_stream:
line=line.rstrip()
ifline.rstrip() ==sep:
found_commit_header=True
break
commit_message.append(line)
yield_LogEntry(sha, '\n'.join(commit_message).rstrip())
def_shas_between(git_dir: str, base_ref: str, head_ref: str) ->Iterable[str]:
rev_list= [
'git',
'-C',
git_dir,
'rev-list',
'--first-parent',
f'{base_ref}..{head_ref}',
]
return (x.strip() forxin_stream_stdout(rev_list))
def_rev_parse(git_dir: str, ref: str) ->str:
returnsubprocess.check_output(
['git', '-C', git_dir, 'rev-parse', ref],
encoding='utf-8',
).strip()
Revert=NamedTuple('Revert', [
('sha', str),
('reverted_sha', str),
])
def_find_common_parent_commit(git_dir: str, ref_a: str, ref_b: str) ->str:
"""Finds the closest common parent commit between `ref_a` and `ref_b`."""
returnsubprocess.check_output(
['git', '-C', git_dir, 'merge-base', ref_a, ref_b],
encoding='utf-8',
).strip()
deffind_reverts(git_dir: str, across_ref: str, root: str) ->List[Revert]:
"""Finds reverts across `across_ref` in `git_dir`, starting from `root`.
These reverts are returned in order of oldest reverts first.
"""
across_sha=_rev_parse(git_dir, across_ref)
root_sha=_rev_parse(git_dir, root)
common_ancestor=_find_common_parent_commit(git_dir, across_sha, root_sha)
ifcommon_ancestor!=across_sha:
raiseValueError(f"{across_sha} isn't an ancestor of {root_sha} "
'(common ancestor: {common_ancestor})')
intermediate_commits=set(_shas_between(git_dir, across_sha, root_sha))
assertacross_shanotinintermediate_commits
logging.debug('%d commits appear between %s and %s',
len(intermediate_commits), across_sha, root_sha)
all_reverts= []
forsha, commit_messagein_log_stream(git_dir, root_sha, across_sha):
reverts=_try_parse_reverts_from_commit_message(commit_message)
ifnotreverts:
continue
resolved_reverts=sorted(set(_resolve_sha(git_dir, x) forxinreverts))
forreverted_shainresolved_reverts:
ifreverted_shainintermediate_commits:
logging.debug('Commit %s reverts %s, which happened after %s', sha,
reverted_sha, across_sha)
continue
try:
object_type=subprocess.check_output(
['git', '-C', git_dir, 'cat-file', '-t', reverted_sha],
encoding='utf-8',
stderr=subprocess.DEVNULL,
).strip()
exceptsubprocess.CalledProcessError:
logging.warning(
'Failed to resolve reverted object %s (claimed to be reverted '
'by sha %s)', reverted_sha, sha)
continue
ifobject_type=='commit':
all_reverts.append(Revert(sha, reverted_sha))
continue
logging.error("%s claims to revert %s -- which isn't a commit -- %s", sha,
object_type, reverted_sha)
# Since `all_reverts` contains reverts in log order (e.g., newer comes before
# older), we need to reverse this to keep with our guarantee of older =
# earlier in the result.
all_reverts.reverse()
returnall_reverts
def_main() ->None:
parser=argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'base_ref', help='Git ref or sha to check for reverts around.')
parser.add_argument(
'-C', '--git_dir', default='.', help='Git directory to use.')
parser.add_argument(
'root', nargs='+', help='Root(s) to search for commits from.')
parser.add_argument('--debug', action='store_true')
opts=parser.parse_args()
logging.basicConfig(
format='%(asctime)s: %(levelname)s: %(filename)s:%(lineno)d: %(message)s',
level=logging.DEBUGifopts.debugelselogging.INFO,
)
# `root`s can have related history, so we want to filter duplicate commits
# out. The overwhelmingly common case is also to have one root, and it's way
# easier to reason about output that comes in an order that's meaningful to
# git.
seen_reverts=set()
all_reverts= []
forrootinopts.root:
forrevertinfind_reverts(opts.git_dir, opts.base_ref, root):
ifrevertnotinseen_reverts:
seen_reverts.add(revert)
all_reverts.append(revert)
forrevertinall_reverts:
print(f'{revert.sha} claims to revert {revert.reverted_sha}')
if__name__=='__main__':
_main()