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