使用nodegit

问题描述

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(),fileContent);
    })
    .then(function () {
      return fse.writeFile(
        path.join(repo.workdir(),directoryName,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",[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(...)。完成 不是功能 在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_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 \ route.js:137:13) 在Route.dispatch(c:\ CodeRepos \ work \ github \ aa \ ac \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [作为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 [作为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)

似乎正在发生:

返回nodegit.Repository.init(path.resolve(__ dirname,newRepoDir),0);

我觉得this可能与之相关,但并没有解决

解决方法

需要将.done更改为.then,最后

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(),fileContent);
    })
    .then(function () {
      return fse.writeFile(
        path.join(repo.workdir(),directoryName,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",[parent]);
    })
    .done(function (commitId) {
      console.log("New Commit: ",commitId);
    });
}
module.exports = {
  createGitRepository,addAndCommit
}