我试图让1Password CLI将我的GitHub令牌交给
gh
命令,并配置Git使用
gh
命令作为凭证助手。但是,当我从命令行运行
gh auth git-credential
时,会遇到问题,它会生成正确的凭据,但是当我在
~/.gitignore
中加入以下内容时,
git clone
仍会提示我输入用户名和密码:
[credential "https://github.com"]
helper = /usr/bin/gh auth git-credential
为了进行一些调试,我首先通过将
GIT_TRACE
环境变量设置为
true
来启用 Git 中的
跟踪日志记录。请注意保留 HTML 标签。
GIT_TRACE=true git clone https://github.com/wheelerlaw/some-private-repo
这将产生类似于以下输出的结果:
$ GIT_TRACE=true git clone https://github.com/Kong/khcp.git
15:19:00.344373 git.c:460 trace: built-in: git clone https://github.com/Kong/khcp.git
Cloning into 'khcp'...
15:19:00.347803 run-command.c:655 trace: run_command: git remote-https origin https://github.com/Kong/khcp.git
15:19:00.349893 git.c:750 trace: exec: git-remote-https origin https://github.com/Kong/khcp.git
15:19:00.349939 run-command.c:655 trace: run_command: git-remote-https origin https://github.com/Kong/khcp.git
15:19:00.625485 run-command.c:655 trace: run_command: '/usr/bin/gh auth git-credential get'
然而,它不会显示传递给/从凭证帮助程序的数据的任何调试输出。为了做到这一点,我编写了一个小的Python脚本,作为凭证帮助程序的包装器:
import sys
import subprocess
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def etrace(str):
eprint("[TRACE] ", str)
args = ['/usr/bin/gh', 'auth', 'git-credential'] + sys.argv[1:]
process = subprocess.Popen(args,
stdout=subprocess.PIPE,
stderr=sys.stderr,
stdin=subprocess.PIPE,
universal_newlines=True)
etrace(args)
for input in sys.stdin:
etrace(input.strip())
process.stdin.write(input)
process.stdin.close()
etrace("Done sending input...")
for output in process.stdout.readlines():
etrace("Printing output...")
etrace(output.strip())
print(output.strip())
etrace("Done receiving output...")
return_code = process.poll()
etrace("Return code: " + str(return_code))
exit(return_code)
接下来,我修改了~/.gitconfig
文件,将Python脚本作为凭据助手调用:
[credential "https://github.com"]
helper = /home/wheeler/wrapper.py
现在,当我使用启用跟踪调试的相同
git clone
命令时,我可以看到
git
和
gh auth git-credential
是如何相互通信的:
$ GIT_TRACE=true git clone https://github.com/Kong/khcp.git
15:54:33.973855 git.c:460 trace: built-in: git clone https://github.com/Kong/khcp.git
Cloning into 'khcp'...
15:54:33.987155 run-command.c:655 trace: run_command: git remote-https origin https://github.com/Kong/khcp.git
15:54:33.989509 git.c:750 trace: exec: git-remote-https origin https://github.com/Kong/khcp.git
15:54:33.989539 run-command.c:655 trace: run_command: git-remote-https origin https://github.com/Kong/khcp.git
15:54:34.191478 run-command.c:655 trace: run_command: '/home/wheeler/test.py get'
[TRACE] ['/usr/bin/gh', 'auth', 'git-credential', 'get']
[TRACE] protocol=https
[TRACE] host=github.com
[TRACE] Done sending input...
[TRACE] Done receiving output...
[TRACE] Return code: 1
这让我意识到我需要将我的
helper
行设置为以下内容,以允许
gh
命令从1Password获取令牌:
[credential "https://github.com"]
helper = /usr/bin/op plugin run -- gh auth git-credential
希望这能帮到您。
credential.helper
似乎不适用于ssh://
URL。如果我错了,请纠正我。(注意:我没有意识到这是因为我设置了git config --global url.ssh://git@github.com/.insteadOf https://github.com/
) - Bruno Bronosky