blob: c3c80b19d12081d747ad7f2287ecd95bba4287d5 [file] [log] [blame]
szager@chromium.org03fd85b2014-06-09 23:43:33 +00001#!/usr/bin/env python
maruel@chromium.org96550942015-05-22 18:46:51 +00002# Copyright 2015 The Chromium Authors. All rights reserved.
szager@chromium.org03fd85b2014-06-09 23:43:33 +00003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
maruel@chromium.org96550942015-05-22 18:46:51 +00006"""Rolls DEPS controlled dependency.
szager@chromium.org03fd85b2014-06-09 23:43:33 +00007
sbc@chromium.org30e5b232015-06-01 18:06:59 +00008Works only with git checkout and git dependencies. Currently this
9script will always roll to the tip of to origin/master.
szager@chromium.org03fd85b2014-06-09 23:43:33 +000010"""
11
sbc@chromium.org30e5b232015-06-01 18:06:59 +000012import argparse
szager@chromium.org03fd85b2014-06-09 23:43:33 +000013import os
14import re
maruel@chromium.org96550942015-05-22 18:46:51 +000015import subprocess
szager@chromium.org03fd85b2014-06-09 23:43:33 +000016import sys
17
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000018NEED_SHELL = sys.platform.startswith('win')
19
spang@chromium.orgfaa4dfc2015-07-31 15:51:03 +000020GITILES_REGEX = r'https?://[^/]*\.googlesource\.com/'
21
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000022
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000023class Error(Exception):
24 pass
25
26
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000027def check_output(*args, **kwargs):
28 """subprocess.check_output() passing shell=True on Windows for git."""
29 kwargs.setdefault('shell', NEED_SHELL)
30 return subprocess.check_output(*args, **kwargs)
31
32
33def check_call(*args, **kwargs):
34 """subprocess.check_call() passing shell=True on Windows for git."""
35 kwargs.setdefault('shell', NEED_SHELL)
36 subprocess.check_call(*args, **kwargs)
37
38
maruel@chromium.org96550942015-05-22 18:46:51 +000039def is_pristine(root, merge_base='origin/master'):
40 """Returns True if a git checkout is pristine."""
41 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000042 return not (check_output(cmd, cwd=root).strip() or
43 check_output(cmd + ['--cached'], cwd=root).strip())
szager@chromium.org03fd85b2014-06-09 23:43:33 +000044
45
maruel@chromium.org96550942015-05-22 18:46:51 +000046def roll(root, deps_dir, key, reviewers, bug):
47 deps = os.path.join(root, 'DEPS')
szager@chromium.org03fd85b2014-06-09 23:43:33 +000048 try:
maruel@chromium.org96550942015-05-22 18:46:51 +000049 with open(deps, 'rb') as f:
50 deps_content = f.read()
maruel@chromium.orga7a229f2015-05-22 21:34:46 +000051 except (IOError, OSError):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000052 raise Error('Ensure the script is run in the directory '
53 'containing DEPS file.')
sbc@chromium.org98201122015-04-22 20:21:34 +000054
maruel@chromium.org96550942015-05-22 18:46:51 +000055 if not is_pristine(root):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000056 raise Error('Ensure %s is clean first.' % root)
maruel@chromium.org96550942015-05-22 18:46:51 +000057
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000058 full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir))
sbc@chromium.org30e5b232015-06-01 18:06:59 +000059 if not os.path.isdir(full_dir):
60 raise Error('Directory not found: %s' % deps_dir)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000061 head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
maruel@chromium.org96550942015-05-22 18:46:51 +000062
63 if not head in deps_content:
64 print('Warning: %s is not checked out at the expected revision in DEPS' %
65 deps_dir)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000066 if key is None:
67 print("Warning: no key specified. Using '%s'." % deps_dir)
68 key = deps_dir
69
maruel@chromium.org96550942015-05-22 18:46:51 +000070 # It happens if the user checked out a branch in the dependency by himself.
71 # Fall back to reading the DEPS to figure out the original commit.
72 for i in deps_content.splitlines():
73 m = re.match(r'\s+"' + key + '": "([a-z0-9]{40})",', i)
74 if m:
75 head = m.group(1)
76 break
77 else:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000078 raise Error('Expected to find commit %s for %s in DEPS' % (head, key))
maruel@chromium.org96550942015-05-22 18:46:51 +000079
80 print('Found old revision %s' % head)
81
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000082 check_call(['git', 'fetch', 'origin'], cwd=full_dir)
83 master = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000084 ['git', 'rev-parse', 'origin/master'], cwd=full_dir).strip()
85 print('Found new revision %s' % master)
86
87 if master == head:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000088 raise Error('No revision to roll!')
maruel@chromium.org96550942015-05-22 18:46:51 +000089
90 commit_range = '%s..%s' % (head[:9], master[:9])
spang@chromium.orgfaa4dfc2015-07-31 15:51:03 +000091 upstream_url = check_output(
92 ['git', 'config', 'remote.origin.url'], cwd=full_dir).strip()
maruel@chromium.org96550942015-05-22 18:46:51 +000093
spang@chromium.orgfaa4dfc2015-07-31 15:51:03 +000094 log_url = None
95 if re.match(GITILES_REGEX, upstream_url):
96 log_url = '%s/+log/%s..%s' % (upstream_url, head, master)
97
98 msg_args = {
99 'deps_dir': deps_dir,
100 'commit_range': commit_range,
101 'log': '%s\n\n' % log_url if log_url else '',
102 'reviewer': 'R=%s\n' % ','.join(reviewers) if reviewers else '',
103 'bug': 'BUG=%s\n' % bug if bug else '',
104 }
maruel@chromium.org96550942015-05-22 18:46:51 +0000105 msg = (
spang@chromium.orgfaa4dfc2015-07-31 15:51:03 +0000106 'Roll %(deps_dir)s %(commit_range)s\n'
maruel@chromium.org96550942015-05-22 18:46:51 +0000107 '\n'
spang@chromium.orgfaa4dfc2015-07-31 15:51:03 +0000108 '%(log)s'
109 '%(reviewer)s'
110 '%(bug)s' % msg_args)
maruel@chromium.org96550942015-05-22 18:46:51 +0000111
112 print('Commit message:')
113 print('\n'.join(' ' + i for i in msg.splitlines()))
114 deps_content = deps_content.replace(head, master)
115 with open(deps, 'wb') as f:
116 f.write(deps_content)
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000117 check_call(['git', 'add', 'DEPS'], cwd=root)
118 check_call(['git', 'commit', '-m', msg], cwd=root)
maruel@chromium.org96550942015-05-22 18:46:51 +0000119 print('')
120 if not reviewers:
121 print('You forgot to pass -r, make sure to insert a R=foo@example.com line')
122 print('to the commit description before emailing.')
123 print('')
124 print('Run:')
125 print(' git cl upload --send-mail')
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000126
127
maruel@chromium.org96550942015-05-22 18:46:51 +0000128def main():
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000129 parser = argparse.ArgumentParser(description=__doc__)
130 parser.add_argument('-r', '--reviewer',
maruel@chromium.org96550942015-05-22 18:46:51 +0000131 help='To specify multiple reviewers, use comma separated list, e.g. '
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000132 '-r joe,jane,john. Defaults to @chromium.org')
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000133 parser.add_argument('-b', '--bug')
134 parser.add_argument('dep_path', help='path to dependency')
135 parser.add_argument('key', nargs='?',
136 help='regexp for dependency in DEPS file')
137 args = parser.parse_args()
maruel@chromium.org96550942015-05-22 18:46:51 +0000138
139 reviewers = None
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000140 if args.reviewer:
141 reviewers = args.reviewer.split(',')
maruel@chromium.org96550942015-05-22 18:46:51 +0000142 for i, r in enumerate(reviewers):
143 if not '@' in r:
144 reviewers[i] = r + '@chromium.org'
145
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000146 try:
147 roll(
148 os.getcwd(),
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000149 args.dep_path,
150 args.key,
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000151 reviewers=reviewers,
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000152 bug=args.bug)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000153
154 except Error as e:
155 sys.stderr.write('error: %s\n' % e)
156 return 1
157
158 return 0
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000159
sbc@chromium.org98201122015-04-22 20:21:34 +0000160
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000161if __name__ == '__main__':
maruel@chromium.org96550942015-05-22 18:46:51 +0000162 sys.exit(main())