2016-11-27 108 views
1

我是新的節點js和git以及,我正在構建一個應用程序,它將在文本文件上寫入結果並快速推送git。 經過許多鬥爭,我已經想出了下面的代碼,它正在編寫文件並推送到github上。但我正在尋求一種方法,我可以通過名稱推送整個目錄而不是每個文件。所以請讓我知道如何推送整個目錄。如何通過nodegit將目錄或所有文件添加到目錄中?

我當前的代碼如下...

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", 
    "[email protected]", 123456789, 60); 
    var committer = nodegit.Signature.create("Scott A Chacon", 
    "[email protected]", 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"); 
}); 

回答

相關問題