From 825e044147d21e5cdb91a133d7f0d30288b18fee Mon Sep 17 00:00:00 2001 From: TiagoRG Date: Thu, 1 Jun 2023 22:26:50 +0100 Subject: [PATCH] [TOOLS] Added git-safe-push --- tools/git-tools/README.md | 24 +++++++++ tools/git-tools/git-safe-push | 99 +++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tools/git-tools/README.md create mode 100644 tools/git-tools/git-safe-push diff --git a/tools/git-tools/README.md b/tools/git-tools/README.md new file mode 100644 index 0000000..7a86c28 --- /dev/null +++ b/tools/git-tools/README.md @@ -0,0 +1,24 @@ +# git-safe-push + +## Description + +This script is used to push the local changes to the remote repository safely. Instead of pushing directly into the main branch, creates a temporary branch that can be safely deleted after the changes are reviewed and merged. This way you don't have to keep many branches in your remote repository which means it's easier to keep a linear commit history ([Squash and Merge](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/about-pull-request-merges#squash-and-merge-your-commits) suggested for this aswell). + +## Usage + +Download the script and then add it to bin with the following commands: + +```bash +sudo mv git-safe-push /usr/bin +sudo chmod +x /usr/bin/git-safe-push +``` + +Then you can use it like this: + +```bash +git safe-push <-n|-e> +``` +- ``: The remote repository to push to. +- ``: The branch to push to. +- `-n`: Pushes to a new branch. +- `-e`: Pushes to an existing branch. diff --git a/tools/git-tools/git-safe-push b/tools/git-tools/git-safe-push new file mode 100644 index 0000000..9c9ae68 --- /dev/null +++ b/tools/git-tools/git-safe-push @@ -0,0 +1,99 @@ +# Handles the arguments +# git-safe-push <-n|-e> +if [ $# -eq 0 ]; then + remote="origin" + branch="temp" + branch_option="-n" +elif [ $# -eq 1 ]; then + remote=$1 + branch="temp" + branch_option="-n" +elif [ $# -eq 2 ]; then + remote=$1 + branch=$2 + branch_option="-n" +elif [ $# -eq 3 ]; then + remote=$1 + branch=$2 + branch_option=$3 +else + echo "Usage: git-safe-push <-n/-e>" + exit +fi + +if git rev-parse --is-inside-work-tree > /dev/null; then + if git ls-remote --exit-code "$remote" >/dev/null 2>/dev/null; then + # Rebases the repository + git config pull.rebase true + git pull + + remote_link=$(git remote get-url "$remote") + link=${remote_link%.git} + + if [ "$branch_option" = "-n" ]; then + # Checks if branch exists in the remote + printf "\nChecking for %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/compare/%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 %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/compare/%s\n\n----------------------------------------------------------------\n\n" "$remote" "$branch$i" "$link" "$branch$i" + fi + else + # Checks if branch exists in the remote + printf "\nChecking for %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 "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" + exit + 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/compare/%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" + exit + 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 \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