简体   繁体   中英

How to use credentials to work with nodegit.push on Windows

Edit: I'm changing the question to suit my current understanding of the problem which has changed significantly.

Original Title: Nodegit seems to be asking for wrong credentials on push

When trying to push using nothing seems to work on Windows (while they work fine on Linux).

  • Using SSH
    • sshKeyFromAgent - error authenticating: failed connecting agent
    • sshKeyNew - credentials callback is repeatedly (looks like an infinite loop but I can't be sure )
    • sshKeyMemoryNew : credentials is called twice and then node exits with no diagnostic (the exit and beforeExit events on process aren't signalled)
  • Using HTTPS
    • userpassPlaintextNew : [Error: unknown certificate check failure] errno: -17

Original question follows.


I'm trying to get nodegit to push and the following question seems to address this situation. However I'm not able to get it to work.

I've cloned a repository using SSH and when I try to push, my credentials callback is being called with user git and not motti (which is the actual git user).

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);
}

I get the following output:

Push asked for credentials for 'git' on git@github. [redacted] .net:motti/tmp.git
Error: { Error: error authenticating: failed connecting agent errno: -1, errorFunction: 'Remote.push' }

If I try to hardcode motti to the sshKeyFromAgent function the error changes to:

Error: { Error: username does not match previous request errno: -1, errorFunction: 'Remote.push' }

This my first time trying to programmatically use git so I may be missing something basic...

Answer for some questions from comments:

  • I'm running on windows 10
  • node v8.9.4
  • git version 2.15.0.windows.1
  • nodegit version 0.24.1
  • the user running node is my primary user which when I use for git in command line works correctly

Instead of using git.Cred.sshKeyFromAgent - you could use git.Cred.sshKeyNew and pass your username / keys along.

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
    }
});

You need to run an ssh agent locally and save your password there. Follow these steps to make it work:

  1. Enable the ssh agent locally (automatically runs on OS X): https://code.visualstudio.com/docs/remote/troubleshooting#_setting-up-the-ssh-agent
  2. Run 'ssh-add' in the same CLI as you're running your nodegit actions and enter your passphrase

I hope this helps because I also struggled a lot with it and it can be very frustrating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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