blob: fcf47645a4ec497744475e2258d07a3722cf5f71 [file] [log] [blame]
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from recipe_engine.recipe_api import Property
DEPS = [
'git',
'recipe_engine/buildbucket',
'recipe_engine/context',
'recipe_engine/file',
'recipe_engine/path',
'recipe_engine/platform',
'recipe_engine/properties',
'recipe_engine/raw_io',
'recipe_engine/step',
'recipe_engine/time',
]
PROPERTIES = {
'path':
Property(
kind=str,
help='Directory to clone into, relative to start_dir',
default=None),
'branch':
Property(kind=str, help='Git branch', default=None),
'revision':
Property(kind=str, help='Revision', default=None),
'remote':
Property(kind=str, help='Remote manifest repository', default=None),
'checkout_file':
Property(
kind=str, help='Optional single file to checkout', default=None),
'cache':
Property(
kind=bool, help='Whether to use the reference cache', default=None),
'snap_ref':
Property(kind=str, help='Commit hash ref to snap to', default=None),
'snap_path':
Property(
kind=str,
help='Directory to checkout snap in, relative to start_dir',
default=None),
}
URL = 'https://fuchsia.googlesource.com/fuchsia.git'
def RunSteps(api, path, branch, revision, remote, checkout_file, cache,
snap_ref, snap_path):
absolute_path = api.path['start_dir'].join(path) if path else None
checkout_kwargs = dict(
path=absolute_path,
recursive=True,
submodules=True,
submodule_force=True,
remote=remote,
checkout_file=checkout_file,
cache=cache,
)
bb_input = api.buildbucket.build.input
if bb_input.gerrit_changes:
api.git.checkout_cl(
bb_input.gerrit_changes[0], rebase_merges=True, **checkout_kwargs)
elif bb_input.gitiles_commit.host:
api.git.checkout_commit(bb_input.gitiles_commit, **checkout_kwargs)
else:
api.git.checkout(URL, ref=revision, **checkout_kwargs)
root_dir = absolute_path or api.path['start_dir'].join('fuchsia')
with api.context(cwd=root_dir):
api.git.get_hash()
api.git.get_remote_url('origin')
api.git.get_timestamp()
api.git.get_commit_message()
api.git.get_commit_message(oneline=True)
api.git.get_changed_files()
api.git.get_changed_files(test_data=['foo.bar', 'bar.foo'])
# You can invoke arbitrary command on api.git.
api.git('status', config={'foo': 'bar'})
# You can use api.git.rebase to rebase the current branch onto another one
api.git.rebase('origin/master')
# You can use api.git.describe to describe a commit by its tag(s).
api.git.describe('foo', tags=True, contains=True)
# You can also specify your expected number of tags.
api.git.describe('foo', tags=True, contains=True, expected_num=2)
try:
api.git.describe('foo', tags=True, contains=True, expected_num=1)
except api.m.step.StepFailure:
pass
# Get remote branch head if it exists.
api.git.get_remote_branch_head(url=URL, branch='master')
# Get remote tag commit if it exists.
api.git.get_remote_tag(url=URL, tag='dummytag')
# Add a new file
api.file.write_text('drop file', root_dir.join('time.txt'),
str(api.time.time()))
api.git('add', root_dir.join('time.txt'))
# Commit the change
api.git.commit('example change 1')
# Commit the change with the file named explicitly
api.git.commit('example change 2', files=[root_dir.join('time.txt')])
# Commit the change with all tracked files
api.git.commit('example change 3', all_tracked=True)
# Commit the change with all tracked and untracked files
api.git.commit('example change 4', all_files=True)
patch_file = api.path['tmp_base'].join('git.patch')
# Take the diff.
api.git.diff()
# Take the diff between the last 2 commits.
api.git.diff(ref_base='HEAD^', ref='HEAD', patch_file=patch_file)
# Apply a patch.
api.git.apply(patch=patch_file)
# Dryrun a push.
api.git.push('HEAD:refs/for/master', dryrun=True)
# Push it for review
api.git.push('HEAD:refs/for/master')
# Atomically push multiple refs.
api.git.push(['HEAD:refs/heads/master', 'refs/tags/foo'], atomic=True)
if snap_ref:
# Snap branch to snap ref.
api.git.snap_branch(
url=URL,
snap_ref=snap_ref,
branch=branch,
message='example snap',
path=api.path['start_dir'].join(snap_path) if snap_path else None,
push=True,
)
def GenTests(api):
api.step_data('git describe', retcode=1)
yield api.test('basic')
yield api.test('basic_ci') + api.buildbucket.ci_build(git_repo=URL)
yield api.test('basic_cq') + api.buildbucket.try_build(git_repo=URL)
yield api.test('basic_path') + api.properties(path='foo')
yield api.test('basic_ref') + api.properties(revision='refs/foo/bar')
yield api.test('basic_branch') + api.properties(revision='refs/heads/testing')
yield api.test('basic_hash') + api.properties(
revision='abcdef0123456789abcdef0123456789abcdef01')
yield api.test('basic_file') + api.properties(checkout_file='README.md')
yield api.test('basic_cache') + api.properties(cache=True)
yield api.test('basic_ref_cache') + api.properties(
revision='refs/foo/bar', cache=True)
yield api.test('basic_branch_cache') + api.properties(
revision='refs/heads/testing', cache=True)
yield api.test('basic_hash_cache') + api.properties(
revision='abcdef0123456789abcdef0123456789abcdef01', cache=True)
yield api.test('snap') + api.properties(
snap_ref='5n4p',
branch='snap_branch',
)
yield api.test('snap_path') + api.properties(
snap_ref='5n4p',
branch='snap_branch',
snap_path='snap_workdir',
)
yield api.test('snap_to_same_ref') + api.properties(
snap_ref='deadbeef',
branch='snap_branch',
)