blob: a0e34149bf20ac813f48050efae1e888ddbd67c8 [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
maruel@chromium.org96550942015-05-22 18:46:51 +000017
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000018NEED_SHELL = sys.platform.startswith('win')
19
20
21def check_output(*args, **kwargs):
22 """subprocess.check_output() passing shell=True on Windows for git."""
23 kwargs.setdefault('shell', NEED_SHELL)
24 return subprocess.check_output(*args, **kwargs)
25
26
27def check_call(*args, **kwargs):
28 """subprocess.check_call() passing shell=True on Windows for git."""
29 kwargs.setdefault('shell', NEED_SHELL)
30 subprocess.check_call(*args, **kwargs)
31
32
maruel@chromium.org96550942015-05-22 18:46:51 +000033def is_pristine(root, merge_base='origin/master'):
34 """Returns True if a git checkout is pristine."""
35 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000036 return not (check_output(cmd, cwd=root).strip() or
37 check_output(cmd + ['--cached'], cwd=root).strip())
szager@chromium.org03fd85b2014-06-09 23:43:33 +000038
39
maruel@chromium.org96550942015-05-22 18:46:51 +000040def roll(root, deps_dir, key, reviewers, bug):
41 deps = os.path.join(root, 'DEPS')
szager@chromium.org03fd85b2014-06-09 23:43:33 +000042 try:
maruel@chromium.org96550942015-05-22 18:46:51 +000043 with open(deps, 'rb') as f:
44 deps_content = f.read()
maruel@chromium.orga7a229f2015-05-22 21:34:46 +000045 except (IOError, OSError):
maruel@chromium.org96550942015-05-22 18:46:51 +000046 print >> sys.stderr, (
47 'Ensure the script is run in the directory containing DEPS file.')
sbc@chromium.org98201122015-04-22 20:21:34 +000048 return 1
49
maruel@chromium.org96550942015-05-22 18:46:51 +000050 if not is_pristine(root):
51 print >> sys.stderr, 'Ensure %s is clean first.' % root
52 return 1
53
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000054 full_dir = os.path.normpath(os.path.join(os.path.dirname(root), deps_dir))
55 head = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000056 ['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
57
58 if not head in deps_content:
59 print('Warning: %s is not checked out at the expected revision in DEPS' %
60 deps_dir)
61 # It happens if the user checked out a branch in the dependency by himself.
62 # Fall back to reading the DEPS to figure out the original commit.
63 for i in deps_content.splitlines():
64 m = re.match(r'\s+"' + key + '": "([a-z0-9]{40})",', i)
65 if m:
66 head = m.group(1)
67 break
68 else:
69 print >> sys.stderr, 'Expected to find commit %s for %s in DEPS' % (
70 head, key)
71 return 1
72
73 print('Found old revision %s' % head)
74
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000075 check_call(['git', 'fetch', 'origin'], cwd=full_dir)
76 master = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000077 ['git', 'rev-parse', 'origin/master'], cwd=full_dir).strip()
78 print('Found new revision %s' % master)
79
80 if master == head:
81 print('No revision to roll!')
82 return 1
83
84 commit_range = '%s..%s' % (head[:9], master[:9])
85
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +000086 logs = check_output(
maruel@chromium.org96550942015-05-22 18:46:51 +000087 ['git', 'log', commit_range, '--date=short', '--format=%ad %ae %s'],
88 cwd=full_dir).strip()
89 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
90 cmd = 'git log %s --date=short --format=\'%%ad %%ae %%s\'' % commit_range
91 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
92 bug = 'BUG=%s\n' % bug if bug else ''
93 msg = (
94 'Roll %s/ to %s.\n'
95 '\n'
96 '$ %s\n'
97 '%s\n\n'
98 '%s'
99 '%s') % (
100 deps_dir,
101 master,
102 cmd,
103 logs,
104 reviewer,
105 bug)
106
107 print('Commit message:')
108 print('\n'.join(' ' + i for i in msg.splitlines()))
109 deps_content = deps_content.replace(head, master)
110 with open(deps, 'wb') as f:
111 f.write(deps_content)
scottmg@chromium.orgc20f4702015-05-23 00:44:46 +0000112 check_call(['git', 'add', 'DEPS'], cwd=root)
113 check_call(['git', 'commit', '-m', msg], cwd=root)
maruel@chromium.org96550942015-05-22 18:46:51 +0000114 print('')
115 if not reviewers:
116 print('You forgot to pass -r, make sure to insert a R=foo@example.com line')
117 print('to the commit description before emailing.')
118 print('')
119 print('Run:')
120 print(' git cl upload --send-mail')
sbc@chromium.org98201122015-04-22 20:21:34 +0000121 return 0
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
144 return roll(
145 os.getcwd(),
146 args[0],
147 args[1] if len(args) > 1 else None,
148 reviewers,
149 options.bug)
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000150
sbc@chromium.org98201122015-04-22 20:21:34 +0000151
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000152if __name__ == '__main__':
maruel@chromium.org96550942015-05-22 18:46:51 +0000153 sys.exit(main())