Browse code

git-repo-creator.sh - v0.2 - SSH port option added

Fred authored on02/06/2019 20:34:44
Showing1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 #!/bin/bash
2 2
 # Title: git-repo-creator.sh
3
-# Version: 0.1
3
+# Version: 0.2
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5 5
 # Created in: 2014-08-31
6
-# Modified in: 2014-09-01
6
+# Modified in: 2019-06-02
7 7
 # Licence : GPL v3
8 8
 
9 9
 
... ...
@@ -20,6 +20,7 @@ aim="Create a git repository locally and remotely on a given server using ssh."
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
+# v0.2 - 2019-06-02: SSH port option added
23 24
 # v0.1 - 2014-09-01: default value for -d using structure of -r added / if remote repository exist, skip instead of interrupt the script / addition of missing part of initialisation step when done for the first time / bug correction in the reinitialisation of remote path
24 25
 # v0.0 - 2014-08-31: creation
25 26
 
... ...
@@ -40,7 +41,7 @@ function test_dep {
40 41
 # Usage message
41 42
 function usage {
42 43
     echo -e "
43
-    \e[32m${0##*/}\e[00m -r|--rep repository_name -d|--drep remote_repository_name -s|--svr server_name -u|--usr user_name -h|--help
44
+    \e[32m${0##*/}\e[00m -r|--rep repository_name -d|--drep remote_repository_name -s|--svr server_name -u|--usr user_name -p|--port ssh_port -h|--help
44 45
 
45 46
 Aim: $aim
46 47
 
... ...
@@ -49,6 +50,7 @@ Options:
49 50
     -d, --drep      name or path of the repository created on the server [default: value of -r]
50 51
     -s, --svr       name of the server
51 52
     -u, --usr       user name for server connection
53
+    -p, --port      SSH port for server connection [default: 22]
52 54
     -h, --help      this message
53 55
 "
54 56
 }
... ...
@@ -88,6 +90,7 @@ do
88 90
         -d|--drep   ) drep="$2" ; shift 2 ;;
89 91
         -s|--svr    ) svr="$2" ; shift 2 ;;
90 92
         -u|--usr    ) usr="$2" ; shift 2 ;;
93
+        -p|--port   ) port="$2" ; shift 2 ;;
91 94
         -h|--help   ) usage ; exit 0 ;;
92 95
         *           ) error "Invalid option: $1\n$(usage)\n" 1 ;;
93 96
     esac
... ...
@@ -119,6 +122,15 @@ then
119 122
     drep="$rep"
120 123
 fi
121 124
 
125
+# Set default value for SSH port
126
+if [[ -n "$port" && $(echo "$port" | egrep -x -v "[[:digit:]]+") ]]
127
+then
128
+    error "The option -p must be numeric only. Exiting...\n$(usage)\n" 1
129
+elif [[ -z $port ]]
130
+then
131
+    port=22
132
+fi
133
+
122 134
 
123 135
 
124 136
 #============#
... ...
@@ -131,14 +143,14 @@ if [[ -n "$drep" ]]
131 143
 then
132 144
 
133 145
     # Test of the ssh connection
134
-    ssh -q "$usr"@"$svr" "echo 2>&1" &> /dev/null
146
+    ssh -p $port -q "$usr"@"$svr" "echo 2>&1" &> /dev/null
135 147
     if [[ $? != 0 ]]
136 148
     then
137 149
         error "Unable to connect to the server $svr. Aborting..." 1
138 150
     fi
139 151
 
140 152
     # Test of the existance of the distant directory
141
-    ssh -q "$usr"@"$svr" "ls $drep" &> /dev/null
153
+    ssh -p $port -q "$usr"@"$svr" "ls $drep" &> /dev/null
142 154
     if [[ $? == 0 ]]
143 155
     then
144 156
         warning "Remote directory exists already on the server."
... ...
@@ -158,7 +170,7 @@ then
158 170
         # Upload of the structure on the server
159 171
         old_pwd="$PWD"
160 172
         cd /tmp
161
-        scp -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
173
+        scp -P $port -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
162 174
         cd "$old_pwd"
163 175
 
164 176
         # Removing of the tmp files
... ...
@@ -204,11 +216,11 @@ then
204 216
             echo "Skipping..."
205 217
         else
206 218
             git remote remove origin
207
-            git remote origin "$usr@$svr:$drep"
219
+            git remote origin "ssh://$usr@$svr:$port/~/$drep"
208 220
         fi
209 221
 
210 222
    else
211
-       git remote add origin "$usr@$svr:$drep"
223
+       git remote add origin "ssh://$usr@$svr:$port/~/$drep"
212 224
    fi
213 225
 
214 226
 fi
Browse code

Version 0.1

Several bugs corrected and functions added (see version part of the script for more details)

Fred authored on01/09/2014 23:33:36
Showing1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 #!/bin/bash
2 2
 # Title: git-repo-creator.sh
3
-# Version: 0.0
3
+# Version: 0.1
4 4
 # Author: Frédéric CHEVALIER <fcheval@txbiomedgenetics.org>
5 5
 # Created in: 2014-08-31
6
-# Modified in:
6
+# Modified in: 2014-09-01
7 7
 # Licence : GPL v3
8 8
 
9 9
 
... ...
@@ -20,7 +20,8 @@ aim="Create a git repository locally and remotely on a given server using ssh."
20 20
 # Versions #
21 21
 #==========#
22 22
 
23
-# v0.0: creation
23
+# v0.1 - 2014-09-01: default value for -d using structure of -r added / if remote repository exist, skip instead of interrupt the script / addition of missing part of initialisation step when done for the first time / bug correction in the reinitialisation of remote path
24
+# v0.0 - 2014-08-31: creation
24 25
 
25 26
 
26 27
 
... ...
@@ -45,7 +46,7 @@ Aim: $aim
45 46
 
46 47
 Options:
47 48
     -r, --rep       name or path of the repository created locally
48
-    -d, --drep      name or path of the repository created on the server
49
+    -d, --drep      name or path of the repository created on the server [default: value of -r]
49 50
     -s, --svr       name of the server
50 51
     -u, --usr       user name for server connection
51 52
     -h, --help      this message
... ...
@@ -66,6 +67,7 @@ function warning {
66 67
 }
67 68
 
68 69
 
70
+
69 71
 #==============#
70 72
 # Dependencies #
71 73
 #==============#
... ...
@@ -98,10 +100,23 @@ then
98 100
     error "The option -r is required. Exiting...\n$(usage)\n" 1
99 101
 fi
100 102
 
103
+# Check variable value for remote repository
101 104
 if [[ -n "$drep" ]]
102 105
 then
103 106
     [[ -z "$svr" ]] && error "Server destination missing. Exiting...\n$(usage)\n" 1
104 107
     [[ -z "$usr" ]] && error "Server user missing. Exiting...\n$(usage)\n" 1
108
+elif [[ -n "$svr" ]]
109
+then
110
+    [[ -z "$usr" ]] && error "Server user missing. Exiting...\n$(usage)\n" 1
111
+elif [[ -n "$usr" ]]
112
+then
113
+    [[ -z "$svr" ]] && error "Server destination missing. Exiting...\n$(usage)\n" 1
114
+fi
115
+
116
+# Set default value for remote repository
117
+if [[ -n "$usr" && -n "$svr" && -z "$drep" ]]
118
+then
119
+    drep="$rep"
105 120
 fi
106 121
 
107 122
 
... ...
@@ -127,28 +142,32 @@ then
127 142
     if [[ $? == 0 ]]
128 143
     then
129 144
         warning "Remote directory exists already on the server."
130
-        read -p "Continue? [y/N] " answer
131
-        if [[ -z "$answer" || "$answer" =~ [NO]|[no] ]]
145
+        read -p "Skipping? [Y/n] " answer
146
+        if [[ -z "$answer" || "$answer" =~ [YES]|[yes] ]]
132 147
         then
133
-            echo "Aborting..."
134
-            exit 1
148
+            echo "Skipping..."
149
+            skip_flag=1
135 150
         fi
136 151
     fi
152
+    
153
+    if [[ -z $skip_flag ]]
154
+    then
155
+        # Creation of the local sturcture in /tmp
156
+        git --bare init "/tmp/$drep" &> /dev/null
137 157
 
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"
158
+        # Upload of the structure on the server
159
+        old_pwd="$PWD"
160
+        cd /tmp
161
+        scp -p -r "${drep%%/*}" $usr@$svr: &> /dev/null
162
+        cd "$old_pwd"
146 163
 
147
-    # Removing of the tmp files
148
-    rm -R "/tmp/$drep"
164
+        # Removing of the tmp files
165
+        rm -R "/tmp/$drep"
166
+    fi
149 167
 fi
150 168
 
151
-# Creation of the directory
169
+
170
+# Creation of the local directory
152 171
 echo -e "Setting local repository up..."
153 172
 mkdir -p "$rep" 2> /dev/null
154 173
 
... ...
@@ -163,16 +182,19 @@ then
163 182
     then
164 183
         echo "Skipping..."
165 184
     else
166
-        git init "$rep"
185
+        git init
167 186
     fi
187
+else
188
+    git init
168 189
 fi
169 190
 
191
+
170 192
 # Adding remote destination
171 193
 if [[ -n "$drep" ]]
172 194
 then
173 195
     echo -e "Adding remote destination in the local repository..."
174 196
 
175
-    if [[ $(git remote &> /dev/null; echo $?) == 0 ]]
197
+    if [[ $(git remote | wc -l) -gt 0 ]]
176 198
     then
177 199
         warning "Remote destination has already been set up for this repository."
178 200
         read -p "Reinitialisation? [y/N] " answer
Browse code

Editing value of origin in remote destination setup

Fred authored on01/09/2014 00:34:03
Showing1 changed files
... ...
@@ -182,11 +182,11 @@ then
182 182
             echo "Skipping..."
183 183
         else
184 184
             git remote remove origin
185
-            git remote origin "$drep"
185
+            git remote origin "$usr@$svr:$drep"
186 186
         fi
187 187
 
188 188
    else
189
-       git remote add origin "$drep"
189
+       git remote add origin "$usr@$svr:$drep"
190 190
    fi
191 191
 
192 192
 fi
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