我已经能够使用以下代码将文件写入裸存储库中的分支,但它仅适用于根目录中的文件。我无法在文档中找到如何为子文件夹构建树并将其用作提交的好例子。
async function writeFile(filename, buffer) {
const signature = NodeGit.Signature.now('Jamie', 'jamie@diffblue.com');
const repo = await NodeGit.Repository.openBare('java-demo.git');
const commit = await repo.getBranchCommit('master');
const rootTree = await commit.getTree();
const builder = await NodeGit.Treebuilder.create(repo, rootTree);
const oid = await NodeGit.Blob.createFromBuffer(repo, buffer, buffer.length);
await builder.insert(filename, oid, NodeGit.TreeEntry.FILEMODE.BLOB);
const finalOid = await builder.write();
await repo.createCommit('refs/heads/master', signature, signature, 'Commit message', finalOid, [commit]);
}
const buffer = new Buffer('Hello\n', 'utf-8');
writeFile('test.txt', buffer).then(() => console.log('Done'));
在(例如)src/test.txt 中而不是 test.txt 中发布需要进行哪些修改?