-
Notifications
You must be signed in to change notification settings - Fork 5
/
git-crecover
executable file
·89 lines (72 loc) · 2.46 KB
/
git-crecover
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
#!/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.
error() { echo "git-crecover ERROR: $*" >&2; exit 1; }
info() { echo "git-crecover $*" >&2; }
git branch >/dev/null 2>&1 || error "not in a git repository"
[[ -r ~/.gitcache.conf ]] && . ~/.gitcache.conf
origin=
setorg() {
git remote|grep -q "^$1\$" || error "bad remote name $1"
origin="$1"
}
reporg() {
git remote -v | awk -vn="$origin" '$1==n && $3=="(push)" {print $2}'
}
do_recover() {
local repo=$(reporg)
info "recovering cache for $repo"
[[ -z "$GIT_CACHE_SERVER" ]] && error "variable GIT_CACHE_SERVER undefined"
info "asking $GIT_CACHE_SERVER a cache for $repo"
local cpath=$(ssh -n "$GIT_CACHE_SERVER" gitcache-server --clone "$repo") || error "server failure"
[[ -z "$cpath" ]] && error "can't get a cache"
local crepo="${GIT_CACHE_SERVER}:${cpath}"
info "init of git $dir"
git remote set-url "$origin" "$crepo" >&2
git remote set-url --push "$origin" "$repo" >&2
git config "remote.$origin.uploadpack" gitcache-server >&2
info "done"
exit 0
}
do_remove() {
local repo=$(reporg)
info "removing cache reference"
git config --unset "remote.$origin.uploadpack" >&2
git remote set-url "$origin" "$repo" >&2
info "done"
exit 0
}
setorg origin
while [[ $# -gt 0 ]]; do
case "$1" in
-a|--auto) do_recover;;
-r|--remove) do_remove;;
*) setorg "$1"; shift;;
esac
done
cat << EOC
You have two ways of recovering from a cache miss of git-cache.
You can either REMOVE the caching indirection or you can RECOVER
the cache and reset it to the current directory.
The target remote is $origin
EOC
PS3="What to do? "
select action in REMOVE RECOVER; do
case $action in
REMOVE) do_remove;;
RECOVER) do_recover;;
esac
done