2
0

git.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use strict'
  2. const Git = require('git-wrapper2-promise')
  3. const Promise = require('bluebird')
  4. const path = require('path')
  5. const fs = Promise.promisifyAll(require('fs'))
  6. const _ = require('lodash')
  7. const URL = require('url')
  8. /**
  9. * Git Model
  10. */
  11. module.exports = {
  12. _git: null,
  13. _url: '',
  14. _repo: {
  15. path: '',
  16. branch: 'master',
  17. exists: false
  18. },
  19. _signature: {
  20. email: 'wiki@example.com'
  21. },
  22. _opts: {
  23. clone: {},
  24. push: {}
  25. },
  26. onReady: null,
  27. /**
  28. * Initialize Git model
  29. *
  30. * @return {Object} Git model instance
  31. */
  32. init () {
  33. let self = this
  34. // -> Build repository path
  35. if (_.isEmpty(appconfig.paths.repo)) {
  36. self._repo.path = path.join(ROOTPATH, 'repo')
  37. } else {
  38. self._repo.path = appconfig.paths.repo
  39. }
  40. // -> Initialize repository
  41. self.onReady = self._initRepo(appconfig)
  42. // Define signature
  43. if (appconfig.git) {
  44. self._signature.email = appconfig.git.serverEmail || 'wiki@example.com'
  45. }
  46. return self
  47. },
  48. /**
  49. * Initialize Git repository
  50. *
  51. * @param {Object} appconfig The application config
  52. * @return {Object} Promise
  53. */
  54. _initRepo (appconfig) {
  55. let self = this
  56. winston.info('[' + PROCNAME + '.Git] Checking Git repository...')
  57. // -> Check if path is accessible
  58. return fs.mkdirAsync(self._repo.path).catch((err) => {
  59. if (err.code !== 'EEXIST') {
  60. winston.error('[' + PROCNAME + '.Git] Invalid Git repository path or missing permissions.')
  61. }
  62. }).then(() => {
  63. self._git = new Git({ 'git-dir': self._repo.path })
  64. // -> Check if path already contains a git working folder
  65. return self._git.isRepo().then((isRepo) => {
  66. self._repo.exists = isRepo
  67. return (!isRepo) ? self._git.exec('init') : true
  68. }).catch((err) => { // eslint-disable-line handle-callback-err
  69. self._repo.exists = false
  70. })
  71. }).then(() => {
  72. if (appconfig.git === false) {
  73. winston.info('[' + PROCNAME + '.Git] Remote syncing is disabled. Not recommended!')
  74. return Promise.resolve(true)
  75. }
  76. // Initialize remote
  77. let urlObj = URL.parse(appconfig.git.url)
  78. if (appconfig.git.auth.type !== 'ssh') {
  79. urlObj.auth = appconfig.git.auth.username + ':' + appconfig.git.auth.password
  80. }
  81. self._url = URL.format(urlObj)
  82. let gitConfigs = [
  83. () => { return self._git.exec('config', ['--local', 'user.name', 'Wiki']) },
  84. () => { return self._git.exec('config', ['--local', 'user.email', self._signature.email]) },
  85. () => { return self._git.exec('config', ['--local', '--bool', 'http.sslVerify', _.toString(appconfig.git.auth.sslVerify)]) }
  86. ]
  87. if (appconfig.git.auth.type === 'ssh') {
  88. gitConfigs.push(() => {
  89. return self._git.exec('config', ['--local', 'core.sshCommand', 'ssh -i "' + appconfig.git.auth.privateKey + '" -o StrictHostKeyChecking=no'])
  90. })
  91. }
  92. return self._git.exec('remote', 'show').then((cProc) => {
  93. let out = cProc.stdout.toString()
  94. return Promise.each(gitConfigs, fn => { return fn() }).then(() => {
  95. if (!_.includes(out, 'origin')) {
  96. return self._git.exec('remote', ['add', 'origin', self._url])
  97. } else {
  98. return self._git.exec('remote', ['set-url', 'origin', self._url])
  99. }
  100. }).catch(err => {
  101. winston.error(err)
  102. })
  103. })
  104. }).catch((err) => {
  105. winston.error('[' + PROCNAME + '.Git] Git remote error!')
  106. throw err
  107. }).then(() => {
  108. winston.info('[' + PROCNAME + '.Git] Git repository is OK.')
  109. return true
  110. })
  111. },
  112. /**
  113. * Gets the repo path.
  114. *
  115. * @return {String} The repo path.
  116. */
  117. getRepoPath () {
  118. return this._repo.path || path.join(ROOTPATH, 'repo')
  119. },
  120. /**
  121. * Sync with the remote repository
  122. *
  123. * @return {Promise} Resolve on sync success
  124. */
  125. resync () {
  126. let self = this
  127. // Is git remote disabled?
  128. if (appconfig.git === false) {
  129. return Promise.resolve(true)
  130. }
  131. // Fetch
  132. winston.info('[' + PROCNAME + '.Git] Performing pull from remote repository...')
  133. return self._git.pull('origin', self._repo.branch).then((cProc) => {
  134. winston.info('[' + PROCNAME + '.Git] Pull completed.')
  135. })
  136. .catch((err) => {
  137. winston.error('[' + PROCNAME + '.Git] Unable to fetch from git origin!')
  138. throw err
  139. })
  140. .then(() => {
  141. // Check for changes
  142. return self._git.exec('log', 'origin/' + self._repo.branch + '..HEAD').then((cProc) => {
  143. let out = cProc.stdout.toString()
  144. if (_.includes(out, 'commit')) {
  145. winston.info('[' + PROCNAME + '.Git] Performing push to remote repository...')
  146. return self._git.push('origin', self._repo.branch).then(() => {
  147. return winston.info('[' + PROCNAME + '.Git] Push completed.')
  148. })
  149. } else {
  150. winston.info('[' + PROCNAME + '.Git] Push skipped. Repository is already in sync.')
  151. }
  152. return true
  153. })
  154. })
  155. .catch((err) => {
  156. winston.error('[' + PROCNAME + '.Git] Unable to push changes to remote!')
  157. throw err
  158. })
  159. },
  160. /**
  161. * Commits a document.
  162. *
  163. * @param {String} entryPath The entry path
  164. * @return {Promise} Resolve on commit success
  165. */
  166. commitDocument (entryPath, author) {
  167. let self = this
  168. let gitFilePath = entryPath + '.md'
  169. let commitMsg = ''
  170. return self._git.exec('ls-files', gitFilePath).then((cProc) => {
  171. let out = cProc.stdout.toString()
  172. return _.includes(out, gitFilePath)
  173. }).then((isTracked) => {
  174. commitMsg = (isTracked) ? 'Updated ' + gitFilePath : 'Added ' + gitFilePath
  175. return self._git.add(gitFilePath)
  176. }).then(() => {
  177. return self._git.exec('commit', ['-m', commitMsg, '--author="' + author.name + ' <' + author.email + '>"']).catch((err) => {
  178. if (_.includes(err.stdout, 'nothing to commit')) { return true }
  179. })
  180. })
  181. },
  182. /**
  183. * Move a document.
  184. *
  185. * @param {String} entryPath The current entry path
  186. * @param {String} newEntryPath The new entry path
  187. * @return {Promise<Boolean>} Resolve on success
  188. */
  189. moveDocument (entryPath, newEntryPath) {
  190. let self = this
  191. let gitFilePath = entryPath + '.md'
  192. let gitNewFilePath = newEntryPath + '.md'
  193. return self._git.exec('mv', [gitFilePath, gitNewFilePath]).then((cProc) => {
  194. let out = cProc.stdout.toString()
  195. if (_.includes(out, 'fatal')) {
  196. let errorMsg = _.capitalize(_.head(_.split(_.replace(out, 'fatal: ', ''), ',')))
  197. throw new Error(errorMsg)
  198. }
  199. return true
  200. })
  201. },
  202. /**
  203. * Commits uploads changes.
  204. *
  205. * @param {String} msg The commit message
  206. * @return {Promise} Resolve on commit success
  207. */
  208. commitUploads (msg) {
  209. let self = this
  210. msg = msg || 'Uploads repository sync'
  211. return self._git.add('uploads').then(() => {
  212. return self._git.commit(msg).catch((err) => {
  213. if (_.includes(err.stdout, 'nothing to commit')) { return true }
  214. })
  215. })
  216. }
  217. }