blob: f853d1e4185c7aab8abc9391ae97057f4c7724d9 [file] [log] [blame]
Eric Boren522efc42019-04-16 15:15:21 -04001#!/usr/bin/env python
2#
3# Copyright 2019 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
Eric Borena1db7992021-03-25 09:04:43 -04009from __future__ import print_function
Eric Borenf19ef9b2019-04-17 12:32:07 -040010import os
11import re
Eric Boren522efc42019-04-16 15:15:21 -040012import subprocess
13import sys
14
15from infra import git
16from infra import go
17
Eric Borenf19ef9b2019-04-17 12:32:07 -040018_TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
19_REPO_ROOT = os.path.realpath(os.path.join(_TOOLS_DIR, os.pardir))
20_INFRA_BOTS = os.path.join(_REPO_ROOT, 'infra', 'bots')
21sys.path.insert(0, _INFRA_BOTS)
22import git_utils
23
Eric Boren522efc42019-04-16 15:15:21 -040024
25REFS_HEADS_PREFIX = 'refs/heads/'
26CHROME_REF_PREFIX = REFS_HEADS_PREFIX + 'chrome/m'
Eric Borenf19ef9b2019-04-17 12:32:07 -040027SK_MILESTONE_H = os.path.join('include', 'core', 'SkMilestone.h')
28SK_MILESTONE_TMPL = r'#define SK_MILESTONE %s'
29SK_MILESTONE_RE = SK_MILESTONE_TMPL % r'(\d+)'
30SKIA_REPO = 'https://skia.googlesource.com/skia.git'
Eric Boren31013702020-05-13 13:48:55 -040031SUPPORTED_CHROME_BRANCHES = 3
Eric Borenf19ef9b2019-04-17 12:32:07 -040032UPDATE_MILESTONE_COMMIT_MSG = '''Update Skia milestone to %d'''
Eric Boren522efc42019-04-16 15:15:21 -040033
34
Eric Borenf19ef9b2019-04-17 12:32:07 -040035def get_current_milestone():
36 '''Read SkMilestone.h and parse out the current milestone.'''
37 sk_milestone = os.path.join(_REPO_ROOT, SK_MILESTONE_H)
38 with open(sk_milestone, 'r') as f:
39 contents = f.read()
40 for line in contents.splitlines():
41 m = re.match(SK_MILESTONE_RE, line)
42 if m:
43 return int(m.groups()[0])
44 print >> sys.stderr, (
45 'Failed to parse %s; has the format changed?' % SK_MILESTONE_H)
46 sys.exit(1)
Eric Boren522efc42019-04-16 15:15:21 -040047
48
Eric Borenf19ef9b2019-04-17 12:32:07 -040049def create_new_branch(new_branch, branch_at):
50 '''Create a temporary checkout of the repo, create the new branch and push.'''
51 b = new_branch[len(REFS_HEADS_PREFIX):]
52 with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT):
53 git.git('checkout', '-b', b)
54 git.git('reset', '--hard', branch_at)
55 git.git('push', '--set-upstream', 'origin', b)
56
57
58def update_milestone(m):
59 '''Update SkMilestone.h to match the given milestone number.'''
60 with git_utils.NewGitCheckout(SKIA_REPO, local=_REPO_ROOT):
61 with git_utils.GitBranch(
62 'update_milestone', UPDATE_MILESTONE_COMMIT_MSG % m):
63 with open(SK_MILESTONE_H, 'r+') as f:
64 contents = re.sub(
65 SK_MILESTONE_RE, SK_MILESTONE_TMPL % str(m), f.read(), flags=re.M)
66 f.seek(0)
67 f.write(contents)
68 f.truncate()
69 git.git('diff')
70
71
72def update_infra_config(old_branch, new_branch):
73 '''Create a CL to add infra support for the new branch and remove the old.'''
Eric Boren522efc42019-04-16 15:15:21 -040074 owner = git.git('config', 'user.email').rstrip()
75 if not owner:
Eric Borenf19ef9b2019-04-17 12:32:07 -040076 print >> sys.stderr, ('No configured git user; please run '
77 '"git config user.email <your email>".')
Eric Boren522efc42019-04-16 15:15:21 -040078 sys.exit(1)
Eric Boren46e2d8d2019-12-05 14:29:38 -050079 go.mod_download()
80 go.install(go.INFRA_GO+'/go/supported_branches/cmd/new-branch')
Eric Boren522efc42019-04-16 15:15:21 -040081 subprocess.check_call(['new-branch',
Eric Boren6fed6d32019-06-04 12:36:43 -040082 '--branch', new_branch[len(REFS_HEADS_PREFIX):],
83 '--delete', old_branch[len(REFS_HEADS_PREFIX):],
Eric Boren06a72572019-04-18 08:54:33 -040084 '--owner', owner,
85 '--exclude-trybots=chromium.*',
Eric Boren7e9dc422019-12-04 07:18:57 -050086 '--exclude-trybots=.*Android_Framework.*',
Ravi Mistry262796e2020-02-28 12:29:53 -050087 '--exclude-trybots=.*G3_Framework.*',
Eric Borenfafb4772021-01-08 09:06:54 -050088 '--exclude-trybots=.*CanvasKit.*',
89 '--exclude-trybots=.*PathKit.*',
Eric Boren7e9dc422019-12-04 07:18:57 -050090 '--submit'])
Eric Boren522efc42019-04-16 15:15:21 -040091
92
Eric Borenf19ef9b2019-04-17 12:32:07 -040093def main():
Eric Boren6fed6d32019-06-04 12:36:43 -040094 if len(sys.argv) != 2 or '--help' in sys.argv or '-h' in sys.argv:
Eric Borenf19ef9b2019-04-17 12:32:07 -040095 print >> sys.stderr, 'Usage: %s <commit hash for branch>' % sys.argv[0]
96 sys.exit(1)
Eric Boren06a72572019-04-18 08:54:33 -040097 go.check()
Eric Borenf19ef9b2019-04-17 12:32:07 -040098 branch_at = sys.argv[1]
99 m = get_current_milestone()
100 new_branch = '%s%d' % (CHROME_REF_PREFIX, m)
101 old_branch = '%s%d' % (CHROME_REF_PREFIX, m-SUPPORTED_CHROME_BRANCHES)
Eric Borena1db7992021-03-25 09:04:43 -0400102 print('Creating branch %s and removing support (eg. CQ) for %s' % (
103 new_branch, old_branch))
Eric Borenf19ef9b2019-04-17 12:32:07 -0400104 create_new_branch(new_branch, branch_at)
105 update_milestone(m+1)
106 update_infra_config(old_branch, new_branch)
107
108
Eric Boren522efc42019-04-16 15:15:21 -0400109if __name__ == '__main__':
110 main()