Motti
Motti

Reputation: 114815

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).

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:

Upvotes: 1

Views: 2364

Answers (2)

patwis
patwis

Reputation: 306

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.

Upvotes: -1

steadweb
steadweb

Reputation: 16611

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

Upvotes: 1

Related Questions