Skip to content

Instantly share code, notes, and snippets.

@JonasGao
Created November 10, 2019 08:54
Show Gist options
  • Save JonasGao/5dfa6add18814e0719ff2e43851fdd6b to your computer and use it in GitHub Desktop.
Save JonasGao/5dfa6add18814e0719ff2e43851fdd6b to your computer and use it in GitHub Desktop.
move git repo
#!/bin/bash
shopt -s extglob
cwd=$(pwd)
target=${1}
ls ${target} | while read dirname; do
gitRepo="${target}/${dirname}"
gitDir="${gitRepo}/.git"
if [[ -d ${gitDir} ]]; then
cd ${gitRepo}
gitRemoteUrl=$(git config --get remote.origin.url)
cd ${cwd}
if [[ ${gitRemoteUrl} == https://* ]]; then
# rm https:// prefix
temp=${gitRemoteUrl/https:\/\//}
# split by '/'
group=(${temp//\// })
# build mv to parent path
mv_to_parent="${HOME}/${group[0]}/${group[1]}"
# create mv to parent path
mkdir -p ${mv_to_parent}
# mv the git repo
mv ${gitRepo} ${mv_to_parent}
elif [[ ${gitRemoteUrl} == git@* ]]; then
# rm prefix
temp=${gitRemoteUrl/git@/}
# replace ':'
temp=${temp/:/\/}
# split by '/'
group=(${temp//\// })
# build mv to parent path
mv_to_parent="${HOME}/${group[0]}/${group[1]}"
# create mv to parent path
mkdir -p ${mv_to_parent}
# mv the git repo
mv ${gitRepo} ${mv_to_parent}
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment