|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +git_cache() { |
| 4 | + git -C "${GIT_CACHE_DIR}" $* |
| 5 | +} |
| 6 | + |
| 7 | +init() { |
| 8 | + set -ex |
| 9 | + test -d "${GIT_CACHE_DIR}/.git" || { |
| 10 | + mkdir -p "${GIT_CACHE_DIR}" |
| 11 | + |
| 12 | + git_cache init --bare |
| 13 | + git_cache config core.compression 1 |
| 14 | + } |
| 15 | + set +ex |
| 16 | +} |
| 17 | + |
| 18 | +add() { |
| 19 | + set -ex |
| 20 | + git_cache remote add $1 $2 |
| 21 | + set +ex |
| 22 | +} |
| 23 | + |
| 24 | +update() { |
| 25 | + set -ex |
| 26 | + local REMOTE=${1:---all} |
| 27 | + git_cache fetch $REMOTE |
| 28 | + set +ex |
| 29 | +} |
| 30 | + |
| 31 | +list() { |
| 32 | + local REMOTES="$(git_cache remote show)" |
| 33 | + for remote in $REMOTES; do |
| 34 | + echo "${remote}: $(git_cache remote get-url $remote)" |
| 35 | + done |
| 36 | +} |
| 37 | + |
| 38 | +drop() { |
| 39 | + set -ex |
| 40 | + local REMOTE=${1} |
| 41 | + [ -z "$REMOTE" ] && { |
| 42 | + echo "usage: git cache drop <name>" |
| 43 | + exit 1 |
| 44 | + } |
| 45 | + git_cache remote remove $REMOTE |
| 46 | + set +ex |
| 47 | +} |
| 48 | + |
| 49 | +_check_commit() { |
| 50 | + git_cache cat-file -e ${1}^{commit} |
| 51 | +} |
| 52 | + |
| 53 | +clone() { |
| 54 | + set -ex |
| 55 | + local REMOTE="${1}" |
| 56 | + local SHA1="${2}" |
| 57 | + local REMOTE_NAME="$(basename $REMOTE)" |
| 58 | + local TARGET_PATH="${3:-${REMOTE_NAME}}" |
| 59 | + |
| 60 | + if _check_commit $2 2>&1; then |
| 61 | + git init "${TARGET_PATH}" |
| 62 | + git_cache tag commit$SHA1 $SHA1 || true # ignore possibly already existing tag |
| 63 | + git -C "${TARGET_PATH}" fetch --depth=1 "${GIT_CACHE_DIR}" refs/tags/commit$SHA1 |
| 64 | + git -C "${TARGET_PATH}" checkout FETCH_HEAD |
| 65 | + else |
| 66 | + git clone "${REMOTE}" "${TARGET_PATH}" |
| 67 | + git -C "${TARGET_PATH}" checkout $SHA1 |
| 68 | + fi |
| 69 | + set +ex |
| 70 | +} |
| 71 | + |
| 72 | +usage() { |
| 73 | + echo "git cache uses a bare git repository containing all objects from multiple" |
| 74 | + echo "upstream git repositories." |
| 75 | + echo "" |
| 76 | + echo "usage:" |
| 77 | + echo "" |
| 78 | + echo " git cache init initialize git cache" |
| 79 | + echo " git cache add <name> <url> add repository <url> with name <name>" |
| 80 | + echo " git cache list list cached repositories" |
| 81 | + echo " git cache drop <name> drop repo from cache" |
| 82 | + echo " git cache update [<name>] fetch repo named <name> (or all)" |
| 83 | + echo " git cache clone <url> <SHA1> clone repository <url> from cache" |
| 84 | + echo " git cache show-path print's the path that can be used as " |
| 85 | + echo " '--reference' parameter" |
| 86 | + echo "" |
| 87 | + echo "To retrieve objects from cache (will use remote repository if needed):" |
| 88 | + echo ' git clone --reference $(git cache show-path) <repo>' |
| 89 | +} |
| 90 | + |
| 91 | +ACTION=$1 |
| 92 | +shift |
| 93 | + |
| 94 | +export GIT_CACHE_DIR=${GIT_CACHE_DIR:-${HOME}/.gitcache} |
| 95 | + |
| 96 | +case $ACTION in |
| 97 | + init) |
| 98 | + init $* |
| 99 | + ;; |
| 100 | + add) |
| 101 | + add $* |
| 102 | + ;; |
| 103 | + update) |
| 104 | + update $* |
| 105 | + ;; |
| 106 | + list) |
| 107 | + list $* |
| 108 | + ;; |
| 109 | + drop) |
| 110 | + drop $* |
| 111 | + ;; |
| 112 | + show-path) |
| 113 | + echo ${GIT_CACHE_DIR} |
| 114 | + ;; |
| 115 | + clone) |
| 116 | + clone $* |
| 117 | + ;; |
| 118 | + *) |
| 119 | + usage |
| 120 | + ;; |
| 121 | +esac |
0 commit comments