简体   繁体   中英

How to push using nodegit?

I am trying to push the file In the repository but getting error that {remote origin already exits errno -4}

Basic Task Open the connection, commit the file, {pull and merge} , push the changes

I am able to open the connection ,commit the file but rest operations are not working.

Not able to identilfy what is problem over here i am new to nodegit.

Code:

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);
    });

Files are getting committed properly but not able to push it.

I think you're getting that error because you're using Git.Remote.create(repo, "origin", url) every time after committing your changes. That would explain an error message stating remote origin already exits errno -4 . Try replacing that call with getRemote , then chain the push call. It would be something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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