Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何通过nodegit添加目录或目录中的所有文件?_Node.js_Git_Nodegit_Node Github - Fatal编程技术网

Node.js 如何通过nodegit添加目录或目录中的所有文件?

Node.js 如何通过nodegit添加目录或目录中的所有文件?,node.js,git,nodegit,node-github,Node.js,Git,Nodegit,Node Github,我对node js和git都是新手,我正在构建一个将结果写入文本文件并快速推送git的应用程序。 经过多次努力,我终于找到了下面的代码,它正在编写文件并推动github。但我正在寻找一种方法,通过这种方法,我可以推送整个目录,而不是按名称推送每个文件。所以,请让我知道如何推动整个目录 我目前的代码如下 var nodegit = require("nodegit"); var path = require("path"); var promisify = require("promisify-n

我对node js和git都是新手,我正在构建一个将结果写入文本文件并快速推送git的应用程序。 经过多次努力,我终于找到了下面的代码,它正在编写文件并推动github。但我正在寻找一种方法,通过这种方法,我可以推送整个目录,而不是按名称推送每个文件。所以,请让我知道如何推动整个目录

我目前的代码如下

var nodegit = require("nodegit");
var path = require("path");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
var fileName = "urls.txt";
var fileContent = "hello world";
var directoryName = "./";
fse.ensureDir = promisify(fse.ensureDir);
var repo;
var index;
var oid;
var remote;

nodegit.Repository.open('data')
.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.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.create("Scott Chacon",
    "schacon@gmail.com", 123456789, 60);
  var committer = nodegit.Signature.create("Scott A Chacon",
    "scott@github.com", 987654321, 90);

  return repo.createCommit("HEAD", author, committer, "message", oid, [parent]);
})
.then(function() {
  return repo.getRemote("origin")
  .then(function(remoteResult) {
    remote = remoteResult;

    // Create the push object for this remote
    return remote.push(
      ["refs/heads/master:refs/heads/master"],
      {
        callbacks: {
          credentials: function(url, userName) {

            return nodegit.Cred.userpassPlaintextNew('username', 'password');
          }
        }
      }
    );
  });
})
.done(function(res) {
  console.log("Done");
});

您应该能够使用
index.addAll
来执行您的请求

见: