Last active
February 7, 2024 18:48
-
-
Save waynenilsen/a6b8e814df61db1e6a938f9d4da9449f to your computer and use it in GitHub Desktop.
A zsh script to streamline Git operations, featuring branch creation, commit amendments, and push actions with user confirmations.
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
#!/usr/bin/env zsh | |
# Explicitly set the script name | |
SCRIPT_NAME=$(basename "$0") | |
# Exit when any command fails, unset variables are an error, and fail on error in any pipeline. | |
set -euo pipefail | |
# Help function for displaying usage information | |
function help() { | |
echo "Usage: $SCRIPT_NAME <subcommand> [options]" | |
echo "" | |
echo "Subcommands:" | |
echo " create Create a branch with the given name, commit, and push changes." | |
echo " Requires one positional argument: branch-name." | |
echo " update Add all changes, amend the last commit, and forcibly push." | |
echo " help Display this help message" | |
echo "" | |
echo "For subcommand-specific help, run: $SCRIPT_NAME <subcommand> -h" | |
} | |
# Subcommand-specific help for 'create' | |
function create_help() { | |
echo "Usage: $SCRIPT_NAME create <branch-name>" | |
echo "Create a branch with the given name, commit, and push changes." | |
} | |
# Subcommand-specific help for 'update' | |
function update_help() { | |
echo "Usage: $SCRIPT_NAME update" | |
echo "Add all changes, amend the last commit, and forcibly push." | |
} | |
# Create subcommand function | |
function create_branch() { | |
if [ "$#" -ne 2 ] || [[ "$2" == "-h" ]] || [[ "$2" == "--help" ]]; then | |
create_help | |
exit 0 | |
fi | |
local branch_name=$2 | |
local name=$(basename "$HOME") | |
# Add all changes to the staging area | |
git add . | |
# Display git status | |
git status | |
# Ask user to confirm | |
echo "Review the staged changes above. Do you want to proceed? (y/n)" | |
read -r proceed | |
if [[ $proceed != "y" ]]; then | |
echo "Aborting as requested." | |
exit 1 | |
fi | |
# Create a new branch and switch to it | |
git checkout -b "${name}/${branch_name}" | |
# Commit the changes | |
git commit | |
# Push the new branch to the remote repository | |
git push origin HEAD | |
} | |
# Update subcommand function | |
function update_branch() { | |
if [ "$#" -eq 2 ] && { [[ "$2" == "-h" ]] || [[ "$2" == "--help" ]]; }; then | |
update_help | |
exit 0 | |
fi | |
# Add all changes to the staging area | |
git add . | |
# Display git status | |
git status | |
# Ask user to confirm | |
echo "Review the staged changes above. Do you want to proceed with amend? (y/n)" | |
read -r proceed | |
if [[ $proceed != "y" ]]; then | |
echo "Aborting as requested." | |
exit 1 | |
fi | |
# Amend the last commit | |
git commit --amend | |
# Force push the amended commit | |
git push origin HEAD --force | |
} | |
# Check if at least one argument is provided | |
if [ "$#" -eq 0 ]; then | |
echo "Error: No subcommand provided." | |
help | |
exit 1 | |
fi | |
# Subcommand parsing | |
case "$1" in | |
create) | |
create_branch "$@" | |
;; | |
update) | |
update_branch "$@" | |
;; | |
help) | |
help | |
;; | |
*) | |
echo "Error: Unknown subcommand '$1'" | |
help | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment