繁体   English   中英

如何使用凭据在 Windows 上使用 nodegit.push

[英]How to use credentials to work with nodegit.push on Windows

编辑:我正在更改问题以适应我目前对已发生重大变化的问题的理解。

原标题: Nodegit 似乎在推送时要求提供错误的凭据

当尝试使用 push ,Windows 上似乎没有任何效果(而它们在 Linux 上工作正常)。

  • 使用 SSH
    • sshKeyFromAgent -验证错误:连接代理失败
    • sshKeyNew - credentials回调是重复的(看起来像一个无限循环,但我不能确定
    • sshKeyMemoryNewcredentials被调用两次,然后节点在没有诊断的情况下exitprocess上的exitbeforeExit事件没有发出信号)
  • 使用 HTTPS
    • userpassPlaintextNew : [错误: 未知证书检查失败] errno: -17

原始问题如下。


我正在尝试让nodegit push ,以下问题似乎解决了这种情况。 但是我无法让它工作。

我已经使用 SSH 克隆了一个存储库,当我尝试推送时,我的credentials回调是使用用户git而不是motti (这是实际的 git 用户)调用的。

try {
    const remote = await repository.getRemote("origin");
    await remote.push(["refs/head/master:refs/heads/master"], {
        callbacks: {
            credentials: (url, user) => {
                console.log(`Push asked for credentials for '${user}' on ${url}`);
                return git.Cred.sshKeyFromAgent(user);
            }
        }
    });
}
catch(err) {
    console.log("Error:", err);
}

我得到以下输出:

Push 要求在 git@github 上提供“git”的凭据。 [编辑] .net:motti/tmp.git
错误:{ 错误:错误验证:连接代理失败 errno:-1,errorFunction:'Remote.push'}

如果我尝试将motti硬编码到sshKeyFromAgent函数,错误将更改为:

错误:{ 错误:用户名与之前的请求不匹配 errno: -1, errorFunction: 'Remote.push' }

这是我第一次尝试以编程方式使用 git 所以我可能会遗漏一些基本的东西......

回答评论中的一些问题:

  • 我在 Windows 10 上运行
  • 节点 v8.9.4
  • git 版本 2.15.0.windows.1
  • nodegit 版本 0.24.1
  • 运行节点的用户是我的主要用户,当我在命令行中使用 git 时它可以正常工作

而不是使用git.Cred.sshKeyFromAgent - 您可以使用git.Cred.sshKeyNew并传递您的用户名/密钥。

const fs = require('fs');

// ...

const username = "git";
const publickey = fs.readFileSync("PATH TO PUBLIC KEY").toString();
const privatekey = fs.readFileSync("PATH TO PRIVATE KEY").toString();
const passphrase = "YOUR PASSPHRASE IF THE KEY HAS ONE";
const cred = await Git.Cred.sshKeyMemoryNew(username, publickey, privatekey, passphrase);

const remote = await repository.getRemote("origin");
await remote.push(["refs/head/master:refs/heads/master"], {
    callbacks: {
        credentials: (url, user) => cred
    }
});

您需要在本地运行 ssh 代理并将您的密码保存在那里。 请按照以下步骤使其工作:

  1. 在本地启用 ssh 代理(在 OS X 上自动运行): https : //code.visualstudio.com/docs/remote/troubleshooting#_setting-up-the-ssh-agent
  2. 在运行 nodegit 操作时在同一 CLI 中运行“ssh-add”并输入密码

我希望这会有所帮助,因为我也为此付出了很多努力,这可能会非常令人沮丧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM