Nodegit: How to modify a file and push the changes?

好久不见. 提交于 2019-11-28 23:28:54
Rafael

There is an example of how to create/add and commit on their repo, which can help you with the modifying the file.

https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js

Regarding the commit and push, here is the snippet of how my code looks like and i hope it helps you a bit. It took me quite a bit to figure this out and thanks to the guys on Gitter, i finaly found an answer.

The code look like this:

var path = require('path');

var nodegit = require('nodegit'),
    repoFolder = path.resolve(__dirname, 'repos/test/.git'),
    fileToStage = 'README.md';

var repo, index, oid, remote;

nodegit.Repository.open(repoFolder)
  .then(function(repoResult) {
    repo = repoResult;
    return repoResult.openIndex();
  })
  .then(function(indexResult) {
    index  = indexResult;

    // this file is in the root of the directory and doesn't need a full path
    index.addByPath(fileToStage);

    // this will write files to the index
    index.write();

    return index.writeTree();
  })
  .then(function(oidResult) {
    oid = oidResult;

    return nodegit.Reference.nameToId(repo, 'HEAD');
  })
  .then(function(head) {
    return repo.getCommit(head);
  })
  .then(function(parent) {
    author = nodegit.Signature.now('Author Name', 'author@email.com');
    committer = nodegit.Signature.now('Commiter Name', 'commiter@email.com');

    return repo.createCommit('HEAD', author, committer, 'Added the Readme file for theme builder', oid, [parent]);
  })
  .then(function(commitId) {
    return console.log('New Commit: ', commitId);
  })

  /// PUSH
  .then(function() {
    return repo.getRemote('origin');
  })
  .then(function(remoteResult) {
    console.log('remote Loaded');
    remote = remoteResult;
    remote.setCallbacks({
      credentials: function(url, userName) {
        return nodegit.Cred.sshKeyFromAgent(userName);
      }
    });
    console.log('remote Configured');

    return remote.connect(nodegit.Enums.DIRECTION.PUSH);
  })
  .then(function() {
    console.log('remote Connected?', remote.connected())

    return remote.push(
      ['refs/heads/master:refs/heads/master'],
      null,
      repo.defaultSignature(),
      'Push to master'
    )
  })
  .then(function() {
    console.log('remote Pushed!')
  })
  .catch(function(reason) {
    console.log(reason);
  })

Hope this helps.

Rafael's solution works for sure although I might add that there is no needed to perform a .connect into the server. Here is a minimal approach on pushing a repo:

  • Pushing to master using only username and password authentication:

            //Previous calls
    
            }).then(function() {
                return repo.getRemote("origin"); //Get origin remote
            }).then(function(remote) {
                return remote.push(["refs/heads/master:refs/heads/master"], {
                    callbacks: {
                        credentials: function(url, userName) {
                            console.log("Requesting creds");
    
                            return NodeGit.Cred.userpassPlaintextNew("[USERNAME]", "[PASSWORD]");
                        }
                    }
                });
            }).then(function(err) {
                console.log("Push error number:");
                console.log(err);
            }).catch(function(err) {
                console.log(err);
            }).done(function(commitId) {
                console.log("All done");
            })
    
  • Pushing to myBranch over SSH auth:

            //Previous calls
    
            }).then(function() {
                return repo.getRemote("origin"); //Get origin remote
            }).then(function(remote) {
                return remote.push(["refs/heads/myBranch:refs/heads/myBranch"], {
                    callbacks: {
                        credentials: function(url, userName) {
                            console.log("Requesting creds");
                            return NodeGit.Cred.sshKeyFromAgent(userName);
                        }
                    }
                });
            }).then(function(err) {
                console.log("Push error number:");
                console.log(err);
            }).catch(function(err) {
                console.log(err);
            }).done(function(commitId) {
                console.log("All done");
            })
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!