Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2023 Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | # Don't print mouthful "You are in 'detached HEAD' state." messages. |
| 9 | git config --global advice.detachedHead false |
| 10 | |
| 11 | # Global defaults |
| 12 | GIT_CLONE_PARAMS="--no-checkout" |
| 13 | |
| 14 | function git_clone() { |
| 15 | # Parse the repo elements |
| 16 | REPO_URL=$1 |
| 17 | REPO_PATH=$2 |
| 18 | |
| 19 | # In case repository is not defined, just skip it |
| 20 | if [ -z "${REPO_URL}" ]; then |
| 21 | return |
| 22 | fi |
| 23 | |
| 24 | # Clone if it does not exit |
| 25 | if [ ! -d ${REPO_PATH} ]; then |
| 26 | git clone --quiet ${GIT_CLONE_PARAMS} ${REPO_URL} ${REPO_PATH} |
| 27 | fi |
| 28 | } |
| 29 | |
| 30 | function git_checkout() { |
| 31 | # Parse the repo elements |
| 32 | REPO_PATH=$1 |
| 33 | REPO_REFSPEC=$2 |
| 34 | |
| 35 | # Checkout if repo exits |
| 36 | if [ -d ${REPO_PATH} ]; then |
| 37 | cd ${REPO_PATH} |
| 38 | |
| 39 | # Fetch the corresponding refspec |
| 40 | REPO_FETCH_HEAD=$(git ls-remote --quiet | grep ${REPO_REFSPEC} | awk -v d=" " '{s=(NR==1?s:s d)$1} END{print s}') |
| 41 | |
| 42 | if [ -z "${REPO_FETCH_HEAD}" ]; then |
| 43 | git fetch --all |
| 44 | else |
| 45 | git fetch origin ${REPO_FETCH_HEAD} |
| 46 | fi |
| 47 | |
| 48 | # Checkout to specified refspec |
Xinyu Zhang | a9afdbf | 2023-07-17 12:26:22 +0800 | [diff] [blame] | 49 | if [[ "${REPO_REFSPEC}" =~ "refs/changes" ]]; then |
| 50 | # Refspec in "refs/changes" format cannot be directly used to checkout |
| 51 | git checkout ${REPO_FETCH_HEAD} |
| 52 | else |
| 53 | git checkout ${REPO_REFSPEC} |
| 54 | fi |
| 55 | |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 56 | echo -e "Share Folder ${REPO_PATH} $(git rev-parse --short HEAD)\n" |
| 57 | cd $OLDPWD |
| 58 | fi |
| 59 | } |