Last active
November 2, 2018 23:26
-
-
Save jandradap/f925473f36d6d045473fcd78ceb3b63b to your computer and use it in GitHub Desktop.
Update all git repos in the path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # -*- ENCODING: UTF-8 -*- | |
| # | |
| # ------------------------------------------------------------------ | |
| # [Jorge Andrada Prieto] [[email protected]] | |
| # Title: update_git_repos.sh | |
| # Fecha: 02/11/2018 | |
| # Description: Update all git repos in the path | |
| # If the repositories are https, need to configure user and pass/token: | |
| # | |
| # git config --global credential.helper 'cache --timeout=36000' | |
| # git config --global credential.https://git.521000.best.username xxxxxxxxx | |
| # git config --global credential.https://git.521000.best.password xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
| # git config --global credential.https://gitlab.com.username xxxxxxxxx | |
| # git config --global credential.https://gitlab.com.password xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
| # ------------------------------------------------------------------ | |
| TIMEOUT_CREDENTIALS=3600 | |
| # store the current dir | |
| CUR_DIR=$(pwd) | |
| # Let the person running the script know what's going on. | |
| echo -e "\n\033[1mPulling in latest changes for all repositories...\033[0m" | |
| # Find all git repositories and update it to the master latest revision | |
| for i in $(find . -name ".git" | cut -c 3-); do | |
| # We have to go to the .git parent directory to call the pull command | |
| cd "$i"; | |
| cd ..; | |
| # finally pull | |
| #git pull origin master; | |
| config_git=$(git config --list) | |
| git_email=$(echo "$config_git"| grep mail | awk -F "=" '{print$2}') | |
| git_name=$(echo "$config_git"| grep user.name | awk -F "=" '{print$2}') | |
| git_url=$(echo "$config_git"| grep url | awk -F "=" '{print$2}') | |
| git_path=$(basename $(echo "$i" | awk -F "/.git" '{print$1}')) | |
| if [[ $git_url == *@* ]]; then { | |
| git_url_base=$(echo "$config_git"| grep url | awk -F "@" '{print$2}' | awk -F ":" '{print$1}') | |
| echo -e "\nUpdating repository \033[33m$git_path\033[0m ssh from $git_url_base" | |
| if git fetch --all && git pull -v --all; then { | |
| echo -e "\t\e[32mUpdate OK.\e[0m" | |
| } else { | |
| echo -e "\t\e[31mError at update.\e[0m" | |
| ERROR_UPDATE="$ERROR_UPDATE\n - $git_path ssh from $git_url_base" | |
| } | |
| fi | |
| } else { | |
| git_url_base=$(echo $git_url | awk -F "//" '{print$2}' | awk -F "/" '{print$1}') | |
| echo -e "\nUpdating repository \033[33m$git_path\e[0m https from $git_url_base" | |
| if git fetch --all && git pull -v --all; then { | |
| echo -e "\t\e[32mUpdate OK.\e[0m" | |
| } else { | |
| echo -e "\t\e[31mError at update.\e[0m" | |
| ERROR_UPDATE="$ERROR_UPDATE\n - $git_path https from $git_url_base" | |
| } | |
| fi | |
| } | |
| fi | |
| # lets get back to the CUR_DIR | |
| cd $CUR_DIR | |
| unset config_git | |
| unset git_email | |
| unset git_name | |
| unset git_url | |
| unset git_url_base | |
| done | |
| echo -e "\n\033[32mComplete!\e[0m" | |
| if [ "$ERROR_UPDATE" != "" ]; then echo -e "\n\e[31mError updating:\e[0m\n\t$ERROR_UPDATE"; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment