Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 3 | # Copyright (c) 2025 Arm Limited. All rights reserved. |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 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 |
Antonio de Angelis | 00beed3 | 2024-04-09 10:23:29 +0100 | [diff] [blame] | 16 | local REPO_URL=$1 |
| 17 | local REPO_PATH=$2 |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 18 | local REPO_REFSPEC=$3 |
| 19 | local REPO_SYNC_CMD=$4 |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 20 | |
| 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 Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 28 | # Shallow clone the repo without checkout |
| 29 | git clone --quiet ${GIT_CLONE_PARAMS} "${REPO_URL}" "${REPO_PATH}" |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 30 | |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 31 | # 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 Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 35 | |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 36 | git -C "${REPO_PATH}" checkout --quiet FETCH_HEAD 2>/dev/null \ |
| 37 | || git -C "${REPO_PATH}" checkout --quiet "${REPO_REFSPEC}" |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 38 | fi |
| 39 | |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 40 | # 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 Zhang | a9afdbf | 2023-07-17 12:26:22 +0800 | [diff] [blame] | 43 | fi |
| 44 | |
Matthew Dalzell | 601e7aa | 2025-08-13 17:22:43 +0100 | [diff] [blame] | 45 | echo -e "Share Folder ${REPO_PATH} $(git -C "${REPO_PATH}" rev-parse --short HEAD)\n" |
Antonio de Angelis | 00beed3 | 2024-04-09 10:23:29 +0100 | [diff] [blame] | 46 | |
Xinyu Zhang | 1536241 | 2023-02-10 15:57:07 +0800 | [diff] [blame] | 47 | fi |
| 48 | } |