Search code examples
javascriptnode.jsgitnodegit

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


Looked around for an example, but couldn't find one. The documentation is not explained and I could not figure it out.

How to modify a file (README.md for example), create a commit for the modified file and then push the commit to the server ?

Nodegit: http://www.nodegit.org/

Nodegit documentation: http://www.nodegit.org/nodegit


Solution

  • 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', '[email protected]');
        committer = nodegit.Signature.now('Commiter Name', '[email protected]');
    
        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.