blob: 1e5cd7cd02cc1a7f6dd54334eddde73022e38d84 [file] [log] [blame]
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -08001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-"
3#
4# Copyright 2020 The Chromium OS Authors. All rights reserved.
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""Module rebuilding database with metadata about chromeos patches."""
9
10from __future__ import print_function
11
12import sqlite3
13import os
14import re
15import subprocess
16from config import CHROMEOS_PATH, CHROMEOS_BRANCHES
17from common import WORKDIR, CHERRYPICK, STABLE, STABLE2, make_downstream_table
18from common import stabledb, chromeosdb, chromeos_branch, createdb
19
20UPSTREAM = re.compile(r'(ANDROID: *|UPSTREAM: *|FROMGIT: *|BACKPORT: *)+(.*)')
21CHROMIUM = re.compile(r'(CHROMIUM: *|FROMLIST: *)+(.*)')
22
23
24def search_usha(sha, description):
25 """Search for upstream SHA.
26
27 If found, return upstream sha associated with this commit sha.
28 """
29
30 usha = ''
31 if not CHROMIUM.match(description):
32 desc = subprocess.check_output(['git', 'show',
33 '-s', sha]).decode('utf-8', errors='ignore')
34 for d in desc.splitlines():
35 m = CHERRYPICK.search(d)
36 if not m:
37 m = STABLE.search(d)
38 if not m:
39 m = STABLE2.search(d)
40 if m:
41 # The patch may have been picked multiple times; only record
42 # the first entry.
43 usha = m.group(2)[:12]
44 return usha
45 return usha
46
47
48
49def update_commits(start, cdb, sdb):
50 """Get list of commits from selected branch, starting with commit 'start'.
51
52 Branch must be checked out in current directory.
53 Skip commit if it is contained in sdb, otherwise add to cdb if it isn't
54 already there.
55 """
56
57 try:
58 conn = sqlite3.connect(cdb)
59 conn.text_factory = str
60 c = conn.cursor()
61
62 sconn = sqlite3.connect(sdb)
63 sconn.text_factory = str
64 sc = sconn.cursor()
65 except sqlite3.Error as e:
66 print('Could not update chromeos commits, raising error: ', e)
67 raise
68
69 subprocess_cmd = ['git', 'log', '--no-merges', '--abbrev=12',
70 '--oneline', '--reverse', '%s..' % start]
71 commits = subprocess.check_output(subprocess_cmd).decode('utf-8')
72
73 last = None
74 for commit in commits.splitlines():
75 if commit:
76 elem = commit.split(' ', 1)
77 sha = elem[0]
78 description = elem[1].rstrip('\n')
79 ps = subprocess.Popen(['git', 'show', sha],
80 stdout=subprocess.PIPE, encoding='utf-8')
81 spid = subprocess.check_output(['git', 'patch-id', '--stable'],
82 stdin=ps.stdout).decode('utf-8', errors='ignore')
83 patchid = spid.split(' ', 1)[0]
84
85 # Do nothing if sha is in stable database
86 sc.execute("select sha from commits where sha='%s'" % sha)
87 found = sc.fetchone()
88 if found:
89 continue
90
91 # Do nothing if sha is already in database
92 c.execute("select sha from commits where sha='%s'" % sha)
93 found = c.fetchone()
94 if found:
95 continue
96
97 last = sha
98
99 usha = search_usha(sha, description)
100
101 c.execute('INSERT INTO commits(sha, usha, patchid,' \
102 'description) VALUES (?, ?, ?, ?)',
103 (sha, usha, patchid, description))
104 if last:
105 c.execute("UPDATE tip set sha='%s' where ref=1" % last)
106
107 conn.commit()
108 conn.close()
109 sconn.close()
110
111
112def update_chromeosdb():
113 """Updates the chromeosdb for all chromeos branches."""
114 os.chdir(CHROMEOS_PATH)
115
116 for branch in CHROMEOS_BRANCHES:
117 start = 'v%s' % branch
118 cdb = chromeosdb(branch)
119 sdb = stabledb(branch)
120 bname = chromeos_branch(branch)
121
122 print('Handling %s' % bname)
123
124 try:
125 conn = sqlite3.connect(cdb)
126 conn.text_factory = str
127
128 c = conn.cursor()
129 c.execute('select sha from tip')
130 sha = c.fetchone()
131 conn.close()
132 if sha and sha[0]:
133 start = sha[0]
134 except sqlite3.Error:
135 createdb(cdb, make_downstream_table)
136
137 subprocess.run(['git', 'checkout', bname])
138 subprocess.run(['git', 'pull'])
139
140 update_commits(start, cdb, sdb)
141
142 os.chdir(WORKDIR)
143
144
145if __name__ == '__main__':
146 update_chromeosdb()