简体   繁体   中英

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

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

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