100 lines
5.9 KiB
Bash
Executable File
100 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Handles the arguments
|
|
remote="origin"
|
|
branch="temp"
|
|
newbranch="true"
|
|
|
|
while getopts ":r:b:n:h:" opt; do
|
|
case $opt in
|
|
r) remote="$OPTARG" ;;
|
|
b) branch="$OPTARG" ;;
|
|
n) newbranch="$OPTARG" ;;
|
|
h)
|
|
printf "Usage: git-safe-push\n Options:\n -r <remote (default=\"origin\")>\n -b <branch (default=\"temp\")>\n -n <newbranch <true/false> (default=true)>"
|
|
exit
|
|
;;
|
|
\?)
|
|
printf "Usage: git-safe-push\n Options:\n -r <remote (default=\"origin\")>\n -b <branch (default=\"temp\")>\n -n <newbranch <true/false> (default=true)>"
|
|
exit
|
|
;;
|
|
esac
|
|
done
|
|
|
|
|
|
if git rev-parse --is-inside-work-tree >/dev/null 2>/dev/null; then
|
|
printf "Trying to connect to remote %s...\n" "$remote"
|
|
if git ls-remote --exit-code "$remote" >/dev/null 2>/dev/null; then
|
|
remote_link=$(git remote get-url "$remote")
|
|
printf "Connected successfully to %s (%s)\n" "$remote" "$remote_link"
|
|
link=${remote_link%.git}
|
|
|
|
if [ "$newbranch" = "true" ]; then
|
|
# Checks if branch exists in the remote
|
|
printf "\nChecking for branch %s...\n\n" "$branch"
|
|
exists_in_remote=$(git ls-remote --heads "$remote" "$branch")
|
|
|
|
# If the branch does not exist
|
|
if [ -z "$exists_in_remote" ]; then
|
|
# Creates the branch, pushes it and deletes the branch locally
|
|
git branch "$branch"
|
|
git push "$remote" "$branch"
|
|
git branch -d "$branch" >/dev/null 2>/dev/null
|
|
|
|
# Prints output message
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Branch does not yet exist, pushed to %s/%s...\n\n You can now create a pull request at:\n > %s/pull/new/%s\n\n----------------------------------------------------------------------------------------------\n\n" "$remote" "$branch" "$link" "$branch"
|
|
else
|
|
# Makes sure to get a free indexed temporary branch
|
|
for i in $(seq 1 100) ; do
|
|
# Checks if branch exists in the remote
|
|
printf "Checking for branch %s...\n" "$branch$i"
|
|
exists_in_remote=$(git ls-remote --heads "$remote" "$branch$i")
|
|
|
|
# If the branch does not exist
|
|
if [ -z "$exists_in_remote" ]; then
|
|
# Creates the branch, pushes it and deletes the branch locally
|
|
git branch "$branch$i"
|
|
git push "$remote" "$branch$i"
|
|
git branch -d "$branch$i" >/dev/null 2>/dev/null
|
|
break
|
|
else
|
|
printf "Branch already exists, trying again...\n\n"
|
|
fi
|
|
done
|
|
# Prints output message
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Branch does not yet exist, pushed to %s/%s...\n\n You can now create a pull request at:\n > %s/pull/new/%s\n\n----------------------------------------------------------------------------------------------\n\n" "$remote" "$branch$i" "$link" "$branch$i"
|
|
fi
|
|
else
|
|
# Checks if branch exists in the remote
|
|
printf "\nChecking for branch %s...\n" "$branch"
|
|
exists_in_remote=$(git ls-remote --heads "$remote" "$branch")
|
|
|
|
# If the branch does not exist
|
|
if [ -z "$exists_in_remote" ]; then
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Branch does not exist, aborting...\n\n----------------------------------------------------------------------------------------------\n\n"
|
|
exit
|
|
fi
|
|
# Creates the branch, pushes it and deletes the branch locally
|
|
git branch "$branch"
|
|
result=$(git push "$remote" "$branch")
|
|
if ! echo "$result" | grep -q "rejected"; then
|
|
git branch -d "$branch" >/dev/null 2>/dev/null
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Failed to push to %s/%s, aborting...\n\n----------------------------------------------------------------------------------------------\n\n" "$remote" "$branch"
|
|
elif ! echo "$result" | grep -q "Everything up-to-date"; then
|
|
git branch -d "$branch" >/dev/null 2>/dev/null
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Branch is up-to-date, aborting...\n\n----------------------------------------------------------------------------------------------\n\n"
|
|
elif ! echo "$result" | grep -q "To $remote.git"; then
|
|
git branch -d "$branch" >/dev/null 2>/dev/null
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Pushed to %s/%s...\n\n You can now create a pull request at:\n > %s/pull/new/%s\n\n----------------------------------------------------------------------------------------------\n\n" "$remote" "$branch" "$link" "$branch"
|
|
else
|
|
git branch -d "$branch" >/dev/null 2>/dev/null
|
|
printf "\n----------------------------------------------------------------------------------------------\n\n Failed to push to %s/%s, aborting...\n\n----------------------------------------------------------------------------------------------\n\n" "$remote" "$branch"
|
|
fi
|
|
fi
|
|
else
|
|
printf "\nRemote %s is unreachable for this repository.\nIf you pretend to use a different remote name specify it using: git-safe-push <remote>\nIf this is the correct name, check if the current remote url is correct.\n" "$remote"
|
|
fi
|
|
else
|
|
printf "You are currently not in a git repository."
|
|
fi
|