-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_git.sh
executable file
·75 lines (69 loc) · 2 KB
/
get_git.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
#!/bin/bash
source ./git_mirror_constants.txt
PREFIX="path_with_namespace"
TIME=$(date +%Y-%m-%d:%R)
PATHS=$(
curl --header "PRIVATE-TOKEN: $TOKEN" \
$GITLAB_URL \
| grep -o "\"$PREFIX\":[^ ,]\+")
UPDATED_REPOS=0;
ALL_REPOS=0;
# $1=$O $2=$S $3=$P
handleProject() {
echo "########## $(date +%s%N | cut -b1-13) Starting with" $P
ALL_REPOS=$(($ALL_REPOS + 1))
pushd Mirrors &> /dev/null
if [ -z $S ]
then
git clone --mirror $HOST/$O/$P.git &> /dev/null
else
git clone --mirror $HOST/$O/$S/$P.git &> /dev/null
fi
pushd $P.git &> /dev/null
git remote update &> /dev/null
popd &> /dev/null
popd &> /dev/null
git clone Mirrors/$P.git Projects/$P-working-dir &> /dev/null
pushd Projects/$P-working-dir &> /dev/null
commit=$(git log --pretty=format:'%h' -n 1)
git pull &> /dev/null
newCommit=$(git log --pretty=format:'%h' -n 1)
if [[ $commit != $newCommit ]]
then
echo "########## $(date +%s%N | cut -b1-13) updating to commit $newCommit"
UPDATED_REPOS=$(($UPDATED_REPOS + 1))
fi
popd &> /dev/null
echo "########## $(date +%s%N | cut -b1-13) Finished with" $P
echo ""
echo "----------------------------------------"
echo ""
}
mkdir -p Mirrors
mkdir -p Projects
echo "----------> Starting Git backup @ $TIME <----------"
read -a projects <<< $PATHS
for path_with_namespace in "${projects[@]}"
do
clean=$(sed -e "s/\"$PREFIX\":/""/g" <<< "$path_with_namespace")
while IFS=/ read -r org proj
do
P=$(sed -e "s/\"/""/g" <<< $proj)
if [[ $P == *\/* ]]
then
while IFS=/ read -r subOrg project
do
S=$(sed -e "s/\"/""/g" <<< $subOrg)
P=$(sed -e "s/\"/""/g" <<< $project)
O=$(sed -e "s/\"/""/g" <<< $org)
handleProject
done <<< "$proj"
else
S=""
P=$(sed -e "s/\"/""/g" <<< $proj)
O=$(sed -e "s/\"/""/g" <<< $org)
handleProject
fi
done <<< "$clean"
done
echo "Finished updating $ALL_REPOS repos. $UPDATED_REPOS had new commits"