使用 Node-git 我只想:
使用 git cli 我会写这样的东西
cd repo
git add file.js
git commit -m "Added file.js"
Run Code Online (Sandbox Code Playgroud)
我正在尝试按照此处描述如何使用 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);
});
Run Code Online (Sandbox Code Playgroud)
有必要这么长吗?什么是角色repo.refreshIndex(),index.write(),index.writeTree()等等,等等?该API的文档是不那么友好的初学者。
多谢指教!
这是我第一次使用这个库,但我想我还是会回答。如果您将 Promise 替换为 ,那么遵循流程就会容易得多await。我以我对正在发生的事情的基本了解发表评论。
const repo = await git.Repository.open(localNotesDir);
const index = await repo.refreshIndex(); // read latest
const files = await repo.getStatus(); // get status of all files
files.forEach(file => index.addByPath(file.path())); // stage each file
await index.write(); // flush changes to index
const changes = await index.writeTree(); // get reference to a set of changes
const head = await git.Reference.nameToId(repo, "HEAD"); // get reference to the current state
const parent = await repo.getCommit(head); // get the commit for current state
const author = git.Signature.now("Scott Chacon", "schacon@gmail.com"); // build auth/committer
const committer = git.Signature.now("Scott A Chacon", "scott@github.com");
// combine all info into commit and return hash
const commitId = await repo.createCommit("HEAD", author, committer, "message", changes, [parent]);
console.log('commitId', commitId);
// changes and head are each oids. Read about them here:
// https://hackage.haskell.org/package/gitlib-0.6.5/docs/Data-Git-Oid.html
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
980 次 |
| 最近记录: |