0

我正在尝试将文件推送到存储库中,但收到 {remote origin already exits 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);
    });

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

4

1 回答 1

3

我认为您会收到该错误,因为您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
    });
})
于 2017-09-14T02:40:40.250 回答