node.jsgitsshlibgit2nodegit

How to clone git repository with nodegit using ssh


I'm trying to clone git repository from our teamforge server in node.js using library nodegit (version 0.2.4) and ssh. Our server requests authentication from user and when I was trying to use only clone method without passing options I was getting error: "Callback failed to initialize SSH credentials".

I have private and public key in files private.key and public.key. They're in directory which I have set to working directory in web storm, so location shouldn't be a problem.

Didn't find example how to do it (maybe I missed it), but below code is the closest which I got:

'use strict';

var nodegit = require("nodegit"),
Clone = nodegit.Clone,
cred = nodegit.Cred;

var options = {
    remoteCallbacks: {
        credentials: function () {
            return cred.sshKeyNew('user', 'public.key', 'private.key', '');
        }
    }
};

Clone.clone("ssh://user@teamforgeserver.net/reponame", "localTmp", options)
  .then(function(repo) {
        var r = repo;
    },
    function(err){
        var e = err;
    }
);

I'm getting this error:

TypeError: Object #<Object> has no method 'isFulfilled' at value
(c:\...\proj\node_modules\nodegit\node_modules\nodegit-promise\lib\core.js:36:15)

Do you have hint what could be wrong or how to do it in general?


Solution

  • This is a bug with creating a new ssh key. I've created an issue here to track it.

    In the meantime if you have an SSH agent running you can get the credentials from that.

    'use strict';
    
    var nodegit = require("nodegit"),
    Clone = nodegit.Clone,
    cred = nodegit.Cred;
    
    var options = {
        fetchOpts: {
            remoteCallbacks: {
                credentials: function (url, userName) {
                    // this is changed from sshKeyNew to sshKeyFromAgent
                    return cred.sshKeyFromAgent(userName);
                }
            }
        }
    };
    
    Clone.clone("ssh://user@teamforgeserver.net/reponame", "localTmp", options)
      .then(function(repo) {
            var r = repo;
        },
        function(err){
            var e = err;
        }
    );
    

    That works for me in v0.2.4. If you need more realtime support feel free to come chat with us over in our gitter channel.

    UPDATE: I just added a fix for this on Pull Request #334. After tests pass I'll put this on master and then the code in the question should work fine.