var nodegit = require("nodegit");
var path = require("path");
var fse = require("fs-extra");
var repoDir = "../git_repositories/";
var repository;
var index;
//eventually need to pass in user here for some metadata (author/committor info)
createGitRepository = (user, repositoryName) => {
var newRepoDir = repoDir.concat(repositoryName);
var fileName = repositoryName.concat(".txt");
var fileContent = repositoryName.concat("DEFAULT CONTENTS");
fse.ensureDir(path.resolve(__dirname, newRepoDir))
.then(function () {
return nodegit.Repository.init(path.resolve(__dirname, newRepoDir), 0);
})
.then(function (repo) {
repository = repo;
return fse.writeFile(path.join(repository.workdir(), fileName), fileContent);
})
.then(function () {
return repository.refreshIndex();
})
.then(function (idx) {
index = idx;
})
.then(function () {
return index.addByPath(fileName);
})
.then(function () {
return index.write();
})
.then(function () {
return index.writeTree();
})
.then(function (oid) {
var author = nodegit.Signature.now("Zaphod Beeblebrox",
"zaphod@gmail.com");
var committer = nodegit.Signature.now("Ford Prefect",
"ford@github.com");
// Since we're creating an inital commit, it has no parents. Note that unlike
// normal we don't get the head either, because there isn't one yet.
var commit = repository.createCommit("HEAD", author, committer, "message", oid, []);
return commit;
})
.done(function (commitId) {
console.log("New Commit: ", commitId);
});
}
addAndCommit = (fileName, fileContent) => {
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function (repoResult) {
repo = repoResult;
return fse.ensureDir(path.join(repo.workdir(), directoryName));
}).then(function () {
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
})
.then(function () {
return fse.writeFile(
path.join(repo.workdir(), directoryName, fileName),
fileContent
);
})
.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.posix.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.now("Scott Chacon",
"schacon@gmail.com");
var committer = nodegit.Signature.now("Scott A Chacon",
"scott@github.com");
return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
})
.done(function (commitId) {
console.log("New Commit: ", commitId);
});
}
module.exports = {
createGitRepository,
addAndCommit
}
它成功创建了文件夹、文件和存储库,但抛出了错误。我不确定错误到底是什么意思,我无法通过代码来弄清楚?
TEST1 TypeError: fse.ensureDir(...).then(...).then(...).then(...).then(...).then(...).then(. ..).then(...).then(...).done 不是 Object.createGitRepository 中的函数 (c:\CodeRepos\work\github\aa\ac\service\GitService.js:50:6 ) 在 Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac) 的 createRepository (c:\CodeRepos\work\github\aa\ac\controllers\repository-ctrl.js:55:33) \node_modules\express\lib\router\layer.js:95:5) 在下一个 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\route.js:137:13) 在Route.dispatch (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\route.js:112:3) 在 Layer.handle [as handle_request] (c:\CodeRepos\work\github \aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:281:22在 Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12)在下一个(c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:275:10) 在 Function.handle (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:174:3) 在路由器 (c:\CodeRepos \work\github\aa\ac\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express \lib\router\layer.js:95:5) 在 trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:317:13) 在 c:\CodeRepos \work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\ index.js:335:12) 在下一个 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10)10)10)10)10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10) 在 Function.handle (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib \router\index.js:174:3) 在路由器 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request ] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express \lib\router\index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (c:\ CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12)在下一个(c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\ index.js:275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10) 在 Function.handle (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib \router\index.js:174:3) 在路由器 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:47:12) 在 Layer.handle [as handle_request ] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express \lib\router\index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (c:\ CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12)在下一个(c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\ index.js:275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:174:3) 在路由器 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:47:12) 在 Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix ( c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:284:7 在 Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12) 在下一个 (c:\CodeRepos\工作\github\aa\ac\node_modules\express\lib\router\index.js:275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:174:3) 在路由器 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:47:12) 在 Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix ( c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:284:7 在 Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12) 在下一个 (c:\CodeRepos\工作\github\aa\ac\node_modules\express\lib\router\index.js:275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (c:\CodeRepos\work\ github\aa\ac\node_modules\express\lib\router\index.js:335:12) 在下一个 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js: 275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) 在 trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router \index.js:317:13) 在 c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 在 Function.process_params (c:\CodeRepos\work\ github\aa\ac\node_modules\express\lib\router\index.js:335:12) 在下一个 (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js: 275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10)\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10)
似乎就行了:
返回 nodegit.Repository.init(path.resolve(__dirname, newRepoDir), 0);
我觉得这可能是相关的,但它没有解决它