簡體   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