Skip to content

Instantly share code, notes, and snippets.

@ImaginativeShohag
Last active August 6, 2019 12:31
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 ImaginativeShohag/04449de4a23df76ea664b562a254751f to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/04449de4a23df76ea664b562a254751f to your computer and use it in GitHub Desktop.
Script for Pushing Source Code to Multiple Git Repository.
#!/bin/bash
# ================================================================================
# This script will push source code to other repository.
# Version: 1.0
#
# Coded by:
# Md. Mahmudul Hasan Shohag
# imaginativeshohag@gmail.com
# github.com/ImaginativeShohag
#
# The script is licensed under the MIT License.
# ================================================================================
# ================================================================================
# Copyright (c) 2019 Md. Mahmudul Hasan Shohag
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ================================================================================
# ================================================================================
# HOW TO USE?
#
# 1. Change the following variables value:
# - git_repo_one : Current working repository ssh or http url
# - git_repo_two : Backup repository ssh or http url
# - default_commit_msg : Default commit message
#
# 2. Make this script executable:
# $ chmod +x this-script-name.sh
#
# 3. If the backup repository already initialized, then copy-paste the [.git]
# folder to the current directory and renamed it to ".git.repo_two".
#
# 4. Finally run the script:
# $ ./this-script-name.sh
# ================================================================================
# ================================================================================
# Repositories
#
# git_repo_one -> is the working repository
# git_repo_two -> is the backup/other repository
# ================================================================================
git_repo_one="git@github.com:ImaginativeShohag/Test-Repo-One.git"
git_repo_two="git@github.com:ImaginativeShohag/Test-Repo-Two.git"
# ================================================================================
# Default commit message
#
# default_commit_msg -> is the default commit message
# ================================================================================
default_commit_msg="Commit at $(date)"
# ================================================================================
# Git folder names
#
# git_folder_one -> is the working git repository folder
# git_folder_two -> is the backup/other git repository folder
# ================================================================================
git_folder_one=".git.repo_one"
git_folder_two=".git.repo_two"
# ================================================================================
# Other variables
# ================================================================================
active_git_folder=".git"
bottom_new_line="\n"
# ================================================================================
# Colors
# ================================================================================
# Reset
Color_Off='\033[0m' # Text Reset
# Regular Colors
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Yellow='\033[0;33m' # Yellow
Blue='\033[0;34m' # Blue
Purple='\033[0;35m' # Purple
Cyan='\033[0;36m' # Cyan
White='\033[0;37m' # White
# Bold
BBlack='\033[1;30m' # Black
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
BYellow='\033[1;33m' # Yellow
BBlue='\033[1;34m' # Blue
BPurple='\033[1;35m' # Purple
BCyan='\033[1;36m' # Cyan
BWhite='\033[1;37m' # White
# Background
On_Black='\033[40m' # Black
On_Red='\033[41m' # Red
On_Green='\033[42m' # Green
On_Yellow='\033[43m' # Yellow
On_Blue='\033[44m' # Blue
On_Purple='\033[45m' # Purple
On_Cyan='\033[46m' # Cyan
On_White='\033[47m' # White
function show_msg_unfortunately_stopped() {
echo -e "${Red}================================
Error occurred!
The script stopped!
================================${Color_Off}$bottom_new_line"
}
function show_msg_type_info() {
echo -e "${BBlue}INFO: ${Blue}$1${Color_Off}$bottom_new_line"
}
function show_msg_type_success() {
echo -e "${Green}$1${Color_Off}$bottom_new_line"
}
function show_msg_type_error() {
echo -e "${BRed}ERROR: ${Red}$1${Color_Off}$bottom_new_line"
}
# ================================================================================
# Print the repos
# ================================================================================
echo -e "${Blue}================================================================================${Color_Off}"
echo -e "${On_Black}${BWhite} REPOSITORIES ${Color_Off}"
echo -e "${BBlue}General Repo:${Color_Off} ${Purple}${git_repo_one}${Color_Off}"
echo -e "${BBlue}Backup Repo:${Color_Off} ${Purple}${git_repo_two}${Color_Off}"
echo -e "${Blue}================================================================================${Color_Off}"
echo ""
# ================================================================================
# Check if git initialized?
# ================================================================================
if ! [[ -e ${active_git_folder} ]]; then
show_msg_type_error "[Git] is not initialized yet!"
show_msg_unfortunately_stopped
exit
fi
# ================================================================================
# Check .gitignore file is exist?
# ================================================================================
if ! [[ -e .gitignore ]]; then
show_msg_type_error "[.gitignore] file is not exist!"
show_msg_unfortunately_stopped
exit
fi
# ================================================================================
# - check $git_folder_one and $git_folder_two exist in .gitignore file?
# - if not exist:
# - add $git_folder_one and $git_folder_two to .gitignore file
# ================================================================================
if grep -Fxq "$git_folder_one" .gitignore && grep -Fxq "$git_folder_two" .gitignore && grep -Fxq "${0##*/}" .gitignore; then
show_msg_type_info "Folder [$git_folder_one] and [$git_folder_two] and this script name [${0##*/}] is found in [.gitignore] file. :)"
else
show_msg_type_info "Folder name [$git_folder_one] and [$git_folder_two] and this script name [${0##*/}] is NOT found in [.gitignore] file!"
echo ${0##*/} >> .gitignore # add this script file also
echo ${git_folder_one} >> .gitignore
echo ${git_folder_two} >> .gitignore
show_msg_type_info "Folder name [$git_folder_one] and [$git_folder_two] and this script name [${0##*/}] is append to [.gitignore] file."
fi
# ================================================================================
# > git remote -v
# --------------------------------------------------------------------------------
# origin git@github.com:ImaginativeShohag/Test-Repo-One.git (fetch)
# origin git@github.com:ImaginativeShohag/Test-Repo-One.git (push)
# --------------------------------------------------------------------------------
#
#
# - check current git remote is $git_repo_one? (git remote -v)
# - if not:
# - show message and stop script!
# ================================================================================
if ! git remote -v | grep -q ${git_repo_one}; then
show_msg_type_error "[$git_repo_one] is not current remote repo of this project! :("
show_msg_unfortunately_stopped
exit
else
show_msg_type_info "Remote repo [$git_repo_one] found in the project."
fi
# ================================================================================
# - rename existing $active_git_folder to $git_folder_one
# ================================================================================
mv ${active_git_folder} ${git_folder_one}
show_msg_type_info "[${active_git_folder}] renamed to [${git_folder_one}]."
# ================================================================================
# - check $git_folder_two exist?
# - if not exist:
# - > git init
# - > git remote add origin $git_repo_two
# - else:
# - rename $git_folder_two to $active_git_folder
# ================================================================================
if ! [[ -e ${git_folder_two} ]]; then
git init
git remote add origin ${git_repo_two}
show_msg_type_info "Backup repo not initialized yet!"
show_msg_type_info "[Git] initialized and remote repo set to [${git_repo_two}]."
else
mv ${git_folder_two} ${active_git_folder}
show_msg_type_info "Backup repo already initialized. :)"
show_msg_type_info "[${git_folder_two}] renamed to [${active_git_folder}]."
fi
# ================================================================================
# - > git add .
# - > git commit -m "commit at $date"
# - > git push -u origin master
# ================================================================================
echo -e "${BGreen}Please enter a commit message: (Default: $default_commit_msg)${Color_Off}"
read input_commit_msg
if ! [[ -z ${input_commit_msg} ]]; then
default_commit_msg=${input_commit_msg}
fi
show_msg_type_info "Source code pushing to the backup repository with commit message: $default_commit_msg"
git add .
git commit -m "$default_commit_msg"
git push -u origin master
echo ""
# ================================================================================
# - rename now $active_git_folder to $git_folder_two
# ================================================================================
mv ${active_git_folder} ${git_folder_two}
show_msg_type_info "[${active_git_folder}] renamed to [${git_folder_two}]."
# ================================================================================
# - rename $git_folder_one to $active_git_folder
# ================================================================================
mv ${git_folder_one} ${active_git_folder}
show_msg_type_info "[${git_folder_one}] renamed to [${active_git_folder}]"
# ================================================================================
# Success message
# ================================================================================
show_msg_type_success "The script successfully executed! :)"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment