blob: bd7716623c3cd0cf6b6b20ea5918b2453d53cc9d [file] [log] [blame]
# Copyright 2019 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""Calls to checkout code.
Usage:
api.checkout(remote='https://pigweed.googlesource.com/pigweed/pigweed')
"""
from recipe_engine import recipe_api
class CheckoutApi(recipe_api.RecipeApi):
"""Calls to checkout code."""
def __init__(self, *args, **kwargs):
super(CheckoutApi, self).__init__(*args, **kwargs)
self._root = None
def __call__(self, remote):
"""Checkout code from git.
Grabs data from buildbucket. If drawing a blank, uses remote. Returns
path to checkout.
Args:
remote (str): URL of git repository
"""
with self.m.step.nest('checkout'):
with self.m.context(infra_steps=True):
self.m.build_input_resolver.resolve(remote)
commit = self.m.buildbucket.build.input.gitiles_commit
self._root = self.m.path['start_dir'].join('checkout')
self.m.git.checkout(
url='https://{}/{}'.format(commit.host, commit.project),
ref=commit.id,
path=self._root,
)
query_host = 'https://{}'.format(commit.host).replace(
'.googlesource.com', '-review.googlesource.com')
details = self.m.gerrit.change_details(
'get change details',
change_id=commit.id,
gerrit_host=query_host,
test_data=self.m.json.test_api.output({'branch': 'master'}))
# Rebase is deliberately not under infra_steps=True so rebase failures
# are not presented as infra failures.
with self.m.context(cwd=self._root):
self.m.git.rebase('origin/{}'.format(details['branch']))
@property
def root(self):
return self._root