################################################# # # (c) 2008 Alexander E Genaud # # This work as-is I provide. # No warranty express or implied. # I've done my best, # to debug and test. # Liability for damages denied. # # Permission is granted hereby, # to copy, share, and modify. # Use as is fit, # free or for profit. # These rights, on this notice, rely. # ################################################### #################### # # Domain constants # ########## # # Uncomment the following variables, modified as # appropriate. Or export/source these variables # on the command line or in, for example, .bash_profile. # NB: Quotes (') are needed inline (here in this file), # but not in an rc file or export (such as .bash_profile). # # SSH_DOMAIN=user@domain.web # WEB_DOMAIN=sub.domain.web # PATH_TO_WEB='~/path/to/public/web' # PATH_TO_PRIV='~/priv/git' #################### # # Default variables # ########## PATH_TO_REPO= REPO_NAME= REPO_ACCESS=PUBLIC REPO_TYPE=BARE PRINT_HELP=FALSE PRINT_USAGE=FALSE REPO_NAME_COUNT=0 #################### # # Input arg check # ########## if [ $# -eq 0 ]; then PRINT_USAGE=TRUE fi while [ $# -gt 0 ]; do if [ `echo $1 | grep "^-[pwh]*$" | wc -c` -gt 0 ]; then if [ `echo $1 | grep "h" | wc -c` -gt 0 ]; then PRINT_HELP=TRUE fi if [ `echo $1 | grep "p" | wc -c` -gt 0 ]; then REPO_ACCESS=PRIVATE fi if [ `echo $1 | grep "w" | wc -c` -gt 0 ]; then REPO_TYPE=WORK fi elif [ `echo $1 | grep "^--help$" | wc -c` -gt 0 ]; then PRINT_HELP=TRUE elif [ `echo $1 | grep "^--priv$" | wc -c` -gt 0 ]; then REPO_ACCESS=PRIVATE elif [ `echo $1 | grep "^--work$" | wc -c` -gt 0 ]; then REPO_TYPE=WORK elif [ `echo $1 | grep "^-" | wc -c` -gt 0 ]; then echo ERROR: Unknown argument \"`echo $1|sed s:[pwh]::g`\" PRINT_USAGE=TRUE else REPO_NAME_COUNT=`expr $REPO_NAME_COUNT + 1` if [ $REPO_NAME_COUNT -gt 1 ]; then echo ERROR: Multiple repo name arguments: $REPO_NAME, $1 PRINT_USAGE=TRUE else REPO_NAME=$1 fi fi shift done #################### # # Print help usage message and exit # ########## if [ $PRINT_USAGE = "TRUE" -o $PRINT_HELP = "TRUE" ]; then # after shift, we might still have no arguments echo usage: $0 [ -hpw ] REPO echo " -h --help display this help and exit" echo " -p --priv private repository" echo " -w --work remote working tree (rather than bare, not recommended)" if [ $PRINT_HELP = "TRUE" ]; then exit 0; else exit 127 fi fi #################### # # Check directory locally # ########## if [ -e $REPO_NAME ]; then echo ERROR: File already exists locally: $REPO_NAME exit 127 fi #################### # # Print pre-work summary # ########## if [ $REPO_ACCESS = "PRIVATE" ]; then if [ x$REPO_TYPE = x"BARE" ]; then PATH_TO_REPO=$PATH_TO_PRIV'/'$REPO_NAME.git echo Generating private Git bare repository accessible via: else PATH_TO_REPO=$PATH_TO_PRIV'/'$REPO_NAME echo Generating private Git work repository accessible via: fi else if [ x$REPO_TYPE = x"BARE" ]; then PATH_TO_REPO=$PATH_TO_WEB'/'$REPO_NAME.git echo Generating public Git bare repository accessible via: echo " http://$WEB_DOMAIN/$REPO_NAME.git" else PATH_TO_REPO=$PATH_TO_WEB'/'$REPO_NAME echo Generating public Git work repository accessible via: echo " http://$WEB_DOMAIN/$REPO_NAME" fi fi echo " ssh://$SSH_DOMAIN/$PATH_TO_REPO" #################### # # Remote repo # ########## if [ x$REPO_TYPE = x"BARE" ]; then REMOTE_COMMAND=' mkdir -p '$PATH_TO_REPO' && cd '$PATH_TO_REPO' && git --bare init && git --bare update-server-info && echo exec git-update-server-info > hooks/post-update && chmod a+x hooks/post-update;' else REMOTE_COMMAND=' mkdir -p '$PATH_TO_REPO' && cd '$PATH_TO_REPO' && git init && git update-server-info && cd .git/hooks && rm post-update && wget http://utsl.gen.nz/git/post-update && chmod a+xrw post-update && cd ../.. && touch .gitignore && git add . && git commit -m "Init with empty gitignore" && git reset --hard' fi ssh $SSH_DOMAIN ' if [ -e '$PATH_TO_REPO' ]; then echo ERROR: File or directory already exists remotely: '$PATH_TO_REPO' && exit 127; else '$REMOTE_COMMAND' fi' if [ $? -ne 0 ]; then echo ERROR: Failed to create new repository remotely. exit 127 fi #################### # # Local repo # ########## if [ x$REPO_TYPE = x"BARE" ]; then mkdir $REPO_NAME cd $REPO_NAME git init git remote add origin ssh://$SSH_DOMAIN/$PATH_TO_REPO touch .gitignore git add . git commit -m 'Init with empty gitignore' echo Pushing commit to remote repository via ssh git push origin master echo " [branch \"master\"] remote = origin merge = refs/heads/master" >>.git/config else # remote work tree git clone ssh://$SSH_DOMAIN/$PATH_TO_REPO fi #################### # # Completed # ########## echo Completed exit 0