|
#!/bin/bash |
|
# |
|
# Makes a repo, tells you how to clone it. You're mustard! |
|
# |
|
# Jay Caines-Gooby |
|
# [email protected] |
|
# @jaygooby on Twitter |
|
|
|
### config for you to change |
|
# |
|
# Where all your repos live |
|
REPO_DIR=/tmp/source #~/source_code/git |
|
|
|
# The user on the remote machine where the $REPO_DIR folder lives |
|
# Usually this would be accessible via ssh |
|
REPO_USER=git |
|
|
|
# The hostname or IP address of the machine hosting the remote repositories |
|
REPO_HOST=127.0.0.1 # REPO_HOST=example.com |
|
|
|
# What other git settings you need |
|
OPTIONAL_GIT_CONFIG=$(cat <<'GITCONFIG' |
|
[hooks "slack"] |
|
username = Repo Bot |
|
channel = git-commits |
|
webhook-url = https://hooks.slack.com/services/your-api-key-goes-here |
|
show-only-last-commit = false |
|
icon-emoji = :metal: |
|
GITCONFIG |
|
) |
|
# |
|
# Do you like hooks? Edit the configure_git_hooks() function |
|
# to create or symlink them as needed |
|
### |
|
|
|
function git_config { |
|
echo "${OPTIONAL_GIT_CONFIG}" >> "$1"/config |
|
} |
|
|
|
# Edit this function to setup new hooks for your repo |
|
# |
|
# This currently symlinks our ~/bin/slask-post-receive script |
|
# as the post-receive hook, so we get notifications of commits into our |
|
# Slack channel. The configuration for this is held in $GITCONFIG |
|
# which is set above. |
|
# You don't need this any of this for a vanilla repository |
|
function configure_git_hooks { |
|
ln -s ~/bin/slack-post-receive "${1}/hooks/post-receive" |
|
} |
|
|
|
# trims leading and trailing whitespace and |
|
# replaces non-alphanumerics with a dash |
|
# TODO: make this more unicode friendly. Do we actually care about ascii-only? |
|
function cleanstring { |
|
# strip trim all whitespace at beginning and end |
|
# http://stackoverflow.com/a/12973694/391826 |
|
CLEANSTRING=$(echo $1 | xargs) |
|
# Now replace remaining non alphanums with a dash |
|
CLEANSTRING=${CLEANSTRING//[^a-zA-Z0-9]/-} |
|
echo "${CLEANSTRING}" |
|
} |
|
|
|
# makes the repo directory, git inits it and sets up the hooks |
|
function init_new_repo { |
|
mkdir "$1" && git init --bare "$1" && configure_git_hooks "$1" && git_config "$1" |
|
} |
|
|
|
function make_new_repo { |
|
local repo_filename="$(basename "$1")" |
|
|
|
# make and git init the bare repository |
|
init_new_repo "$1" |
|
|
|
if [ $? == 0 ]; then |
|
echo "Created your remote repository" |
|
echo -e "Now you need to call:\n" |
|
echo "git remote add origin ${REPO_USER}@${REPO_HOST}:${REPO_DIR}/${repo_filename}" |
|
echo -e "\nin your local git repository to add it as a remote." |
|
echo -e "\nIf you don't yet have a local git repository, you first need to run:\n" |
|
echo "cd /some/directory" |
|
echo "git init ." |
|
echo -e "\nThen run the git add remote call. Then you can git push origin" |
|
echo "and git pull origin as you normally would." |
|
else |
|
echo "Sorry, but ${REPO_DIR}/${repo_filename} couldn't be made" |
|
exit 1 |
|
fi |
|
} |
|
|
|
function existing_repos { |
|
ls -1d "${REPO_DIR}/"*.git | while read file; do basename "${file}"; done || >&2 echo -e "\nNo remote repositories exist yet\n" |
|
} |
|
|
|
function help { |
|
echo -e "Enter exit, quit or q to quit\nl to list current repositories\nr to show the remote add command for the remote repositories\nc to show the clone command for the remote repositories\n? for this help" |
|
} |
|
|
|
function ask_for_repo_name { |
|
local repo |
|
local repo_dir |
|
local repo_path |
|
|
|
echo -e "What do you want to call your repository?\n" |
|
help |
|
|
|
read repo < /dev/tty |
|
|
|
repo_dir=$(cleanstring "${repo}") |
|
repo_path="${REPO_DIR}/${repo_dir}.git" |
|
|
|
case ${repo} in |
|
"") exit 0 ;; |
|
".") exit 0 ;; |
|
"..") exit 0 ;; |
|
"exit") exit 0 ;; |
|
"quit") exit 0 ;; |
|
"q") exit 0 ;; |
|
"l") existing_repos |
|
ask_for_repo_name |
|
;; |
|
"r") existing_repos | while read file; do echo "git remote add origin ${REPO_USER}@${REPO_HOST}:${REPO_DIR}/$file"; done |
|
echo "" |
|
ask_for_repo_name |
|
;; |
|
"c") existing_repos | while read file; do echo "git clone ${REPO_USER}@${REPO_HOST}:${REPO_DIR}/$file"; done |
|
echo "" |
|
ask_for_repo_name |
|
;; |
|
"?") help && ask_for_repo_name ;; |
|
*) if [ -e "${repo_path}" ]; then |
|
echo -e "${repo} already exists - try again\n" |
|
help |
|
ask_for_repo_name |
|
else |
|
make_new_repo "${repo_path}" |
|
fi |
|
|
|
;; |
|
esac |
|
} |
|
|
|
ask_for_repo_name |