-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-code.sh
executable file
·66 lines (58 loc) · 1.53 KB
/
get-code.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
#!/bin/bash
set -x
# redirect stdout/stderr to a file
exec >/tmp/get-code.log 2>&1
fetch_head_path() {
local path="$1"
cd "${path}" || exit 1
cd .git || exit 1
path=$(readlink -f HEAD)
if [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
"$(cygpath -w "$path")"
else
echo "$path"
fi
}
switch_to_ref() {
local ref="$1"
local pull="$2"
if [ "${ref}" == "0" ]; then
ref=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
fi
git checkout "${ref}"
if [ "${pull}" != "true" ]; then
return
fi
if git show-ref --heads | grep -q "refs/heads/${REF}$"; then
git pull
fi
}
REPO_URL="$1"
REF="$2"
REPO_PATH="$3"
REPO_PATH="${REPO_PATH%.git}"
repo_name=$(basename "${REPO_PATH}")
dir_path=$(dirname "${REPO_PATH}")
fetch_head="$(fetch_head_path "${REPO_PATH}")"
mkdir -p "${dir_path}"
cd "${dir_path}" || exit 1
echo "Getting code from ${REPO_URL} to ${REPO_PATH} with ref ${REF}"
if [ -f "${fetch_head}" ]; then
cd "${REPO_PATH}" || exit 1
# Update only if last updated time is more that one day
last_fetch_time=$(stat -c %Y "${fetch_head}")
current_time=$(date +%s)
time_diff=$((current_time - last_fetch_time))
if [ "${time_diff}" -gt 86400 ]; then
git fetch
switch_to_ref "${REF}" "true"
else
echo "Not fetching code as last fetch was less than a day ago"
switch_to_ref "${REF}" "false"
fi
else
rm -rf "${repo_name}"
git clone "${REPO_URL}" "${REPO_PATH}"
cd "${REPO_PATH}" || exit 1
switch_to_ref "${REF}" "false"
fi