Skip to content

Instantly share code, notes, and snippets.

@bynaki
Created December 9, 2015 05:24
Show Gist options
  • Save bynaki/5b6b8074aaad40044e4b to your computer and use it in GitHub Desktop.
Save bynaki/5b6b8074aaad40044e4b to your computer and use it in GitHub Desktop.
NodeGit :: init

NodeGit :: init

// git init and initial commit.
//

var nodegit = require("nodegit");
var path = require("path");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
var fileName = "newfile.txt";
var fileContent = "hello world";
var repoDir = "../../newRepo";

// fse.ensureDir = promisify(fse.ensureDir);
// fse.remove = promisify(fse.remove);

var repository;
var index;

fse.remove(path.resolve(__dirname, repoDir))
.then(function () {
  return fse.ensureDir(path.resolve(__dirname, repoDir))
})
.then(function() {
  var isBare = 0; // repository안에 .git을 생성한다.
  return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
})
.then(function(repo) {
  repository = repo;
  return fse.writeFile(path.join(repository.workdir(), fileName), fileContent);
})
.then(function(){
  // add 하기위해 필요하다.
  return repository.openIndex();
})
.then(function(idx) {
  index = idx;
  return index.read(1);
})
.then(function() {
  // fileName을 stage한다.
  return index.addByPath(fileName);
})
.then(function() {
  return index.write();
})
.then(function() {
  return index.writeTree();
})
.then(function(oid) {
  // 이름과 이메일과 시간이 담기 signature을 생성한다. repository의 디폴트로.
  var sig = nodegit.Signature.default(repository);
  var author = sig;
  var committer = sig;
  // var author = nodegit.Signature.create("Scott Chacon",
  //   "[email protected]", 123456789, 60);
  // var committer = nodegit.Signature.create("Scott A Chacon",
  //   "[email protected]", 987654321, 90);

  // 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.
  // commit 한다. 처음 commit하는 거라 맨끝 파리미터인 parent는 필요없지만, 다음부턴 필요하다.
  return repository.createCommit("HEAD", author, committer, "message", oid, []);
})
.done(function(commitId) {
  console.log("New Commit: ", commitId);
});
{"noteId":"15185316b10-d33535a8","main":"15185316b10-d33535a8.md","title":"NodeGit :: init"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment