2017-06-13 80 views
0

我试图把文件在存储库中,但得到错误{远程起源已经存在错误号-4}如何推送使用nodegit?

基本任务打开连接,提交文件,{拉和合并},变更推

我可以打开连接,提交文件但休息操作不起作用。

无法识别什么是问题在这里我是新的nodegit。

代码:

import Git from "nodegit"; 
import path from "path"; 
import fs from "fs"; 
import promisify from "promisify-node"; 
import fs_extra from "fs-extra"; 

let url = "XXX/tutorial.git", 
    local = "./Cloned", 
    directoryName = "Code", 
    cloneOpts = { 
     fetchOpts: { 
      callbacks: { 
       credentials: function(url, userName) { 
        return Git.Cred.userpassPlaintextNew("***[email protected]","*****"); 
       } 
      } 
     } 
    }; 

let repo, 
    index, 
    oid, 
    remote; 

var fse = promisify(fs_extra); 
var fileName = "letmebe.txt"; 
var fileContent = "Costal Area is good"; 
fse.ensureDir = promisify(fse.ensureDir); 
let repoDir = "../../Code"; 

Git.Repository.open(local) 
    .then(function (repoResult) { 
     repo = repoResult; 
     return fse.ensureDir(path.join(repo.workdir(), directoryName)); 
    }) 
    .then(function() { 
     return fs.writeFile(path.join(repo.workdir(), directoryName, fileName), fileContent); 
    }) 
    .then(function() { 
     return repo.refreshIndex(); 
    }) 
    .then(function (indexResult) { 
     index = indexResult; 
    }) 
    .then(function() { 
     return index.addByPath(path.join(directoryName, fileName)) 
      .then(function() { 
       return index.write(); 
      }) 
      .then(function() { 
       return index.writeTree(); 
      }); 
    }) 
    .then(function (oidResult) { 
     oid = oidResult; 
     return Git.Reference.nameToId(repo, "HEAD"); 
    }) 
    .then(function (head) { 
     return repo.getCommit(head); 
    }) 
    .then(function (parent) { 
     var author = Git.Signature.create("Kunal Vashist", 
      "[email protected]", 123456789, 60); 
     var committer = Git.Signature.create("Kunal Vashist", 
      "[email protected]", 987654321, 90); 
     return repo.createCommit("HEAD", author, committer, "message", oid, [parent]); 
    }) 
    .then(function() { 
     return Git.Remote.create(repo, "origin",url) 
      .then(function(remoteResult) { 
       remote = remoteResult; 
       // Create the push object for this remote 
       return remote.push(
        ["refs/heads/master:refs/heads/master"], 
        { 
         callbacks: { 
          credentials: function(url, userName) { 
           return Git.Cred.userpassPlaintextNew("****@gmail.com","****"); 
          } 
         } 
        } 
       ); 
      }); 
    }) 
    .catch(function (err) { 
     console.log(err); 
    }) 
    .done(function (commitId) { 
     console.log("New Commit: ", commitId); 
    }); 

文件越来越承诺正常,但不能推。

回答

0

我想你会得到那个错误,因为你在提交你的改变后每次都使用Git.Remote.create(repo, "origin", url)。这将解释错误消息,说明remote origin already exits errno -4。 尝试用getRemote替换该呼叫,然后链接推送呼叫。它会是这样的:

.then(function(commitId) { 
    return repository.getRemote('origin'); 
}) 
.then(function(remote) { 
    return remote.push(['refs/heads/master:refs/heads/master'], { 
     callbacks: // your own callback 
    }); 
})