Skip to content

Instantly share code, notes, and snippets.

@techzilla
Created January 20, 2016 22:25
Show Gist options
  • Save techzilla/85a6ecdd3493606405c5 to your computer and use it in GitHub Desktop.
Save techzilla/85a6ecdd3493606405c5 to your computer and use it in GitHub Desktop.
Git Clone, Gogs Create, Git Push
#!/bin/sh
##
## FILE: gogs-clonecreatepush.sh
##
## DESCRIPTION: Git Clone, Gogs Create, Git Push
##
## AUTHOR: J. M. Becker
##
## Exit Point
die() {
[ -n "$2" ] && echo "$2"
exit $1
}
command -v git > /dev/null || die 78 'Required, git'
command -v curl > /dev/null || die 78 'Required, curl'
## Option Parsing
VERSION='0.1'
USAGE="Usage: $0 [-H https://example.com/] [-T ACCESS_TOKEN] [-O GORG] [REPO_URL ...]"
HELP="$USAGE"
while getopts "hH:O:T:V" OPTION
do
case $OPTION in
h)
die 0 "$HELP"
;;
H)
GHOST="${OPTARG}"
;;
O)
GORG="${OPTARG}"
;;
T)
GTOKEN="${OPTARG}"
;;
V)
die 0 "$VERSION"
;;
?)
die 64 "$USAGE"
;;
esac
done
##
[ -z "$GHOST" ] && die 64 "$USAGE"
[ -z "$GORG" ] && die 64 "$USAGE"
[ -z "$GTOKEN" ] && die 64 "$USAGE"
##
shift $(( $OPTIND - 1 ))
##
[ -z "$1" ] && die 64 "$USAGE"
##
## TEMP Directory
TDIR=`mktemp -d tdir.XXXXX`
trap "rm -rf $TDIR" 0 1 2 3 15
## Assert HTTP Status
curl -kIsSf "${GHOST%/}/explore" >/dev/null || die 78 "Unexpected: Responce Code"
GSTRING="${GHOST%/}/api/v1/org/${GORG}/repos?token=${GTOKEN}"
GCHOST="$( echo "${GHOST%/}" | sed "s/^https\?:\/\//\0${GTOKEN}@/" )"
OWD="$PWD"
cd "$TDIR"
## Git Clone
echo "$@" | tr -s ' ' '\n' | xargs -L1 -P3 sh -c \
"GREPO=\$( echo \$0 | sed 's/\(.*\)\/\([[:alnum:].]\+\)/\2/' ) &&
git clone --mirror \$0 && \
curl -ksSf -H \"Content-Type: application/json\" -d '{ \"name\": \"'\"\${GREPO%.git}\"'\", \"private\": false }' ${GSTRING} && \
cd \$GREPO && \
git push --mirror ${GCHOST}/${GORG}/\$GREPO && \
cd .. && rm -rf \$GREPO"
cd "$OWD"
##
## Exit Jump
die 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment