-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Look for branches that are literally the word main #1981
Look for branches that are literally the word main #1981
Conversation
I have a repo with many other branches that contain the string `main`, but our default branch is still master. This grep was seeing those other branches and deciding that my default branch was `main`. This tighter regex fixes that behavior for me.
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.
Hi @william-richard
Thanks for the PR!
Regarding the tests- we sadly don't have a full test coverage of the repo. I you have time and want to contribute, I will gladly accept PRs which add test coverage 😄
Heh, my bash-fu is not very strong... I'm not sure I'm the right person to take on that job. Sorry 😢 |
Hi All ! The proposed regex appears to be broken relative to the actual output of when i do
try the grep (adapted for master)
The regex could probably work as:
And, adapted to just fetch the master vs main branch in one go: thanks stack overflow
There's (currently) no great way to solve the "default branch" problem, but I think that a more general approach could be supporting a user-definable config property. ie something like :
|
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 regex appears to be broken (per my previous comment)
@davidpfarrell is indeed correct. we need to change the regex to |
I don't know if
with
with
|
whoops, I do not even know how I wrote `\w', I meant '\b'.... brain bug probably |
Ok, I've updated the PR to use I just remembered that I have a hacky script that checks out the default branch of a repo - a lot of the codebases at my company use DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') That said, should |
\b matches things like - and _, which is often used in branch names
I thin the spirit of the g*m aliases (gcom, gswm, etc) are meant to target the "default" branch. I think I saw versions of your script in my stack overflow discussion I linked in my previous comment. I'm contemplating submitting a request to have git create a |
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.
Hi again.
Q: Did you test this?
adapted for master
$ cd $BASH_IT
# with \b
$git branch | grep -q '\bmaster\b' && echo "FOUND IT" || echo "NOPE"
FOUND IT
# with \s
$ git branch | grep -q '\smaster\s' && echo "FOUND IT" || echo "NOPE"
NOPE
So git branch names are (currently) not allowed to contain whitespace, so the \b
solution is likely fine.
\smain$
might be a bit better, or we could go all-out with :
'^. master$'
Which will match the whole line
Hi David, Yep, I've been running with my branch for the last week, and this has been working pretty reliably for me. But, I also don't regularly use any repos that use
I think your suggestion to try to match the entire line is a good one - I'll take another stab at this, and make sure to test in both flavors of repos before I request reviews again. Sorry for my incomplete testing - I do appreciate your time and effort, and I'll do the best to make better use of your time in the future. 😸 |
Hi @william-richard, I don't think you caught all of my previous comment. I believe I showed that I think the "full line" matcher is the way to go ... |
Apologies @davidpfarrell - I thought I had pushed up the full line match commit, but didn't notice the error message and moved onto something else. I've been running with the full line match version for about 2 weeks now, and I agree, I think it's the way to go forward. |
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'm not convinced that the \s*
is needed,
BUT I am convinced that this works in my local, so ...
Approved and thanks for contributing to Bash-It !
Description
When finding the default git branch, use a regex that matches only the word
main
, instead of any branch that has the substringmain
in it.Motivation and Context
I have a repo with many other branches that contain the string
main
,but our default branch is still
master
. This grep was seeing those other branchesand deciding that my default branch was
main
.How Has This Been Tested?
I tested this change on the repository I described above, and it correctly decided my default branch was
master
again.Screenshots (if appropriate):
Types of changes
Checklist:
clean_files.txt
and formatted it usinglint_clean_files.sh
.