1# Copyright 2014 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5 6# pylint: disable=W0201 7 8 9from recipe_engine import recipe_api 10from recipe_engine import config_types 11 12 13class CheckoutApi(recipe_api.RecipeApi): 14 15 @property 16 def default_checkout_root(self): 17 """The default location for cached persistent checkouts.""" 18 return self.m.vars.cache_dir.join('work') 19 20 def assert_git_is_from_cipd(self): 21 """Fail if git is not obtained from CIPD.""" 22 self.m.run(self.m.python.inline, 'Assert that Git is from CIPD', program=''' 23from __future__ import print_function 24import subprocess 25import sys 26 27which = 'where' if sys.platform == 'win32' else 'which' 28git = subprocess.check_output([which, 'git']).decode('utf-8') 29print('git was found at %s' % git) 30if 'cipd_bin_packages' not in git: 31 print('Git must be obtained through CIPD.', file=sys.stderr) 32 sys.exit(1) 33''') 34 35 def git(self, checkout_root): 36 """Run the steps to perform a pure-git checkout without DEPS.""" 37 self.assert_git_is_from_cipd() 38 skia_dir = checkout_root.join('skia') 39 self.m.git.checkout( 40 self.m.properties['repository'], dir_path=skia_dir, 41 ref=self.m.properties['revision'], submodules=False) 42 if self.m.vars.is_trybot: 43 self.m.git('fetch', 'origin', self.m.properties['patch_ref']) 44 self.m.git('checkout', 'FETCH_HEAD') 45 self.m.git('rebase', self.m.properties['revision']) 46 return self.m.properties['revision'] 47 48 def bot_update(self, checkout_root, gclient_cache=None, 49 skip_patch=False, override_revision=None): 50 """Run the steps to obtain a checkout using bot_update. 51 52 Args: 53 checkout_root: Root directory where the code will be synced. 54 gclient_cache: Optional, directory of the gclient cache. 55 skip_patch: Ignore changelist/patchset when syncing the Skia repo. 56 """ 57 self.assert_git_is_from_cipd() 58 if not gclient_cache: 59 gclient_cache = self.m.vars.cache_dir.join('git') 60 61 cfg_kwargs = {} 62 63 # Use a persistent gclient cache for Swarming. 64 cfg_kwargs['CACHE_DIR'] = gclient_cache 65 66 # Create the checkout path if necessary. 67 # TODO(borenet): 'makedirs checkout_root' 68 self.m.file.ensure_directory('makedirs checkout_path', checkout_root) 69 70 # Initial cleanup. 71 gclient_cfg = self.m.gclient.make_config(**cfg_kwargs) 72 73 main_repo = self.m.properties['repository'] 74 main_name = self.m.path.basename(main_repo) 75 if main_name.endswith('.git'): 76 main_name = main_name[:-len('.git')] 77 main = gclient_cfg.solutions.add() 78 main.name = main_name 79 main.managed = False 80 main.url = main_repo 81 main.revision = (override_revision or 82 self.m.properties.get('revision') or 'origin/main') 83 m = gclient_cfg.got_revision_mapping 84 m[main_name] = 'got_revision' 85 patch_root = main_name 86 patch_repo = main.url 87 if self.m.properties.get('patch_repo'): 88 patch_repo = self.m.properties['patch_repo'] 89 patch_root = patch_repo.split('/')[-1] 90 if patch_root.endswith('.git'): 91 patch_root = patch_root[:-4] 92 93 # TODO(rmistry): Remove the below block after there is a solution for 94 # crbug.com/616443 95 entries_file = checkout_root.join('.gclient_entries') 96 if self.m.path.exists(entries_file) or self._test_data.enabled: 97 self.m.file.remove('remove %s' % entries_file, 98 entries_file) 99 100 # Run bot_update. 101 patch_refs = None 102 patch_ref = self.m.properties.get('patch_ref') 103 if patch_ref and not skip_patch: 104 patch_refs = ['%s@%s:%s' % (self.m.properties['patch_repo'], 105 self.m.properties['revision'], 106 patch_ref)] 107 108 self.m.gclient.c = gclient_cfg 109 with self.m.context(cwd=checkout_root): 110 # https://chromium.googlesource.com/chromium/tools/depot_tools.git/+/dca14bc463857bd2a0fee59c86ffa289b535d5d3/recipes/recipe_modules/bot_update/api.py#105 111 update_step = self.m.bot_update.ensure_checkout( 112 patch_root=patch_root, 113 # The logic in ensure_checkout for this arg is fairly naive, so if 114 # patch=False, we'll see "... (without patch)" in the step names, even 115 # for non-trybot runs, which is misleading and confusing. Therefore, 116 # always specify patch=True. 117 patch=True, 118 patch_refs=patch_refs, 119 # Download the patches of all changes with the same Gerrit topic. 120 # For context see go/sk-topics. 121 download_topics=True, 122 ) 123 124 return update_step.presentation.properties['got_revision'] 125