mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
5.9 KiB
259 lines
5.9 KiB
8 years ago
|
"use strict";
|
||
|
|
||
8 years ago
|
var Git = require("git-wrapper2-promise"),
|
||
8 years ago
|
Promise = require('bluebird'),
|
||
|
path = require('path'),
|
||
|
os = require('os'),
|
||
|
fs = Promise.promisifyAll(require("fs")),
|
||
8 years ago
|
moment = require('moment'),
|
||
8 years ago
|
_ = require('lodash'),
|
||
|
URL = require('url');
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Git Model
|
||
|
*/
|
||
|
module.exports = {
|
||
|
|
||
|
_git: null,
|
||
8 years ago
|
_url: '',
|
||
8 years ago
|
_repo: {
|
||
|
path: '',
|
||
8 years ago
|
branch: 'master',
|
||
8 years ago
|
exists: false
|
||
8 years ago
|
},
|
||
8 years ago
|
_signature: {
|
||
|
name: 'Wiki',
|
||
|
email: 'user@example.com'
|
||
|
},
|
||
8 years ago
|
_opts: {
|
||
|
clone: {},
|
||
|
push: {}
|
||
8 years ago
|
},
|
||
8 years ago
|
onReady: null,
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Initialize Git model
|
||
|
*
|
||
|
* @param {Object} appconfig The application config
|
||
|
* @return {Object} Git model instance
|
||
|
*/
|
||
8 years ago
|
init(appconfig) {
|
||
8 years ago
|
|
||
|
let self = this;
|
||
|
|
||
|
//-> Build repository path
|
||
|
|
||
8 years ago
|
if(_.isEmpty(appconfig.paths.repo)) {
|
||
8 years ago
|
self._repo.path = path.join(ROOTPATH, 'repo');
|
||
|
} else {
|
||
8 years ago
|
self._repo.path = appconfig.paths.repo;
|
||
8 years ago
|
}
|
||
|
|
||
|
//-> Initialize repository
|
||
|
|
||
8 years ago
|
self.onReady = self._initRepo(appconfig);
|
||
8 years ago
|
|
||
8 years ago
|
// Define signature
|
||
|
|
||
8 years ago
|
self._signature.name = appconfig.git.signature.name || 'Wiki';
|
||
|
self._signature.email = appconfig.git.signature.email || 'user@example.com';
|
||
8 years ago
|
|
||
8 years ago
|
return self;
|
||
|
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Initialize Git repository
|
||
|
*
|
||
|
* @param {Object} appconfig The application config
|
||
|
* @return {Object} Promise
|
||
|
*/
|
||
|
_initRepo(appconfig) {
|
||
|
|
||
|
let self = this;
|
||
|
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Checking Git repository...');
|
||
8 years ago
|
|
||
|
//-> Check if path is accessible
|
||
|
|
||
|
return fs.mkdirAsync(self._repo.path).catch((err) => {
|
||
|
if(err.code !== 'EEXIST') {
|
||
8 years ago
|
winston.error('[' + PROCNAME + '][GIT] Invalid Git repository path or missing permissions.');
|
||
8 years ago
|
}
|
||
|
}).then(() => {
|
||
|
|
||
8 years ago
|
self._git = new Git({ 'git-dir': self._repo.path });
|
||
|
|
||
8 years ago
|
//-> Check if path already contains a git working folder
|
||
|
|
||
8 years ago
|
return self._git.isRepo().then((isRepo) => {
|
||
|
self._repo.exists = isRepo;
|
||
|
return (!isRepo) ? self._git.exec('init') : true;
|
||
8 years ago
|
}).catch((err) => {
|
||
|
self._repo.exists = false;
|
||
|
});
|
||
|
|
||
|
}).then(() => {
|
||
|
|
||
8 years ago
|
// 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]);
|
||
|
})
|
||
|
}
|
||
|
});
|
||
8 years ago
|
|
||
|
}).catch((err) => {
|
||
8 years ago
|
winston.error('[' + PROCNAME + '][GIT] Git remote error!');
|
||
8 years ago
|
throw err;
|
||
|
}).then(() => {
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Git repository is OK.');
|
||
8 years ago
|
return true;
|
||
8 years ago
|
});
|
||
|
|
||
|
},
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Gets the repo path.
|
||
|
*
|
||
|
* @return {String} The repo path.
|
||
|
*/
|
||
|
getRepoPath() {
|
||
|
|
||
|
return this._repo.path || path.join(ROOTPATH, 'repo');
|
||
|
|
||
|
},
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Sync with the remote repository
|
||
|
*
|
||
|
* @return {Promise} Resolve on sync success
|
||
|
*/
|
||
8 years ago
|
resync() {
|
||
|
|
||
|
let self = this;
|
||
|
|
||
|
// Fetch
|
||
|
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Performing pull from remote repository...');
|
||
8 years ago
|
return self._git.pull('origin', self._repo.branch).then((cProc) => {
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Pull completed.');
|
||
8 years ago
|
})
|
||
|
.catch((err) => {
|
||
8 years ago
|
winston.error('[' + PROCNAME + '][GIT] Unable to fetch from git origin!');
|
||
8 years ago
|
throw err;
|
||
8 years ago
|
})
|
||
|
.then(() => {
|
||
8 years ago
|
|
||
8 years ago
|
// Check for changes
|
||
8 years ago
|
|
||
8 years ago
|
return self._git.exec('log', 'origin/' + self._repo.branch + '..HEAD').then((cProc) => {
|
||
8 years ago
|
let out = cProc.stdout.toString();
|
||
8 years ago
|
|
||
8 years ago
|
if(_.includes(out, 'commit')) {
|
||
8 years ago
|
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Performing push to remote repository...');
|
||
8 years ago
|
return self._git.push('origin', self._repo.branch).then(() => {
|
||
8 years ago
|
return winston.info('[' + PROCNAME + '][GIT] Push completed.');
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
} else {
|
||
8 years ago
|
|
||
8 years ago
|
winston.info('[' + PROCNAME + '][GIT] Push skipped. Repository is already in sync.');
|
||
8 years ago
|
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
return true;
|
||
8 years ago
|
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
|
})
|
||
|
.catch((err) => {
|
||
8 years ago
|
winston.error('[' + PROCNAME + '][GIT] Unable to push changes to remote!');
|
||
8 years ago
|
throw err;
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* 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(() => {
|
||
8 years ago
|
return self._git.commit(commitMsg).catch((err) => {
|
||
|
if(_.includes(err.stdout, 'nothing to commit')) { return true; }
|
||
|
});
|
||
8 years ago
|
});
|
||
|
|
||
8 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
})
|
||
|
|
||
8 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* Commits uploads changes.
|
||
|
*
|
||
8 years ago
|
* @param {String} msg The commit message
|
||
8 years ago
|
* @return {Promise} Resolve on commit success
|
||
|
*/
|
||
8 years ago
|
commitUploads(msg) {
|
||
8 years ago
|
|
||
|
let self = this;
|
||
8 years ago
|
msg = msg || "Uploads repository sync";
|
||
8 years ago
|
|
||
|
return self._git.add('uploads').then(() => {
|
||
8 years ago
|
return self._git.commit(msg).catch((err) => {
|
||
8 years ago
|
if(_.includes(err.stdout, 'nothing to commit')) { return true; }
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
}
|
||
|
|
||
|
};
|