-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone_repository
More file actions
executable file
·95 lines (79 loc) · 4.09 KB
/
clone_repository
File metadata and controls
executable file
·95 lines (79 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash
: '
Clones a GitHub repository using the GitHub CLI command.
'
ME=$(basename "$0") && readonly ME
# shellcheck disable=SC2164
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
readonly SCRIPT_PATH
source "${SCRIPT_PATH}"/common
# Constants
function printUsageAndExit { logError "${ME}" "Usage: 'clone_repository $(blue "CONFIG_FILE")'"; exit 1; }
# Check that we have a single argument
if [[ $# -ne 1 ]]; then printUsageAndExit; fi
CONFIG_FILE_PATH="${1}" && readonly CONFIG_FILE_PATH
# Check that the provided configuration file argument value exists
if [[ ! -f $CONFIG_FILE_PATH ]]; then
logError "${ME}" "Configuration file $(sQuote "$(red "${CONFIG_FILE_PATH}")") inaccessible or nonexistent." && printUsageAndExit
fi
ERRORS=0
# Ensure that the GitHub CLI installed
gh --version &>/dev/null || { ERRORS=1 && logError "${ME}" "$(sQuote "$(red "gh")") command not found." ; }
# Ensure that jq installed
jq --version &>/dev/null || { ERRORS=1 && logError "${ME}" "$(sQuote "$(red "jq")") command not found." ; }
# If there's any error, exit
if [ "$ERRORS" -ne "0" ]; then
logError "${ME}" "Exiting due to previous errors."; exit 1
else
# shellcheck disable=SC2162,SC2046,SC2005
read OWNER REPOSITORY BRANCH HTML_CONTENT_ROOT WORKING_DIR < <(echo $(jq -r '.github.owner,.github.repository,.github.branch,.github.html_content_root,.github.working_directory' "${CONFIG_FILE_PATH}"))
OWNER_AND_REPO="${OWNER}/${REPOSITORY}"
readonly OWNER REPOSITORY BRANCH HTML_CONTENT_ROOT WORKING_DIR OWNER_AND_REPO
USING_TEMP_WORKING_DIR=0
if [ "$WORKING_DIR" = "null" ]; then
# Create a temporary working directory
WORKING_DIR="$(mktemp -d)"
USING_TEMP_WORKING_DIR=1
else
if [ -d "${WORKING_DIR}" ]; then
mkdir -p "${WORKING_DIR}" &>/dev/null || \
{ logError "${ME}" "Failed to create working directory $(sQuote "$(red "${WORKING_DIR}")")." && exit 1 ; }
fi
fi
OWNER_DIR="${WORKING_DIR}"/"${OWNER}"
REPO_DIR="${OWNER_DIR}"/"${REPOSITORY}"
readonly WORKING_DIR USING_TEMP_WORKING_DIR OWNER_DIR REPO_DIR
logInfo "${ME}" "Using working directory $(sQuote "$(green "${WORKING_DIR}")")."
logInfo "${ME}" "Using $(sQuote "$(green "ssh")") protocol with $(sQuote "$(blue "gh")")."
gh config set git_protocol ssh
mkdir -p "${REPO_DIR}" &>/dev/null \
|| { logError "${ME}" "Failed to create repository directory $(sQuote "$(red "${REPO_DIR}")")." && exit 1 ; }
pushDir "${ME}" "${REPO_DIR}"
if [ $USING_TEMP_WORKING_DIR = 1 ]; then
logInfo "${ME}" "Cloning $(sQuote "$(blue "${OWNER_AND_REPO}")") into $(sQuote "$(blue "${REPO_DIR}")")."
gh repo clone "${OWNER_AND_REPO}" "${REPO_DIR}" -- -b "${BRANCH}" --single-branch &>/dev/null \
|| { logError "${ME}" "Failed to clone $(sQuote "$(red "${OWNER_AND_REPO}")")." && exit 1 ; }
else
if git rev-parse --show-toplevel &>/dev/null; then
REV_PARSE_DIR=$(git rev-parse --show-toplevel) && readonly REV_PARSE_DIR
if [ "${REV_PARSE_DIR}" = "${REPO_DIR}" ]; then
git fetch
git reset --hard origin/"${BRANCH}" >/dev/null
else
logError "${ME}" "Unknown directory status $(sQuote "$(red "${REPO_DIR}")"), aborting." && exit 1
fi
else
gh repo clone "${OWNER_AND_REPO}" "${REPO_DIR}" -- -b "${BRANCH}" --single-branch &>/dev/null \
|| { logError "${ME}" "Failed to clone $(sQuote "$(red "${OWNER_AND_REPO}")")." && exit 1 ; }
fi
fi
SHA=$(git rev-parse @) && readonly SHA
logInfo "${ME}" "Branch $(sQuote "$(blue "${BRANCH}")") has SHA of $(sQuote "$(green "${SHA}")")."
popDir "${ME}"
# Update the github.sha property
cat <<< "$(jq ".github.sha |= \"${SHA}\"" "${CONFIG_FILE_PATH}")" > "${CONFIG_FILE_PATH}"
# Update the serving.html_content_root property
REPO_HTML_CONTENT_ROOT="${REPO_DIR}/${HTML_CONTENT_ROOT}" && readonly REPO_HTML_CONTENT_ROOT
logInfo "${ME}" "Setting configuration property $(sQuote "$(blue ".serving.html_content_root")") to $(sQuote "$(green "${REPO_HTML_CONTENT_ROOT}")")."
cat <<< "$(jq ".serving.html_content_root |= \"${REPO_HTML_CONTENT_ROOT}\"" "${CONFIG_FILE_PATH}")" > "${CONFIG_FILE_PATH}"
fi