|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +##################################### |
| 4 | +# # |
| 5 | +# @author : 00xWolf # |
| 6 | +# GitHub : @mmsaeed509 # |
| 7 | +# Developer : Mahmoud Mohamed # |
| 8 | +# Copyright : Exodia OS # |
| 9 | +# # |
| 10 | +##################################### |
| 11 | + |
| 12 | +## ------------ COLORS ------------ ## |
| 13 | + |
| 14 | +# Reset # |
| 15 | +RESET_COLOR='\033[0m' # Text Reset |
| 16 | + |
| 17 | +# Regular Colors # |
| 18 | +Black='\033[0;30m' Red='\033[0;31m' Green='\033[0;32m' Yellow='\033[0;33m' |
| 19 | +Blue='\033[0;34m' Purple='\033[0;35m' Cyan='\033[0;36m' White='\033[0;37m' |
| 20 | + |
| 21 | +# Bold # |
| 22 | +BBlack='\033[1;30m' BRed='\033[1;31m' BGreen='\033[1;32m' BYellow='\033[1;33m' |
| 23 | +BBlue='\033[1;34m' BPurple='\033[1;35m' BCyan='\033[1;36m' BWhite='\033[1;37m' |
| 24 | + |
| 25 | +# Underline # |
| 26 | +UBlack='\033[4;30m' URed='\033[4;31m' UGreen='\033[4;32m' UYellow='\033[4;33m' |
| 27 | +UBlue='\033[4;34m' UPurple='\033[4;35m' UCyan='\033[4;36m' UWhite='\033[4;37m' |
| 28 | + |
| 29 | +# Background # |
| 30 | +On_Black='\033[40m' On_Red='\033[41m' On_Green='\033[42m' On_Yellow='\033[43m' |
| 31 | +On_Blue='\033[44m' On_Purple='\033[45m' On_Cyan='\033[46m' On_White='\033[47m' |
| 32 | + |
| 33 | +# High Intensity # |
| 34 | +IBlack='\033[0;90m' IRed='\033[0;91m' IGreen='\033[0;92m' IYellow='\033[0;93m' |
| 35 | +IBlue='\033[0;94m' IPurple='\033[0;95m' ICyan='\033[0;96m' IWhite='\033[0;97m' |
| 36 | + |
| 37 | +# Bold High Intensity # |
| 38 | +BIBlack='\033[1;90m' BIRed='\033[1;91m' BIGreen='\033[1;92m' BIYellow='\033[1;93m' |
| 39 | +BIBlue='\033[1;94m' BIPurple='\033[1;95m' BICyan='\033[1;96m' BIWhite='\033[1;97m' |
| 40 | + |
| 41 | +# High Intensity backgrounds # |
| 42 | +On_IBlack='\033[0;100m' On_IRed='\033[0;101m' On_IGreen='\033[0;102m' On_IYellow='\033[0;103m' |
| 43 | +On_IBlue='\033[0;104m' On_IPurple='\033[0;105m' On_ICyan='\033[0;106m' On_IWhite='\033[0;107m' |
| 44 | + |
| 45 | +echo "" |
| 46 | +echo -e "${BCyan}#############################${RESET_COLOR}" |
| 47 | +echo -e "${BCyan}# Git Push Script #${RESET_COLOR}" |
| 48 | +echo -e "${BCyan}#############################${RESET_COLOR}" |
| 49 | + |
| 50 | +# get branch name (e.g master, main, etc... ) # |
| 51 | +DEFAULT_BRANCH=$(git branch --show-current) |
| 52 | +TARGET_BRANCH=${DEFAULT_BRANCH} |
| 53 | + |
| 54 | +# get default commit message based on changes # |
| 55 | +DEFAULT_COMMIT_MSG="" |
| 56 | + |
| 57 | +# IFS -> Internal Field Separator |
| 58 | +# IFS in Bash. It is a special variable that determines how Bash recognizes word boundaries. |
| 59 | +# By default, IFS is set to space, tab, and newline characters. |
| 60 | +while IFS=$'\n' read -r -d '' line; |
| 61 | + |
| 62 | + do |
| 63 | + |
| 64 | + status=$(echo "$line" | awk '{print $1}') |
| 65 | + file=$(echo "$line" | awk '{$1=""; print $0}' | sed -e 's/^[ \t]*//') |
| 66 | + |
| 67 | + case "$status" |
| 68 | + |
| 69 | + in |
| 70 | + |
| 71 | + D) |
| 72 | + # Deleted # |
| 73 | + DEFAULT_COMMIT_MSG+=" ${BRed}==> deleted : $file${RESET_COLOR}" |
| 74 | + DEFAULT_COMMIT_MSG+="" |
| 75 | + |
| 76 | + ;; |
| 77 | + |
| 78 | + M) |
| 79 | + # Modified # |
| 80 | + DEFAULT_COMMIT_MSG+=" ${BCyan}==> modified : $file ${RESET_COLOR}" |
| 81 | + DEFAULT_COMMIT_MSG+="" |
| 82 | + |
| 83 | + ;; |
| 84 | + |
| 85 | + \?\?) |
| 86 | + |
| 87 | + # Added # |
| 88 | + DEFAULT_COMMIT_MSG+=" ${BGreen}==> added : $file ${RESET_COLOR}" |
| 89 | + DEFAULT_COMMIT_MSG+="" |
| 90 | + |
| 91 | + ;; |
| 92 | + |
| 93 | + esac |
| 94 | + |
| 95 | + # Add a newline character after each line # |
| 96 | + DEFAULT_COMMIT_MSG+="\n" |
| 97 | + |
| 98 | +done < <(git status -s -z) |
| 99 | + |
| 100 | +# Remove the trailing comma and space, if any # |
| 101 | +DEFAULT_COMMIT_MSG=$(echo -e "$DEFAULT_COMMIT_MSG" | sed 's/, $//' | tr '\\' '\n') |
| 102 | + |
| 103 | +# If no changes detected, use a default message # |
| 104 | +if [ -z "$DEFAULT_COMMIT_MSG" ]; |
| 105 | + |
| 106 | + then |
| 107 | + |
| 108 | + DEFAULT_COMMIT_MSG=" ==> NO changes" |
| 109 | + |
| 110 | +fi |
| 111 | + |
| 112 | +echo -e "\n${BRed}[*] Your Current Branch : ${BYellow}${DEFAULT_BRANCH}${RESET_COLOR}" |
| 113 | + |
| 114 | +# get new updates if it founded # |
| 115 | +echo -e "\n${BPurple}[+] Updating Repo... \n${RESET_COLOR}" |
| 116 | +git pull |
| 117 | + |
| 118 | +echo -e "\n${BPurple}[+] The new changes in the repo:\n\n${BYellow}${DEFAULT_COMMIT_MSG}${RESET_COLOR}" |
| 119 | + |
| 120 | +# Loop through all arguments # |
| 121 | +while [[ $# -gt 0 ]]; |
| 122 | + |
| 123 | + do |
| 124 | + |
| 125 | + case "$1" in |
| 126 | + |
| 127 | + -t|--target-branch) |
| 128 | + TARGET_BRANCH="$2" |
| 129 | + shift 2 |
| 130 | + ;; |
| 131 | + |
| 132 | + -m|--commit-msg) |
| 133 | + DEFAULT_COMMIT_MSG="$2" |
| 134 | + shift 2 |
| 135 | + ;; |
| 136 | + |
| 137 | + --create-pr) |
| 138 | + CREATE_PR=true |
| 139 | + TARGET_PR_BRANCH="$2" |
| 140 | + shift 2 |
| 141 | + ;; |
| 142 | + |
| 143 | + *) |
| 144 | + # Ignore unrecognized options # |
| 145 | + shift |
| 146 | + ;; |
| 147 | + |
| 148 | + esac |
| 149 | + |
| 150 | +done |
| 151 | + |
| 152 | +echo -e "\n${BRed}[+] Target Branch : ${BYellow}${TARGET_BRANCH}${RESET_COLOR}" |
| 153 | +if [ "${TARGET_BRANCH}" != "${DEFAULT_BRANCH}" ]; |
| 154 | + |
| 155 | + then |
| 156 | + |
| 157 | + if git show-ref --verify --quiet "refs/heads/${TARGET_BRANCH}"; |
| 158 | + |
| 159 | + then |
| 160 | + |
| 161 | + echo -e "${BBlue} └──> Changing to the Target Branch: ${BYellow}${TARGET_BRANCH}${RESET_COLOR}" |
| 162 | + git checkout ${TARGET_BRANCH} |
| 163 | + |
| 164 | + else |
| 165 | + |
| 166 | + echo -e "${BBlue} └──> Creating and changing to the Target Branch: ${BYellow}${TARGET_BRANCH}${RESET_COLOR}" |
| 167 | + git checkout -b ${TARGET_BRANCH} |
| 168 | + |
| 169 | + fi |
| 170 | + |
| 171 | +fi |
| 172 | + |
| 173 | + |
| 174 | +echo -e "\n${BPurple}[+] Adding new changes to the repo... \n${RESET_COLOR}" |
| 175 | +git add --all . |
| 176 | + |
| 177 | +# Check for a custom commit message # |
| 178 | +if [ -n "${DEFAULT_COMMIT_MSG}" ]; |
| 179 | + |
| 180 | + then |
| 181 | + |
| 182 | + echo "" |
| 183 | + git commit -m "${DEFAULT_COMMIT_MSG}" |
| 184 | + |
| 185 | +fi |
| 186 | + |
| 187 | +# push to Target Branch # |
| 188 | +echo "" |
| 189 | +git push -u origin ${TARGET_BRANCH} |
| 190 | + |
| 191 | +# Check and create a pull request # |
| 192 | +if [ "${CREATE_PR}" == true ] && [ -n "${TARGET_PR_BRANCH}" ]; |
| 193 | + |
| 194 | + then |
| 195 | + |
| 196 | + echo -e "\n${BBlue}[+] Creating a pull request from ${BRed}${TARGET_BRANCH} ${BBlue}to ${BYellow}${TARGET_PR_BRANCH}...${RESET_COLOR}" |
| 197 | + gh pr create --base ${TARGET_PR_BRANCH} --head ${TARGET_BRANCH} --title "Pull Request: ${TARGET_BRANCH} to ${TARGET_PR_BRANCH}" --body "Please review and merge." |
| 198 | + |
| 199 | +fi |
| 200 | + |
| 201 | + |
| 202 | +# D O N E! # |
| 203 | +echo -e "\n${BGreen}[✔] D O N E \n${RESET_COLOR}" |
0 commit comments