-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitfile.sh
executable file
·89 lines (79 loc) · 2.84 KB
/
gitfile.sh
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
#!/usr/bin/env bash
set -euo pipefail
function printHelpText
{
echo "Usage:"
echo " ${0} [OPTIONS]"
echo ""
echo "application Options:"
echo " -p, --default-clone-path= Default path to git clone into (default: ./)"
echo " -f, --gitfile= File path to the Gitfile (default: ./.gitfile)"
echo ""
echo "Help Options:"
echo " -h, --help Show this help message and exit"
echo ""
exit 0
}
function cloneRepo
{
SOURCE=${1}
VERSION=${2}
GIT_CLONE_PATH=${3//\~/$HOME}
echo "${SOURCE}"
if [ ! -d "${GIT_CLONE_PATH}" ]; then
git clone "${SOURCE}" ${GIT_CLONE_PATH} -q
fi
(
cd ${GIT_CLONE_PATH}
if [ -n "$(git status --porcelain)" ]; then
echo "[SKIP] local changes detected"
return
fi
sed -i -e "s|url =.*.git\$|url = ${SOURCE}|" ./.git/config
git fetch -q
git checkout ${VERSION} -q
git pull origin ${VERSION} -q
)
}
function parseYaml
{
YAML_FILE_CONTENT=$(cat ${1} | egrep -v "^\s*#")
DEFAULT_GIT_CLONE_PATH=${2}
LAST_LINE_NUMBER="$(echo "${YAML_FILE_CONTENT}" | wc -l | awk '{print $1}')"
mapfile -t LINE_NUMBERS < <(echo "${YAML_FILE_CONTENT}" | cat -n | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $1}')
mapfile -t DIR_NAMES < <(echo "${YAML_FILE_CONTENT}" | cat -n | egrep -v "source:|version:|path:" | egrep "[0-9]{1,10}.*:\s*$" | awk '{print $2}')
for (( i=0; i < ${#LINE_NUMBERS[@]}; ++i ))
do
FROM=$(expr ${LINE_NUMBERS[$i]} + 1)
TO=$(expr ${LAST_LINE_NUMBER} + 1)
if [ "$i" -ne "$(expr ${#LINE_NUMBERS[@]} - 1 )" ]; then
TO=$(expr ${LINE_NUMBERS[$i + 1]} - 1)
fi
SOURCE=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "source:" | awk '{print $2}' | cut -d'"' -f2)
VERSION=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "version:" | awk '{print $2}' | cut -d'"' -f2 || echo "master")
GIT_CLONE_PATH=$(echo "${YAML_FILE_CONTENT}" | sed -n "${FROM},${TO}p" | grep "path:" | awk '{print $2}' | cut -d'"' -f2 || echo "${DEFAULT_GIT_CLONE_PATH}")
cloneRepo ${SOURCE} ${VERSION} ${GIT_CLONE_PATH%/}/${DIR_NAMES[$i]%:}
done
}
DEFAULT_GIT_CLONE_PATH="."
YAML_FILE="./.gitfile"
while [ "$#" -gt 0 ]; do
case "$1" in
-p) DEFAULT_GIT_CLONE_PATH="${2%/}"; shift 2;;
-f) YAML_FILE="$2"; shift 2;;
--default-clone-path=*) DEFAULT_GIT_CLONE_PATH="${1#*=}"; shift 1;;
--gitfile=*) YAML_FILE="${1#*=}"; shift 1;;
--default-clone-path|--gitfile) echo "$1 requires an argument" >&2; exit 1;;
-h) printHelpText;;
--help) printHelpText;;
-*) echo "unknown option: $1" >&2; exit 1;;
*) handle_argument "$1"; shift 1;;
esac
done
if [ ! -f "${YAML_FILE}" ]; then
echo "[ERROR] '${YAML_FILE}' does not exist"
exit 1
fi
cd "$(dirname "${YAML_FILE}")"
YAML_FILE="./$(echo ${YAML_FILE} | rev | cut -d "/" -f 1 | rev)"
parseYaml ${YAML_FILE} ${DEFAULT_GIT_CLONE_PATH}