Using Node-git I just want to:
- Open a repo (where a file has been written/updated)
- Stage the file
- Do commit
Using the git cli I would write something like this
cd repo
git add file.js
git commit -m "Added file.js"
I'm trying to follow the examples here describing how to do it with nodegit but have a hard time following these lines of code:
.then(function() {
return repo.refreshIndex();
})
.then(function(indexResult) {
index = indexResult;
})
.then(function() {
// this file is in the root of the directory and doesn't need a full path
return index.addByPath(fileName);
})
.then(function() {
// this file is in a subdirectory and can use a relative path
return index.addByPath(path.join(directoryName, fileName));
})
.then(function() {
// this will write both files to the index
return index.write();
})
.then(function() {
return index.writeTree();
})
.then(function(oidResult) {
oid = oidResult;
return nodegit.Reference.nameToId(repo, "HEAD");
})
.then(function(head) {
return repo.getCommit(head);
})
.then(function(parent) {
var author = nodegit.Signature.create("Scott Chacon",
"[email protected]", 123456789, 60);
var committer = nodegit.Signature.create("Scott A Chacon",
"[email protected]", 987654321, 90);
return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
})
.done(function(commitId) {
console.log("New Commit: ", commitId);
});
Does it have to be so long? What are the roles of repo.refreshIndex(), index.write(), index.writeTree() etc. etc.? The API-docs is not so beginner friendly.
Thankful for enlightenment!