Last active
January 21, 2022 10:41
-
-
Save aeciopires/2457cccebb9f30fe66ba1d67ae617ee9 to your computer and use it in GitHub Desktop.
Shell Script for update git repositories local
This file contains 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 | |
#------------------------ | |
# Authors: Aecio Pires | |
# Date: 13 jan 2020 | |
# | |
# Objective: Update git repositories local | |
# | |
#--------------------- REQUISITES --------------------# | |
# 1) Install packages: git | |
# | |
#------------------------ | |
#------------------------ | |
# Local Functions | |
#-------------------------------------------------------- | |
# comment: Print usage help. | |
# sintax: usage | |
# | |
function usage() { | |
echo "Script to update git repositories local." | |
echo "Usage:" | |
echo | |
echo "$progpathname dir_git_base" | |
exit 3 | |
} | |
#-------------------------------------------------------- | |
# comment: Check if exists command | |
# | |
# sintax: | |
# checkCommand command1 command2 command3 | |
# | |
# return: 0 is correct or code error | |
# | |
# Example: | |
# checkCommand git kubectl helm | |
# | |
function checkCommand() { | |
local commands="$@" | |
for command in $commands ; do | |
if ! type $command > /dev/null 2>&1; then | |
echo "[ERROR] Command '$command' not found." | |
exit 4 | |
fi | |
done | |
} | |
#-------------------------------------------------------- | |
# comment: Check if a variable is empty | |
# | |
# sintax: | |
# checkVariable name value | |
# | |
# return: 0 is correct or code error | |
# | |
# Example: | |
# checkVariable "variable1" "value1" | |
# | |
function checkVariable() { | |
local variable_name="$1" | |
local value="$2" | |
local debug="${debug}" | |
if [ -z $value ] ; then | |
echo "[ERROR] The variable $variable_name is empty." | |
exit 3 | |
else | |
if [ "$debug" == true ]; then | |
echo "[DEBUG] ${variable_name}: ${value}" | |
fi | |
fi | |
} | |
#------------------------------------------------------------ | |
# comment: Check if the directory exists. | |
# syntax: | |
# checkDir DIR | |
# return: 0 is exists or 1 if not exists | |
# | |
function checkDir(){ | |
local dir="$1" | |
if [ ! -d "$dir" ] ; then | |
echo "[ERROR] Directory '$dir' is not exists." | |
exit 4 | |
fi | |
return 0 | |
} | |
#------------------------ | |
# Variables | |
#------------------------ | |
# Variables general of script | |
progpathname=$BASH_SOURCE | |
progfilename=$(basename "$progpathname") | |
progdirname=$(dirname "$progpathname") | |
debug=true | |
dir_git_base=$1 | |
#------------------------ | |
#------------------------ | |
# Main | |
#------------------------ | |
# Checking if has options | |
if [ $# -eq 0 ]; then | |
usage | |
fi | |
# Testing if commands exists | |
checkCommand git find | |
# Testing if variable is empty | |
checkVariable dir_git_base "$dir_git_base" | |
# Testing if directory exists | |
checkDir "$dir_git_base" | |
cd "$dir_git_base" > /dev/null 2>&1 ; | |
list_dir_git_repositories=$(find . -maxdepth 1 -mindepth 1 -type d) | |
for dir in $list_dir_git_repositories; do | |
if [ -d "$dir/.git" ] ; then | |
cd $dir > /dev/null 2>&1 ; | |
branch_name=$(git symbolic-ref HEAD | cut -d/ -f3-) | |
echo "------------- BEGIN -------------" | |
if [ "$debug" == true ]; then | |
echo "[DEBUG] Directory => '$dir'" | |
echo "[DEBUG] Local branches =>" | |
git branch | |
echo | |
fi | |
echo "[INFO] Updating dir: '$dir' in branch '$branch_name'. | |
If necessary, enter with login/password." | |
git pull | |
echo "---------------------------------" | |
echo | |
echo | |
echo | |
cd - > /dev/null 2>&1 ; | |
else | |
echo "[WARNING] Directory '$dir' is not local git repository." | |
echo | |
echo | |
echo | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment