Last active
August 29, 2015 14:17
-
-
Save kimushu/b9100420b1ac232a381b to your computer and use it in GitHub Desktop.
git cache command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
DIR=~/.git-cache | |
usage() { | |
echo "usage: git cache init" | |
echo " or: git cache deinit [-f|--force]" | |
echo " or: git cache list" | |
echo " or: git cache add [-f] [--] <path>..." | |
echo " or: git cache remove <name>..." | |
echo " or: git cache fetch [--all] [--] [<name>...]" | |
echo " or: git cache clone [<options>...]" | |
echo " or: git cache submodule [--quiet] <command> [<options>...]" | |
exit 1 | |
} | |
noarg() { | |
echo "error: option requires an argument -- $1" 1>&2 | |
usage | |
} | |
ilopt() { | |
echo "error: unknown option \`$(echo $1 | sed 's/^-*//')'" 1>&2 | |
usage | |
} | |
ilarg() { | |
echo "error: unknown argument \`$1'" 1>&2 | |
usage | |
} | |
cache_chdir() { | |
if test ! -d $DIR; then | |
echo "fatal: cache directory '$DIR' does not exist." 1>&2 | |
exit 1 | |
fi | |
if ! cd $DIR 2> /dev/null; then | |
echo "fatal: cache directory '$DIR' is not readable." 1>&2 | |
exit 1 | |
fi | |
if [[ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" != "false" ]]; then | |
echo "fatal: cache directory is not a bare git repository." 1>&2 | |
exit 1 | |
fi | |
} | |
declare -a argv=() | |
cmd=$1 | |
shift 1 | |
case "$cmd" in | |
init) | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-*) | |
ilopt $1 | |
;; | |
*) | |
ilarg $1 | |
;; | |
esac | |
done | |
if test -a $DIR; then | |
echo "fatal: '$DIR' already exists." 1>&2 | |
exit 1 | |
fi | |
mkdir -p $DIR && cd $DIR && git init --bare | |
exit $? | |
;; | |
deinit) | |
# TODO | |
echo "fatal: 'deinit' command is not implemented yet. Sorry." | |
exit 1 | |
;; | |
list) | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-*) | |
ilopt $1 | |
;; | |
*) | |
ilarg $1 | |
;; | |
esac | |
done | |
cache_chdir | |
git remote -v | grep '/ (fetch)$/' | |
exit $? | |
;; | |
add) | |
fetch= | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
-f) | |
fetch=-f | |
shift 1 | |
;; | |
--) | |
shift 1 | |
argv+=("$@") | |
break | |
;; | |
-*) | |
ilopt $1 | |
;; | |
*) | |
argv+=("$1") | |
shift 1 | |
;; | |
esac | |
done | |
test ${#argv[@]} -eq 0 && usage | |
cache_chdir | |
for repo in ${argv[@]} | |
do | |
name=${repo##*/} | |
name=${name%.git} | |
git remote add $fetch $name $repo | |
ret=$? | |
test $ret -ne 0 && exit $ret | |
done | |
exit 0 | |
;; | |
fetch) | |
all= | |
while [[ $# -gt 0 ]] | |
do | |
case "$1" in | |
--all) | |
all=--all | |
shift 1 | |
;; | |
--) | |
shift 1 | |
argv+=("$@") | |
break | |
;; | |
-*) | |
ilopt $1 | |
;; | |
*) | |
argv+=("$1") | |
shift 1 | |
;; | |
esac | |
done | |
cache_chdir | |
if [[ -n "$all" && ${#argv[@]} -gt 0 ]]; then | |
echo "fatal: cache fetch --all does not take a repository argument." 1>&2 | |
exit 1 | |
fi | |
git fetch $all ${argv[@]} | |
exit $? | |
;; | |
clone) | |
git clone --reference $DIR $@ | |
exit $? | |
;; | |
submodule) | |
preopt= | |
postopt= | |
while [[ -n "$1" ]] | |
do | |
case "$1" in | |
-*) | |
preopt=$1 | |
shift 1 | |
;; | |
add | update) | |
postopt="--reference $DIR" | |
break | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
subcmd=$1 | |
shift 1 | |
git submodule $preopt $subcmd $postopt $@ | |
exit $? | |
;; | |
"" | help) | |
usage | |
;; | |
*) | |
echo "git-cache: '$cmd' is not a git-cache command. See 'git cache help'." 1>&2 | |
exit 1 | |
;; | |
esac | |
# vim:set et sts=2 sw=2: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment