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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…