Node.js 使用nodegit创建git存储库时出错

Node.js 使用nodegit创建git存储库时出错,node.js,nodegit,Node.js,Nodegit,它成功创建文件夹、文件和存储库,但抛出错误。我不确定错误到底指示了什么,我无法通过代码一步一步地找出它 TEST1类型错误: 然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。完成 这不是一个函数 位于Object.createGitRepository(c:\CodeRepos\work\github\aa\ac\service\GitService.js:50:6) 在createRepository(c:\C

它成功创建文件夹、文件和存储库,但抛出错误。我不确定错误到底指示了什么,我无法通过代码一步一步地找出它

TEST1类型错误: 然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。然后(…)。完成 这不是一个函数 位于Object.createGitRepository(c:\CodeRepos\work\github\aa\ac\service\GitService.js:50:6) 在createRepository(c:\CodeRepos\work\github\aa\ac\controllers\repository ctrl.js:55:33) 在Layer.handle[作为handle\u请求](c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\Layer.js:95:5) 下一步(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\route.js:137:13) 在Route.dispatch(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\Route.js:112:3) 在Layer.handle[作为handle\u请求](c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\Layer.js:95:5) 位于c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:281:22 在Function.process_参数处(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:335:12) 下一步(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:275:10) 位于Function.handle(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:174:3) 在路由器上(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:47:12) 在Layer.handle[作为handle\u请求](c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\Layer.js:95:5) 在trim_前缀处(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:317:13) 位于c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:284:7 在Function.process_参数处(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:335:12) 下一步(c:\CodeRepos\work\github\aa\ac\node\u modules\express\lib\router\index.js:275:10)

似乎是在网上发生的:

返回nodegit.Repository.init(path.resolve(\uu dirname,newRepoDir),0)


我觉得可能是相关的,但它没有修复它需要更改。完成。然后在最后

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
}
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;
    })
    .then(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
}