Skip to content

Instantly share code, notes, and snippets.

@mebibou
Created December 18, 2014 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebibou/87ff79904387224e6ae7 to your computer and use it in GitHub Desktop.
Save mebibou/87ff79904387224e6ae7 to your computer and use it in GitHub Desktop.
Changelog Commit
(function() {
'use strict';
var nodegit = require('nodegit'),
path = require('path');
var ChangelogCommit = function() {
var _config = {
branch: 'master'
},
_repo = null,
_author = null,
_index = null,
_oid = null;
function _checkConfigElement(key) {
if (!(key in _config)) {
throw new Error('Config ' + key + ' is missing. USage: node changelog-commit.js --' + key + '=<value>');
}
}
process.argv.forEach(function(arg) {
var type = arg.split('=')[0],
value = arg.split('=')[1];
if (type.substring(0, 2) == '--') {
_config[type.substring(2)] = value;
}
});
_checkConfigElement('file');
_checkConfigElement('branch');
this.commit = function() {
nodegit.Repository
.open(path.resolve(__dirname, './.git'))
.then(function(repo) {
_repo = repo;
return repo.getBranchCommit(_config.branch);
})
.then(function(firstCommit) {
_author = firstCommit.author();
return _author;
})
.then(function() {
return _repo.openIndex();
})
.then(function(index) {
_index = index;
return index.read(1);
})
.then(function() {
return _index.addByPath(_config.file);
})
.then(function() {
return _index.write();
})
.then(function() {
return _index.writeTree();
})
.then(function(oid) {
_oid = oid;
return nodegit.Reference.nameToId(_repo, 'HEAD');
})
.then(function(head) {
return _repo.getCommit(head);
})
.then(function(parent) {
return _repo.createCommit('HEAD', _author, _author, 'chore(changelog): update changelog', _oid, [parent]);
})
.done(function(commitId) {
console.log('Commit for changelog: ' + commitId);
});
};
};
new ChangelogCommit().commit();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment