Skip to content

Instantly share code, notes, and snippets.

@Yolo-Jerome
Last active December 28, 2017 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yolo-Jerome/e7e32f5c588f4209b362bff2f120d2e1 to your computer and use it in GitHub Desktop.
Save Yolo-Jerome/e7e32f5c588f4209b362bff2f120d2e1 to your computer and use it in GitHub Desktop.
Repair the Git-LFS support for repositories imported from Github to Gitlab
#!/bin/bash
##############################################
# This is a script for repairing Git-LFS #
# support on all imported repositories from #
# github.com to a Gitlab-Server of an #
# organisation #
# #
# This requires all Repositories already #
# imported from Github to Gitlab using the #
# Gitlab import function #
# #
# Author: Jerome Pönisch, Munich 27.07.2017 #
##############################################
######################################################################
## Adjust those
githubUrlStart="https://"
githubLink="github.com/<orga name>"
githubApiLink="api.github.com/orgs/<orga name>/repos"
# maxRepoCount is needed if the organisation has more than 30 repos
# otherwise only the first 30 repos would be mirrored
maxRepoCount="300"
gitlabUrlStart="http://"
gitlabLink="<gitlab server>/<team name>"
######################################################################
######################################################################
## Global formatting #
######################################################################
BOLD=$(tput bold)
GREEN="${BOLD}$(tput setaf 2)"
RED="${BOLD}$(tput setaf 1)"
YELLOW="${BOLD}$(tput setaf 3)"
NORMAL=$(tput sgr0)
function SetBashTitle() {
echo -ne "\e]0; $1 \a"
}
######################################################################
## Progress Bar Functions #
######################################################################
function FinishProgress() {
tput el
tput cuu1
tput el
}
function PrintInfo() {
## Uncomment those lines to print info
tput el
echo $1
UpdateProgress $JOBCOUNT $JOBSDONE
}
function UpdateProgress() {
TOTAL=$1
ACTUAL=$2
PERCENT=$((ACTUAL*100/TOTAL))
COLS=$(tput cols)
AVAILABLECOLS=$((COLS))
PERCENTLENGTH=$(echo $PERCENT | wc -c)
PERCENTLENGTH=$((PERCENTLENGTH+2))
TOTALLENGTH=$(echo $TOTAL | wc -c)
TOTALLENGTH=$((TOTALLENGTH+1))
ACTUALLENGTH=$(echo $ACTUAL | wc -c)
ACTUALLENGTH=$((ACTUALLENGTH+1))
MAXLEN=$((AVAILABLECOLS-PERCENTLENGTH-TOTALLENGTH-ACTUALLENGTH-1))
DONELEN=$((MAXLEN*PERCENT/100))
RESTLEN=$((MAXLEN-DONELEN))
PROBAR="${NORMAL}[\e[30;42m"
for (( i=1; i<${DONELEN}; i++ ))
do
PROBAR+="#"
done
PROBAR+="${NORMAL}"
for (( i=1; i<${RESTLEN}; i++ ))
do
PROBAR+="_"
done
PROBAR+=" "${GREEN}${ACTUAL}"|"${TOTAL}" ~ "${PERCENT}"% "${NORMAL}"]"
SetBashTitle "MIGRATING REPOSITORIES ["${JOBSDONE}"|"${JOBCOUNT}"] ~ "${PERCENT}"%"
echo -ne ${PROBAR}"\r"
}
JOBCOUNT=0
JOBSDONE=0
######################################################################
## Get Sensible Data at runtime #
######################################################################
bold=$(tput bold)
normal=$(tput sgr0)
echo "${bold} Provide your credentils for Github ${normal}"
echo -n "${bold}Github Username: ${normal}"
read -e githubName
echo -n "${bold}Github Password: ${normal}"
read -e -s githubPassword
echo ""
echo ""
echo "${bold} Provide your credentials for Gitlab ${normal}"
echo -n "${bold}Gitlab Username: ${normal}"
read -e gitlabName
echo -n "${bold}Gitlab Password: ${normal}"
read -e -s gitlabPassword
######################################################################
## Add folder for cloned git repos #
######################################################################
rm -rf gits &> /dev/null
mkdir gits &> /dev/null
######################################################################
## GET JSON of all REPOS from the Github API in steps of 30 #
## writes output to file test for later use #
######################################################################
apiRounds=$((maxRepoCount / 30))
rm test*
for i in $(seq 1 ${apiRounds})
do
apiURL="${githubUrlStart}${githubName//\"}:${githubPassword//\"}@${githubApiLink//\"}?page=$i"
curl ${apiURL} > test
# Get repo names
# -> http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html
cat test | jq ".[] | .name" >> test2
done
JOBCOUNT=$(cat test2 | wc -l)
echo ""
PrintInfo "Migrating ${JOBCOUNT} repositories from github.com to holo-git.holo.local"
######################################################################
## Clone each repo local and use it to repair git lfs #
######################################################################
JOBSDONE=0
while read -r reponame
do
PrintInfo ""
PrintInfo "${bold} REPAIRING ${reponame//\"} ${normal}"
PrintInfo ""
echo ""
# Build full link for cloning
fulllink="$githubUrlStart$githubName:$githubPassword@$githubLink/${reponame//\"}"
# Clone from github using git lfs
git lfs clone --mirror "${fulllink//\"}" "gits/${reponame//\"}"
# Enter folder of cloned repository
cd "gits/${reponame//\"}"
# repair git lfs
# get lfs from github
git lfs fetch --all
# Just for security remove the github remote
git remote remove origin
# add the remote for gitlab
gitlabRemote="$gitlabUrlStart$gitlabName:$gitlabPassword@$gitlabLink/${reponame//\"}.git"
git remote add origin "${gitlabRemote//\"}"
# Push lfs to the gitlab remote
git lfs push origin --all
# leave folder!
cd ../..
# remove folder to save disk space
rm -rf gits/${reponame//\"}
JOBSDONE=$((JOBSDONE+1))
PrintInfo ""
PrintInfo "${bold} ${reponame//\"} DONE ${normal}"
PrintInfo "--------------------------------------"
done < test2
FinishProgress
######################################################################
## Remove the gits folder #
######################################################################
rm -rf gits
echo ""
echo "${bold} ########################################### ${normal}"
echo "${bold} # FINISHED GITHUB -> GITLAB LFS MIGRATION # ${normal}"
echo "${bold} ########################################### ${normal}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment