blob: 60398791b8d1865dd41e33e10b65dd270323d48f [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
18def is_pristine(root, merge_base='origin/master'):
19 """Returns True if a git checkout is pristine."""
20 cmd = ['git', 'diff', '--ignore-submodules', merge_base]
21 return not (
22 subprocess.check_output(cmd, cwd=root).strip() or
23 subprocess.check_output(cmd + ['--cached'], cwd=root).strip())
szager@chromium.org03fd85b2014-06-09 23:43:33 +000024
25
maruel@chromium.org96550942015-05-22 18:46:51 +000026def roll(root, deps_dir, key, reviewers, bug):
27 deps = os.path.join(root, 'DEPS')
szager@chromium.org03fd85b2014-06-09 23:43:33 +000028 try:
maruel@chromium.org96550942015-05-22 18:46:51 +000029 with open(deps, 'rb') as f:
30 deps_content = f.read()
maruel@chromium.orga7a229f2015-05-22 21:34:46 +000031 except (IOError, OSError):
maruel@chromium.org96550942015-05-22 18:46:51 +000032 print >> sys.stderr, (
33 'Ensure the script is run in the directory containing DEPS file.')
sbc@chromium.org98201122015-04-22 20:21:34 +000034 return 1
35
maruel@chromium.org96550942015-05-22 18:46:51 +000036 if not is_pristine(root):
37 print >> sys.stderr, 'Ensure %s is clean first.' % root
38 return 1
39
40 full_dir = os.path.join(os.path.dirname(root), deps_dir)
41 head = subprocess.check_output(
42 ['git', 'rev-parse', 'HEAD'], cwd=full_dir).strip()
43
44 if not head in deps_content:
45 print('Warning: %s is not checked out at the expected revision in DEPS' %
46 deps_dir)
47 # It happens if the user checked out a branch in the dependency by himself.
48 # Fall back to reading the DEPS to figure out the original commit.
49 for i in deps_content.splitlines():
50 m = re.match(r'\s+"' + key + '": "([a-z0-9]{40})",', i)
51 if m:
52 head = m.group(1)
53 break
54 else:
55 print >> sys.stderr, 'Expected to find commit %s for %s in DEPS' % (
56 head, key)
57 return 1
58
59 print('Found old revision %s' % head)
60
61 subprocess.check_call(['git', 'fetch', 'origin'], cwd=full_dir)
62 master = subprocess.check_output(
63 ['git', 'rev-parse', 'origin/master'], cwd=full_dir).strip()
64 print('Found new revision %s' % master)
65
66 if master == head:
67 print('No revision to roll!')
68 return 1
69
70 commit_range = '%s..%s' % (head[:9], master[:9])
71
72 logs = subprocess.check_output(
73 ['git', 'log', commit_range, '--date=short', '--format=%ad %ae %s'],
74 cwd=full_dir).strip()
75 logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
76 cmd = 'git log %s --date=short --format=\'%%ad %%ae %%s\'' % commit_range
77 reviewer = 'R=%s\n' % ','.join(reviewers) if reviewers else ''
78 bug = 'BUG=%s\n' % bug if bug else ''
79 msg = (
80 'Roll %s/ to %s.\n'
81 '\n'
82 '$ %s\n'
83 '%s\n\n'
84 '%s'
85 '%s') % (
86 deps_dir,
87 master,
88 cmd,
89 logs,
90 reviewer,
91 bug)
92
93 print('Commit message:')
94 print('\n'.join(' ' + i for i in msg.splitlines()))
95 deps_content = deps_content.replace(head, master)
96 with open(deps, 'wb') as f:
97 f.write(deps_content)
98 subprocess.check_call(['git', 'add', 'DEPS'], cwd=root)
99 subprocess.check_call(['git', 'commit', '-m', msg], cwd=root)
100 print('')
101 if not reviewers:
102 print('You forgot to pass -r, make sure to insert a R=foo@example.com line')
103 print('to the commit description before emailing.')
104 print('')
105 print('Run:')
106 print(' git cl upload --send-mail')
sbc@chromium.org98201122015-04-22 20:21:34 +0000107 return 0
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000108
109
maruel@chromium.org96550942015-05-22 18:46:51 +0000110def main():
111 parser = optparse.OptionParser(
112 description=sys.modules[__name__].__doc__,
113 usage='roll-dep [flags] <dependency path> <variable>')
114 parser.add_option(
115 '-r', '--reviewer', default='',
116 help='To specify multiple reviewers, use comma separated list, e.g. '
117 '-r joe,jack,john. Defaults to @chromium.org')
118 parser.add_option('-b', '--bug', default='')
119 options, args = parser.parse_args()
120 if not len(args) or len(args) > 2:
121 parser.error('Expect one or two arguments' % args)
122
123 reviewers = None
124 if options.reviewer:
125 reviewers = options.reviewer.split(',')
126 for i, r in enumerate(reviewers):
127 if not '@' in r:
128 reviewers[i] = r + '@chromium.org'
129
130 return roll(
131 os.getcwd(),
132 args[0],
133 args[1] if len(args) > 1 else None,
134 reviewers,
135 options.bug)
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000136
sbc@chromium.org98201122015-04-22 20:21:34 +0000137
szager@chromium.org03fd85b2014-06-09 23:43:33 +0000138if __name__ == '__main__':
maruel@chromium.org96550942015-05-22 18:46:51 +0000139 sys.exit(main())