blob: 568550a8b6f3c2dbe89ef6558c32ad1bd09f6bee [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
maruel@chromium.org96550942015-05-22 18:46:51 +00008Works only with git checkout and git dependencies.
szager@chromium.org03fd85b2014-06-09 23:43:33 +00009"""
10
machenbach@chromium.orge0c89732014-12-18 15:42:43 +000011import optparse
szager@chromium.org03fd85b2014-06-09 23:43:33 +000012import os
13import re
maruel@chromium.org96550942015-05-22 18:46:51 +000014import subprocess
szager@chromium.org03fd85b2014-06-09 23:43:33 +000015import sys
16
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000017NEED_SHELL = sys.platform.startswith('win')
18
19
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000020class Error(Exception):
21 pass
22
23
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000024def check_output(*args, **kwargs):
25 """subprocess.check_output() passing shell=True on Windows for git."""
26 kwargs.setdefault('shell', NEED_SHELL)
27 return subprocess.check_output(*args, **kwargs)
28
29
30def check_call(*args, **kwargs):
31 """subprocess.check_call() passing shell=True on Windows for git."""
32 kwargs.setdefault('shell', NEED_SHELL)
33 subprocess.check_call(*args, **kwargs)
34
35
maruel@chromium.org96550942015-05-22 18:46:51 +000036def is_pristine(root, merge_base='origin/master'):
37 """Returns True if a git checkout is pristine."""
38 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000039 return not (check_output(cmd, cwd=root).strip() or
40 check_output(cmd + ['--cached'], cwd=root).strip())
szager@chromium.org03fd85b2014-06-09 23:43:33 +000041
42
maruel@chromium.org96550942015-05-22 18:46:51 +000043def roll(root, deps_dir, key, reviewers, bug):
44 deps = os.path.join(root, 'DEPS')
szager@chromium.org03fd85b2014-06-09 23:43:33 +000045 try:
maruel@chromium.org96550942015-05-22 18:46:51 +000046 with open(deps, 'rb') as f:
47 deps_content = f.read()
maruel@chromium.orga7a229f2015-05-22 21:34:46 +000048 except (IOError, OSError):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000049 raise Error('Ensure the script is run in the directory '
50 'containing DEPS file.')
sbc@chromium.org98201122015-04-22 20:21:34 +000051
maruel@chromium.org96550942015-05-22 18:46:51 +000052 if not is_pristine(root):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000053 raise Error('Ensure %s is clean first.' % root)
maruel@chromium.org96550942015-05-22 18:46:51 +000054
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000055 full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir))
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000056 head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
maruel@chromium.org96550942015-05-22 18:46:51 +000057
58 if not head in deps_content:
59 print('Warning: %s is not checked out at the expected revision in DEPS' %
60 deps_dir)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000061 if key is None:
62 print("Warning: no key specified. Using '%s'." % deps_dir)
63 key = deps_dir
64
maruel@chromium.org96550942015-05-22 18:46:51 +000065 # It happens if the user checked out a branch in the dependency by himself.
66 # Fall back to reading the DEPS to figure out the original commit.
67 for i in deps_content.splitlines():
68 m = re.match(r'\s+"' + key + '": "([a-z0-9]{40})",', i)
69 if m:
70 head = m.group(1)
71 break
72 else:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000073 raise Error('Expected to find commit %s for %s in DEPS' % (head, key))
maruel@chromium.org96550942015-05-22 18:46:51 +000074
75 print('Found old revision %s' % head)
76
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000077 check_call(['git', 'fetch', 'origin'], cwd=full_dir)
78 master = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000079 ['git', 'rev-parse', 'origin/master'], cwd=full_dir).strip()
80 print('Found new revision %s' % master)
81
82 if master == head:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000083 raise Error('No revision to roll!')
maruel@chromium.org96550942015-05-22 18:46:51 +000084
85 commit_range = '%s..%s' % (head[:9], master[:9])
86
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000087 logs = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000088 ['git', 'log', commit_range, '--date=short', '--format=%ad %ae %s'],
89 cwd=full_dir).strip()
90 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
91 cmd = 'git log %s --date=short --format=\'%%ad %%ae %%s\'' % commit_range
92 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
93 bug = 'BUG=%s\n' % bug if bug else ''
94 msg = (
95 'Roll %s/ to %s.\n'
96 '\n'
97 '$ %s\n'
98 '%s\n\n'
99 '%s'
100 '%s') % (
101 deps_dir,
102 master,
103 cmd,
104 logs,
105 reviewer,
106 bug)
107
108 print('Commit message:')
109 print('\n'.join(' ' + i for i in msg.splitlines()))
110 deps_content = deps_content.replace(head, master)
111 with open(deps, 'wb') as f:
112 f.write(deps_content)
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000113 check_call(['git', 'add', 'DEPS'], cwd=root)
114 check_call(['git', 'commit', '-m', msg], cwd=root)
maruel@chromium.org96550942015-05-22 18:46:51 +0000115 print('')
116 if not reviewers:
117 print('You forgot to pass -r, make sure to insert a R=foo@example.com line')
118 print('to the commit description before emailing.')
119 print('')
120 print('Run:')
121 print(' git cl upload --send-mail')
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000122
123
maruel@chromium.org96550942015-05-22 18:46:51 +0000124def main():
125 parser = optparse.OptionParser(
126 description=sys.modules[__name__].__doc__,
127 usage='roll-dep [flags] <dependency path> <variable>')
128 parser.add_option(
129 '-r', '--reviewer', default='',
130 help='To specify multiple reviewers, use comma separated list, e.g. '
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000131 '-r joe,jane,john. Defaults to @chromium.org')
maruel@chromium.org96550942015-05-22 18:46:51 +0000132 parser.add_option('-b', '--bug', default='')
133 options, args = parser.parse_args()
134 if not len(args) or len(args) > 2:
135 parser.error('Expect one or two arguments' % args)
136
137 reviewers = None
138 if options.reviewer:
139 reviewers = options.reviewer.split(',')
140 for i, r in enumerate(reviewers):
141 if not '@' in r:
142 reviewers[i] = r + '@chromium.org'
143
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000144 try:
145 roll(
146 os.getcwd(),
147 deps_dir=args[0],
148 key=args[1] if len(args) > 1 else None,
149 reviewers=reviewers,
150 bug=options.bug)
151
152 except Error as e:
153 sys.stderr.write('error: %s\n' % e)
154 return 1
155
156 return 0
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000157
sbc@chromium.org98201122015-04-22 20:21:34 +0000158
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000159if __name__ == '__main__':
maruel@chromium.org96550942015-05-22 18:46:51 +0000160 sys.exit(main())