如何推送使用nodegit?

时间:2017-06-13 18:21:31

标签: node.js github nodegit node-github

我正在尝试将文件推送到存储库但是收到错误{remote origin已经退出errno -4}

基本任务打开连接,提交文件,{pull and merge},推送更改

我可以打开连接,提交文件但是休息操作无效。

无法识别这里的问题我是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("***8@gmail.com","*****");
                }
            }
        }
    };

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",
            "kunal.vash@yopmail.com", 123456789, 60);
        var committer = Git.Signature.create("Kunal Vashist",
            "kunal@yopmail.com", 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);
    });

文件正在正确提交但无法推送。

1 个答案:

答案 0 :(得分: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
    });
})