GetCommitDetail.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const API = require("../../lib/API")
  2. const AccessControl = require("../../lib/AccessControl")
  3. const { BaseStdResponse } = require("../../BaseStdResponse")
  4. const db = require("../../plugin/DataBase/db")
  5. const simpleGit = require('simple-git')
  6. class GetCommitDetail extends API {
  7. constructor() {
  8. super()
  9. this.setMethod("GET")
  10. this.setPath("/Repos/GetCommitDetail")
  11. }
  12. async onRequest(req, res) {
  13. let { uuid, session, id, hash } = req.query
  14. if ([uuid, session, id, hash].some(value => value === '' || value === null || value === undefined))
  15. return res.json({
  16. ...BaseStdResponse.MISSING_PARAMETER
  17. })
  18. // 检查 session
  19. if (!await AccessControl.checkSession(uuid, session))
  20. return res.status(401).json({
  21. ...BaseStdResponse.ACCESS_DENIED
  22. })
  23. let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
  24. let r = await db.query(sql, [uuid, id])
  25. if (!r || r.length === 0)
  26. return res.json({
  27. ...BaseStdResponse.ERR,
  28. msg: '未找到仓库'
  29. })
  30. if (r[0].state !== 1 || !r[0].path)
  31. return res.json({
  32. ...BaseStdResponse.ERR,
  33. msg: '仓库未成功克隆!'
  34. })
  35. try {
  36. const git = simpleGit()
  37. await git.cwd(r[0].path)
  38. git.show([hash], (err, result) => {
  39. if (err)
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '获取提交详情失败!'
  43. })
  44. // 修改正则表达式,捕获多行 commit message(包括 body)
  45. const commitRegex = /^commit (\w+)\nAuthor: (.+) <(.+)>\nDate:\s+(.+)\n\n([\s\S]+?)(?=diff --git|$)/;
  46. const match = result.match(commitRegex)
  47. if (!match)
  48. return res.json({
  49. ...BaseStdResponse.ERR,
  50. msg: '解析提交详情失败!'
  51. })
  52. // 拆分 message 和 body
  53. const fullMessage = match[5].trim().split("\n");
  54. const message = fullMessage[0]; // 第一行短描述
  55. const body = fullMessage.slice(1).join("\n").trim(); // 多行 body
  56. const commitInfo = {
  57. hash: match[1],
  58. author: match[2],
  59. email: match[3],
  60. date: match[4],
  61. message,
  62. body,
  63. files: [],
  64. diffs: []
  65. }
  66. // 解析 diff 信息
  67. const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=diff --git a\/|\Z)/g
  68. let diffMatch
  69. while ((diffMatch = diffRegex.exec(result)) !== null) {
  70. commitInfo.files.push(diffMatch[1])
  71. commitInfo.diffs.push({
  72. file: diffMatch[1],
  73. changes: diffMatch[2].trim().split('\n')
  74. })
  75. }
  76. res.json({
  77. ...BaseStdResponse.OK,
  78. data: commitInfo
  79. })
  80. })
  81. } catch (error) {
  82. this.logger.error('获取提交详情失败!' + error.stack)
  83. return res.json({
  84. ...BaseStdResponse.ERR,
  85. msg: '获取提交详情失败!'
  86. })
  87. }
  88. }
  89. }
  90. module.exports.GetCommitDetail = GetCommitDetail