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