2016-08-02 15:42:30 -04:00
|
|
|
#! /bin/bash
|
|
|
|
|
2016-08-04 07:31:55 -04:00
|
|
|
# This clones a project from github into a named subdirectory
|
|
|
|
# If the project has a branch with the same name as this branch
|
|
|
|
# then it will checkout that branch after cloning.
|
|
|
|
# Otherwise it will checkout "origin/develop."
|
|
|
|
# The first argument is the name of the directory to checkout
|
|
|
|
# the branch into.
|
|
|
|
# The second argument is the URL of the remote repository to checkout.
|
|
|
|
# Usually something like https://github.com/matrix-org/sytest.git
|
|
|
|
|
|
|
|
set -eux
|
|
|
|
|
2016-08-02 15:42:30 -04:00
|
|
|
NAME=$1
|
|
|
|
PROJECT=$2
|
|
|
|
BASE=".$NAME-base"
|
|
|
|
|
2016-08-04 07:31:55 -04:00
|
|
|
# Update our mirror.
|
|
|
|
if [ ! -d ".$NAME-base" ]; then
|
|
|
|
# Create a local mirror of the source repository.
|
|
|
|
# This saves us from having to download the entire repository
|
|
|
|
# when this script is next run.
|
|
|
|
git clone "$PROJECT" "$BASE" --mirror
|
2016-08-02 15:42:30 -04:00
|
|
|
else
|
2016-08-04 07:31:55 -04:00
|
|
|
# Fetch any updates from the source repository.
|
|
|
|
(cd "$BASE"; git fetch -p)
|
2016-08-02 15:42:30 -04:00
|
|
|
fi
|
|
|
|
|
2016-08-04 07:31:55 -04:00
|
|
|
# Remove the existing repository so that we have a clean copy
|
|
|
|
rm -rf "$NAME"
|
|
|
|
# Cloning with --shared means that we will share portions of the
|
|
|
|
# .git directory with our local mirror.
|
|
|
|
git clone "$BASE" "$NAME" --shared
|
2016-08-02 15:42:30 -04:00
|
|
|
|
2016-08-04 07:31:55 -04:00
|
|
|
# Jenkins may have supplied us with the name of the branch in the
|
|
|
|
# environment. Otherwise we will have to guess based on the current
|
|
|
|
# commit.
|
2016-08-02 15:42:30 -04:00
|
|
|
: ${GIT_BRANCH:="origin/$(git rev-parse --abbrev-ref HEAD)"}
|
2016-08-04 07:31:55 -04:00
|
|
|
cd "$NAME"
|
2016-08-02 15:42:30 -04:00
|
|
|
# check out the relevant branch
|
|
|
|
git checkout "${GIT_BRANCH}" || (
|
|
|
|
echo >&2 "No ref ${GIT_BRANCH} found, falling back to develop"
|
|
|
|
git checkout "origin/develop"
|
|
|
|
)
|