blob: b7a775bd15ea0ed7b2952e26b79fadb9aec8dc23 [file] [log] [blame]
Xinyu Zhang15362412023-02-10 15:57:07 +08001#!/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.
9git config --global advice.detachedHead false
10
11# Global defaults
12GIT_CLONE_PARAMS="--no-checkout"
13
14function git_clone() {
15 # Parse the repo elements
Antonio de Angelis00beed32024-04-09 10:23:29 +010016 local REPO_URL=$1
17 local REPO_PATH=$2
Xinyu Zhang15362412023-02-10 15:57:07 +080018
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
30function git_checkout() {
31 # Parse the repo elements
Antonio de Angelis00beed32024-04-09 10:23:29 +010032 local REPO_PATH=$1
33 local REPO_REFSPEC=$2
34 local SYNC_CMD=$3
Xinyu Zhang15362412023-02-10 15:57:07 +080035
36 # Checkout if repo exits
37 if [ -d ${REPO_PATH} ]; then
38 cd ${REPO_PATH}
39
40 # Fetch the corresponding refspec
41 REPO_FETCH_HEAD=$(git ls-remote --quiet | grep ${REPO_REFSPEC} | awk -v d=" " '{s=(NR==1?s:s d)$1} END{print s}')
42
43 if [ -z "${REPO_FETCH_HEAD}" ]; then
44 git fetch --all
45 else
46 git fetch origin ${REPO_FETCH_HEAD}
47 fi
48
49 # Checkout to specified refspec
Xinyu Zhangdb8159d2023-10-11 14:18:25 +080050 if [[ "${REPO_REFSPEC}" =~ "refs/" ]]; then
51 # Refspec in "refs/" format cannot be directly used to checkout
Xinyu Zhanga9afdbf2023-07-17 12:26:22 +080052 git checkout ${REPO_FETCH_HEAD}
53 else
54 git checkout ${REPO_REFSPEC}
55 fi
56
Antonio de Angelis00beed32024-04-09 10:23:29 +010057 if [ "${SYNC_CMD}" = "SYNC_ALL_SUBMODULES" ]; then
58 # Make sure that any submodule is also inited and updated if present
59 git submodule update --init --recursive
60 fi
61
Xinyu Zhang15362412023-02-10 15:57:07 +080062 echo -e "Share Folder ${REPO_PATH} $(git rev-parse --short HEAD)\n"
63 cd $OLDPWD
64 fi
65}