-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-cache
executable file
·104 lines (91 loc) · 2.67 KB
/
git-cache
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
#!/bin/bash
echo "machine ${DRONE_NETRC_MACHINE}" > "${HOME}/.netrc"
echo "login ${DRONE_NETRC_USERNAME}" >> "${HOME}/.netrc"
echo "password ${DRONE_NETRC_PASSWORD}" >> "${HOME}/.netrc"
set -e
WORKDIR=`pwd`
LOCAL_REPO="/var/git_local/${DRONE_REPO_NAME}"
RETRY_THRESHOLD=30
echo "Workspace: ${WORKDIR}"
echo "Local cached repo path: ${LOCAL_REPO}"
mkdir -p $LOCAL_REPO
cd $LOCAL_REPO
## Clone
CLONE_LOCK_PATH="/var/git_local/clone_lock/${DRONE_REPO_NAME}/"
if [[ -d $CLONE_LOCK_PATH ]];
then
echo "Another worker is cloning. Wait cloning to be done."
retry_times=0
until [ $retry_times -ge $RETRY_THRESHOLD ]; do
if [[ ! -d $CLONE_LOCK_PATH ]]; then
break
fi
retry_times=$[$retry_times+1]
sleep 10
done
fi
if [[ -d .git ]]; then
echo 'Local cached git repo exists.'
else
mkdir -p $CLONE_LOCK_PATH
echo 'Local cached git repo does not exist. Start init...'
retry_times=0
until [[ $retry_times -ge $RETRY_THRESHOLD ]]; do
git clone $DRONE_GIT_HTTP_URL $PWD --depth=1 && break
echo 'Try clone repo again'
retry_times=$[$retry_times+1]
sleep 10
done
rmdir $CLONE_LOCK_PATH
fi
# Fetch unshallow
REPO_SHALLOW_LOCK_PATH="/var/git_local/clone_lock/${DRONE_REPO_NAME}_shallow/"
if [[ -d $REPO_SHALLOW_LOCK_PATH ]];
then
echo "Another worker is fetching unshallow. Wait fetching to be done."
retry_times=0
until [ $retry_times -ge $RETRY_THRESHOLD ]; do
if [[ ! -d $REPO_SHALLOW_LOCK_PATH ]]; then
break
fi
retry_times=$[$retry_times+1]
sleep 10
done
fi
if $(git rev-parse --is-shallow-repository); then
mkdir -p $REPO_SHALLOW_LOCK_PATH
echo 'Repo is shallow. Fetching unshallow...'
retry_times=0
until [[ $retry_times -ge $RETRY_THRESHOLD ]]; do
git fetch --unshallow && break
echo 'Try fetch unshallow again'
retry_times=$[$retry_times+1]
sleep 10
done
rmdir $REPO_SHALLOW_LOCK_PATH
echo 'Done.'
else
echo "Repo is unshallow."
fi
echo 'Updating local cached repo...'
retry_times=0
until [ $retry_times -ge $RETRY_THRESHOLD ]; do
git fetch origin ${DRONE_COMMIT_SHA} && git checkout ${DRONE_COMMIT_SHA} && break
retry_times=$[$retry_times+1]
sleep 10
done
echo 'Done.'
echo 'Checkout commit from local cached repo...'
cd $WORKDIR
LOCAL_SOURCE="${LOCAL_REPO}/.git"
if [[ ! -d .git ]]; then
git init
git remote add origin $DRONE_GIT_HTTP_URL
git remote add local-source $LOCAL_SOURCE
fi
# overwrite old settings
git remote set-url local-source $LOCAL_SOURCE
git fetch local-source ${DRONE_COMMIT_SHA}
git reset ${DRONE_COMMIT_SHA} --hard
git submodule update --init --recursive --remote
echo 'Done.'