Skip to content

Instantly share code, notes, and snippets.

@rmnscnce
Last active November 8, 2020 17:47
Show Gist options
  • Save rmnscnce/0190c288f7d5f15df7a82f4bec864a2e to your computer and use it in GitHub Desktop.
Save rmnscnce/0190c288f7d5f15df7a82f4bec864a2e to your computer and use it in GitHub Desktop.
Git hard-rename commit
#!/bin/bash
# Git Hard Rename Commit (git-hrc)
# Copyright (C) rmnscnce@github
# Licensed under GNU General Public License v3
# This software comes with absolutely NO WARRANTY
# LICENSE RETRIEVAL (yes this is required, DO NOT REMOVE)
if [ ! -f $HOME/git-hrnc/COPYING ] ; then
mkdir -p $HOME/git-hrc
curl https://www.gnu.org/licenses/gpl-3.0.txt -o $HOME/git-hrnc/COPYING
fi
# MAKE SURE RUNNING ON A GIT REPO
if [ ! -d .git ] ; then
echo -e "\n\nError: Not a Git repository\n\n"
exit 1
fi
# ARG CHECK
if [ -z "$1" ] ; then
echo -e "Error: Invalid argument $1. Valid arguments: <#numberofcommit|help> [nopush]"
exit 64
fi
if [ ! -n "$1" ] && [ ! "$1" -eq "$1" ] 2>/dev/null ; then
echo -e "Error: Invalid argument $1. Valid arguments: <#numberofcommit|help> [nopush]"
exit 64
fi
if [ "$2" == "nopush" ] ; then
EXNOPUSH="No"
else
EXNOPUSH="Yes"
fi
# FUNCTIONS
helper () {
echo -e "Git Hard Rename Commit (git-hrc)"
echo -e "Licensed under GNU General Public License v3"
echo -e "This software comes with absolutely NO WARRANTY"
echo -e "\nUsage: ./git-hrnc.sh <#numberofcommit|help> [nopush]"
}
exiterr () {
if [ ! "$?" -eq "0" ] ; then
exit $?
fi
}
# CUE HELP MESSAGE
if [ "$1" == "help" ] ; then
helper
exit 0
fi
BRANCHNAME=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
HRNCBRANCH=${BRANCHNAME}-hrc
while true; do
echo -e "\n\nCommits being hard-renamed into one: $1"
echo -e "Automatically push to remote origin: $EXNOPUSH"
echo -e "\nDo you really want to hard-rename the commits? (case-insensitive:y|n)"
read hrcprompt
if [ "$hrcprompt" != "${hrcprompt#[Nn]}" ] ; then
echo -e "\n\nOperation aborted. Exiting..."
exit $?
elif [ "$hrcprompt" != "${hrcprompt#[Yy]}" ] ; then
break
else
echo -e "\n\nInvalid response $hrcprompt. Please input your choice."
fi
done
git branch -D ${HRNCBRANCH}
git checkout -b ${HRNCBRANCH} HEAD~$1 ; exiterr
git checkout ${BRANCHNAME} ; exiterr
git format-patch -$1 HEAD~$1 --stdout > ../hrc.patch ; exiterr
git checkout ${HRNCBRANCH} ; exiterr
patch -f -p1 < ../hrc.patch
find . -name "*.orig" -exec rm -rf {} +
find . -name "*.rej" -exec rm -rf {} +
git add *
git commit
if [ ! -z "$2" ] ; then
if [ ! "$2" == "nopush" ] ; then
git push --delete origin ${BRANCHNAME} ; exiterr
else
echo -e "\n\nError: Invalid argument $2. Valid arguments: <#numberofcommit|help> [nopush]"
exit 64
fi
fi
git branch -D ${BRANCHNAME} ; exiterr
git branch -m ${BRANCHNAME}
if [ ! -z "$2" ] ; then
if [ ! "$2" == "nopush" ] ; then
git push -u origin ${BRANCHNAME} ; exiterr
else
echo -e "\n\nError: Invalid argument $2. Valid arguments: <#numberofcommit|help> [nopush]"
exit 64
fi
fi
echo -e "\n\nDone."
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment