#!/bin/sh # # Retrieve public web-interface for git repositories, # then ensure that the same repository tree exists and # is up to date in the current directory. # Currently ONLY CGIT is supported for web scrapping. # user and repository informations http_url="" git_remote="" git_user="git" git_dir="." git_branches="trunk master main" # regexp patterns pattern_before="" log() { echo "[${0}] $@" 2>/dev/null } usage() { [[ ! -z ${1} ]] && log "${1}" cat <<-EOF usage: git-dlall [-u user] [-d root directory] [-p git_repositories] -l http_url -r git_remote EOF } _git_setup() { valid_branch="" for branch in ${git_branches}; do if git branch | grep "${branch}" >/dev/null; then valid_branch="${branch}" break fi done # only continue if a valid principal branch is found [ -z "${valid_branch}" ] && return 1 git pull origin "${valid_branch}" >/dev/null git branch --set-upstream-to="origin/${valid_branch}" "${valid_branch}" >/dev/null } main() { # retrieve command line arguments while getopts "d:l:r:u:" arg; do case "${arg}" in l) http_url=${OPTARG} ;; d) git_dir=${OPTARG} ;; u) git_user=${OPTARG} ;; p) git_repositories=${OPTARG} ;; r) git_remote=${OPTARG} ;; *) usage exit 1 ;; esac done # ensure required variables are not empty if [ -z "${git_remote}" ] || [ -z "${http_url}" ]; then usage "missing remote informations" exit 1 fi # ensure git root directory exists if [ ! -d "${git_dir}" ]; then mkdir "${git_dir}" [ "${?}" -ne 0 ] && usage && exit 1 fi # retrieve available repositories http_content=$(curl --silent --insecure "${http_url}") repo_list=$(echo "${http_content}" | grep -o "$pattern_before.*$pattern_after" | cut -d "'" -f 4) # sanitize repositories for repository in ${repo_list}; do repository="${repository#/}" repositories="${repositories} ${repository%/}" done # sync repository for repository in ${repositories}; do repository_fullpath="${git_dir}/${repository}" if [ -d "${repository_fullpath}" ]; then log "repository : ${repository}. check" else log "repository : ${repository}. clone" git clone "${git_user}@${git_remote}:${repository}" "${repository_fullpath}" fi (cd "${repository_fullpath}" ; _git_setup) done } main $@