-
Notifications
You must be signed in to change notification settings - Fork 19
/
repo-admin.sh
202 lines (188 loc) · 5.08 KB
/
repo-admin.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
#GIT repo admin script for gitbox
#https://github.com/nmarus/docker-gitbox
set -e
USER="git"
GROUP="git"
MOD="644"
REPO="/repos"
QUIET=false
#SFLOG="/repo-admin.log"
#help text
showhelp() {
cat 1>&2 << EOF
╔═══════════════════════════════════════════════════════════╗
║ Usage: ║
╠═══════════════════════════════════════════════════════════╣
║ List: repo-admin -l ║
║ Create new empty: repo-admin -n <repo> -d <description> ║
║ Clone from URL: repo-admin -c <url> ║
║ Remove: repo-admin -r <repo> ║
╚═══════════════════════════════════════════════════════════╝
EOF
}
#print timestamp
timestamp() {
date +"%Y-%m-%d %T"
}
#screen/file logger
sflog() {
#if $1 is not null
if [ ! -z ${1+x} ]; then
message=$1
else
#exit function
return 1;
fi
#if $QUIET is not true
if ! $($QUIET); then
echo "${message}"
fi
#if $SFLOG is not null
if [ ! -z ${SFLOG+x} ]; then
#if $2 is regular file or does not exist
if [ -f ${SFLOG} ] || [ ! -e ${SFLOG} ]; then
echo "$(timestamp) ${message}" >> ${SFLOG}
fi
fi
}
#screen/file error logger
sferror() {
#if $1 is not null
if [ ! -z ${1+x} ]; then
message=$1
echo ""
sflog "ERROR: ${message}" >&2
echo ""
showhelp
fi
exit 1
}
#init repository
repo-init() {
#if directory
if [ -d ${REPO} ]; then
chown -R ${USER}:${GROUP} ${REPO} &> /dev/null
#if $MOD is not null
if [ ! -z ${MOD+x} ]; then
find ${REPO} -type f -exec chmod ${MOD} '{}' + &> /dev/null
fi
else
sferror "${REPO} not found"
fi
}
#list repos
repo-list() {
repo-init
while read indir; do
echo $indir | sed -e "s,${REPO}/,,"
done < <(find ${REPO} -type d | grep .git$)
}
#create new repository
repo-new() {
repo-init
#if ends with .git
if [[ $NEW =~ \.git$ ]]; then
if $(git init --bare --shared $REPO/$NEW &> /dev/null && true || false); then
echo $DESC > $REPO/$NEW/description
repo-init
sflog "Created new repository ${NEW} in ${REPO}"
else
sferror "Error creating ${NEW} in ${REPO}"
fi
else
NEW="${NEW}.git"
repo-new
fi
}
#clone repository
repo-clone() {
repo-init
#Get repo name from $URL
CLONE="$(echo "$(echo "$URL" | grep / | cut -d/ -f $(($(grep -o '/' <<< "$URL" | wc -l)+1)) -)")"
#if ends with .git
if [[ $CLONE =~ \.git$ ]]; then
if $(git clone --bare --shared $URL $REPO/$CLONE/ &> /dev/null && true || false); then
echo "Cloned from ${URL}" > $REPO/$CLONE/description
repo-init
sflog "Cloned repository ${CLONE} in ${REPO}"
else
sferror "Error cloning ${URL} in ${REPO}"
fi
else
URL="${URL}.git"
repo-clone
fi
}
#remove repository
repo-remove() {
repo-init
#if ends with .git
if [[ $REMOVE =~ \.git$ ]]; then
rm -rf $REPO/$REMOVE &> /dev/null
sflog "Removed repository ${REMOVE} in ${REPO}"
else
REMOVE="${REMOVE}.git"
repo-remove
fi
}
#parse arguments
while getopts ":vln:d:c:r:" opt; do
case "${opt}" in
v)
repo-init
sflog "Initialized ${REPO}"
exit 0
;;
l)
repo-list
exit 0
;;
n)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
NEW=${OPTARG}
#if $DESC has already been parsed
if [ ! -z ${DESC+x} ]; then
repo-new
exit 0
fi
;;
d)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
DESC=${OPTARG}
#if $NEW has already been parsed
if [ ! -z ${NEW+x} ]; then
repo-new
exit 0
fi
;;
c)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
URL=${OPTARG}
repo-clone
exit 0
;;
r)
if [[ ${OPTARG} =~ ^- ]]; then
sferror "Bad argument string"
fi
REMOVE=${OPTARG}
repo-remove
exit 0
;;
esac
done
#check partial entry for -n -d
if [ ! -z ${NEW+x} ] || [ ! -z ${DESC+x} ]; then
sferror "Both -n and -d must be specified"
fi
#default action if no cmd line args are found
showhelp
exit 0