gitnew
------
gitnew() {
# token path
tokenPath='P:/'
# name of the remote in the config
remoteName='all'
# order of remote repos
# first is main, others are secondary
# the values here are keys for remoteUrl and remoteUser
remoteOrder=(
'github'
'gitlab'
'bitbucket'
)
# remote urls (where to push)
declare -A remoteUrl=(
['github']='git@github.com:user'
['gitlab']='git@gitlab.com:user'
['bitbucket']='git@bitbucket.org:user'
)
# remote users (who is pushing)
declare -A remoteUser=(
['github']='user'
['gitlab']='user'
['bitbucket']='user'
)
##### no config edits below this point
# token folder does not exist
if [ ! -d "$tokenPath" ]; then
echo 'error: token folder not available'
# .git subfolder does not exist
elif [ ! -d ".git" ]; then
echo 'error: .git folder is missing'
# we got everything we need
else
# repo name
repoName="$(basename "$PWD")"
for i in "${!remoteOrder[@]}"
do
# get key
key=${remoteOrder[$i]}
# build full url
urlFull="${remoteUrl[$key]}"'/'"$repoName"'.git'
echo 'Adding remote url'
echo " $key"
echo " $urlFull"
# add main url
if [ "$i" = "0" ]; then
echo ' [main]'
git remote add "$remoteName" "$urlFull"
# add secondary url
else
echo ' [secondary]'
# avoid duplicate
git remote set-url "$remoteName" --delete "$urlFull"
# add
git remote set-url "$remoteName" --add "$urlFull"
fi
done
echo $'\nQuerying github\n--------------------'
curl \
-u "${remoteUser[github]}":"$(cat "$tokenPath"'github.token.txt')" \
-d '{"name":"'"$repoName"'"}' \
'https://api.github.com/user/repos'
echo $'\nQuerying gitlab\n--------------------'
curl \
-H 'PRIVATE-TOKEN: '"$(cat "$tokenPath"'gitlab.token.txt')" \
--data-urlencode 'name='"$repoName" \
--data-urlencode 'public=true' \
'https://gitlab.com/api/v3/projects'
echo $'\nQuerying bitbucket\n--------------------'
curl \
-X POST \
-H 'Content-Type: application/json' \
-u "${remoteUser[bitbucket]}":"$(cat "$tokenPath"'bitbucket.token.txt')" \
-d '{"scm":"git", "has_issues":true, "has_wiki":true}' \
'https://api.bitbucket.org/2.0/repositories/'"${remoteUser[bitbucket]}"'/'"$repoName"
fi
} >&2 # http://stackoverflow.com/a/31656923/3512867
# http://stackoverflow.com/a/1500592/3512867
export -f gitnew
Bonus
-----
Updating multiple subfolders to add another remote storage.
for i in *
do
if [ -d $i ]; then
# enter subdir
cd $i
dir=$(basename "$PWD")
# user feedback
echo $'\n--------------------\nprocessing: '$dir
$(gitnew)
$(git paa) # alias of "push --all all"
# exit subdir
cd '..'
fi
done