blob: 164bd57d4b76bbc63384b4ae85f3e82227dfa655 [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
20
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000021class Error(Exception):
22 pass
23
24
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000025def check_output(*args, **kwargs):
26 """subprocess.check_output() passing shell=True on Windows for git."""
27 kwargs.setdefault('shell', NEED_SHELL)
28 return subprocess.check_output(*args, **kwargs)
29
30
31def check_call(*args, **kwargs):
32 """subprocess.check_call() passing shell=True on Windows for git."""
33 kwargs.setdefault('shell', NEED_SHELL)
34 subprocess.check_call(*args, **kwargs)
35
36
maruel@chromium.org96550942015-05-22 18:46:51 +000037def is_pristine(root, merge_base='origin/master'):
38 """Returns True if a git checkout is pristine."""
39 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000040 return not (check_output(cmd, cwd=root).strip() or
41 check_output(cmd + ['--cached'], cwd=root).strip())
szager@chromium.org03fd85b2014-06-09 23:43:33 +000042
43
maruel@chromium.org96550942015-05-22 18:46:51 +000044def roll(root, deps_dir, key, reviewers, bug):
45 deps = os.path.join(root, 'DEPS')
szager@chromium.org03fd85b2014-06-09 23:43:33 +000046 try:
maruel@chromium.org96550942015-05-22 18:46:51 +000047 with open(deps, 'rb') as f:
48 deps_content = f.read()
maruel@chromium.orga7a229f2015-05-22 21:34:46 +000049 except (IOError, OSError):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000050 raise Error('Ensure the script is run in the directory '
51 'containing DEPS file.')
sbc@chromium.org98201122015-04-22 20:21:34 +000052
maruel@chromium.org96550942015-05-22 18:46:51 +000053 if not is_pristine(root):
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000054 raise Error('Ensure %s is clean first.' % root)
maruel@chromium.org96550942015-05-22 18:46:51 +000055
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000056 full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir))
sbc@chromium.org30e5b232015-06-01 18:06:59 +000057 if not os.path.isdir(full_dir):
58 raise Error('Directory not found: %s' % deps_dir)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000059 head = check_output(['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
maruel@chromium.org96550942015-05-22 18:46:51 +000060
61 if not head in deps_content:
62 print('Warning: %s is not checked out at the expected revision in DEPS' %
63 deps_dir)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000064 if key is None:
65 print("Warning: no key specified. Using '%s'." % deps_dir)
66 key = deps_dir
67
maruel@chromium.org96550942015-05-22 18:46:51 +000068 # It happens if the user checked out a branch in the dependency by himself.
69 # Fall back to reading the DEPS to figure out the original commit.
70 for i in deps_content.splitlines():
71 m = re.match(r'\s+"' + key + '": "([a-z0-9]{40})",', i)
72 if m:
73 head = m.group(1)
74 break
75 else:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000076 raise Error('Expected to find commit %s for %s in DEPS' % (head, key))
maruel@chromium.org96550942015-05-22 18:46:51 +000077
78 print('Found old revision %s' % head)
79
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000080 check_call(['git', 'fetch', 'origin'], cwd=full_dir)
81 master = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000082 ['git', 'rev-parse', 'origin/master'], cwd=full_dir).strip()
83 print('Found new revision %s' % master)
84
85 if master == head:
sbc@chromium.orge5d984b2015-05-29 22:09:39 +000086 raise Error('No revision to roll!')
maruel@chromium.org96550942015-05-22 18:46:51 +000087
88 commit_range = '%s..%s' % (head[:9], master[:9])
89
maruel@chromium.orgc6e39fe2015-09-23 13:45:52 +000090 logs = check_output(
91 ['git', 'log', commit_range, '--date=short', '--format=%ad %ae %s'],
92 cwd=full_dir).strip()
93 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
94 cmd = 'git log %s --date=short --format=\'%%ad %%ae %%s\'' % commit_range
95 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
96 bug = 'BUG=%s\n' % bug if bug else ''
maruel@chromium.org96550942015-05-22 18:46:51 +000097 msg = (
maruel@chromium.orgc6e39fe2015-09-23 13:45:52 +000098 'Roll %s/ to %s.\n'
maruel@chromium.org96550942015-05-22 18:46:51 +000099 '\n'
maruel@chromium.orgc6e39fe2015-09-23 13:45:52 +0000100 '$ %s\n'
101 '%s\n\n'
102 '%s'
103 '%s') % (
104 deps_dir,
105 master,
106 cmd,
107 logs,
108 reviewer,
109 bug)
maruel@chromium.org96550942015-05-22 18:46:51 +0000110
111 print('Commit message:')
112 print('\n'.join(' ' + i for i in msg.splitlines()))
113 deps_content = deps_content.replace(head, master)
114 with open(deps, 'wb') as f:
115 f.write(deps_content)
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000116 check_call(['git', 'add', 'DEPS'], cwd=root)
117 check_call(['git', 'commit', '-m', msg], cwd=root)
maruel@chromium.org96550942015-05-22 18:46:51 +0000118 print('')
119 if not reviewers:
120 print('You forgot to pass -r, make sure to insert a R=foo@example.com line')
121 print('to the commit description before emailing.')
122 print('')
123 print('Run:')
124 print(' git cl upload --send-mail')
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000125
126
maruel@chromium.org96550942015-05-22 18:46:51 +0000127def main():
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000128 parser = argparse.ArgumentParser(description=__doc__)
129 parser.add_argument('-r', '--reviewer',
maruel@chromium.org96550942015-05-22 18:46:51 +0000130 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')
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000132 parser.add_argument('-b', '--bug')
133 parser.add_argument('dep_path', help='path to dependency')
134 parser.add_argument('key', nargs='?',
135 help='regexp for dependency in DEPS file')
136 args = parser.parse_args()
maruel@chromium.org96550942015-05-22 18:46:51 +0000137
138 reviewers = None
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000139 if args.reviewer:
140 reviewers = args.reviewer.split(',')
maruel@chromium.org96550942015-05-22 18:46:51 +0000141 for i, r in enumerate(reviewers):
142 if not '@' in r:
143 reviewers[i] = r + '@chromium.org'
144
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000145 try:
146 roll(
147 os.getcwd(),
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000148 args.dep_path,
149 args.key,
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000150 reviewers=reviewers,
sbc@chromium.org30e5b232015-06-01 18:06:59 +0000151 bug=args.bug)
sbc@chromium.orge5d984b2015-05-29 22:09:39 +0000152
153 except Error as e:
154 sys.stderr.write('error: %s\n' % e)
155 return 1
156
157 return 0
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000158
sbc@chromium.org98201122015-04-22 20:21:34 +0000159
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000160if __name__ == '__main__':
maruel@chromium.org96550942015-05-22 18:46:51 +0000161 sys.exit(main())