Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions example/add-and-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ var path = require('path');
var Promise = require('nodegit-promise');
var promisify = require('promisify-node');
var fse = promisify(require('fs-extra'));
var mkdirp = promisify(require('mkdirp'));
var fileName = 'newfile.txt';
var fileContent = 'hello world';
var directoryName = 'salad/toast/strangerinastrangeland/theresnowaythisexists';
// ensureDir is an alias to mkdirp, which has the callback with a weird name
// and in the 3rd position of 4 (the 4th being used for recursion). We have to force
// promisify it, because promisify-node won't detect it on its own and assumes sync
fse.ensureDir = promisify(fse.ensureDir);

/**
* This example creates a certain file `newfile.txt`, adds it to the git index and
Expand All @@ -19,8 +25,13 @@ var parent;
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.openIndex();
})
Expand All @@ -31,6 +42,9 @@ nodegit.Repository.open(path.resolve(__dirname, '../.git'))
.then(function() {
return index.addByPath(fileName);
})
.then(function() {
return index.addByPath(path.join(directoryName, fileName));
})
.then(function() {
return index.write();
})
Expand Down