简体   繁体   中英

How add directory or all files in a directory by nodegit?

I am new on node js and on git as well, i am building an app that will write results on text files and will push on git rapidly. After many struggle I have figured out following code that is writing file and pushing on github. But I am seeking a way by which I can push whole directory instead of each file by name. So please let me know the way how to push whole directory.

My current code is as following...

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");
});

You should be able to use index.addAll to do what you are asking.

See: https://github.com/nodegit/nodegit/issues/131

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM