Skip to content

Instantly share code, notes, and snippets.

@raws
Created July 15, 2012 06:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raws/3115343 to your computer and use it in GitHub Desktop.
Save raws/3115343 to your computer and use it in GitHub Desktop.
Bash helper script for creating a Git repository on systems hosting them
#!/bin/bash
#
# create-git-repo
#
# Create and publish a Git repository so that it is able to be
# cloned via `git clone git@host:repo.git'.
#
# Author: Ross Paffett <ross@rosspaffett.com>
GIT_DATA="/var/git" # Where to store the actual git repositories
GIT_USER="git" # The system git user
GIT_HOME="/home/${GIT_USER}" # Create symlinks to repositories here
if [ -n "$1" ]; then
export GIT_DIR="${GIT_DATA}/${1}.git"
# Initialize the repository in $GIT_DATA
if [ ! -d "$GIT_DIR" ]; then
git init --bare
else
echo "warning: ${GIT_DIR} already exists, skipping repository creation"
fi
# Correct permissions and create convenience symlink
if [ -d "$GIT_DIR" ]; then
chown -R "${GIT_USER}:${GIT_USER}" "${GIT_DIR}"
ln -s "$GIT_DIR" "${GIT_HOME}/$(basename $GIT_DIR)"
else
echo "error: ${GIT_DIR} was not created (\`git init' exited with code ${?})"
exit 1
fi
else
echo "usage: create-git-repo NAME"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment