繁体   English   中英

如何使用nodegit推送?

[英]How to push using nodegit?

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

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

我认为您收到该错误是因为您每次提交更改后都在使用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
    });
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM