2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
2016-08-20 04:50:29 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
const Git = require('git-wrapper2-promise')
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = Promise.promisifyAll(require('fs'))
|
|
|
|
const _ = require('lodash')
|
|
|
|
const URL = require('url')
|
2016-08-20 04:50:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Git Model
|
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
_git: null,
|
|
|
|
_url: '',
|
|
|
|
_repo: {
|
|
|
|
path: '',
|
|
|
|
branch: 'master',
|
|
|
|
exists: false
|
|
|
|
},
|
|
|
|
_signature: {
|
2017-04-26 02:04:11 +00:00
|
|
|
email: 'wiki@example.com'
|
2017-02-09 01:52:37 +00:00
|
|
|
},
|
|
|
|
_opts: {
|
|
|
|
clone: {},
|
|
|
|
push: {}
|
|
|
|
},
|
|
|
|
onReady: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Git model
|
|
|
|
*
|
|
|
|
* @return {Object} Git model instance
|
|
|
|
*/
|
|
|
|
init () {
|
|
|
|
let self = this
|
|
|
|
|
|
|
|
// -> Build repository path
|
|
|
|
|
|
|
|
if (_.isEmpty(appconfig.paths.repo)) {
|
|
|
|
self._repo.path = path.join(ROOTPATH, 'repo')
|
|
|
|
} else {
|
|
|
|
self._repo.path = appconfig.paths.repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Initialize repository
|
|
|
|
|
|
|
|
self.onReady = self._initRepo(appconfig)
|
|
|
|
|
|
|
|
// Define signature
|
|
|
|
|
2017-02-12 03:54:27 +00:00
|
|
|
if (appconfig.git) {
|
2017-04-26 02:04:11 +00:00
|
|
|
self._signature.email = appconfig.git.serverEmail || 'wiki@example.com'
|
2017-02-12 03:54:27 +00:00
|
|
|
}
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
return self
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Git repository
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Object} Promise
|
|
|
|
*/
|
|
|
|
_initRepo (appconfig) {
|
|
|
|
let self = this
|
|
|
|
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Checking Git repository...')
|
2017-02-09 01:52:37 +00:00
|
|
|
|
|
|
|
// -> Check if path is accessible
|
|
|
|
|
|
|
|
return fs.mkdirAsync(self._repo.path).catch((err) => {
|
|
|
|
if (err.code !== 'EEXIST') {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.error('Invalid Git repository path or missing permissions.')
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
}).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) => { // eslint-disable-line handle-callback-err
|
|
|
|
self._repo.exists = false
|
|
|
|
})
|
|
|
|
}).then(() => {
|
2017-02-12 03:54:27 +00:00
|
|
|
if (appconfig.git === false) {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Remote Git syncing is disabled. Not recommended!')
|
2017-02-12 03:54:27 +00:00
|
|
|
return Promise.resolve(true)
|
|
|
|
}
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
// Initialize remote
|
|
|
|
|
|
|
|
let urlObj = URL.parse(appconfig.git.url)
|
2017-02-11 21:21:50 +00:00
|
|
|
if (appconfig.git.auth.type !== 'ssh') {
|
|
|
|
urlObj.auth = appconfig.git.auth.username + ':' + appconfig.git.auth.password
|
|
|
|
}
|
2017-02-09 01:52:37 +00:00
|
|
|
self._url = URL.format(urlObj)
|
|
|
|
|
2017-02-11 21:21:50 +00:00
|
|
|
let gitConfigs = [
|
2017-04-26 02:04:11 +00:00
|
|
|
() => { return self._git.exec('config', ['--local', 'user.name', 'Wiki']) },
|
2017-02-11 21:21:50 +00:00
|
|
|
() => { return self._git.exec('config', ['--local', 'user.email', self._signature.email]) },
|
|
|
|
() => { return self._git.exec('config', ['--local', '--bool', 'http.sslVerify', _.toString(appconfig.git.auth.sslVerify)]) }
|
|
|
|
]
|
|
|
|
|
|
|
|
if (appconfig.git.auth.type === 'ssh') {
|
|
|
|
gitConfigs.push(() => {
|
|
|
|
return self._git.exec('config', ['--local', 'core.sshCommand', 'ssh -i "' + appconfig.git.auth.privateKey + '" -o StrictHostKeyChecking=no'])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
return self._git.exec('remote', 'show').then((cProc) => {
|
|
|
|
let out = cProc.stdout.toString()
|
2017-04-16 16:15:09 +00:00
|
|
|
return Promise.each(gitConfigs, fn => { return fn() }).then(() => {
|
|
|
|
if (!_.includes(out, 'origin')) {
|
2017-02-09 01:52:37 +00:00
|
|
|
return self._git.exec('remote', ['add', 'origin', self._url])
|
2017-04-16 16:15:09 +00:00
|
|
|
} else {
|
|
|
|
return self._git.exec('remote', ['set-url', 'origin', self._url])
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
winston.error(err)
|
|
|
|
})
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
|
|
|
}).catch((err) => {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.error('Git remote error!')
|
2017-02-09 01:52:37 +00:00
|
|
|
throw err
|
|
|
|
}).then(() => {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Git repository is OK.')
|
2017-02-09 01:52:37 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the repo path.
|
|
|
|
*
|
|
|
|
* @return {String} The repo path.
|
|
|
|
*/
|
|
|
|
getRepoPath () {
|
|
|
|
return this._repo.path || path.join(ROOTPATH, 'repo')
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync with the remote repository
|
|
|
|
*
|
|
|
|
* @return {Promise} Resolve on sync success
|
|
|
|
*/
|
|
|
|
resync () {
|
|
|
|
let self = this
|
|
|
|
|
2017-02-12 03:54:27 +00:00
|
|
|
// Is git remote disabled?
|
|
|
|
|
|
|
|
if (appconfig.git === false) {
|
|
|
|
return Promise.resolve(true)
|
|
|
|
}
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
// Fetch
|
|
|
|
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Performing pull from remote Git repository...')
|
2017-02-09 01:52:37 +00:00
|
|
|
return self._git.pull('origin', self._repo.branch).then((cProc) => {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Git Pull completed.')
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.error('Unable to fetch from git origin!')
|
2017-02-09 01:52:37 +00:00
|
|
|
throw err
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// Check for changes
|
|
|
|
|
|
|
|
return self._git.exec('log', 'origin/' + self._repo.branch + '..HEAD').then((cProc) => {
|
|
|
|
let out = cProc.stdout.toString()
|
|
|
|
|
|
|
|
if (_.includes(out, 'commit')) {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Performing push to remote Git repository...')
|
2017-02-09 01:52:37 +00:00
|
|
|
return self._git.push('origin', self._repo.branch).then(() => {
|
2017-04-28 22:46:27 +00:00
|
|
|
return winston.info('Git Push completed.')
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
|
|
|
} else {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.info('Git Push skipped. Repository is already in sync.')
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-04-28 22:46:27 +00:00
|
|
|
winston.error('Unable to push changes to remote Git repository!')
|
2017-02-09 01:52:37 +00:00
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits a document.
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise} Resolve on commit success
|
|
|
|
*/
|
2017-04-28 21:58:38 +00:00
|
|
|
commitDocument (entryPath, author) {
|
2017-02-09 01:52:37 +00:00
|
|
|
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(() => {
|
2017-04-28 21:58:38 +00:00
|
|
|
return self._git.exec('commit', ['-m', commitMsg, '--author="' + author.name + ' <' + author.email + '>"']).catch((err) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (_.includes(err.stdout, 'nothing to commit')) { return true }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits uploads changes.
|
|
|
|
*
|
|
|
|
* @param {String} msg The commit message
|
|
|
|
* @return {Promise} Resolve on commit success
|
|
|
|
*/
|
|
|
|
commitUploads (msg) {
|
|
|
|
let self = this
|
|
|
|
msg = msg || 'Uploads repository sync'
|
|
|
|
|
|
|
|
return self._git.add('uploads').then(() => {
|
|
|
|
return self._git.commit(msg).catch((err) => {
|
|
|
|
if (_.includes(err.stdout, 'nothing to commit')) { return true }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|