Git:Clone All Remote Repos

To my knowledge, there isn’t a good way to clone all git remote repos in a path that doesn’t involve either installing a program or writing a script. That said, here’s the script I wrote to do it.

#!/usr/bin/env bash
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# @author nullspoon <nullspoon@iohq.net>
#

argv=( ${@} )
for (( i=0; i<${#argv[*]}; i++ )); do
  if [[ ${argv[$i]} == "-u" ]]; then
    user=${argv[$i+1]};
    i=$[$i+1]
  elif [[ ${argv[$i]} == "-s" ]]; then
    server=${argv[$i+1]};
    i=$[$i+1]
  elif [[ ${argv[$i]} == "-r" ]]; then
    rbase=${argv[$i+1]};
    i=$[$i+1]
  elif [[ ${argv[$i]} == "-l" ]]; then
    lbase=${argv[$i+1]};
    i=$[$i+1]
  fi
done

if [[ -z $user ]]; then
  echo -e "\nPlease specify the user (-u) to log in to the remote server as.\n"
  exit
elif [[ -z $server ]]; then
  echo -e "\nPlease specify the server (-s) where the remote repos are located.\n"
  exit
elif [[ -z $rbase ]]; then
  echo -e "\nPlease specify a base path (-r) where the repos are located on the remote.\n"
  exit
elif [[ -z $lbase ]]; then
  echo -e "\nPlease specify a desginated path for local clone (-l).\n"
  exit
fi

# Escape our base path for use in regex
rbase_esc=$(echo $rbase | sed 's/\//\\\//g')

if [[ ! -e $lbase ]]; then
  echo -n -e "\n$lbase does not exist. Create? [Y/n] "
  read -n 1 c
  if [[ $c == y ]]; then
    mkdir $lbase
  else
    echo
    exit
  fi
fi
echo -e "\nCloning all...\n"

# Get our repo list
#conn="ssh -q ${user}@${server}"
cmd="find $rbase -name \"*.git\""
repos=( $( ssh -q ${user}@${server} ${cmd} | sed 's/$rbase_esc\(.*\)/\1/' ) )

# This is so we can easily handle relative destination paths
start_path=$(pwd)
for(( i=0; i < ${#repos[*]}; i++ )); do
  # Clean up our strings first
  lrepo=$( echo ${repos[$i]} | sed 's/\(.*\)\.git/\1/' )
  lrepo=$( echo ${lrepo} | sed "s/$rbase_esc\(.*\)/\1/" )
  labs_path=$( echo "${lbase}/${lrepo}" | sed 's/\/\{1,\}/\//g' )
  rabs_path=$( echo "${repos[$i]}" | sed 's/\/\{1,\}/\//g' )
  # Do some real work
  mkdir -p "${labs_path}"
  cd "${labs_path}"
  echo -e "\nFetching ${user}@${server}:${rabs_path}\n"
  # Clone the remote
  cd ..
  git clone ${user}@${server}:${rabs_path}
  # Do not pass Go
  cd ${start_path}
done

Category:Linux

Category:Git