-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify to work when only a single commit, add parameter checks #850
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the debug log. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh! I looked at that right before my last round of testing and said, "Don't forget to get rid of that!" and promptly didn't get rid of that. |
||
if [ $undo_cnt -gt $ccnt ]; then | ||
echo "Only $ccnt $cstr, cannot undo $undo_cnt" | ||
elif [ "$type" = "hard" -a $ccnt -eq $undo_cnt ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I disagree that it's clearer, but it's not a hill worth dying on, so I'll change it. |
||
echo "Cannot hard undo all commits" | ||
elif [ "$type" = "soft" -a $ccnt -eq 1 ]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
git update-ref -d HEAD | ||
else | ||
git reset --$type HEAD~$undo_cnt | ||
fi | ||
if [ "$reset" != "" ]; then git reset; fi | ||
} | ||
|
||
case "$1" in | ||
-h) | ||
|
@@ -12,26 +34,40 @@ 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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use more reasonable argument name than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept them generic on purpose. The names in the function are what matter. If you would like to name the parameters, I'll be glad to use them. |
||
;; | ||
* ) | ||
exit 0 | ||
;; | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be |
||
echo "Invalid undo count: $parm2" | ||
exit 1 | ||
fi | ||
|
||
_undo $parm1 $parm2 $parm3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
$back
is unused now. Please remove it too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, should have caught that. Thanks.