| 1 | #! /bin/bash |
| 2 | |
| 3 | # usage: create <PROJECT> - create a git bare repository named PROJECT.git |
| 4 | # this command will setup the repo and send a mail for confirmation. |
| 5 | |
| 6 | GITDIR="/var/git" |
| 7 | BASEDIR=".gitbase" |
| 8 | MULTIMAIL="/usr/doc/git-2.14.5/contrib/hooks/multimail/git_multimail.py" |
| 9 | GITUSER="git" |
| 10 | GITGRP="git" |
| 11 | VENVSDIR="/usr/local/venvs" |
| 12 | USEVENV=false |
| 13 | |
| 14 | function is_bare() { |
| 15 | repodir=$1 |
| 16 | if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true |
| 17 | then |
| 18 | true |
| 19 | else |
| 20 | false |
| 21 | fi |
| 22 | } |
| 23 | |
| 24 | function git_init() { |
| 25 | PROJECT=$1 |
| 26 | echo "creating project \"${PROJECT}.git\"" |
| 27 | echo -n "Describe your project in one line: " |
| 28 | read DESCRIPTION |
| 29 | if [ ! -d ${GITDIR}/${PROJECT}.git ]; then |
| 30 | mkdir ${GITDIR}/${PROJECT}.git |
| 31 | fi |
| 32 | cd ${GITDIR}/${PROJECT}.git |
| 33 | git init --bare |
| 34 | echo "Remember to insert a README.md file to explain what your project is about." |
| 35 | mkdir custom-hooks |
| 36 | ln -s $MULTIMAIL custom-hooks/git_multimail.py |
| 37 | |
| 38 | # check if we are creating a python project |
| 39 | if [ $USEVENV == true ]; then |
| 40 | # if yes |
| 41 | cp ${GITDIR}/${BASEDIR}/post-receive-py hooks/post-receive |
| 42 | sed -i "s|{{GITDIR}}|${GITDIR}|g" hooks/post-receive |
| 43 | sed -i "s|{{PROJECT}}|${PROJECT}|g" hooks/post-receive |
| 44 | cp ${GITDIR}/${BASEDIR}/pip_install custom-hooks/pip_install |
| 45 | sed -i "s|{{VENVSDIR}}|${VENVSDIR}|g" custom-hooks/pip_install |
| 46 | sed -i "s|{{PROJECT}}|${PROJECT}|g" custom-hooks/pip_install |
| 47 | chmod 0755 custom-hooks/pip_install |
| 48 | else |
| 49 | # otherwise |
| 50 | cp ${GITDIR}/${BASEDIR}/post-receive hooks/post-receive |
| 51 | sed -i "s|{{GITDIR}}|${GITDIR}|g" hooks/post-receive |
| 52 | sed -i "s|{{PROJECT}}|${PROJECT}|g" hooks/post-receive |
| 53 | fi |
| 54 | # in any case we setup so that we can deploy |
| 55 | cp ${GITDIR}/${BASEDIR}/deploy.sh custom-hooks/deploy.sh |
| 56 | sed -i "s|{{GITDIR}}|${GITDIR}|g" custom-hooks/deploy.sh |
| 57 | sed -i "s|{{PROJECT}}|${PROJECT}|g" custom-hooks/deploy.sh |
| 58 | # add multimail settings to config |
| 59 | cat >> config <<EOT |
| 60 | |
| 61 | [multimailhook] |
| 62 | mailer = "sendmail" |
| 63 | refchangeShowGraph = true |
| 64 | mailingList = "danixland@gmail.com" |
| 65 | commitEmailFormat = "html" |
| 66 | htmlInIntro = true |
| 67 | htmlInFooter = true |
| 68 | from = "danix@danix.xyz" |
| 69 | administrator = "danix@danix.xyz" |
| 70 | quiet = true |
| 71 | logFile = "/var/log/multimail.log" |
| 72 | errorLogFile = "/var/log/multimail_err.log" |
| 73 | EOT |
| 74 | |
| 75 | # fix permissions to post-receive and deploy.sh scripts |
| 76 | chmod 0755 hooks/post-receive custom-hooks/deploy.sh |
| 77 | echo $DESCRIPTION > description |
| 78 | cd ${GITDIR}/ |
| 79 | chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git |
| 80 | echo "All done, you can now work on \"${PROJECT}.git\"" |
| 81 | echo "You can clone this repository at ssh://danix_git:/${GITDIR}/${PROJECT}.git" |
| 82 | exit 0 |
| 83 | } |
| 84 | |
| 85 | if [ ! -z $1 ]; then |
| 86 | if [ "python" == $1 ]; then |
| 87 | USEVENV=true |
| 88 | # this is a python project. Let's create a virtualenv |
| 89 | if [ ! -z $2 ]; then |
| 90 | PROJECT=$2 |
| 91 | else |
| 92 | read -p 'Python project name: ' PROJECT |
| 93 | fi |
| 94 | virtualenv ${VENVSDIR}/${PROJECT} |
| 95 | echo "virtual environment created inside ${VENVSDIR}/${PROJECT}" |
| 96 | else |
| 97 | PROJECT=$1 |
| 98 | fi |
| 99 | else |
| 100 | read -p 'Project name: ' PROJECT |
| 101 | fi |
| 102 | |
| 103 | if [ ! -d ${GITDIR}/${PROJECT}.git ]; then |
| 104 | git_init $PROJECT |
| 105 | else |
| 106 | echo "Project directory ${PROJECT}.git already exists." |
| 107 | if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then |
| 108 | if is_bare ${GITDIR}/${PROJECT}.git |
| 109 | then |
| 110 | echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name." |
| 111 | exit 171 |
| 112 | else |
| 113 | echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name." |
| 114 | exit 172 |
| 115 | fi |
| 116 | else |
| 117 | echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]" |
| 118 | read answer |
| 119 | case $answer in |
| 120 | Y|y) |
| 121 | git_init $PROJECT |
| 122 | ;; |
| 123 | N|n) |
| 124 | echo "Aborting due to user request." |
| 125 | exit 173 |
| 126 | ;; |
| 127 | *) |
| 128 | # we assume no as default answer. |
| 129 | echo "you said \"$answer\" which I don't understand, so to me is no. Aborting." |
| 130 | exit 177 |
| 131 | ;; |
| 132 | esac |
| 133 | fi |
| 134 | fi |
| 135 | |
| 136 | |