roll_dep.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #!/usr/bin/env python3
  2. # Copyright 2015 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Rolls DEPS controlled dependency.
  6. Works only with git checkout and git dependencies. Currently this script will
  7. always roll to the tip of to origin/main.
  8. """
  9. import argparse
  10. import itertools
  11. import os
  12. import re
  13. import subprocess2
  14. import sys
  15. import tempfile
  16. import gclient_utils
  17. NEED_SHELL = sys.platform.startswith('win')
  18. GCLIENT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  19. 'gclient.py')
  20. _DEPENDENCY_DIVIDER_PATTERN = re.compile(r'^-{20} DEPENDENCY DIVIDER -{20}$', re.MULTILINE)
  21. _REVISION_LINE_PATTERN = re.compile(r'^Revision: ([a-f0-9]+|DEPS)$', re.MULTILINE)
  22. # Commit subject that will be considered a roll. In the format generated by the
  23. # git log used, so it's "<year>-<month>-<day> <author> <subject>"
  24. _ROLL_SUBJECT = re.compile(
  25. # Date
  26. r'^\d\d\d\d-\d\d-\d\d '
  27. # Author
  28. r'[^ ]+ '
  29. # Subject
  30. r'('
  31. # Generated by
  32. # https://skia.googlesource.com/buildbot/+/HEAdA/autoroll/go/repo_manager/deps_repo_manager.go
  33. r'Roll [^ ]+ [a-f0-9]+\.\.[a-f0-9]+ \(\d+ commits\)'
  34. r'|'
  35. # Generated by
  36. # https://chromium.googlesource.com/infra/infra/+/HEAD/recipes/recipe_modules/recipe_autoroller/api.py
  37. r'Roll recipe dependencies \(trivial\)\.'
  38. r')$')
  39. _PUBLIC_GERRIT_HOSTS = {
  40. 'android',
  41. 'aomedia',
  42. 'boringssl',
  43. 'chromium',
  44. 'dart',
  45. 'dawn',
  46. 'fuchsia',
  47. 'gn',
  48. 'go',
  49. 'llvm',
  50. 'pdfium',
  51. 'quiche',
  52. 'skia',
  53. 'swiftshader',
  54. 'webrtc',
  55. }
  56. class Error(Exception):
  57. pass
  58. class AlreadyRolledError(Error):
  59. pass
  60. def check_output(*args, **kwargs):
  61. """subprocess2.check_output() passing shell=True on Windows for git."""
  62. kwargs.setdefault('shell', NEED_SHELL)
  63. return subprocess2.check_output(*args, **kwargs).decode('utf-8')
  64. def check_call(*args, **kwargs):
  65. """subprocess2.check_call() passing shell=True on Windows for git."""
  66. kwargs.setdefault('shell', NEED_SHELL)
  67. subprocess2.check_call(*args, **kwargs)
  68. def return_code(*args, **kwargs):
  69. """subprocess2.call() passing shell=True on Windows for git and
  70. subprocess2.DEVNULL for stdout and stderr."""
  71. kwargs.setdefault('shell', NEED_SHELL)
  72. kwargs.setdefault('stdout', subprocess2.DEVNULL)
  73. kwargs.setdefault('stderr', subprocess2.DEVNULL)
  74. return subprocess2.call(*args, **kwargs)
  75. def is_pristine(root):
  76. """Returns True if a git checkout is pristine."""
  77. # `git rev-parse --verify` has a non-zero return code if the revision
  78. # doesn't exist.
  79. diff_cmd = ['git', 'diff', '--ignore-submodules', 'origin/main']
  80. return (not check_output(diff_cmd, cwd=root).strip()
  81. and not check_output(diff_cmd + ['--cached'], cwd=root).strip())
  82. def get_gerrit_host(url):
  83. """Returns the host for a given Gitiles URL."""
  84. m = re.match(r'https://([^/]*)\.googlesource\.com/', url)
  85. return m and m.group(1)
  86. def get_log_url(upstream_url, head, tot):
  87. """Returns an URL to read logs via a Web UI if applicable."""
  88. if get_gerrit_host(upstream_url):
  89. return '%s/+log/%s..%s' % (upstream_url, head[:12], tot[:12])
  90. if upstream_url.startswith('https://github.com/'):
  91. upstream_url = upstream_url.rstrip('/')
  92. if upstream_url.endswith('.git'):
  93. upstream_url = upstream_url[:-len('.git')]
  94. return '%s/compare/%s...%s' % (upstream_url, head[:12], tot[:12])
  95. return None
  96. def should_show_log(upstream_url):
  97. """Returns True if a short log should be included in the tree."""
  98. # Skip logs for very active projects.
  99. if upstream_url.endswith('/v8/v8.git'):
  100. return False
  101. if 'webrtc' in upstream_url:
  102. return False
  103. return get_gerrit_host(upstream_url) in _PUBLIC_GERRIT_HOSTS
  104. def gclient(args):
  105. """Executes gclient with the given args and returns the stdout."""
  106. return check_output([sys.executable, GCLIENT_PATH] + args).strip()
  107. def generate_commit_message(full_dir, dependency, head, roll_to, upstream_url,
  108. show_log, log_limit):
  109. """Creates the commit message for this specific roll."""
  110. commit_range = '%s..%s' % (head, roll_to)
  111. commit_range_for_header = '%s..%s' % (head[:9], roll_to[:9])
  112. cmd = ['git', 'log', commit_range, '--date=short', '--no-merges']
  113. logs = check_output(
  114. # Args with '=' are automatically quoted.
  115. cmd + ['--format=%ad %ae %s', '--'],
  116. cwd=full_dir).rstrip()
  117. logs = re.sub(r'(?m)^(\d\d\d\d-\d\d-\d\d [^@]+)@[^ ]+( .*)$', r'\1\2', logs)
  118. lines = logs.splitlines()
  119. cleaned_lines = [l for l in lines if not _ROLL_SUBJECT.match(l)]
  120. logs = '\n'.join(cleaned_lines) + '\n'
  121. nb_commits = len(lines)
  122. rolls = nb_commits - len(cleaned_lines)
  123. header = 'Roll %s/ %s (%d commit%s%s)\n\n' % (
  124. dependency, commit_range_for_header, nb_commits,
  125. 's' if nb_commits > 1 else '',
  126. ('; %s trivial rolls' % rolls) if rolls else '')
  127. log_section = ''
  128. if log_url := get_log_url(upstream_url, head, roll_to):
  129. log_section = log_url + '\n\n'
  130. # It is important that --no-log continues to work, as it is used by
  131. # internal -> external rollers. Please do not remove or break it.
  132. if show_log:
  133. log_section += '$ %s ' % ' '.join(cmd)
  134. log_section += '--format=\'%ad %ae %s\'\n'
  135. log_section = log_section.replace(commit_range, commit_range_for_header)
  136. if len(cleaned_lines) > log_limit:
  137. # Keep the first N/2 log entries and last N/2 entries.
  138. lines = logs.splitlines(True)
  139. lines = lines[:log_limit // 2] + ['(...)\n'
  140. ] + lines[-log_limit // 2:]
  141. logs = ''.join(lines)
  142. log_section += logs + '\n'
  143. return header + log_section
  144. def is_submoduled():
  145. """Returns true if gclient root has submodules"""
  146. return os.path.isfile(os.path.join(gclient(['root']), ".gitmodules"))
  147. def get_submodule_rev(submodule):
  148. """Returns revision of the given submodule path"""
  149. rev_output = check_output(['git', 'submodule', 'status', submodule],
  150. cwd=gclient(['root'])).strip()
  151. # git submodule status <path> returns all submodules with its rev in the
  152. # pattern: `(+|-| )(<revision>) (submodule.path)`
  153. revision = rev_output.split(' ')[0]
  154. return revision[1:] if revision[0] in ('+', '-') else revision
  155. def calculate_roll(full_dir, dependency, roll_to):
  156. """Calculates the roll for a dependency by processing gclient_dict, and
  157. fetching the dependency via git.
  158. """
  159. # if the super-project uses submodules, get rev directly using git.
  160. if is_submoduled():
  161. head = get_submodule_rev(dependency)
  162. else:
  163. head = gclient(['getdep', '-r', dependency])
  164. if not head:
  165. raise Error('%s is unpinned.' % dependency)
  166. check_call(['git', 'fetch', 'origin', '--quiet'], cwd=full_dir)
  167. if roll_to == 'origin/HEAD':
  168. check_output(['git', 'remote', 'set-head', 'origin', '-a'],
  169. cwd=full_dir)
  170. roll_to = check_output(['git', 'rev-parse', roll_to], cwd=full_dir).strip()
  171. return head, roll_to
  172. def gen_commit_msg(logs, cmdline, reviewers, bug):
  173. """Returns the final commit message."""
  174. commit_msg = ''
  175. if len(logs) > 1:
  176. commit_msg = 'Rolling %d dependencies\n\n' % len(logs)
  177. commit_msg += '\n\n'.join(logs)
  178. commit_msg += 'Created with:\n ' + cmdline + '\n'
  179. commit_msg += 'R=%s\n' % ','.join(reviewers) if reviewers else ''
  180. commit_msg += '\nBug: %s\n' % bug if bug else ''
  181. return commit_msg
  182. def finalize(args, commit_msg, current_dir, rolls):
  183. """Commits changes to the DEPS file, then uploads a CL."""
  184. print('Commit message:')
  185. print('\n'.join(' ' + i for i in commit_msg.splitlines()))
  186. # Pull the dependency to the right revision. This is surprising to users
  187. # otherwise. The revision update is done before committing to update
  188. # submodule revision if present.
  189. for dependency, (_head, roll_to, full_dir) in sorted(rolls.items()):
  190. check_call(['git', 'checkout', '--quiet', roll_to], cwd=full_dir)
  191. # Attempt to update README.chromium.
  192. if not args.no_update_readme:
  193. update_readme_chromium(dependency, roll_to, current_dir)
  194. # This adds the submodule revision update to the commit.
  195. if is_submoduled():
  196. check_call([
  197. 'git', 'update-index', '--add', '--cacheinfo',
  198. '160000,{},{}'.format(roll_to, dependency)
  199. ],
  200. cwd=current_dir)
  201. check_call(['git', 'add', 'DEPS'], cwd=current_dir)
  202. # We have to set delete=False and then let the object go out of scope so
  203. # that the file can be opened by name on Windows.
  204. with tempfile.NamedTemporaryFile('w+', newline='', delete=False) as f:
  205. commit_filename = f.name
  206. f.write(commit_msg)
  207. check_call(['git', 'commit', '--quiet', '--file', commit_filename],
  208. cwd=current_dir)
  209. os.remove(commit_filename)
  210. def update_readme_chromium(dependency, roll_to, current_dir):
  211. """Attempts to update the README.chromium file with the new revision.
  212. TODO(b/390067679): Handle README.chromium files with multiple dependencies.
  213. TODO(b/390067679): Add flag to provide custom location for README.chromium.
  214. Args:
  215. dependency: Path to the dependency being rolled.
  216. roll_to: New revision hash to roll to.
  217. current_dir: Current working directory.
  218. """
  219. # README.chromium is typically one directory up from the dependency.
  220. gclient_root = gclient(['root'])
  221. readme_path = os.path.normpath(
  222. os.path.join(gclient_root, dependency, os.path.pardir,
  223. 'README.chromium'))
  224. if not os.path.isfile(readme_path):
  225. print(f'No README.chromium found at {readme_path}')
  226. return
  227. with open(readme_path, 'r') as f:
  228. content = f.read()
  229. # TODO(b/390067679): Handle README.chromium files with multiple dependencies.
  230. if _DEPENDENCY_DIVIDER_PATTERN.match(content):
  231. print('README.chromium contains "- DEPENDENCY DIVIDER -"\n'
  232. 'Files with multiple dependencies are not supported')
  233. return
  234. # Only update when there is exactly one `Revision: line`.
  235. revision_count = len(_REVISION_LINE_PATTERN.findall(content))
  236. if revision_count != 1:
  237. print(f'README.chromium contains {revision_count} Revision: lines, skipping update.\n'
  238. 'Files with multiple dependencies are not supported')
  239. return
  240. # Update the revision line.
  241. new_content = _REVISION_LINE_PATTERN.sub(
  242. f'Revision: {roll_to}',
  243. content)
  244. if new_content == content:
  245. print(f'README.chromium already has revision {roll_to}, \ncontent:{new_content}')
  246. return
  247. with open(readme_path, 'w') as f:
  248. f.write(new_content)
  249. check_call(['git', 'add', readme_path], cwd=current_dir)
  250. print(f'Updated revision in README.chromium for {dependency} to {roll_to}')
  251. def main():
  252. if gclient_utils.IsEnvCog():
  253. print('"roll-dep" is not supported in non-git environment',
  254. file=sys.stderr)
  255. return 1
  256. parser = argparse.ArgumentParser(description=__doc__)
  257. parser.add_argument('--ignore-dirty-tree',
  258. action='store_true',
  259. help='Roll anyways, even if there is a diff.')
  260. parser.add_argument(
  261. '-r',
  262. '--reviewer',
  263. action='append',
  264. help='To specify multiple reviewers, either use a comma separated '
  265. 'list, e.g. -r joe,jane,john or provide the flag multiple times, e.g. '
  266. '-r joe -r jane. Defaults to @chromium.org')
  267. parser.add_argument('-b',
  268. '--bug',
  269. help='Associate a bug number to the roll')
  270. # It is important that --no-log continues to work, as it is used by
  271. # internal -> external rollers. Please do not remove or break it.
  272. parser.add_argument(
  273. '--no-log',
  274. action='store_true',
  275. help='Do not include the short log in the commit message')
  276. parser.add_argument(
  277. '--always-log',
  278. action='store_true',
  279. help='Always include the short log in the commit message')
  280. parser.add_argument('--log-limit',
  281. type=int,
  282. default=100,
  283. help='Trim log after N commits (default: %(default)s)')
  284. parser.add_argument(
  285. '--roll-to',
  286. default='origin/HEAD',
  287. help='Specify the new commit to roll to (default: %(default)s)')
  288. parser.add_argument('--key',
  289. action='append',
  290. default=[],
  291. help='Regex(es) for dependency in DEPS file')
  292. parser.add_argument('dep_path', nargs='+', help='Path(s) to dependency')
  293. parser.add_argument('--no-update-readme',
  294. action='store_true',
  295. help='Do not try to update Revision in README.chromium')
  296. args = parser.parse_args()
  297. if len(args.dep_path) > 1:
  298. if args.roll_to != 'origin/HEAD':
  299. parser.error(
  300. 'Can\'t use multiple paths to roll simultaneously and --roll-to'
  301. )
  302. if args.key:
  303. parser.error(
  304. 'Can\'t use multiple paths to roll simultaneously and --key')
  305. if args.no_log and args.always_log:
  306. parser.error('Can\'t use both --no-log and --always-log')
  307. reviewers = None
  308. if args.reviewer:
  309. reviewers = list(itertools.chain(*[r.split(',')
  310. for r in args.reviewer]))
  311. for i, r in enumerate(reviewers):
  312. if not '@' in r:
  313. reviewers[i] = r + '@chromium.org'
  314. gclient_root = gclient(['root'])
  315. current_dir = os.getcwd()
  316. dependencies = sorted(
  317. d.replace('\\', '/').rstrip('/') for d in args.dep_path)
  318. cmdline = 'roll-dep ' + ' '.join(dependencies) + ''.join(' --key ' + k
  319. for k in args.key)
  320. if not args.no_update_readme:
  321. cmdline += ' --update-readme'
  322. try:
  323. if not args.ignore_dirty_tree and not is_pristine(current_dir):
  324. raise Error('Ensure %s is clean first (no non-merged commits).' %
  325. current_dir)
  326. # First gather all the information without modifying anything, except
  327. # for a git fetch.
  328. rolls = {}
  329. for dependency in dependencies:
  330. full_dir = os.path.normpath(os.path.join(gclient_root, dependency))
  331. if not os.path.isdir(full_dir):
  332. print('Dependency %s not found at %s' % (dependency, full_dir))
  333. full_dir = os.path.normpath(
  334. os.path.join(current_dir, dependency))
  335. print('Will look for relative dependency at %s' % full_dir)
  336. if not os.path.isdir(full_dir):
  337. raise Error('Directory not found: %s (%s)' %
  338. (dependency, full_dir))
  339. head, roll_to = calculate_roll(full_dir, dependency, args.roll_to)
  340. if roll_to == head:
  341. if len(dependencies) == 1:
  342. raise AlreadyRolledError('No revision to roll!')
  343. print('%s: Already at latest commit %s' % (dependency, roll_to))
  344. else:
  345. print('%s: Rolling from %s to %s' %
  346. (dependency, head[:10], roll_to[:10]))
  347. rolls[dependency] = (head, roll_to, full_dir)
  348. logs = []
  349. setdep_args = []
  350. for dependency, (head, roll_to, full_dir) in sorted(rolls.items()):
  351. upstream_url = check_output(['git', 'config', 'remote.origin.url'],
  352. cwd=full_dir).strip()
  353. show_log = args.always_log or \
  354. (not args.no_log and should_show_log(upstream_url))
  355. if not show_log:
  356. print(
  357. f'{dependency}: Omitting git log from the commit message. '
  358. 'Use the `--always-log` flag to include it.')
  359. log = generate_commit_message(full_dir, dependency, head, roll_to,
  360. upstream_url, show_log,
  361. args.log_limit)
  362. logs.append(log)
  363. setdep_args.extend(['-r', '{}@{}'.format(dependency, roll_to)])
  364. # DEPS is updated even if the repository uses submodules.
  365. gclient(['setdep'] + setdep_args)
  366. commit_msg = gen_commit_msg(logs, cmdline, reviewers, args.bug)
  367. finalize(args, commit_msg, current_dir, rolls)
  368. except Error as e:
  369. sys.stderr.write('error: %s\n' % e)
  370. return 2 if isinstance(e, AlreadyRolledError) else 1
  371. except subprocess2.CalledProcessError:
  372. return 1
  373. print('')
  374. if not reviewers:
  375. print('You forgot to pass -r, make sure to insert a R=foo@example.com '
  376. 'line')
  377. print('to the commit description before emailing.')
  378. print('')
  379. print('Run:')
  380. print(' git cl upload --send-mail')
  381. return 0
  382. if __name__ == '__main__':
  383. sys.exit(main())