blob: 5bfca07608183777f7e77f7e56684f39a618d225 [file] [log] [blame]
Xinyu Zhang15362412023-02-10 15:57:07 +08001#!/usr/bin/env bash
2#
Matthew Dalzell601e7aa2025-08-13 17:22:43 +01003# Copyright (c) 2025 Arm Limited. All rights reserved.
Xinyu Zhang15362412023-02-10 15:57:07 +08004#
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
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010018 local REPO_REFSPEC=$3
19 local REPO_SYNC_CMD=$4
Xinyu Zhang15362412023-02-10 15:57:07 +080020
21 # In case repository is not defined, just skip it
22 if [ -z "${REPO_URL}" ]; then
23 return
24 fi
25
26 # Clone if it does not exit
27 if [ ! -d ${REPO_PATH} ]; then
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010028 # Shallow clone the repo without checkout
29 git clone --quiet ${GIT_CLONE_PARAMS} "${REPO_URL}" "${REPO_PATH}"
Xinyu Zhang15362412023-02-10 15:57:07 +080030
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010031 # If a refspec or commit SHA was provided, fetch & checkout shallowly
32 if [ -n "${REPO_REFSPEC}" ]; then
33 git -C "${REPO_PATH}" fetch --quiet --depth=1 origin "${REPO_REFSPEC}" \
34 || git -C "${REPO_PATH}" fetch --quiet --all --depth=1
Xinyu Zhang15362412023-02-10 15:57:07 +080035
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010036 git -C "${REPO_PATH}" checkout --quiet FETCH_HEAD 2>/dev/null \
37 || git -C "${REPO_PATH}" checkout --quiet "${REPO_REFSPEC}"
Xinyu Zhang15362412023-02-10 15:57:07 +080038 fi
39
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010040 # If requested, shallow-init and update all submodules
41 if [ "${REPO_SYNC_CMD}" = "SYNC_ALL_SUBMODULES" ]; then
42 git -C "${REPO_PATH}" submodule update --init --recursive --depth=1 --quiet
Xinyu Zhanga9afdbf2023-07-17 12:26:22 +080043 fi
44
Matthew Dalzell601e7aa2025-08-13 17:22:43 +010045 echo -e "Share Folder ${REPO_PATH} $(git -C "${REPO_PATH}" rev-parse --short HEAD)\n"
Antonio de Angelis00beed32024-04-09 10:23:29 +010046
Xinyu Zhang15362412023-02-10 15:57:07 +080047 fi
48}