GetCommitDetail.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. const showCommitDetails = (hash) => {
  39. return new Promise((resolve, reject) => {
  40. git.show([hash], (err, result) => {
  41. if (err) reject(err)
  42. resolve(result)
  43. })
  44. })
  45. }
  46. const result = await showCommitDetails(hash)
  47. // 更新正则表达式,捕获多行 commit message(包括 body)
  48. const commitRegex = /^commit (\w+)\nAuthor: (.+) <(.+)>\nDate:\s+(.+)\n\n([\s\S]+?)(?=\ndiff --git|$)/
  49. const match = result.match(commitRegex)
  50. if (!match)
  51. return res.json({
  52. ...BaseStdResponse.ERR,
  53. msg: '解析提交详情失败!'
  54. })
  55. // 拆分 message 和 body
  56. const fullMessage = match[5].trim().split("\n")
  57. const message = fullMessage[0] // 第一行短描述
  58. const body = fullMessage.slice(1).join("\n").trim() // 多行 body
  59. const commitInfo = {
  60. hash: match[1],
  61. author: match[2],
  62. email: match[3],
  63. date: match[4],
  64. message,
  65. body,
  66. files: [],
  67. diffs: []
  68. }
  69. // 检查是否为合并提交
  70. const isMergeCommit = message.startsWith('Merge')
  71. if (isMergeCommit) {
  72. // 合并提交处理:如果是合并提交,直接返回合并信息
  73. commitInfo.mergeInfo = {
  74. message: message, // 这里返回合并提交的信息
  75. body: body // 可能包含合并的详情
  76. }
  77. } else {
  78. // 解析 diff 信息
  79. const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=(diff --git a\/|$))/g
  80. let diffMatch
  81. while ((diffMatch = diffRegex.exec(result)) !== null) {
  82. commitInfo.files.push(diffMatch[1])
  83. commitInfo.diffs.push({
  84. file: diffMatch[1],
  85. changes: diffMatch[2].trim().split('\n')
  86. })
  87. }
  88. }
  89. res.json({
  90. ...BaseStdResponse.OK,
  91. data: commitInfo
  92. })
  93. } catch (error) {
  94. this.logger.error('获取提交详情失败!' + error.stack)
  95. return res.json({
  96. ...BaseStdResponse.ERR,
  97. msg: '获取提交详情失败!'
  98. })
  99. }
  100. }
  101. }
  102. module.exports.GetCommitDetail = GetCommitDetail