Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 203 lines (163 sloc) 7.7 KB
#!/bin/bash
################################################################################
# archbranch - Script for archiving git branches as tags
#
# Usage - $/your/location/of/script/archbranch [branchname] [defaultbranch]
#
# WARNING - This is still in the development phase and should be used with care,
# do not use if you have not read through the script and understands the process.
#
# @author: Banezaka - banezaka@gmail.com
#
################################################################################
# todo: use the fetch git branch function and save it in a variable and then run the script, then we dont need the second parameter
firstParam=$1
secondParam=$2
cAbort=$'\e[00;31m'
cWarning=$'\e[00;33m'
cOK=$'\e[00;32m'
cReset=$'\e[00m'
if [[ "$firstParam" == "" || "$secondParam" = "" ]]; then
echo -e "\e[00;31m \n - You didn't give me the complete command.\e[00m"
echo -e " - If you want to do that next time you write 'archbranch [name_of_branch_to_archive] [home_branch]'"
echo -e " - The home branch is the branch I should go to when I have archived your branch."
echo -e " - I'll ask you a couple of questions instead."
echo -e ""
fi
if [ "$firstParam" == "" ]; then
read -p " - Which branch do you want to archive? Enter the name of the branch: " prompt
firstParam=${prompt}
fi
if [ "$secondParam" == "" ]; then
read -p " - Which branch do you want to checkout after the archiving? Press enter if you want the branch master or write the branch name and press enter: " prompt
if [ "$prompt" == "" ]; then
secondParam="master"
else
secondParam=${prompt}
fi
fi
if [ "$firstParam" == "master" ]; then
read -p "$cWarning - You said that you want to archive the '$firstParam' branch. Are you sure you want to do that? It is very very very rarely done!$cReset [y/n] " prompt
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
echo -e "$cWarning - Sure! I'll let you through, hope you know what you are doing!$cReset"
else
echo -e "$cOK - Good! You did the right thing!$cReset"
exit 0
fi
fi
if [ "$firstParam" != "" ]; then
# Updating the repo with a fetch so I get all branches and tags
echo -e "\n - Making sure the I have the latest branches and tags"
git fetch
# Checking if the branch exists anywhere
#branchExistsStatus=$(git branch -a | grep -w "$firstParam")
# Checking if the branch exists remotely
branchExistsRemotely=$(git branch -r | grep -w "$firstParam")
# Checking if the branch exists locally
branchExistsLocally=$(git branch | grep -w "$firstParam")
# Checking if the archive tag exists locally
ArchiveExistsLocally=$(git tag | grep -w "archive/$firstParam")
# Checking if the branch is checked out locally
branchCheckedOutLocally=$(git status | grep -w "$firstParam")
# Checking if the default branch exists locally
homeBranchExistsLocally=$(git branch | grep -w "$secondParam")
# If the archive tag exists abort the script
if [ "$ArchiveExistsLocally" == "archive/$firstParam" ]; then
echo -e "\e[00;31m - The branch '$firstParam' already has a archive tag locally. Aborting script\e[00m"
exit 0
else
echo -e "\e[00;32m - No tag found locally for branch '$firstParam'. Continuing.\e[00m"
fi
# If the branch doesnt exists on remote, abort the script
if [ "$branchExistsRemotely" == "" ]; then
echo -e "\e[00;31m - The branch '$firstParam' was not found on remote. Push it before you archive it. Aborting script.\e[00m"
exit 0
else
echo -e "\e[00;32m - Branch '$firstParam' found on remote.\e[00m"
fi
# If the default branch doesn't exists locally, abort the script
if [ "$homeBranchExistsLocally" == "" ]; then
echo -e "\e[00;31m - The branch '$secondParam' doesn't exist locally. I need a branch to default to after the script. Aborting script.\e[00m"
exit 0
else
echo -e "\e[00;32m - The home branch '$secondParam' was found locally.\e[00m"
fi
echo -e "You just told me to archive the branch '$firstParam' and then go back to the the branch '$secondParam'"
read -p "$cWarning Are you sure you want to archive the branch '$firstParam'$cReset? [y/n] " prompt
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
# if the branch exists locally
if [ "$branchExistsLocally" != "" ]; then
# if the branch is checked out locally
if [ "$branchCheckedOutLocally" != "" ]; then
echo -e "The branch '$firstParam' is already checked out locally"
else
echo -e "Checking out the branch '$firstParam'"
git checkout $firstParam
fi
else
echo -e "\n - Checking out the branch '$firstParam' from remote"
git checkout --track -b $firstParam origin/$firstParam
fi
echo -e "\n - Pulling on branch '$firstParam'"
git pull origin $firstParam
echo -e "\n - Creating the tag 'archive/$firstParam'"
git tag archive/$firstParam
# doing a git log and saving the values
rBranchLog=$(git log -n 10 --oneline origin/$firstParam)
rArchiveLog=$(git log -n 10 --oneline archive/$firstParam)
if [ "$rBranchLog" == "" ]; then
echo -e "I couldn't get the branch '$firstParam' on origin"
# should not create a md5 of nothing
rBranchHash='nologtomd5'
else
echo -e "\n################################################### commits on branch 'origin/$firstParam'"
echo -e "$rBranchLog \n"
# if there is a log generate a md5
rBranchHash=$(echo -n $rBranchLog | openssl md5)
fi
if [ "$rArchiveLog" == "" ]; then
echo -e "I couldnt get the archived branch"
# should not create a md5 of nothing
rArchiveHash='nologtomd5'
else
echo -e "################################################### commits on 'archive/$firstParam'"
echo -e "$rArchiveLog \n"
# if there is a log generate a md5
rArchiveHash=$(echo -n $rArchiveLog | openssl md5)
fi
if [ "$rBranchHash" == "nologtomd5" ] || [ "$rArchiveHash" == "nologtomd5" ]; then
echo "Atleast one of the branches don't have a log to md5, no comparison will be made"
else
echo -e "\nHash for remote branch: $rBranchHash"
echo -e "Hash for local archive: $rArchiveHash\n"
fi
# comparing the hashes to see if the branch and the tag have the same log
if [ "$rBranchHash" == "$rArchiveHash" ]; then
echo -e '\e[00;32mThe branch hashes are identical\e[00m'
else
echo -e '\e[00;31mThe hash is different for the branches, you should not archive the branch '$firstParam', you need to check what the difference is between the archived tag and the branch on origin\e[00m'
# diff -y <(echo "$rBranchLog") <(echo "$vArchiveLog")
fi
echo -e "\n"
read -p " - The branch '$firstParam' has now been tagged but not yet archived. Are you sure you want to delete the branch '$firstParam' ? [y/n] " prompt
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
echo -e "\n - Pushing tag 'archive/$firstParam' to remote"
git push origin archive/$firstParam
echo -e "\n - Checking out $secondParam"
git checkout $secondParam
echo -e "\n - Removing the branch '$firstParam' locally"
git branch -D $firstParam
echo -e "\n - Removing the branch '$firstParam' on remote"
git push origin :$firstParam
echo -e "\n - The branch '$firstParam' has now been archived"
else
echo -e "\n - Aborted. The branch '$firstParam' was not removed locally or on the remote repository"
exit 0
fi
else
echo -e "\n - $cAbort Aborted archbranch.$cReset"
exit 0
fi
else
echo -e "\e[00;31m\n - Necessary parameters not provided, please provide branch name and name of branch to go back to." '\e[00mExample $archive mybranch master'
fi