Alex Gray
Alex Gray

Reputation: 16473

How to obtain github "short" username at the command line?

So if..

$ git config user.name
↳ Alex Gray              # OK (my name)
$ git config user.email
↳ [email protected]        # OK (my email).

and..

GithubUserForProject() {  # in pwd
    ORIGIN=$(git config --get remote.origin.url) && echo $ORIGIN
    OWNER=${ORIGIN%/*}     && echo $OWNER  # trim URL tail
    OWNER=${OWNER#*.com/}  && echo $OWNER  # trim URL head
    OWNER=${OWNER#*:}      && echo $OWNER  # trim ssh URL head
}

$ cd /local/git/MyGitHubRepo && GithubUserForProject
↓ [email protected]:mralexgray/MyGitHubRepo.git
↓ [email protected]:mralexgray
↳ mralexgray            # OK (my username, but skanky way of finding it) 

but...

$ cd /local/git/SomeGuysProject && GithubUserForProject
↓ git://github.com/someguy/SomeGuysProject.git
↓ git://github.com/someguy
↳ someguy              # WRONG! (cloned repo's user!)

So, how can I determine my github "short username" programmatically, either from the environment, a github API request, etc., or otherwise (via a script or terminal session?

Upvotes: 2

Views: 1190

Answers (2)

Zombo
Zombo

Reputation: 1

I am going to agree with the commenters: how would Bash know your username, unless you tell it?

You could test with git push to know if it is "your" repo, but even that will fail if your SSH keys aren't properly set. In short, you should just set it in ~/.bash_profile or something and be done with it.

Upvotes: 0

Alex Gray
Alex Gray

Reputation: 16473

I thought it silly that such an inane question go so glaringly unsolved... so for lack of knowing how to cleverly parse strings in bash without resorting to the one SED combo I know by heart, and......

security find-internet-password -s github.com | grep acct | sed 's/"acct"<blob>="//g' | sed 's/"//g'

ét voila....

mralexgray

This may depend on having the Github mac client installed... and yet again... it might not.

Upvotes: 1

Related Questions