-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloneGitRepository.sh
More file actions
executable file
·127 lines (111 loc) · 3.15 KB
/
cloneGitRepository.sh
File metadata and controls
executable file
·127 lines (111 loc) · 3.15 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env bash
# Provides safe-guarded (security checked parameters) git repository cloning.
# Note: This script needs the path to target directory to clone the git repository to. It defaults to SOURCE_DIRECTORY ("source").
# Note: This script needs git to be installed.
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
set -o errexit -o pipefail
# Overrideable Defaults
SOURCE_DIRECTORY=${SOURCE_DIRECTORY:-"source"} # Get the source repository directory (defaults to "source")
# Local constants
SCRIPT_NAME=$(basename "${0}")
fail() {
local ERROR_COLOR='\033[0;31m' # red
local DEFAULT_COLOR='\033[0m'
local errorMessage="${1}"
echo -e "${ERROR_COLOR}${SCRIPT_NAME}: Error: ${errorMessage}${DEFAULT_COLOR}" >&2
exit 1
}
# Default and initial values for command line options
url=""
branch="main"
history_only="false"
target="${SOURCE_DIRECTORY}"
dry_run="false"
# Read command line options
USAGE="${SCRIPT_NAME}: Usage: $0 --url <github-repository-url> --branch <branch-name> [--history-only <true|false>] [--target <clone directory>(default=source)]"
while [ "$#" -gt "0" ]; do
key="$1"
case ${key} in
--url)
url="$2"
shift
;;
--branch)
branch="$2"
shift
;;
--history-only)
history_only="$2"
shift
;;
--target)
target="$2"
shift
;;
--dry-run)
dry_run="true"
;;
*)
fail "Unknown option: ${key}"
echo "${USAGE}" >&2
exit 1
esac
shift
done
# --- Validate URL (mandatory)
if [ -z "${url}" ] ; then
fail "The git repository URL (--url) must be provided."
echo "${USAGE}" >&2
exit 1
fi
case "${url}" in
https://github.com/*/*|https://github.com/*/*.git)
;;
*)
fail "The source repository (--url) must be a valid GitHub repository URL."
;;
esac
# --- Validate branch (mandatory, defaults to "main")
if [ -z "${branch}" ] ; then
fail "The git repository branch (--branch) must be provided."
echo "${USAGE}" >&2
exit 1
fi
case "${branch}" in
*[\ ~^:?*[\]\\]*)
fail "The source repository branch contains invalid characters."
;;
esac
# --- Validate history-only (mandatory, defaults to "false")
case "${history_only}" in
true|false)
;;
*)
fail "The source repository history-only option must be either 'true' or 'false'."
echo "${USAGE}" >&2
;;
esac
# --- Validate target directory (mandatory, defaults to SOURCE_DIRECTORY)
if [ -z "${target}" ] ; then
fail "The target directory (--target) ${target} must be provided." >&2
echo "${USAGE}" >&2
exit 1
else
mkdir -p "${target}"
fi
if [ ${dry_run} = "true" ] ; then
echo "Dry run mode enabled. The following command(s) would be executed:" >&2
fi
# --- Clone the git repository
bare_option=""
bare_folder=""
if [ "${history_only}" = "true" ]; then
bare_option="--bare"
bare_folder="/.git" # bare clones need the .git folder to be used as target
fi
if [ ${dry_run} = "true" ] ; then
echo "git clone ${bare_option} --single-branch ${url} --branch ${branch} ${target}${bare_folder}"
exit 0
else
git clone ${bare_option} --single-branch "${url}" --branch "${branch}" "${target}${bare_folder}"
fi