2016-08-20 04:50:29 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
var Git = require("git-wrapper2-promise"),
|
2016-08-20 04:50:29 +00:00
|
|
|
Promise = require('bluebird'),
|
|
|
|
path = require('path'),
|
|
|
|
os = require('os'),
|
|
|
|
fs = Promise.promisifyAll(require("fs")),
|
2016-08-25 22:55:42 +00:00
|
|
|
moment = require('moment'),
|
2016-08-27 05:04:08 +00:00
|
|
|
_ = require('lodash'),
|
|
|
|
URL = require('url');
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Git Model
|
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
_git: null,
|
2016-08-27 05:04:08 +00:00
|
|
|
_url: '',
|
2016-08-20 04:50:29 +00:00
|
|
|
_repo: {
|
|
|
|
path: '',
|
2016-08-25 02:10:03 +00:00
|
|
|
branch: 'master',
|
2016-09-04 05:12:42 +00:00
|
|
|
exists: false
|
2016-08-25 02:10:03 +00:00
|
|
|
},
|
2016-08-25 22:55:42 +00:00
|
|
|
_signature: {
|
|
|
|
name: 'Wiki',
|
|
|
|
email: 'user@example.com'
|
|
|
|
},
|
2016-08-25 02:10:03 +00:00
|
|
|
_opts: {
|
|
|
|
clone: {},
|
|
|
|
push: {}
|
2016-08-20 04:50:29 +00:00
|
|
|
},
|
2016-09-04 05:12:42 +00:00
|
|
|
onReady: null,
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Git model
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Object} Git model instance
|
|
|
|
*/
|
2016-09-04 05:12:42 +00:00
|
|
|
init(appconfig) {
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
//-> Build repository path
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
if(_.isEmpty(appconfig.paths.repo)) {
|
2016-08-20 04:50:29 +00:00
|
|
|
self._repo.path = path.join(ROOTPATH, 'repo');
|
|
|
|
} else {
|
2016-10-15 03:31:15 +00:00
|
|
|
self._repo.path = appconfig.paths.repo;
|
2016-08-20 04:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-> Initialize repository
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
self.onReady = self._initRepo(appconfig);
|
2016-08-20 04:50:29 +00:00
|
|
|
|
2016-08-25 22:55:42 +00:00
|
|
|
// Define signature
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
self._signature.name = appconfig.git.signature.name || 'Wiki';
|
|
|
|
self._signature.email = appconfig.git.signature.email || 'user@example.com';
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-20 04:50:29 +00:00
|
|
|
return self;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Git repository
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Object} Promise
|
|
|
|
*/
|
|
|
|
_initRepo(appconfig) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Checking Git repository...');
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
//-> Check if path is accessible
|
|
|
|
|
|
|
|
return fs.mkdirAsync(self._repo.path).catch((err) => {
|
|
|
|
if(err.code !== 'EEXIST') {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.error('[' + PROCNAME + '][GIT] Invalid Git repository path or missing permissions.');
|
2016-08-20 04:50:29 +00:00
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
self._git = new Git({ 'git-dir': self._repo.path });
|
|
|
|
|
2016-08-20 04:50:29 +00:00
|
|
|
//-> Check if path already contains a git working folder
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
return self._git.isRepo().then((isRepo) => {
|
|
|
|
self._repo.exists = isRepo;
|
|
|
|
return (!isRepo) ? self._git.exec('init') : true;
|
2016-08-20 04:50:29 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
self._repo.exists = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
}).then(() => {
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
// Initialize remote
|
|
|
|
|
|
|
|
let urlObj = URL.parse(appconfig.git.url);
|
|
|
|
urlObj.auth = appconfig.git.auth.username + ((appconfig.git.auth.type !== 'ssh') ? ':' + appconfig.git.auth.password : '');
|
|
|
|
self._url = URL.format(urlObj);
|
|
|
|
|
|
|
|
return self._git.exec('remote', 'show').then((cProc) => {
|
|
|
|
let out = cProc.stdout.toString();
|
|
|
|
if(_.includes(out, 'origin')) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return Promise.join(
|
|
|
|
self._git.exec('config', ['--local', 'user.name', self._signature.name]),
|
|
|
|
self._git.exec('config', ['--local', 'user.email', self._signature.email])
|
|
|
|
).then(() => {
|
|
|
|
return self._git.exec('remote', ['add', 'origin', self._url]);
|
2016-10-17 23:52:04 +00:00
|
|
|
});
|
2016-08-27 05:04:08 +00:00
|
|
|
}
|
|
|
|
});
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
}).catch((err) => {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.error('[' + PROCNAME + '][GIT] Git remote error!');
|
2016-08-27 05:04:08 +00:00
|
|
|
throw err;
|
|
|
|
}).then(() => {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Git repository is OK.');
|
2016-08-27 05:04:08 +00:00
|
|
|
return true;
|
2016-08-20 04:50:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-09-17 04:26:02 +00:00
|
|
|
/**
|
|
|
|
* Gets the repo path.
|
|
|
|
*
|
|
|
|
* @return {String} The repo path.
|
|
|
|
*/
|
|
|
|
getRepoPath() {
|
|
|
|
|
|
|
|
return this._repo.path || path.join(ROOTPATH, 'repo');
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
/**
|
|
|
|
* Sync with the remote repository
|
|
|
|
*
|
|
|
|
* @return {Promise} Resolve on sync success
|
|
|
|
*/
|
2016-08-25 02:10:03 +00:00
|
|
|
resync() {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// Fetch
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Performing pull from remote repository...');
|
2016-08-27 05:04:08 +00:00
|
|
|
return self._git.pull('origin', self._repo.branch).then((cProc) => {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Pull completed.');
|
2016-08-25 02:10:03 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.error('[' + PROCNAME + '][GIT] Unable to fetch from git origin!');
|
2016-08-27 05:04:08 +00:00
|
|
|
throw err;
|
2016-08-25 02:10:03 +00:00
|
|
|
})
|
|
|
|
.then(() => {
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
// Check for changes
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
return self._git.exec('log', 'origin/' + self._repo.branch + '..HEAD').then((cProc) => {
|
2016-08-27 05:04:08 +00:00
|
|
|
let out = cProc.stdout.toString();
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
if(_.includes(out, 'commit')) {
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Performing push to remote repository...');
|
2016-08-29 05:21:35 +00:00
|
|
|
return self._git.push('origin', self._repo.branch).then(() => {
|
2016-09-29 01:58:18 +00:00
|
|
|
return winston.info('[' + PROCNAME + '][GIT] Push completed.');
|
2016-08-27 05:04:08 +00:00
|
|
|
});
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
} else {
|
2016-08-29 05:21:35 +00:00
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[' + PROCNAME + '][GIT] Push skipped. Repository is already in sync.');
|
2016-08-29 05:21:35 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
}
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
return true;
|
2016-08-25 22:55:42 +00:00
|
|
|
|
2016-08-25 02:10:03 +00:00
|
|
|
});
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.error('[' + PROCNAME + '][GIT] Unable to push changes to remote!');
|
2016-08-27 05:04:08 +00:00
|
|
|
throw err;
|
2016-08-25 02:10:03 +00:00
|
|
|
});
|
2016-08-20 04:50:29 +00:00
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits a document.
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise} Resolve on commit success
|
|
|
|
*/
|
|
|
|
commitDocument(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let gitFilePath = entryPath + '.md';
|
|
|
|
let commitMsg = '';
|
|
|
|
|
|
|
|
return self._git.exec('ls-files', gitFilePath).then((cProc) => {
|
|
|
|
let out = cProc.stdout.toString();
|
|
|
|
return _.includes(out, gitFilePath);
|
|
|
|
}).then((isTracked) => {
|
|
|
|
commitMsg = (isTracked) ? 'Updated ' + gitFilePath : 'Added ' + gitFilePath;
|
|
|
|
return self._git.add(gitFilePath);
|
|
|
|
}).then(() => {
|
2016-09-05 23:23:49 +00:00
|
|
|
return self._git.commit(commitMsg).catch((err) => {
|
|
|
|
if(_.includes(err.stdout, 'nothing to commit')) { return true; }
|
|
|
|
});
|
2016-08-29 05:21:35 +00:00
|
|
|
});
|
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move a document.
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The current entry path
|
|
|
|
* @param {String} newEntryPath The new entry path
|
|
|
|
* @return {Promise<Boolean>} Resolve on success
|
|
|
|
*/
|
|
|
|
moveDocument(entryPath, newEntryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let gitFilePath = entryPath + '.md';
|
|
|
|
let gitNewFilePath = newEntryPath + '.md';
|
|
|
|
|
|
|
|
return self._git.exec('mv', [gitFilePath, gitNewFilePath]).then((cProc) => {
|
|
|
|
let out = cProc.stdout.toString();
|
|
|
|
if(_.includes(out, 'fatal')) {
|
|
|
|
let errorMsg = _.capitalize(_.head(_.split(_.replace(out, 'fatal: ', ''), ',')));
|
|
|
|
throw new Error(errorMsg);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
|
2016-10-09 05:26:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits uploads changes.
|
|
|
|
*
|
2016-10-15 03:31:15 +00:00
|
|
|
* @param {String} msg The commit message
|
2016-10-09 05:26:25 +00:00
|
|
|
* @return {Promise} Resolve on commit success
|
|
|
|
*/
|
2016-10-15 03:31:15 +00:00
|
|
|
commitUploads(msg) {
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
let self = this;
|
2016-10-15 03:31:15 +00:00
|
|
|
msg = msg || "Uploads repository sync";
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
return self._git.add('uploads').then(() => {
|
2016-10-15 03:31:15 +00:00
|
|
|
return self._git.commit(msg).catch((err) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
if(_.includes(err.stdout, 'nothing to commit')) { return true; }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-20 04:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|