如何调试Git凭证助手?

5
我已经阅读了以下内容:

不幸的是,我无法使最基本的科学方法™测试起作用。 我使用我的替代命令尝试记录git调用helper时发生的情况。不幸的是,我的命令没有记录任何内容。 因此,我被迫得出结论,git没有调用它的helper。 我能做些什么?


2
自从我问了这个问题,我发现 credential.helper 似乎不适用于 ssh:// URL。如果我错了,请纠正我。(注意:我没有意识到这是因为我设置了 git config --global url.ssh://git@github.com/.insteadOf https://github.com/ - Bruno Bronosky
1个回答

1
我试图让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脚本,作为凭证帮助程序的包装器:

#!/usr/bin/env python3

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命令时,我可以看到gitgh 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

希望这能帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接