GetCommitDetail.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. console.log(result)
  45. // 修改正则表达式,捕获多行 commit message(包括 body)
  46. const commitRegex = /^commit (\w+)\nAuthor: (.+) <(.+)>\nDate:\s+(.+)\n\n([\s\S]+?)(?=diff --git|$)/;
  47. const match = result.match(commitRegex)
  48. if (!match)
  49. return res.json({
  50. ...BaseStdResponse.ERR,
  51. msg: '解析提交详情失败!'
  52. })
  53. // 拆分 message 和 body
  54. const fullMessage = match[5].trim().split("\n");
  55. const message = fullMessage[0]; // 第一行短描述
  56. const body = fullMessage.slice(1).join("\n").trim(); // 多行 body
  57. const commitInfo = {
  58. hash: match[1],
  59. author: match[2],
  60. email: match[3],
  61. date: match[4],
  62. message,
  63. body,
  64. files: [],
  65. diffs: []
  66. }
  67. // 解析 diff 信息
  68. const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=diff --git a\/|\Z)/g
  69. let diffMatch
  70. while ((diffMatch = diffRegex.exec(result)) !== null) {
  71. commitInfo.files.push(diffMatch[1])
  72. commitInfo.diffs.push({
  73. file: diffMatch[1],
  74. changes: diffMatch[2].trim().split('\n')
  75. })
  76. }
  77. console.log(commitInfo)
  78. res.json({
  79. ...BaseStdResponse.OK,
  80. data: commitInfo
  81. })
  82. })
  83. } catch (error) {
  84. this.logger.error('获取提交详情失败!' + error.stack)
  85. return res.json({
  86. ...BaseStdResponse.ERR,
  87. msg: '获取提交详情失败!'
  88. })
  89. }
  90. }
  91. }
  92. module.exports.GetCommitDetail = GetCommitDetail