node.jsnodegit

error creating a git repository with 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(), 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
}

it creates the folder, file and repository successfully but throws error. Im not sure though what the error is indicating exactly and i cant figure it out stepping through the code?

TEST1 TypeError: fse.ensureDir(...).then(...).then(...).then(...).then(...).then(...).then(...).then(...).then(...).done is not a function at Object.createGitRepository (c:\CodeRepos\work\github\aa\ac\service\GitService.js:50:6) at createRepository (c:\CodeRepos\work\github\aa\ac\controllers\repository-ctrl.js:55:33) at Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) at next (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) at c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:281:22 at Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12) at next (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10) at Function.handle (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:174:3) at router (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:317:13) at c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:284:7 at Function.process_params (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:335:12) at next (c:\CodeRepos\work\github\aa\ac\node_modules\express\lib\router\index.js:275:10)

seems to happen on the line:

return nodegit.Repository.init(path.resolve(__dirname, newRepoDir), 0);

I feel like this is possibly related but it didnt fix it


Solution

  • needed to change .done to .then at the end

    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
    }