Last active
December 1, 2023 11:31
-
-
Save vishesh-baghel/bef0fd87dae1574550bf95e43c627b18 to your computer and use it in GitHub Desktop.
Automation scripts for git
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
RED=$(tput setaf 1) | |
GREEN=$(tput setaf 2) | |
YELLOW=$(tput setaf 3) | |
RESET=$(tput sgr0) | |
push() { | |
validate_parameters $1 $2 | |
if [ $? -eq 0 ]; then | |
check_git_repository | |
if [ $? -eq 0 ]; then | |
build_project | |
fi | |
if [ $? -eq 0 ]; then | |
check_working_tree | |
fi | |
if [ $? -eq 0 ]; then | |
check_changes_in_current_branch | |
fi | |
if [ $? -eq 0 ]; then | |
local commit_message="$1" | |
local target_branch="$2" | |
commit $commit_message | |
update_and_rebase_branch $target_branch | |
push_to_remote_repository | |
fi | |
else | |
echo "${RED}\n[Parameter validation failed]\n${RESET}" | |
fi | |
} | |
validate_parameters() { | |
if [ -z "$1" ]; then | |
echo "${RED}\n[Error: Commit message not provided.]\n${RESET}" | |
return 1 | |
fi | |
if [ -z "$2" ]; then | |
echo "${RED}\n[Error: Target branch not provided.]\n${RESET}" | |
return 1 | |
fi | |
} | |
check_git_repository() { | |
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
echo "Error: Current directory is not a Git repository." | |
return 1 | |
fi | |
} | |
build_project() { | |
mvn clean install | |
if [ $? -eq 0 ]; then | |
echo "${GREEN}\n[Build successful]\n${RESET}" | |
return 0 | |
fi | |
} | |
check_working_tree() { | |
if [ -z "$(git status --porcelain)" ]; then | |
echo "${YELLOW}[No changes to commit]${RESET}" | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
check_changes_in_current_branch() { | |
echo "${GREEN}\n[Stashing changes before pull]\n${RESET}" | |
git stash | |
git pull | |
if [ $? -ne 0 ]; then | |
echo "${YELLOW}\n[Merge conflicts detected. Resolve conflicts and press enter to continue.]\n${RESET}" | |
read -p "" | |
if git diff --quiet --staged; then | |
echo "${RED}\n[Conflicts were not resolved. Returning to the terminal.]\n${RESET}" | |
return | |
else | |
git stash pop | |
return 0 | |
fi | |
else | |
git stash pop | |
fi | |
} | |
commit() { | |
echo "${GREEN}\n[Staging all files]\n${RESET}" | |
git add . | |
local commit_message="$1" | |
local final_message="$(get_ticket_number_from_current_branch) $commit_message" | |
git commit -m "$final_message" | |
echo "${GREEN}\n[Generated commit message: "$final_message"]\n${RESET}" | |
} | |
get_ticket_number_from_current_branch() { | |
local current_branch | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
ticket_number=$(echo "$current_branch" | cut -d'-' -f1-2) | |
if [ "$ticket_number" != "$current_branch" ]; then | |
echo "$ticket_number" | |
return 0 | |
else | |
echo "${YELLOW}\n[Invalid branch name format]\n${RESET}" | |
return 1 | |
fi | |
} | |
update_and_rebase_branch() { | |
local target_branch=$1 | |
echo "${GREEN}\n[Starting and update and rebase process]\n${RESET}" | |
git fetch | |
if git rev-parse --verify "$target_branch" >/dev/null 2>&1; then | |
echo "${GREEN}\n[Updating branch '$target_branch' and rebasing with the current branch]\n${RESET}" | |
git checkout "$target_branch" | |
git pull origin "$target_branch" | |
git checkout - | |
git rebase "$target_branch" | |
if [ $? -ne 0 ]; then | |
echo "${YELLOW}\n[Rebase conflicts detected. Returning to the terminal]\n${RESET}" | |
return | |
fi | |
echo "${GREEN}\n[Update and rebase complete]\n${RESET}" | |
else | |
echo "${YELLOW}\n[Branch '$target_branch' doesn't exist, cannot proceed to rebase with current branch]\n${RESET}" | |
fi | |
} | |
push_to_remote_repository() { | |
git push | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
if git ls-remote --exit-code origin "$current_branch" >/dev/null 2>&1; then | |
echo "${GREEN}\n[Your commits were pushed successfully!]\n${RESET}" | |
else | |
echo "${YELLOW}\n[Branch '$current_branch' not found in the remote repository. Pushing it now]\n${RESET}" | |
git push -u origin "$current_branch" | |
echo "${GREEN}\n[Branch '$current_branch' pushed to the remote repository]\n${RESET}" | |
echo "${GREEN}\n[Your commits were pushed successfully!]\n${RESET}" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment