1
0

clone.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #! /bin/bash
  2. # This clones a project from github into a named subdirectory
  3. # If the project has a branch with the same name as this branch
  4. # then it will checkout that branch after cloning.
  5. # Otherwise it will checkout "origin/develop."
  6. # The first argument is the name of the directory to checkout
  7. # the branch into.
  8. # The second argument is the URL of the remote repository to checkout.
  9. # Usually something like https://github.com/matrix-org/sytest.git
  10. set -eux
  11. NAME=$1
  12. PROJECT=$2
  13. BASE=".$NAME-base"
  14. # Update our mirror.
  15. if [ ! -d ".$NAME-base" ]; then
  16. # Create a local mirror of the source repository.
  17. # This saves us from having to download the entire repository
  18. # when this script is next run.
  19. git clone "$PROJECT" "$BASE" --mirror
  20. else
  21. # Fetch any updates from the source repository.
  22. (cd "$BASE"; git fetch -p)
  23. fi
  24. # Remove the existing repository so that we have a clean copy
  25. rm -rf "$NAME"
  26. # Cloning with --shared means that we will share portions of the
  27. # .git directory with our local mirror.
  28. git clone "$BASE" "$NAME" --shared
  29. # Jenkins may have supplied us with the name of the branch in the
  30. # environment. Otherwise we will have to guess based on the current
  31. # commit.
  32. : ${GIT_BRANCH:="origin/$(git rev-parse --abbrev-ref HEAD)"}
  33. cd "$NAME"
  34. # check out the relevant branch
  35. git checkout "${GIT_BRANCH}" || (
  36. echo >&2 "No ref ${GIT_BRANCH} found, falling back to develop"
  37. git checkout "origin/develop"
  38. )