-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Modify to work when only a single commit, add parameter checks #850
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
Conversation
bin/git-undo
Outdated
| @@ -1,6 +1,28 @@ | |||
| #!/usr/bin/env bash | |||
|
|
|||
| back="^" | |||
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.
bin/git-undo
Outdated
| 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 comment
The 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 comment
The 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.
| git reset --hard HEAD$back | ||
| exit $? | ||
| parm1=hard | ||
| parm2=${2:-1} |
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.
Better to use more reasonable argument name than parmX.
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.
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.
bin/git-undo
Outdated
| ;; | ||
| esac | ||
|
|
||
| if [[ ! $parm2 =~ [0-9]*([0-9]) ]]; then |
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.
Should it be [1-9]* instead?
bin/git-undo
Outdated
| 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 |
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.
Better to use [ "$type" = "hard" ] && [ $ccnt -eq $undo_cnt ], which is more clear.
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.
Well, I disagree that it's clearer, but it's not a hill worth dying on, so I'll change it.
bin/git-undo
Outdated
| 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 |
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.
Ditto.
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.
Ditto.
|
Fixed everything but the parmX names. Again, I named them that way on purpose, but if you have suggestions I'll be glad to use them instead. |
bin/git-undo
Outdated
| ;; | ||
| esac | ||
|
|
||
| if [[ ! $parm2 =~ [1-9]*([0-9]) ]]; then |
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.
I have changed my mind. Since 0 is not a valid count, maybe the pattern should be [1-9][0-9]*?
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.
That's not really changing your mind, that's what you brought up initially, which is why I changed the first one to be 1-9 instead of 0-9. What I have does exactly what you're suggesting, using [[ regex syntax.
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.
@vr8hub
Err... This regex pattern is better: if [[ ! $parm2 =~ ^[1-9][0-9]*$ ]].
It can detect all three invalid cases:0, 1s, 01.
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.
Done.
|
@vr8hub |
Undo currently does not work when there is only a single commit. There are instances when doing a soft undo on a repository with only a single commit is desired.
In implementing that, I added a few parameter checks, including:
Tested on OSX 10.14.6, which is all I have available. I don't believe I used anything that isn't portable, however. (Famous last words.)