Given ANY GitHub repository url string like:
git://github.com/some-user/my-repo.git
or
[email protected]:some-user/my-repo.git
or
https://github.com/some-user/my-repo.git
What is the best way in bash
to extract the repository name my-repo
from any of the following strings? The solution MUST work for all types of urls specified above.
Thanks.
I'd go with
basename $URL .git
.Old post, but I faced the same problem recently.
The regex
^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$
works for the three types of URL.Explaination (see it in action on regex101):
^
matches the start of a string(https|git)
matches and captures the charactershttps
orgit
(:\/\/|@)
matches and captures the characters://
or@
([^\/:]+)
matches and captures one character or more that is not/
nor:
[\/:]
matches one character that is/
or:
([^\/:]+)
matches and captures one character or more that is not/
nor:
, yet again[\/:]
matches the character/
(.+)
matches and captures one character or more(.git)*
matches optional.git
suffix at the end$
matches the end of a stringThis if far from perfect, as something like
[email protected]:some-user/my-repo.git
would match, but I think it's fine enough for extraction.Summing up:
Get url without (optional) suffix:
Get repository name:
Get user (host) name afterwards:
use regular expression:
/([^/]+)\.git$/
basename is my favorite, but you can also use
sed
:"sed" will delete all text until the last
/
+ the.git
extension (if exists), and will retain the match of group\1
which is everything except dot([^.]+)
Using Hitcham's awesome answer above allowed me to come up with this, using sed to output exactly what needed:
org/reponame
with sed.Works well in ubuntu, doesn't work for the sed available by default on macosx.