使用 Node-git 我只想:
- 打开一个仓库(文件已被写入/更新的地方)
- 暂存文件
- 做承诺
使用 git cli 我会写这样的东西
cd repo
git add file.js
git commit -m "Added file.js"
我正在尝试按照此处描述如何使用 nodegit 执行此操作的示例,但很难遵循这些代码行:
.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",
"schacon@gmail.com", 123456789, 60);
var committer = nodegit.Signature.create("Scott A Chacon",
"scott@github.com", 987654321, 90);
return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
})
.done(function(commitId) {
console.log("New Commit: ", commitId);
});
有必要这么长吗?repo.refreshIndex()、index.write()、index.writeTree()等的作用是什么?API 文档对初学者不太友好。
感谢开导!