我的原始答案对我自己来说并不是很有用,所以我进一步研究了一下,并找到了一个hack(尽管有点复杂)。
因此,我在MSYS2下使用git
,我想使用credential-cache
,只是暂时记住我的密码(我还没有看到wincred
或其他适用于Windows的方法有这样的用例)。
基本上,这需要在https://github.com/git/git/blob/55144cc/builtin/credential-cache--daemon.c#L239中进行hack-在那一行而不是die
,我们想要继续。
因此,首先,我们想要在MSYS2下构建git
。
- 问题1:你不能仅在MSYS2下构建适当的https://github.com/git/git,链接阶段将失败,并显示“src/git/cache.h:1262: undefined reference to `win32_has_dos_drive_prefix'”和类似的消息
因此,我们需要构建实际在MSYS2中使用的git
。首先,请检查版本:
$ git --version
git version 2.33.0
$ pacman -Ss git | grep installed
然后,根据https://www.msys2.org/wiki/Creating-Packages/,我们可以这样做:
$ git clone "https://github.com/msys2/MSYS2-packages"
$ cd MSYS2-packages/
$ cd git
$ makepkg -sCLf
==> Making package: git 2.33.0-1 (Thu, Sep 23, 2021 12:47:33 PM)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> Installing missing dependencies...
...
make[1]: Entering directory '/c/src/MSYS2-packages/git/src/git-2.33.0'
make[1]: 'GIT-VERSION-FILE' is up to date.
make[1]: Leaving directory '/c/src/MSYS2-packages/git/src/git-2.33.0'
sed -e '1s|#!.*/sh|#!/bin/sh|' git-subtree.sh >git-subtree
chmod +x git-subtree
make: Leaving directory '/c/src/MSYS2-packages/git/src/git-2.33.0/contrib/subtree'
==> Starting check()...
请注意:
- 此构建过程首先经过 ASCIIDOC/XMLTO 部分,在我的计算机上大约需要半个小时
- 然后进入一个
*** prove ***
部分,这需要更长的时间,但可以使用 Ctrl-C 中断,并且构建的可执行文件不会被删除。
因此,现在我们想要在源代码中进行黑客攻击,请注意:
- 如果我们在源代码中进行黑客攻击,我们不希望使用
makepkg -sCLf
,因为这将擦除源目录(连同所有已构建的 .exe 工件),然后在构建之前重新构建它
因此,我们使用 sed
进行黑客攻击,然后进行构建:
$ sed -i 's/die(_(permissions_advice), dir);/fprintf(stderr, "Permissions on cached credentials socket directory %s are too loose, but HACK: going on\\n", dir);/' ./src/git-2.33.0/builtin/credential-cache--daemon.c
$ (cd src/git-2.33.0/; make)
CC builtin/credential-cache--daemon.o
LINK git.exe
...
SUBDIR templates
此时请注意,这个黑客最终会生成至少三个可执行文件,可以通过以下方式进行确认:
$ grep -ao ....HACK........ ./src/git-2.33.0/git-credential-cache--daemon.exe
$ grep -ao ....HACK........ ./src/git-2.33.0/git-credential-cache.exe
$ grep -ao ....HACK........ ./src/git-2.33.0/git.exe
…我只能在替换了所有三个后,才使其正常工作:
$ mv /usr/lib/git-core/git-credential-cache--daemon.exe /usr/lib/git-core/__git-credential-cache--daemon_orig.exe
$ mv -v /usr/lib/git-core/git-credential-cache.exe /usr/lib/git-core/__git-credential-cache__orig.exe
$ mv -v /usr/bin/git.exe /usr/bin/__git_orig.exe
$ mv -v /usr/lib/git-core/git.exe /usr/lib/git-core/__git_orig.exe
cp -v ./src/git-2.33.0/git-credential-cache--daemon.exe /usr/lib/git-core/
cp -v ./src/git-2.33.0/git-credential-cache.exe /usr/lib/git-core/
cp -v ./src/git-2.33.0/git.exe /usr/bin/
cp -v ./src/git-2.33.0/git.exe /usr/lib/git-core/
此时,在 MSYS2 上 credential-cache
也开始工作了(缓存密码一段有限的时间),只是在启动时它会把被黑掉的行倾倒出来:
$ git pull
Password for 'https://user@git.mysite.com':
Permissions on cached credentials socket directory /home/user/.cache/git/credential are too loose, but HACK: going on
Already up to date.
$ git pull
Already up to date.
有点棘手,但似乎可以工作。
附注:一个棘手的问题是,我最初用 printf
替换了 die
来输出到 stdout
,但一直失败;原来,stdout
用于进程间通信,为了成功,显然需要在 stdout
上回答 ok\0
,这是三个字节;因此解决方案是将通知打印到 stderr
中。
(原始答案):
虽然不是完全回答了问题,但这是我能找到的最合适的问题,以此作为答案记录:
我在 Windows 10 的 MSYS2 下使用 git
,目前版本为:
$ git --version
git version 2.32.0
通常我只想让git在有限的时间内(大约10分钟左右)缓存我的密码,然后忘记它;而且我还没有看到使用wincred
或其他Windows特定凭据管理器的用例。
话虽如此,对我来说,事实证明有一个“简单的解决方法”——基本上,在凭据管理器第一次运行时,它是正常的;只有在后续使用中才会出现:
$ git push
Password for 'http://user@githost.example.com':
fatal: The permissions on your socket directory are too loose; other
users may be able to read your cached credentials. Consider running:
chmod 0700 /home/user/.cache/git/credential
fatal: cache daemon did not start:
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
...
所以基本上,解决方法是删除
credential
目录 - 之后,凭据缓存管理器会像首次运行一样运行,并为有限的时间缓存密码 - 这正是我想要的:
$ rm -rf ~/.cache/git/credential
$ git pull
Password for 'http://user@githost.example.com':
Already up to date.
$ git pull
Already up to date.
对我来说还不错,我猜 :)
编辑:实际上不是这样的,我发现在这之后立即在另一个选项卡中尝试拉取时,错误会返回。
git config credential.helper cache
确实可以在Cygwin git版本2.13.2中使用。 - Flash Sheridan