From 9dca93eeb2de723d3cc95a5fa1244776a8fde747 Mon Sep 17 00:00:00 2001 From: vr8ce Date: Wed, 17 Jun 2020 20:11:07 -0500 Subject: [PATCH 1/3] Modify to work when only a single commit, add parameter checks --- bin/git-undo | 56 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/bin/git-undo b/bin/git-undo index e6742d2..973c712 100755 --- a/bin/git-undo +++ b/bin/git-undo @@ -1,6 +1,28 @@ #!/usr/bin/env bash back="^" +cstr="commits" +ccnt=$(git rev-list --count HEAD) +if [ $ccnt -eq 1 ]; then cstr="commit"; fi +parm3="" + +function _undo() +{ + type=${1:-soft} + undo_cnt=${2:-1} + reset=${3:-""} +echo "type=$type, cnt=$undo_cnt, reset=$reset" + if [ $undo_cnt -gt $ccnt ]; then + echo "Only $ccnt $cstr, cannot undo $undo_cnt" + elif [ "$type" = "hard" -a $ccnt -eq $undo_cnt ]; then + echo "Cannot hard undo all commits" + elif [ "$type" = "soft" -a $ccnt -eq 1 ]; then + git update-ref -d HEAD + else + git reset --$type HEAD~$undo_cnt + fi + if [ "$reset" != "" ]; then git reset; fi +} case "$1" in -h) @@ -12,9 +34,8 @@ EOL read -r res case "${res}" in "Y" | "y") - test $2 -gt 1 > /dev/null 2>&1 && back="~$2" - git reset --hard HEAD$back - exit $? + parm1=hard + parm2=${2:-1} ;; * ) exit 0 @@ -22,16 +43,31 @@ EOL esac ;; --hard) - test $2 -gt 1 > /dev/null 2>&1 && back="~$2" - git reset --hard HEAD$back + parm1=hard + parm2=${2:-1} ;; -s|--soft) - test $2 -gt 1 > /dev/null 2>&1 && back="~$2" - git reset --soft HEAD$back + parm1=soft + parm2=${2:-1} + parm3=reset + ;; + "") + parm1=soft + parm2=1 + ;; + *[!0-9]*) + echo "Invalid parameter: $1" + exit 1 ;; *) - test $1 -gt 1 > /dev/null 2>&1 && back="~$1" - git reset --soft HEAD$back - git reset + parm1=soft + parm2="$1" ;; esac + +if [[ ! $parm2 =~ [0-9]*([0-9]) ]]; then + echo "Invalid undo count: $parm2" + exit 1 +fi + +_undo $parm1 $parm2 $parm3 \ No newline at end of file From 6ccd2fa439c26b05a76e5c9b6a686294d6b71b4b Mon Sep 17 00:00:00 2001 From: vr8ce Date: Thu, 18 Jun 2020 23:38:55 -0500 Subject: [PATCH 2/3] Cleanup code --- bin/git-undo | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bin/git-undo b/bin/git-undo index 973c712..5cccbfe 100755 --- a/bin/git-undo +++ b/bin/git-undo @@ -1,6 +1,5 @@ #!/usr/bin/env bash -back="^" cstr="commits" ccnt=$(git rev-list --count HEAD) if [ $ccnt -eq 1 ]; then cstr="commit"; fi @@ -11,12 +10,12 @@ function _undo() type=${1:-soft} undo_cnt=${2:-1} reset=${3:-""} -echo "type=$type, cnt=$undo_cnt, reset=$reset" + if [ $undo_cnt -gt $ccnt ]; then echo "Only $ccnt $cstr, cannot undo $undo_cnt" - elif [ "$type" = "hard" -a $ccnt -eq $undo_cnt ]; then + elif [ "$type" = "hard" ] && [ $ccnt -eq $undo_cnt ]; then echo "Cannot hard undo all commits" - elif [ "$type" = "soft" -a $ccnt -eq 1 ]; then + elif [ "$type" = "soft" ] && [ $ccnt -eq 1 ]; then git update-ref -d HEAD else git reset --$type HEAD~$undo_cnt @@ -65,7 +64,7 @@ EOL ;; esac -if [[ ! $parm2 =~ [0-9]*([0-9]) ]]; then +if [[ ! $parm2 =~ [1-9]*([0-9]) ]]; then echo "Invalid undo count: $parm2" exit 1 fi From 27f42beb8127d4ec0b7f21eaec5f573c29bc1e85 Mon Sep 17 00:00:00 2001 From: vr8ce Date: Fri, 19 Jun 2020 22:56:55 -0500 Subject: [PATCH 3/3] Tighten numeric regex --- bin/git-undo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/git-undo b/bin/git-undo index 5cccbfe..bd561a8 100755 --- a/bin/git-undo +++ b/bin/git-undo @@ -64,7 +64,7 @@ EOL ;; esac -if [[ ! $parm2 =~ [1-9]*([0-9]) ]]; then +if [[ ! $parm2 =~ ^[1-9][0-9]*$ ]]; then echo "Invalid undo count: $parm2" exit 1 fi