initial commit
[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"
7MULTIMAIL="/usr/doc/git-2.14.5/contrib/hooks/multimail/git_multimail.py"
8GITUSER="git"
9GITGRP="git"
10VENVSDIR="/usr/local/venvs"
11USEVENV=false
12
13function is_bare() {
14 repodir=$1
15 if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true
16 then
17 true
18 else
19 false
20 fi
21}
22
23function git_init() {
24 PROJECT=$1
25 echo "creating project \"${PROJECT}.git\""
26 read -p "Describe your project in one line: " DESCRIPTION
27 if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
28 mkdir ${GITDIR}/${PROJECT}.git
29 fi
30 cd ${GITDIR}/${PROJECT}.git
31 git init --bare
32 echo "Remember to insert a README.md file to explain what your project is about."
33 mkdir custom-hooks
34 ln -s $MULTIMAIL custom-hooks/git_multimail.py
35 touch hooks/post-receive
36 cat > hooks/post-receive <<EOPR
37#!/bin/sh
38/usr/bin/pee ${GITDIR}/${PROJECT}.git/custom-hooks/deploy.sh \
39 ${GITDIR}/${PROJECT}.git/custom-hooks/pip_install \
40 ${GITDIR}/${PROJECT}.git/custom-hooks/git_multimail.py
41
42EOPR
43 if [ $USEVENV == true ]; then
44 cat > custom-hooks/pip_install << EOPIP
45#!/bin/sh
46while read oldrev newrev refname; do
47 if [[ \$refname =~ .*/master$ ]]; then
48 # definitely updating master;
49 CHECKFILE=\$(git ls-tree --full-tree -r HEAD |grep requirements.txt |awk '{print \$3}')
50 TMPREQ=\$(git cat-file -p \$CHECKFILE > /tmp/${PROJECT}-req.txt)
51 if [ \$CHECKFILE ]; then
52 ${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt
53 fi
54 if git diff-tree --name-only -r -z \$oldrev \$newrev \$CHECKFILE; then
55 ${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt
56 fi
57 fi
58done
59EOPIP
60 fi
61 cat >> config <<EOT
62
63[multimailhook]
64 mailer = "sendmail"
65 refchangeShowGraph = true
66 mailingList = "danixland@gmail.com"
67 commitEmailFormat = "html"
68 htmlInIntro = true
69 htmlInFooter = true
70 from = "danix@danix.xyz"
71 administrator = "danix@danix.xyz"
72 quiet = true
73 logFile = "/var/log/multimail.log"
74 errorLogFile = "/var/log/multimail_err.log"
75EOT
76 touch custom-hooks/deploy.sh
77 cat > custom-hooks/deploy.sh <<EODP
78#!/bin/bash
79# Directory where to deploy files from repository
80DPTARGET=""
81# Directory containing repository
82DPGIT_DIR="${GITDIR}/${PROJECT}.git"
83# Branch that is going to be deployed to server
84DPBRANCH="master"
85
86while read oldrev newrev ref
87do
88 # if DPTARGET is empty we don't want deploy for this project
89 if [[ ! "" == \$DPTARGET ]]; then
90 # let's check that we are deploying to the correct branch
91 if [[ \$ref = refs/heads/\${DPBRANCH} ]]; then
92 echo "Ref \$ref received. Deploying \${DPBRANCH} branch to production..."
93 git --work-tree=\$DPTARGET --git-dir=\$DPGIT_DIR checkout -f \$DPBRANCH
94 NOW=\$(date +"%d%m%Y-%H%M")
95 git tag release_\$NOW \$DPBRANCH
96 echo " /==============================="
97 echo " | DEPLOYMENT COMPLETED"
98 echo " | Target branch: \$DPTARGET"
99 echo " | Target folder: \$DPGIT_DIR"
100 echo " | Tag name : release_\$NOW"
101 echo " \=============================="
102 else
103 echo "Ref \$ref received. Doing nothing: only the \${DPBRANCH} branch may be deployed on this server."
104 fi
105 else
106 echo "Target directory not declared. Skipping deploy to server."
107 fi
108done
109
110EODP
111 chmod 0755 hooks/post-receive custom-hooks/deploy.sh
112 echo $DESCRIPTION > description
113 cd ${GITDIR}/
114 chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git
115 echo "All done, you can now work on \"${PROJECT}.git\""
116 exit 0
117}
118
119if [ ! -z $1 ]; then
120 if [ "python" == $1 ]; then
121 USEVENV=true
122 # this is a python project. Let's create a virtualenv
123 if [ ! -z $2 ]; then
124 PROJECT=$2
125 else
126 read -p 'Python project name: ' PROJECT
127 fi
128 virtualenv ${VENVSDIR}/${PROJECT}
129 echo "virtual environment created inside ${VENVSDIR}/${PROJECT}"
130 else
131 PROJECT=$1
132 fi
133else
134 read -p 'Project name: ' PROJECT
135fi
136
137if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
138 git_init $PROJECT
139else
140 echo "Project directory ${PROJECT}.git already exists."
141 if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then
142 if is_bare ${GITDIR}/${PROJECT}.git
143 then
144 echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name."
145 exit 171
146 else
147 echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name."
148 exit 172
149 fi
150 else
151 echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]"
152 read answer
153 case $answer in
154 Y|y)
155 git_init $PROJECT
156 ;;
157 N|n)
158 echo "Aborting due to user request."
159 exit 173
160 ;;
161 *)
162 # we assume no as default answer.
163 echo "you said \"$answer\" which I don't understand, so to me is no. Aborting."
164 exit 177
165 ;;
166 esac
167 fi
168fi
169
170