mirror of https://github.com/Requarks/wiki.git
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.
183 lines
3.9 KiB
183 lines
3.9 KiB
"use strict";
|
|
|
|
var Git = require("git-wrapper2-promise"),
|
|
Promise = require('bluebird'),
|
|
path = require('path'),
|
|
os = require('os'),
|
|
fs = Promise.promisifyAll(require("fs")),
|
|
moment = require('moment'),
|
|
_ = require('lodash'),
|
|
URL = require('url');
|
|
|
|
/**
|
|
* Git Model
|
|
*/
|
|
module.exports = {
|
|
|
|
_git: null,
|
|
_url: '',
|
|
_repo: {
|
|
path: '',
|
|
branch: 'master',
|
|
exists: false,
|
|
sync: true
|
|
},
|
|
_signature: {
|
|
name: 'Wiki',
|
|
email: 'user@example.com'
|
|
},
|
|
_opts: {
|
|
clone: {},
|
|
push: {}
|
|
},
|
|
|
|
/**
|
|
* Initialize Git model
|
|
*
|
|
* @param {Object} appconfig The application config
|
|
* @return {Object} Git model instance
|
|
*/
|
|
init(appconfig) {
|
|
|
|
let self = this;
|
|
|
|
//-> Build repository path
|
|
|
|
if(_.isEmpty(appconfig.datadir.repo)) {
|
|
self._repo.path = path.join(ROOTPATH, 'repo');
|
|
} else {
|
|
self._repo.path = appconfig.datadir.repo;
|
|
}
|
|
|
|
//-> Initialize repository
|
|
|
|
self._initRepo(appconfig).then((repo) => {
|
|
|
|
if(self._repo.sync) {
|
|
self.resync();
|
|
}
|
|
|
|
});
|
|
|
|
// Define signature
|
|
|
|
self._signature.name = appconfig.git.signature.name || 'Wiki';
|
|
self._signature.email = appconfig.git.signature.email || 'user@example.com';
|
|
|
|
return self;
|
|
|
|
},
|
|
|
|
/**
|
|
* Initialize Git repository
|
|
*
|
|
* @param {Object} appconfig The application config
|
|
* @return {Object} Promise
|
|
*/
|
|
_initRepo(appconfig) {
|
|
|
|
let self = this;
|
|
|
|
winston.info('[GIT] Checking Git repository...');
|
|
|
|
//-> Check if path is accessible
|
|
|
|
return fs.mkdirAsync(self._repo.path).catch((err) => {
|
|
if(err.code !== 'EEXIST') {
|
|
winston.error('Invalid Git repository path or missing permissions.');
|
|
}
|
|
}).then(() => {
|
|
|
|
self._git = new Git({ 'git-dir': self._repo.path });
|
|
|
|
//-> Check if path already contains a git working folder
|
|
|
|
return self._git.isRepo().then((isRepo) => {
|
|
self._repo.exists = isRepo;
|
|
return (!isRepo) ? self._git.exec('init') : true;
|
|
}).catch((err) => {
|
|
self._repo.exists = false;
|
|
});
|
|
|
|
}).then(() => {
|
|
|
|
// 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]);
|
|
})
|
|
}
|
|
});
|
|
|
|
}).catch((err) => {
|
|
winston.error('Git remote error!');
|
|
throw err;
|
|
}).then(() => {
|
|
winston.info('[GIT] Git repository is now ready.');
|
|
return true;
|
|
});
|
|
|
|
},
|
|
|
|
resync() {
|
|
|
|
let self = this;
|
|
|
|
// Fetch
|
|
|
|
winston.info('[GIT] Performing pull from remote repository...');
|
|
return self._git.pull('origin', self._repo.branch).then((cProc) => {
|
|
winston.info('[GIT] Pull completed.');
|
|
})
|
|
.catch((err) => {
|
|
winston.error('Unable to fetch from git origin!');
|
|
throw err;
|
|
})
|
|
.then(() => {
|
|
|
|
// Check for changes
|
|
|
|
return self._git.exec('status').then((cProc) => {
|
|
let out = cProc.stdout.toString();
|
|
if(!_.includes(out, 'nothing to commit')) {
|
|
|
|
// Add, commit and push
|
|
|
|
winston.info('[GIT] Performing push to remote repository...');
|
|
return self._git.add('-A').then(() => {
|
|
return self._git.commit("Resync");
|
|
}).then(() => {
|
|
return self._git.push('origin', self._repo.branch);
|
|
}).then(() => {
|
|
return winston.info('[GIT] Push completed.');
|
|
});
|
|
|
|
} else {
|
|
winston.info('[GIT] Repository is already up to date. Nothing to commit.');
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
})
|
|
.catch((err) => {
|
|
winston.error('Unable to push changes to remote!');
|
|
throw err;
|
|
});
|
|
|
|
}
|
|
|
|
};
|