2016-03-20 12:40:20 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
git_cache() {
|
|
|
|
git -C "${GIT_CACHE_DIR}" $*
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:43:33 +01:00
|
|
|
git_cache_initialized() {
|
|
|
|
local _git_dir="$(git_cache rev-parse --git-dir 2>/dev/null)"
|
|
|
|
test "$_git_dir" = "." -o "$_git_dir" = ".git"
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:40:20 +01:00
|
|
|
init() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
2017-01-10 21:43:33 +01:00
|
|
|
git_cache_initialized || {
|
2016-03-20 12:40:20 +01:00
|
|
|
mkdir -p "${GIT_CACHE_DIR}"
|
|
|
|
|
|
|
|
git_cache init --bare
|
|
|
|
git_cache config core.compression 1
|
|
|
|
}
|
2016-12-27 16:29:22 +01:00
|
|
|
set +e
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
add() {
|
2017-01-10 21:43:33 +01:00
|
|
|
local repo="$1"
|
|
|
|
|
|
|
|
_locked "$GIT_CACHE_DIR/$name.addlock" _add "$repo"
|
|
|
|
}
|
|
|
|
|
|
|
|
_add() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
|
|
|
local repo="$1"
|
|
|
|
local name="$(_remote_name $repo)"
|
2016-12-15 10:59:47 +01:00
|
|
|
|
|
|
|
if ! is_cached "$repo"; then
|
|
|
|
git_cache remote add "$name" "$repo"
|
2017-01-10 21:43:33 +01:00
|
|
|
git_cache config --add remote.${name}.fetch "+refs/tags/*:refs/tags/${name}/*"
|
2016-12-15 10:59:47 +01:00
|
|
|
else
|
|
|
|
echo "git-cache: $url already in cache"
|
|
|
|
fi
|
2016-12-27 16:29:22 +01:00
|
|
|
set +e
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:43:33 +01:00
|
|
|
_locked() {
|
|
|
|
local lockfile="$1"
|
|
|
|
shift
|
|
|
|
|
|
|
|
(
|
|
|
|
flock -w 600 9 || exit 1
|
|
|
|
$*
|
|
|
|
) 9>"$lockfile"
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:40:20 +01:00
|
|
|
update() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
2017-01-10 21:43:33 +01:00
|
|
|
|
|
|
|
local REMOTE=${1}
|
|
|
|
if [ -n "$REMOTE" ]; then
|
|
|
|
local REMOTES=$(_remote_name $REMOTE)
|
|
|
|
else
|
|
|
|
local REMOTES="$(git_cache remote show)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
for remote in $REMOTES; do
|
|
|
|
echo "git-cache: updating remote $remote"
|
|
|
|
_locked "$GIT_CACHE_DIR/$remote.lock" git_cache --namespace $remote fetch -n $remote
|
|
|
|
done
|
|
|
|
|
2016-12-27 16:29:22 +01:00
|
|
|
set +e
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:59:47 +01:00
|
|
|
is_cached() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set +e
|
2016-12-15 10:59:47 +01:00
|
|
|
local url="$1"
|
|
|
|
local REMOTES="$(git_cache remote show)"
|
|
|
|
for remote in $REMOTES; do
|
2016-12-27 16:29:22 +01:00
|
|
|
[ "$(git_cache ls-remote --get-url $remote)" = "$url" ] && return 0
|
2016-12-15 10:59:47 +01:00
|
|
|
done
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
2016-12-15 10:59:47 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:40:20 +01:00
|
|
|
list() {
|
|
|
|
local REMOTES="$(git_cache remote show)"
|
|
|
|
for remote in $REMOTES; do
|
2016-12-27 16:29:22 +01:00
|
|
|
echo "$(git_cache ls-remote --get-url $remote)"
|
2016-03-20 12:40:20 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
drop() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
2016-03-20 12:40:20 +01:00
|
|
|
local REMOTE=${1}
|
|
|
|
[ -z "$REMOTE" ] && {
|
2016-12-27 16:29:22 +01:00
|
|
|
echo "usage: git cache drop <url>"
|
2016-03-20 12:40:20 +01:00
|
|
|
exit 1
|
|
|
|
}
|
2016-12-27 16:29:22 +01:00
|
|
|
local REMOTES="$(git_cache remote show)"
|
|
|
|
for remote in $REMOTES; do
|
|
|
|
[ "$(git_cache ls-remote --get-url $remote)" = "$REMOTE" ] && {
|
|
|
|
git_cache remote remove $remote
|
|
|
|
break
|
|
|
|
}
|
|
|
|
done
|
|
|
|
set +e
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_check_commit() {
|
2017-01-10 21:43:33 +01:00
|
|
|
git_cache cat-file -e ${1}^{commit} 2>/dev/null
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:59:47 +01:00
|
|
|
_remote_name() {
|
2016-12-27 16:29:22 +01:00
|
|
|
echo "$*" | md5sum | cut -d\ -f1
|
2016-12-15 10:59:47 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:43:33 +01:00
|
|
|
_tag_to_sha1() {
|
|
|
|
local out="$(git_cache log -n 1 --pretty=oneline $1 -- 2>/dev/null)"
|
|
|
|
[ -n "$out" ] && echo $out | cut -f 1 -d" "
|
|
|
|
}
|
|
|
|
|
|
|
|
_check_tag_or_commit() {
|
|
|
|
local SHA1=$1
|
|
|
|
local REMOTE_NAME=$2
|
|
|
|
|
|
|
|
if _check_commit $SHA1 ; then
|
|
|
|
git_cache tag commit$SHA1 $SHA1 2> /dev/null || true # ignore possibly already existing tag
|
|
|
|
echo "commit$SHA1"
|
|
|
|
elif _tag_to_sha1 ${REMOTE_NAME}/$SHA1 > /dev/null; then
|
|
|
|
echo "${REMOTE_NAME}/$SHA1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:40:20 +01:00
|
|
|
clone() {
|
2016-12-27 16:29:22 +01:00
|
|
|
set -e
|
2016-03-20 12:40:20 +01:00
|
|
|
local REMOTE="${1}"
|
|
|
|
local SHA1="${2}"
|
2016-12-15 10:59:47 +01:00
|
|
|
local REMOTE_NAME="$(_remote_name $REMOTE)"
|
2017-01-10 21:43:33 +01:00
|
|
|
local TARGET_PATH="${3}"
|
|
|
|
|
|
|
|
[ -z "$TARGET_PATH" ] && TARGET_PATH="$(basename $REMOTE)"
|
|
|
|
|
|
|
|
# make sure git won't ask for credentials
|
|
|
|
export GIT_TERMINAL_PROMPT=0
|
2016-03-20 12:40:20 +01:00
|
|
|
|
2017-01-10 21:43:33 +01:00
|
|
|
if git_cache_initialized; then
|
2016-12-15 10:59:47 +01:00
|
|
|
if ! is_cached "$REMOTE"; then
|
2016-12-27 16:29:22 +01:00
|
|
|
echo "git cache: auto-adding $REMOTE"
|
2016-12-15 10:59:47 +01:00
|
|
|
add "$REMOTE"
|
|
|
|
fi
|
|
|
|
|
2017-01-10 21:43:33 +01:00
|
|
|
local tag="$(_check_tag_or_commit $SHA1 $REMOTE_NAME)"
|
|
|
|
if [ -z "$tag" ]; then
|
|
|
|
# commit / tag not in cache, try updating repo
|
|
|
|
update "$REMOTE" || true
|
|
|
|
tag="$(_check_tag_or_commit $SHA1 $REMOTE_NAME)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$tag" ]; then
|
|
|
|
echo "git-cache: cloning from cache."
|
|
|
|
git -c advice.detachedHead=false clone --reference "${GIT_CACHE_DIR}" --shared "${GIT_CACHE_DIR}" "${TARGET_PATH}" --branch $tag
|
|
|
|
git -C "${TARGET_PATH}" fetch origin "refs/tags/${REMOTE_NAME}/*:refs/tags/*" > /dev/null
|
|
|
|
else
|
|
|
|
echo "git-cache: trying checkout from source"
|
|
|
|
git clone --reference "${GIT_CACHE_DIR}" --shared "${REMOTE}" "${TARGET_PATH}"
|
|
|
|
git -c advice.detachedHead=false -C "${TARGET_PATH}" checkout $SHA1
|
|
|
|
fi
|
2016-03-20 12:40:20 +01:00
|
|
|
else
|
2017-01-10 21:43:33 +01:00
|
|
|
git clone "${REMOTE}" "${TARGET_PATH}"
|
|
|
|
git -c advice.detachedHead=false -C "${TARGET_PATH}" checkout $SHA1
|
2016-03-20 12:40:20 +01:00
|
|
|
fi
|
2016-12-27 16:29:22 +01:00
|
|
|
set +e
|
2016-03-20 12:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "git cache uses a bare git repository containing all objects from multiple"
|
|
|
|
echo "upstream git repositories."
|
|
|
|
echo ""
|
|
|
|
echo "usage:"
|
|
|
|
echo ""
|
|
|
|
echo " git cache init initialize git cache"
|
2016-12-27 16:29:22 +01:00
|
|
|
echo " git cache add <url> add repository <url>"
|
2016-03-20 12:40:20 +01:00
|
|
|
echo " git cache list list cached repositories"
|
2016-12-27 16:29:22 +01:00
|
|
|
echo " git cache drop <url> drop repo from cache"
|
|
|
|
echo " git cache update [<url>] fetch repo <url> (or all)"
|
2016-03-20 12:40:20 +01:00
|
|
|
echo " git cache clone <url> <SHA1> clone repository <url> from cache"
|
|
|
|
echo " git cache show-path print's the path that can be used as "
|
|
|
|
echo " '--reference' parameter"
|
|
|
|
echo ""
|
|
|
|
echo "To retrieve objects from cache (will use remote repository if needed):"
|
|
|
|
echo ' git clone --reference $(git cache show-path) <repo>'
|
|
|
|
}
|
|
|
|
|
2016-12-27 16:29:22 +01:00
|
|
|
[ $# -eq 0 ] && {
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:40:20 +01:00
|
|
|
ACTION=$1
|
2016-12-27 16:29:22 +01:00
|
|
|
shift 1
|
2016-03-20 12:40:20 +01:00
|
|
|
|
|
|
|
export GIT_CACHE_DIR=${GIT_CACHE_DIR:-${HOME}/.gitcache}
|
|
|
|
|
|
|
|
case $ACTION in
|
|
|
|
init)
|
|
|
|
init $*
|
|
|
|
;;
|
|
|
|
add)
|
|
|
|
add $*
|
|
|
|
;;
|
|
|
|
update)
|
|
|
|
update $*
|
|
|
|
;;
|
|
|
|
list)
|
|
|
|
list $*
|
|
|
|
;;
|
|
|
|
drop)
|
|
|
|
drop $*
|
|
|
|
;;
|
|
|
|
show-path)
|
|
|
|
echo ${GIT_CACHE_DIR}
|
|
|
|
;;
|
|
|
|
clone)
|
|
|
|
clone $*
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|