'use strict';
const Git = require("nodegit");
const fs = require('fs');
const cnst = require('./constant');
const common = require('./common');
/**
* This example creates a certain file `newfile.txt`, adds it to the git
* index and commits it to head. Similar to a `git add newfile.txt`
* followed by a `git commit`
**/
let repo;
let index;
let oid;
// api
/*
Repository.open :
Repository.createCommit : commit
Index.write : Index.addByPathIndex
Index.writeTree : Index.write, tree(git)
*/
Git.Repository.open(cnst.test_git_path)
.then((repoResult) => (repo = repoResult))
.then(() => {
return fs.writeFileSync(
cnst.add_commit_filename,
'add & commit file content new_again 2'
);
})
.then(() => repo.refreshIndex())
.then((indexResult) => (index = indexResult))
.then(() => index.addByPath(cnst.add_commit_filename))
.then(() => index.write())
.then(() => index.writeTree())
// addcommit
.then((oidResult) => {
oid = oidResult;
return Git.Reference.nameToId(repo, 'HEAD');
})
.then((head) => repo.getCommit(head))
.then((parent) => {
let commit_msg = 'commit msg from add & commit';
return repo.createCommit(
'HEAD',
common.get_commiter_sign(cnst),
common.get_commiter_sign(cnst),
commit_msg,
oid,
[parent]
);
})
.done((commitId) => {
repo.free();
console.log('New Commit: ', commitId);
});