blob: c4da40f4560a9ab25674b06e50375d8196eb7e02 [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 parses and stores data from stable linux patch."""
9
10from __future__ import print_function
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080011import sqlite3
12import os
13import subprocess
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080014from common import STABLE_PATH, SUPPORTED_KERNELS, \
15 WORKDIR, CHERRYPICK, STABLE, STABLE2, make_downstream_table, \
16 stabledb, stable_branch, createdb
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080017
18
19def search_usha(sha):
20 """Search for upstream SHA.
21
22 If found, return upstream SHA associated with this commit sha.
23 """
24
25 usha = ''
26 desc = subprocess.check_output(['git', 'show',
27 '-s', sha]).decode('utf-8', errors='ignore')
28 for d in desc.splitlines():
29 m = CHERRYPICK.search(d)
30 if not m:
31 m = STABLE.search(d)
32 if not m:
33 m = STABLE2.search(d)
34 if m:
35 # The patch may have been picked multiple times; only record
36 # the first entry.
37 usha = m.group(2)[:12]
38 return usha
39 return usha
40
41
42def update_commits(start, db):
43 """Get complete list of commits from stable branch.
44
45 Assume that stable branch exists and has been checked out.
46 """
47
48 conn = sqlite3.connect(db)
49 conn.text_factory = str
50 c = conn.cursor()
51
52 cmd = ['git', 'log', '--no-merges', '--abbrev=12', '--oneline',
53 '--reverse', '%s..' % start]
54 commits = subprocess.check_output(cmd).decode('utf-8', errors='ignore')
55
56 last = None
57 for commit in commits.splitlines():
58 if commit:
59 elem = commit.split(' ', 1)
60 sha = elem[0]
61 description = elem[1].rstrip('\n')
62
63 ps = subprocess.Popen(['git', 'show', sha], stdout=subprocess.PIPE)
64 spid = subprocess.check_output(['git', 'patch-id', '--stable'],
65 stdin=ps.stdout).decode('utf-8', errors='ignore')
66 patchid = spid.split(' ', 1)[0]
67
68 # Do nothing if the sha is already in the database
69 c.execute("select sha from commits where sha='%s'" % sha)
70 found = c.fetchone()
71 if found:
72 continue
73
74 last = sha
75 usha = search_usha(sha)
76
77 c.execute('INSERT INTO commits(sha, usha,' \
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080078 'patchid, description, changeid) VALUES (?, ?, ?, ?, ?)',
79 (sha, usha, patchid, description, None))
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080080 if last:
81 c.execute("UPDATE tip set sha='%s' where ref=1" % last)
82
83 conn.commit()
84 conn.close()
85
86
87def update_stabledb():
88 """Updates the stabledb index for all stable branches."""
89 os.chdir(STABLE_PATH)
90
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080091 for branch in SUPPORTED_KERNELS:
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080092 start = 'v%s' % branch
93 db = stabledb(branch)
94 bname = stable_branch(branch)
95
96 print('Handling %s' % bname)
97
98 try:
99 conn = sqlite3.connect(db)
100 conn.text_factory = str
101
102 c = conn.cursor()
103 c.execute('select sha from tip')
104 sha = c.fetchone()
105 conn.close()
106 if sha and sha[0] != '':
107 start = sha[0]
108 except sqlite3.Error:
109 createdb(db, make_downstream_table)
110
111 subprocess.check_output(['git', 'checkout', bname])
112 subprocess.check_output(['git', 'pull'])
113
114 update_commits(start, db)
115
116 os.chdir(WORKDIR)
117
118
119if __name__ == '__main__':
120 update_stabledb()