blob: 77d05233df550a07d9fc074f4ed68baecbc2a10e (
plain)
1 Git:Clone All Remote Repos
2 ==========================
3 :author: Aaron Ball
4 :email: nullspoon@iohq.net
5
6
7 == {doctitle}
8
9 To my knowledge, there isn't a good way to clone all git remote repos in a path
10 that doesn't involve either installing a program or writing a script. That
11 said, here's the script I wrote to do it.
12
13 ----
14 #!/usr/bin/env bash
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # @author nullspoon <nullspoon@iohq.net>
30 #
31
32 argv=( ${@} )
33 for (( i=0; i<${#argv[*]}; i++ )); do
34 if [[ ${argv[$i]} == "-u" ]]; then
35 user=${argv[$i+1]};
36 i=$[$i+1]
37 elif [[ ${argv[$i]} == "-s" ]]; then
38 server=${argv[$i+1]};
39 i=$[$i+1]
40 elif [[ ${argv[$i]} == "-r" ]]; then
41 rbase=${argv[$i+1]};
42 i=$[$i+1]
43 elif [[ ${argv[$i]} == "-l" ]]; then
44 lbase=${argv[$i+1]};
45 i=$[$i+1]
46 fi
47 done
48
49 if [[ -z $user ]]; then
50 echo -e "\nPlease specify the user (-u) to log in to the remote server as.\n"
51 exit
52 elif [[ -z $server ]]; then
53 echo -e "\nPlease specify the server (-s) where the remote repos are located.\n"
54 exit
55 elif [[ -z $rbase ]]; then
56 echo -e "\nPlease specify a base path (-r) where the repos are located on the remote.\n"
57 exit
58 elif [[ -z $lbase ]]; then
59 echo -e "\nPlease specify a desginated path for local clone (-l).\n"
60 exit
61 fi
62
63 # Escape our base path for use in regex
64 rbase_esc=$(echo $rbase | sed 's/\//\\\//g')
65
66 if [[ ! -e $lbase ]]; then
67 echo -n -e "\n$lbase does not exist. Create? [Y/n] "
68 read -n 1 c
69 if [[ $c == y ]]; then
70 mkdir $lbase
71 else
72 echo
73 exit
74 fi
75 fi
76 echo -e "\nCloning all...\n"
77
78 # Get our repo list
79 #conn="ssh -q ${user}@${server}"
80 cmd="find $rbase -name \"*.git\""
81 repos=( $( ssh -q ${user}@${server} ${cmd} | sed 's/$rbase_esc\(.*\)/\1/' ) )
82
83 # This is so we can easily handle relative destination paths
84 start_path=$(pwd)
85 for(( i=0; i < ${#repos[*]}; i++ )); do
86 # Clean up our strings first
87 lrepo=$( echo ${repos[$i]} | sed 's/\(.*\)\.git/\1/' )
88 lrepo=$( echo ${lrepo} | sed "s/$rbase_esc\(.*\)/\1/" )
89 labs_path=$( echo "${lbase}/${lrepo}" | sed 's/\/\{1,\}/\//g' )
90 rabs_path=$( echo "${repos[$i]}" | sed 's/\/\{1,\}/\//g' )
91 # Do some real work
92 mkdir -p "${labs_path}"
93 cd "${labs_path}"
94 echo -e "\nFetching ${user}@${server}:${rabs_path}\n"
95 # Clone the remote
96 cd ..
97 git clone ${user}@${server}:${rabs_path}
98 # Do not pass Go
99 cd ${start_path}
100 done
101 ----
102
103
104 Category:Linux
105
106 Category:Git
107
108
109 // vim: set syntax=asciidoc:
|