0

Hi I am trying to do a git push origin master command with the help of Nodegit, but it is resulting in an error,

var Git = require('nodegit');
function gitRemoteLookUp(repoPath) {
var open = Git.Repository.open;
var Remote = Git.Remote;    
open(repoPath).then(function(repo){
    return Remote.lookup(repo, "origin");
}).then(function(remote){       
    var ref = "refs/heads/master:remotes/origin/master";                
    var firstPass = true;
    var options = {
      callbacks: {
        credentials: function(url, userName) {
          if (firstPass) {
            firstPass = false;
            if (url.indexOf("https") === -1) {                  
              return Git.Cred.sshKeyFromAgent('XYZ');
            } else {                    
                return Git.Cred.userpassPlaintextNew('XYZ', "XYZ");
            }
          } else {
            return Git.Cred.defaultNew();
          }
        },
        certificateCheck: function() {
          return 1;
        }
      }
    };
    return remote.push(ref, options);
}).catch(function(err){
    console.log(err);
})
}

I am using our internal ssh github server, which from the git Bash, for each push is asking for Username and Password.

Hence in hence in the code I have used the similar example as mentioned in the github site and also in the test site.

Pls help on this !!!!!!

2
  • What is the error you are getting? Commented Aug 12, 2015 at 16:55
  • [Error: Request failed with status code: 401] I am getting this error Commented Aug 12, 2015 at 16:57

1 Answer 1

1

I have found the answer to my own question,

The confusion lies in the SSH server and generic server,

i found two awesome post in the past nodegit questions,

var remote;
var repository;
function gitRemoteLookUp(repoPath) {
    var open = Git.Repository.open;     
    open(repoPath).then(function(repo){
        repository = repo;
        return repo.getRemote('origin');
    }).then(function(remoteResult){
        remote = remoteResult;          
        remote.setCallbacks({
              credentials: function(url, userName) {
                  // return Git.Cred.sshKeyFromAgent(userName);
                  return Git.Cred.userpassPlaintextNew('XYZ', "XYZ");
              }
          });


        return remote.connect(Git.Enums.DIRECTION.PUSH);
    }).then(function() {
      console.log('remote Connected?', remote.connected())

      return remote.push(
                ["refs/heads/master:refs/heads/master"],
                null,
                repository.defaultSignature(),
                "Push to master")
    }).then(function() {
        console.log('remote Pushed!')
    })
    .catch(function(reason) {
        console.log(reason);
    });
}

The main issue lies in configuring the credentials in the Nodegit Library. Pls be careful in checking whether it is a SSH key based push or whether it works well with generic userpassPainTestNow mode. I have commented both the scenarios out there. These links turned out to be really useful for troubleshooting this. Nodegit: How to modify a file and push the changes? https://github.com/nodegit/nodegit/issues/463

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.