Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A bash script for your server which creates git repos that you can deploy code to
#!/usr/bin/env bash
# creates a git repo on a remote server to which to deploy to!
#
# Check for git first
type git >/dev/null 2>&1 || { echo >&2 "I require git but it's not installed. Aborting."; exit 1; }
a='N'
while [[ $a == 'N' || $a == 'n' ]]
do
# Collect repo name
if [[ -z "$1" ]]
then
echo -n "Name this repo: "
read REPO
else
REPO=$1
fi
echo -n "$REPO is that correct? [Y/n]"
read a
done
GIT_DIR=$HOME/git/${REPO}.git
# Test for existing repo
while [ -d $GIT_DIR ]; do
echo $GIT_DIR already exists!
echo -n "Use a different name: "
read REPO
GIT_DIR=$HOME/git/${REPO}.git
done
# Collect Target
if [[ -z "$2" ]]
then
echo "The ABSOLUTE PATH to the repository target: "
read -e TARGET
else
TARGET=$2
fi
# confirm with the user what we're about to do
cat <<EOT
==================================================
Great! we are going to initialize a git repo here:
$GIT_DIR
It will point to:
$TARGET
Make it so? [Y/n]
EOT
read a
if [[ $a == 'N' || $a == 'n' ]]
then
exit 42
fi
# Start doing the thing!
# If there is no git dir make it
if [ ! -d $HOME/git/ ];
then
mkdir $HOME/git/
fi
git init --bare $GIT_DIR
echo "#!/bin/sh" > $GIT_DIR/hooks/post-receive
echo "TARGET="$TARGET >> $GIT_DIR/hooks/post-receive
echo "GIT_DIR="$GIT_DIR >> $GIT_DIR/hooks/post-receive
echo 'git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f' >> $GIT_DIR/hooks/post-receive
echo 'echo "latest files moved to $TARGET"' >> $GIT_DIR/hooks/post-receive
chmod +x $GIT_DIR/hooks/post-receive
echo "---------"
echo "post-receive created, check it out: "
ls -la $GIT_DIR/hooks/post-receive
echo "---------"
echo "here are the content of the file:"
cat $GIT_DIR/hooks/post-receive
echo "---------"
echo "now we are creating the folder at $TARGET"
mkdir $TARGET
echo "that's everything, your repo is here: git/${REPO}.git"
echo "add the following to your local git repo:"
echo "git remote add dev ssh@host:git/${REPO}.git"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment