Browse code

Initial commit

Fred authored on01/09/2014 00:30:03
Showing1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,194 @@
1
+#!/bin/bash
2
+# Title: git-repo-creator.sh
3
+# Version: 0.0
4
+# Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5
+# Created in: 2014-08-31
6
+# Modified in:
7
+# Licence : GPL v3
8
+
9
+
10
+
11
+#======#
12
+# Aims #
13
+#======#
14
+
15
+aim="Create a git repository locally and remotely on a given server using ssh."
16
+
17
+
18
+
19
+#==========#
20
+# Versions #
21
+#==========#
22
+
23
+# v0.0: creation
24
+
25
+
26
+
27
+#===========#
28
+# Functions #
29
+#===========#
30
+
31
+# Dependency test
32
+function test_dep {
33
+    if [[ ! $(which $1) ]]
34
+    then
35
+        error "Package $1 is needed. Exiting..." 1
36
+    fi
37
+}
38
+
39
+# Usage message
40
+function usage {
41
+    echo -e "
42
+    \e[32m${0##*/}\e[00m -r|--rep repository_name -d|--drep remote_repository_name -s|--svr server_name -u|--usr user_name -h|--help
43
+
44
+Aim: $aim
45
+
46
+Options:
47
+    -r, --rep       name or path of the repository created locally
48
+    -d, --drep      name or path of the repository created on the server
49
+    -s, --svr       name of the server
50
+    -u, --usr       user name for server connection
51
+    -h, --help      this message
52
+"
53
+}
54
+
55
+
56
+# Error message
57
+function error {
58
+    echo -e "\e[31mError:\e[00m $1"
59
+    exit $2
60
+}
61
+
62
+
63
+# Warning message
64
+function warning {
65
+    echo -e "\e[33mWarning:\e[00m $1"
66
+}
67
+
68
+
69
+#==============#
70
+# Dependencies #
71
+#==============#
72
+
73
+test_dep git
74
+
75
+
76
+
77
+#==========================#
78
+# Declaration of variables #
79
+#==========================#
80
+
81
+# Options
82
+while [[ $# -gt 0 ]]
83
+do
84
+    case $1 in
85
+        -r|--rep    ) rep="$2" ; shift 2 ;;
86
+        -d|--drep   ) drep="$2" ; shift 2 ;;
87
+        -s|--svr    ) svr="$2" ; shift 2 ;;
88
+        -u|--usr    ) usr="$2" ; shift 2 ;;
89
+        -h|--help   ) usage ; exit 0 ;;
90
+        *           ) error "Invalid option: $1\n$(usage)\n" 1 ;;
91
+    esac
92
+done
93
+
94
+
95
+# Check the existence of obligatory options
96
+if [[ -z "$rep" ]]
97
+then
98
+    error "The option -r is required. Exiting...\n$(usage)\n" 1
99
+fi
100
+
101
+if [[ -n "$drep" ]]
102
+then
103
+    [[ -z "$svr" ]] && error "Server destination missing. Exiting...\n$(usage)\n" 1
104
+    [[ -z "$usr" ]] && error "Server user missing. Exiting...\n$(usage)\n" 1
105
+fi
106
+
107
+
108
+
109
+#============#
110
+# Processing #
111
+#============#
112
+
113
+# Creation of the distant repository
114
+echo -e "Setting remote repository up... (may take a while)"
115
+if [[ -n "$drep" ]]
116
+then
117
+
118
+    # Test of the ssh connection
119
+    ssh -q "$usr"@"$svr" "echo 2>&1" &> /dev/null
120
+    if [[ $? != 0 ]]
121
+    then
122
+        error "Unable to connect to the server $svr. Aborting..." 1
123
+    fi
124
+
125
+    # Test of the existance of the distant directory
126
+    ssh -q "$usr"@"$svr" "ls $drep" &> /dev/null
127
+    if [[ $? == 0 ]]
128
+    then
129
+        warning "Remote directory exists already on the server."
130
+        read -p "Continue? [y/N] " answer
131
+        if [[ -z "$answer" || "$answer" =~ [NO]|[no] ]]
132
+        then
133
+            echo "Aborting..."
134
+            exit 1
135
+        fi
136
+    fi
137
+
138
+    # Creation of the local sturcture in /tmp
139
+    git --bare init "/tmp/$drep" &> /dev/null
140
+
141
+    # Upload of the structure on the server
142
+    old_pwd="$PWD"
143
+    cd /tmp
144
+    scp -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
145
+    cd "$old_pwd"
146
+
147
+    # Removing of the tmp files
148
+    rm -R "/tmp/$drep"
149
+fi
150
+
151
+# Creation of the directory
152
+echo -e "Setting local repository up..."
153
+mkdir -p "$rep" 2> /dev/null
154
+
155
+# Git initialisation
156
+cd "$rep"
157
+if [[ -d .git ]]
158
+then
159
+    warning "This repository have already been initialized."
160
+    read -p "Reinitialisation? [y/N] " answer
161
+
162
+    if [[ -z "$answer" || "$answer" =~ [N]|[no] ]]
163
+    then
164
+        echo "Skipping..."
165
+    else
166
+        git init "$rep"
167
+    fi
168
+fi
169
+
170
+# Adding remote destination
171
+if [[ -n "$drep" ]]
172
+then
173
+    echo -e "Adding remote destination in the local repository..."
174
+
175
+    if [[ $(git remote &> /dev/null; echo $?) == 0 ]]
176
+    then
177
+        warning "Remote destination has already been set up for this repository."
178
+        read -p "Reinitialisation? [y/N] " answer
179
+
180
+        if [[ -z "$answer" || "$answer" =~ [N]|[no] ]]
181
+        then
182
+            echo "Skipping..."
183
+        else
184
+            git remote remove origin
185
+            git remote origin "$drep"
186
+        fi
187
+
188
+   else
189
+       git remote add origin "$drep"
190
+   fi
191
+
192
+fi
193
+
194
+exit 0