-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgitcache-server
More file actions
executable file
·142 lines (122 loc) · 5.32 KB
/
gitcache-server
File metadata and controls
executable file
·142 lines (122 loc) · 5.32 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Copyright (c) 2013: José Bollo (jobol), Stéphane Desneux (kooltux)
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
ts_start=$(date +%s)
op_start=$ts_start
op_elapsed() { echo $(date +%s) | awk -vts1="$op_start" '{printf("%d",($1-ts1+1));}'; }
srvid="gitcache-server($(id -un)@$(hostname -f))"
userip=$(echo $SSH_CONNECTION | awk '{print $1}')
prefixoutput() { awk -vX="$srvid" -vts1="$ts_start" -vRS='[\r\n]' '{ ts2=strftime("%s"); printf("%s - %ds - %s%s",X,(ts2-ts1+1),$0,RT);fflush();}' >&2; }
exec 2> >(prefixoutput)
log() {
[[ -n "$GIT_CACHE_LOG" ]] && echo $(date "+%Y%m%d %H:%M:%S") [$userip] $(op_elapsed)s - "$@" >>$GIT_CACHE_LOG;
op_start=$(date +%s)
}
error() { echo "ERROR: $*" >&2; log "ERROR: $*"; exit 1; }
info() { echo "$*" >&2; }
[[ -r ~/.gitcache-server.conf ]] && . ~/.gitcache-server.conf
[[ $# -eq 0 ]] && error "Invalid arguments."
[[ -z "$GIT_CACHE_DIR" ]] && error "GIT_CACHE_DIR is not defined"
#####################################################
# FETCHING (not cloning mean FETCHING)
#####################################################
arg2=$2
if [[ "$1" != "--clone" ]]; then
shift $(($# - 1))
info using cache directory $1
if [[ -d "$1" ]]; then
cd "$1" 2>/dev/null || error "directory not found $1"
info refreshing cache from $(git ls-remote --get-url origin)
# remove the tags to fetch them (for syncing with deleted tags)
git tag -l 2>/dev/null | xargs git tag -d >&/dev/null
# fetch with prune (for syncing with deleted branches)
#git fetch --prune --progress origin +refs/heads/*:refs/remotes/origin/* +refs/changes/*:refs/changes/* >&2
git fetch --prune --progress >&2
log "fetch => fetch : $1"
info uploading changes to client
git-upload-pack .
log "fetch <= upload : $1"
exit
else
# for any reason, the cache dir has disappeared
# rebuild url from directory to clone
arg2=$(sed -e "s|$GIT_CACHE_DIR/*||" -e 's|/|:|' <<<$1)
info "Cache directory is not present. Cloning from $arg2"
fi
fi
#####################################################
# CLONING (but it may mean fetching...)
#####################################################
# get the uri to clone
uri=$(sed 's/ /%20/g' <<<$arg2)
info "clone request for $uri"
# scan the domain and projet parts of the uri
if [[ $uri =~ ^(.+://)?(.+@)?([^:/]+)(:[0-9]+)?(/.+)$ ]]; then
domain=${BASH_REMATCH[3]}
project=${BASH_REMATCH[5]}
# info "sheme ${BASH_REMATCH[1]} user ${BASH_REMATCH[2]} domain ${BASH_REMATCH[3]} port ${BASH_REMATCH[4]} path ${BASH_REMATCH[5]}"
elif [[ $uri =~ ^(.+@)?([^:/]+):(.+)$ ]]; then
domain=${BASH_REMATCH[2]}
project=${BASH_REMATCH[3]}
# info "sheme - user ${BASH_REMATCH[1]} domain ${BASH_REMATCH[2]} port - path ${BASH_REMATCH[3]}"
else
error "Bad uri $uri"
fi
# enter the cachedir
if [[ ! -d $GIT_CACHE_DIR ]]; then
mkdir -p $GIT_CACHE_DIR || error "can't create $GIT_CACHE_DIR"
fi
cd $GIT_CACHE_DIR
# create the domain/ip root directories
ip=$(dig +short $domain)
[[ -z "$ip" ]] && error "can't resolve ip for $domain"
if [[ ! -d $ip ]]; then
mkdir -p $ip || error "can't create $(pwd)/$ip"
fi
ln -sf $ip $domain
# try to fetch if cached and remove on error
gcdir=$(pwd)/$domain/${project%.git}.git
if [[ -d $gcdir ]]; then
cd $gcdir
info "cache hit - refreshing cache from $uri"
# remove the tags to fetch them (for syncing with deleted tags)
git tag -l 2>/dev/null | xargs git tag -d >&/dev/null
# fetch with prune (for syncing with deleted branches)
#git fetch --prune --progress origin +refs/heads/*:refs/remotes/origin/* +refs/changes/*:refs/changes/* >&2 || { rm -rf $gcdir; info "Fetching cached repo failed"; }
git fetch --prune --progress >&2 || { rm -rf $gcdir; info "Fetching cached repo failed"; }
log "clone => fetch : $domain/${project%.git}"
fi
# if not cached or had fetch error, clone
if [[ ! -d $gcdir ]]; then
dir=$(dirname $gcdir)
[[ -d $dir ]] || mkdir -p $dir || error "can't create directory $dir"
cd $dir
edir="$(basename $gcdir)"
if [[ -n "$GIT_CACHE_SERVER_PRIMARY" ]]; then
# clone from a git-cache (chaining)
export GIT_CACHE_SERVER="$GIT_CACHE_SERVER_PRIMARY"
info "cache miss - chain-cloning $uri into $gcdir through $GIT_CACHE_SERVER"
git-cclone "$uri" "$edir" --mirror >&2 || { rm -rf $gcdir; error "chain-cloning failure"; }
log "clone => cclone : $domain/${project%.git}"
else
# direct clone
info "cache miss - cloning $uri into $gcdir"
git clone --progress --mirror "$uri" "$edir" >&2 || { rm -rf $gcdir; error "cloning failure"; }
log "clone => clone : $domain/${project%.git}"
fi
fi
echo "$gcdir"
exit 0