2

因此,如果..

$ git config user.name
↳ Alex Gray              # OK (my name)
$ git config user.email
↳ alex@mrgray.com        # OK (my email).

和..

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
↓ git@github.com:mralexgray/MyGitHubRepo.git
↓ git@github.com:mralexgray
↳ mralexgray            # OK (my username, but skanky way of finding it) 

但...

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

那么,我如何编程方式确定我的 github“短用户名”,无论是从envIronment、github API 请求等还是以其他方式(通过脚本或终端会话?

4

2 回答 2

1

我认为这样一个愚蠢的问题如此明显地没有解决是很愚蠢的......所以因为不知道如何巧妙地解析 bash 中的字符串而不诉诸SED我心中知道的一个组合,并且......

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

瞧瞧....

mralexgray

这可能取决于是否安装了 Github mac 客户端......再一次......它可能不会

于 2012-12-07T11:04:38.570 回答
0

我同意评论者的观点:Bash 怎么知道你的用户名,除非你告诉它?

您可以测试git push它是否是“您的”存储库,但如果您的 SSH 密钥设置不正确,即使这样也会失败。简而言之,你应该把它放进去~/.bash_profile或其他东西,然后用它完成。

于 2012-12-07T07:22:00.110 回答