Skip to content
Closed
Show file tree
Hide file tree
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
138 changes: 138 additions & 0 deletions bin/dump-to-git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'use strict';

var fs = require('fs');
var process = require('process');
var child_process = require('child_process');
var Promise = require('bluebird');
var NodeGit = require('nodegit');
var LZString = require('lz-string');
var models = require("../lib/models");
var config = require('../config.json').dumpToGit;

var repoRootDir = config.targetDirectory;

try {
process.chdir(repoRootDir);
} catch (e) {
console.error("The target directory for your notes does not exist, please create %s", repoRootDir);
process.exit(1);
}


function exportNotes() {
return models.Note
.findAll()
.then(function (notes) {
// Iterate over all notes
return Promise.all(notes.map(function (note) {
var fileName = LZString.decompressFromBase64(note.title) + " - " + LZString.compressToBase64(note.id) + ".md";
fileName = fileName.replace('/', '_');

// Export note to file
return new Promise(function (resolve) {
console.log('Exporting ' + note.id + ' to ' + fileName);
var body = LZString.decompressFromBase64(note.content);
fs.writeFile(fileName, body, resolve);
})
.then(function () {
return fileName;
});

}));
});
}


function addToIndexAndReturnTree(files, index) {
return Promise.all(files.map(function (file) {
console.log('Adding ' + file + ' to git index');
return index.addByPath(file);
}))
.then(function () {
console.log('Write files to git index');
return index.write();
})
.then(function () {
console.log('Write git index to tree');
return index.writeTree();
});
}


function getHeadCommit(repo) {
return NodeGit.Reference
.nameToId(repo, 'HEAD')
.then(function (headRef) {
return repo.getCommit(headRef);
});
}


function commit(repo, tree) {
return getHeadCommit(repo)
.then(function (head) {
console.log('Commiting...');
var sig = NodeGit.Signature.now(config.name, config.email);
return repo.createCommit('HEAD', sig, sig, 'Automated import', tree, [head]);
});
}



NodeGit.Repository
.open('.')
.then(function (repo) {

return exportNotes()
.then(function(files) {

return getHeadCommit(repo)
.then(function (head) {
return head.getTree();
})
.then(function (tree) {
console.log('Diff HEAD to workdir');
var opts = new NodeGit.DiffOptions();
opts.flags |= NodeGit.Diff.OPTION.INCLUDE_UNTRACKED;
return NodeGit.Diff.treeToWorkdir(repo, tree, opts);
})
.then(function (diff) {
console.log('Number of deltas: ' + diff.numDeltas());
if (diff.numDeltas() == 0) {
console.log('Nothing to commit');
return;
}

return repo.refreshIndex()
.then(function (index) {
return addToIndexAndReturnTree(files, index);
})
.then(function (tree) {
return commit(repo, tree);
});
})
.then(function () {
console.log('Get remote...');
return repo.getRemote('origin')
})
.then(function (remote) {
console.log('Push to origin');
// Didn't get remote.push() to work...
child_process.exec('git push');
// return remote.push(
// ['refs/heads/master:refs/heads/master'],
// {
// callbacks: {
// credentials: function(url, userName) {
// console.log('credential for ' + url + ' and ' + userName);
// return nodegit.Cred.sshKeyFromAgent(userName);
// }
// }
// }
// );
});
});
})
.catch(function (error) {
console.log(error.message);
});
5 changes: 5 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@
"imgur": {
"clientID": "change this"
}
},
"dumpToGit": {
"targetDirectory": "./notes",
"name": "Autocommitter",
"email": "john@example.com"
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"Idle.Js": "github:shawnmclean/Idle.js",
"async": "^2.1.4",
"aws-sdk": "^2.7.15",
"bluebird": "^3.4.0",
"blueimp-md5": "^2.6.0",
"body-parser": "^1.15.2",
"bootstrap": "^3.3.7",
Expand Down Expand Up @@ -78,6 +79,7 @@
"morgan": "^1.7.0",
"mysql": "^2.12.0",
"node-uuid": "^1.4.7",
"nodegit": "^0.13.0",
"octicons": "~3.5.0",
"passport": "^0.3.2",
"passport-dropbox-oauth2": "^1.1.0",
Expand Down