-
Notifications
You must be signed in to change notification settings - Fork 4
/
initdb.py
executable file
·319 lines (265 loc) · 11.6 KB
/
initdb.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# -*- coding: utf-8 -*-"
"""Initialize rebase database"""
import sqlite3
import os
import re
import subprocess
import time
from common import rebasedb
from common import stable_path, android_path, chromeos_path
from common import stable_baseline, rebase_baseline, rebase_target_tag
stable_commits = rebase_baseline() + '..' + stable_baseline()
baseline_commits = rebase_baseline() + '..'
# "commit" is sometimes seen multiple times, such as with commit 6093aabdd0ee
cherrypick = re.compile(r'cherry picked from (commit )+([a-z0-9]+)')
stable = re.compile(r'(commit )+([a-z0-9]+) upstream')
stable2 = re.compile(r'Upstream (commit )+([a-z0-9]+)')
upstream = re.compile(r'(ANDROID: *|UPSTREAM: *|FROMGIT: *|BACKPORT: *)+(.*)')
chromium = re.compile(r'(CHROMIUM: *|FROMLIST: *)+(.*)')
changeid = re.compile(r'\s*Change-Id:\s+(I[a-z0-9]+)')
def NOW():
"""Return current time"""
return int(time.time())
def removedb():
"""remove database if it exists"""
try:
os.remove(rebasedb)
except OSError:
pass
def createdb():
"""remove and recreate database"""
removedb()
conn = sqlite3.connect(rebasedb)
c = conn.cursor()
# Create table
c.execute('CREATE TABLE commits (date integer, \
created timestamp, updated timestamp, \
authored timestamp, committed timestamp, \
sha text, usha text, \
patchid text, \
changeid text, \
subject text, topic integer, \
contact text, \
email text, \
disposition text, reason text, \
sscore integer, pscore integer, dsha text)')
c.execute('CREATE UNIQUE INDEX commit_date ON commits (date)')
c.execute('CREATE INDEX commit_sha ON commits (sha)')
c.execute('CREATE INDEX upstream_sha ON commits (usha)')
c.execute('CREATE INDEX patch_id ON commits (patchid)')
c.execute('CREATE TABLE files (sha text, filename text)')
c.execute('CREATE INDEX file_sha ON files (sha)')
c.execute('CREATE INDEX file_name ON files (filename)')
c.execute('CREATE TABLE stable (sha, origin)')
c.execute('CREATE UNIQUE INDEX stable_sha ON stable (sha)')
c.execute('CREATE TABLE topics (topic integer, name text)')
c.execute('CREATE UNIQUE INDEX topics_index ON topics (topic)')
# Save (commit) the changes
conn.commit()
conn.close()
# date:
# git show --format="%ct" -s ${sha}
# subject:
# git show --format="%s" -s ${sha}
# file list:
# git show --name-only --format="" ${sha}
# Insert a row of data
# c.execute("INSERT INTO commits VALUES (1489758183,sha,subject)")
# c.execute("INSERT INTO files VALUES (sha,filename)")
# sha and filename must be in ' '
def update_stable(path, sha_list, origin):
"""Create list of SHAs from provided path and commit list.
Skip if entry (sha) is already in database.
"""
conn = sqlite3.connect(rebasedb)
c = conn.cursor()
cmd = ['git', '-C', path, 'log', '--no-merges', '--abbrev=12', '--oneline',
'--reverse', sha_list]
commits = subprocess.check_output(cmd, encoding='utf-8', errors='ignore')
for commit in commits.splitlines():
if commit != '':
elem = commit.split(' ')[:1]
sha = elem[0]
c.execute("select sha from stable where sha is '%s'" % sha)
found = c.fetchall()
if found == []:
c.execute('INSERT INTO stable(sha, origin) VALUES (?, ?)', (
sha,
origin,
))
conn.commit()
conn.close()
def get_contact(path, sha):
"""Return first commit signer, tester, or submitter with a Google e-mail address.
If there is none, pick the last reviewer.
If there is none, return None, None.
"""
contact = None
email = None
cmd = ['git', '-C', path, 'log', '--format=%B', '-n', '1', sha]
commit_message = subprocess.check_output(
cmd, encoding='utf-8', errors='ignore')
tags = 'Signed-off-by|Commit-Queue|Tested-by'
domains = 'chromium.org|google.com|collabora.com'
m = '^(?:%s): (.*) <(.*@(?:%s))>$' % (tags, domains)
emails = re.findall(m, commit_message, re.M)
if emails:
contact, email = emails[0]
else:
tags = 'Reviewed-by'
m = '^(?:%s): (.*) <(.*@(?:%s))>$' % (tags, domains)
emails = re.findall(m, commit_message, re.M)
if emails:
contact, email = emails[-1]
return contact, email
def update_commits():
"""Get complete list of commits from rebase baseline.
Assume that the baseline branch exists and has been checked out.
"""
conn = sqlite3.connect(rebasedb)
c = conn.cursor()
cmd = ['git', '-C', chromeos_path, 'log', '--no-merges', '--abbrev=12',
'--reverse', '--format=%at%x01%ct%x01%h%x01%an%x01%ae%x01%s',
rebase_baseline() + '..']
commits = subprocess.check_output(cmd, encoding='utf-8', errors='ignore')
prevdate = 0
mprevdate = 0
for commit in commits.splitlines(): # pylint: disable=too-many-nested-blocks
if commit != '':
elem = commit.split('\001', 5)
authored = elem[0]
committed = elem[1]
sha = elem[2]
contact = elem[3]
email = elem[4]
if ('@google.com' not in email and '@chromium.org' not in email
and '@collabora.com' not in email):
ncontact, nemail = get_contact(chromeos_path, sha)
if ncontact:
contact = ncontact
email = nemail
subject = elem[5].rstrip('\n')
ps = subprocess.Popen(['git', '-C', chromeos_path, 'show', sha], stdout=subprocess.PIPE)
spid = subprocess.check_output(['git', '-C', chromeos_path, 'patch-id'],
stdin=ps.stdout, encoding='utf-8', errors='ignore')
patchid = spid.split(' ', 1)[0]
# Make sure date is unique and in ascending order.
date = int(committed)
if date == prevdate:
date = mprevdate + 1
else:
prevdate = date
date = date * 1000
mprevdate = date
# Do nothing if the sha is already in the commit table.
c.execute("select sha from commits where sha='%s'" % sha)
found = c.fetchone()
if found:
continue
# check for cherry pick lines. If so, record the upstream SHA associated
# with this commit. Only look for commits which may be upstream or may
# have been merged from a stable release.
usha = ''
if not chromium.match(subject):
u = upstream.match(subject)
desc = subprocess.check_output(['git', '-C', chromeos_path, 'show', '-s', sha],
encoding='utf-8', errors='ignore')
for d in desc.splitlines():
m = None
if u:
m = cherrypick.search(d)
else:
m = stable.search(d)
if not m:
m = stable2.search(d)
if m:
usha = m.group(2)[:12]
# The patch may have been picked multiple times; only record
# the first entry.
break
# Search for embedded Change-Id string.
# If found, add it to database.
desc = subprocess.check_output(['git', '-C', chromeos_path, 'show', '-s', sha],
encoding='utf-8', errors='ignore')
for d in desc.splitlines():
chid = changeid.match(d)
if chid:
chid = chid.group(1)
break
# Initially assume we'll drop everything because it is not listed when
# running "rebase -i". Before doing that, check if the commit is a
# stable release commit. If so, mark it accordingly.
reason = 'upstream'
c.execute("select sha from stable where sha is '%s'" % sha)
if c.fetchone():
reason = 'stable'
q = """
INSERT INTO commits(date, created, updated, authored, committed, contact,
email, sha, usha, patchid, changeid, subject,
disposition, reason)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
c.execute(q,
(date, NOW(), NOW(), authored, committed, contact, email,
sha, usha, patchid, chid, subject, 'drop', reason))
filenames = subprocess.check_output(
['git', '-C', chromeos_path, 'show', '--name-only', '--format=', sha],
encoding='utf-8', errors='ignore')
for fn in filenames.splitlines():
if fn != '':
c.execute('INSERT INTO files(sha, filename) VALUES (?, ?)',
(
sha,
fn,
))
conn.commit()
# "git cherry -v <target>" on branch rebase_baseline gives us a list
# of patches to apply.
patches = subprocess.check_output(
['git', '-C', chromeos_path, 'cherry', '-v', rebase_target_tag()],
encoding='utf-8', errors='ignore')
for patch in patches.splitlines():
elem = patch.split(' ', 2)
# print("patch: " + patch)
# print("elem[0]: '%s' elem[1]: '%s' elem[2]: '%s'" % (elem[0], elem[1], elem[2]))
if elem[0] == '+':
# patch not found upstream
sha = elem[1][:12]
# Try to find patch in stable branch. If it is there, drop it after all.
# If not, we may need to apply it.
c.execute("select sha, origin from stable where sha is '%s'" % sha)
found = c.fetchone()
if found:
c.execute(
"UPDATE commits SET disposition=('drop') where sha='%s'" %
sha)
c.execute("UPDATE commits SET reason=('%s') where sha='%s'" %
(found[1], sha))
c.execute("UPDATE commits SET updated=('%d') where sha='%s'" %
(NOW(), sha))
else:
# We need to check if the commit is already marked as drop
# with a reason other than "upstream". If so, don't update it.
c.execute(
"select disposition, reason from commits where sha='%s'" %
sha)
found = c.fetchone()
if found and found[0] == 'drop' and found[1] == 'upstream':
c.execute(
"UPDATE commits SET disposition=('pick') where sha='%s'"
% sha)
c.execute("UPDATE commits SET reason=('') where sha='%s'" %
sha)
c.execute(
"UPDATE commits SET updated=('%d') where sha='%s'" %
(NOW(), sha))
conn.commit()
conn.close()
if not os.path.isfile(rebasedb):
createdb()
if stable_path:
update_stable(stable_path, stable_commits, 'stable')
if android_path:
update_stable(android_path, baseline_commits, 'android')
update_commits()