From cc1e6b4432fb46eeef77822d4d318a47a5b4a3aa Mon Sep 17 00:00:00 2001 From: NGPixel Date: Thu, 25 Aug 2016 18:55:42 -0400 Subject: [PATCH] Git commit handling --- models/git.js | 64 ++++++++++++++++++++++++++++++++++++++++++++------- server.js | 8 +++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/models/git.js b/models/git.js index 2a390be9..b25e6878 100644 --- a/models/git.js +++ b/models/git.js @@ -5,6 +5,7 @@ var NodeGit = require("nodegit"), path = require('path'), os = require('os'), fs = Promise.promisifyAll(require("fs")), + moment = require('moment'), _ = require('lodash'); /** @@ -20,6 +21,10 @@ module.exports = { inst: null, sync: true }, + _signature: { + name: 'Wiki', + email: 'user@example.com' + }, _opts: { clone: {}, push: {} @@ -54,6 +59,11 @@ module.exports = { }); + // Define signature + + self._signature.name = appconfig.git.userinfo.name || 'Wiki'; + self._signature.email = appconfig.git.userinfo.email || 'user@example.com'; + return self; }, @@ -166,9 +176,12 @@ module.exports = { let remoteCallbacks = new NodeGit.RemoteCallbacks(); let credFunc = this._generateCredentials(appconfig); remoteCallbacks.credentials = () => { return credFunc; }; + remoteCallbacks.transferProgress = _.noop; if(os.type() === 'Darwin') { remoteCallbacks.certificateCheck = () => { return 1; }; // Bug in OS X, bypass certs check workaround + } else { + remoteCallbacks.certificateCheck = _.noop; } return remoteCallbacks; @@ -232,14 +245,49 @@ module.exports = { .then(() => { return self._repo.inst.getRemote('origin').then((remote) => { - self._repo.inst.getStatus().then(function(arrayStatusFile) { - console.log(arrayStatusFile[0].status()); - }); - /*remote.push( ["refs/heads/master:refs/heads/master"], self._opts.push ).then((errNum) => { - console.log('DUDE' + errNum); - }).catch((err) => { - console.log(err); - });*/ + + // Get modified files + + return self._repo.inst.refreshIndex().then((index) => { + return self._repo.inst.getStatus().then(function(arrayStatusFile) { + + let addOp = []; + + // Add to next commit + + _.forEach(arrayStatusFile, (v) => { + addOp.push(arrayStatusFile[0].path()); + }); + + console.log('DUDE1'); + + // Create Commit + + let sig = NodeGit.Signature.create(self._signature.name, self._signature.email, moment().utc().unix(), 0); + return self._repo.inst.createCommitOnHead(addOp, sig, sig, "Wiki Sync").then(() => { + + console.log('DUDE2'); + + return remote.connect(NodeGit.Enums.DIRECTION.PUSH, self._opts.push.callbacks).then(() => { + + console.log('DUDE3'); + + // Push to remote + + return remote.push( ["refs/heads/master:refs/heads/master"], self._opts.push).then((errNum) => { + console.log('DUDE' + errNum); + }).catch((err) => { + console.log(err); + }); + + }); + + }); + + }); + }) + + /**/ }); }).catch((err) => { winston.error('Unable to push to git origin!' + err); diff --git a/server.js b/server.js index 425e29b5..63d38a94 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,14 @@ // Licensed under AGPLv3 // =========================================== +process.on('uncaughtException', function (exception) { + console.log(exception); +}); +process.on('unhandledRejection', (reason, p) => { + console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason); + // application specific logging, throwing an error, or other logic here +}); + global.ROOTPATH = __dirname; // ----------------------------------------