extra scripts are now separated from main create script.
[git_shell.git] / create
CommitLineData
81873de4 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
6GITDIR="/var/git"
d96d4d16 7BASEDIR=".gitbase"
81873de4 8MULTIMAIL="/usr/doc/git-2.14.5/contrib/hooks/multimail/git_multimail.py"
9GITUSER="git"
10GITGRP="git"
11VENVSDIR="/usr/local/venvs"
12USEVENV=false
13
14function 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
24function git_init() {
25 PROJECT=$1
26 echo "creating project \"${PROJECT}.git\""
13686e1e 27 echo -n "Describe your project in one line: "
28 read DESCRIPTION
81873de4 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
81873de4 37
d96d4d16 38 # check if we are creating a python project
81873de4 39 if [ $USEVENV == true ]; then
d96d4d16 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
81873de4 53 fi
d96d4d16 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
81873de4 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"
73EOT
81873de4 74
d96d4d16 75 # fix permissions to post-receive and deploy.sh scripts
81873de4 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 exit 0
82}
83
84if [ ! -z $1 ]; then
85 if [ "python" == $1 ]; then
86 USEVENV=true
87 # this is a python project. Let's create a virtualenv
88 if [ ! -z $2 ]; then
89 PROJECT=$2
90 else
91 read -p 'Python project name: ' PROJECT
92 fi
93 virtualenv ${VENVSDIR}/${PROJECT}
94 echo "virtual environment created inside ${VENVSDIR}/${PROJECT}"
95 else
96 PROJECT=$1
97 fi
98else
99 read -p 'Project name: ' PROJECT
100fi
101
102if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
103 git_init $PROJECT
104else
105 echo "Project directory ${PROJECT}.git already exists."
106 if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then
107 if is_bare ${GITDIR}/${PROJECT}.git
108 then
109 echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name."
110 exit 171
111 else
112 echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name."
113 exit 172
114 fi
115 else
116 echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]"
117 read answer
118 case $answer in
119 Y|y)
120 git_init $PROJECT
121 ;;
122 N|n)
123 echo "Aborting due to user request."
124 exit 173
125 ;;
126 *)
127 # we assume no as default answer.
128 echo "you said \"$answer\" which I don't understand, so to me is no. Aborting."
129 exit 177
130 ;;
131 esac
132 fi
133fi
134
135