-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Avoding --short option in git symbolic-ref for getting the current branch #2025
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
Avoding --short option in git symbolic-ref for getting the current branch #2025
Conversation
…ack to symbolic-ref
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.
Thank you for tackling this!
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 code looks good to me. Thank you for adding tests 🙇
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.
Looks great! Just a few tiny bits, please ✨
|
|
||
| func getBranchShortName(output []byte) string { | ||
| branch := firstLine(output) | ||
| return strings.TrimPrefix(branch, "refs/heads/") |
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.
looking at the code for git branch --show-current it looks like we maybe should also error if HEAD doesn't point to a branch (if the refs/heads/ prefix isn't present). I don't know when this could happen, but git seems to check for it: https://github.com/git/git/blob/master/builtin/branch.c#L456-L466
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 --shorten rules mention tags and remote-tracking branches https://github.com/git/git/blob/master/refs.c#L520-L528, but i can't get either of those to get reported for HEAD, so this may be a non-issue and just overly-defensive code.
$ git checkout v0.9.0
HEAD is now at bcc2c38 v0.9.0
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref
$ git checkout origin/master
Previous HEAD position was bcc2c38 v0.9.0
HEAD is now at 2822914 precompiled formik
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref
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.
@nightpool Thanks for the thorough review!
I'm not aware that a successful git symbolic-ref HEAD can return any values other than ones prefixed with refs/heads/, so I think the current approach is fine without adding any additional conditionals.
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.
@mmontes11 Thanks for the updates! This looks great 👍
Closes #1897