|
@@ -0,0 +1,97 @@
|
|
|
+const API = require("../../lib/API")
|
|
|
+const AccessControl = require("../../lib/AccessControl")
|
|
|
+const { BaseStdResponse } = require("../../BaseStdResponse")
|
|
|
+const db = require("../../plugin/DataBase/db")
|
|
|
+const simpleGit = require('simple-git')
|
|
|
+
|
|
|
+class GetCommitDetail extends API {
|
|
|
+ constructor() {
|
|
|
+ super()
|
|
|
+
|
|
|
+ this.setMethod("GET")
|
|
|
+ this.setPath("/Repos/GetCommitDetail")
|
|
|
+ }
|
|
|
+
|
|
|
+ async onRequest(req, res) {
|
|
|
+ let { uuid, session, id, hash } = req.query
|
|
|
+
|
|
|
+ if ([uuid, session, id, hash].some(value => value === '' || value === null || value === undefined))
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.MISSING_PARAMETER
|
|
|
+ })
|
|
|
+
|
|
|
+ // 检查 session
|
|
|
+ if (!await AccessControl.checkSession(uuid, session))
|
|
|
+ return res.status(401).json({
|
|
|
+ ...BaseStdResponse.ACCESS_DENIED
|
|
|
+ })
|
|
|
+
|
|
|
+ let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
|
|
|
+ let r = await db.query(sql, [uuid, id])
|
|
|
+ if (!r || r.length === 0)
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '未找到仓库'
|
|
|
+ })
|
|
|
+
|
|
|
+ if (r[0].state !== 1 || !r[0].path)
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '仓库未成功克隆!'
|
|
|
+ })
|
|
|
+
|
|
|
+ try {
|
|
|
+ const git = simpleGit()
|
|
|
+ await git.cwd(r[0].path)
|
|
|
+
|
|
|
+ git.show([hash], (err, result) => {
|
|
|
+ if (err)
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '获取提交详情失败!'
|
|
|
+ })
|
|
|
+
|
|
|
+ const commitRegex = /^commit (\w+)\nAuthor: (.+)\nDate:\s+(.+)\n\n\s*(.+)/
|
|
|
+ const match = result.match(commitRegex)
|
|
|
+ if (!match)
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '解析提交详情失败!'
|
|
|
+ })
|
|
|
+
|
|
|
+ const commitInfo = {
|
|
|
+ hash: match[1],
|
|
|
+ author: match[2],
|
|
|
+ date: match[3],
|
|
|
+ message: match[4],
|
|
|
+ files: [],
|
|
|
+ diffs: []
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 diff 信息
|
|
|
+ const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=diff --git a\/|\Z)/g
|
|
|
+ let diffMatch
|
|
|
+ while ((diffMatch = diffRegex.exec(result)) !== null) {
|
|
|
+ commitInfo.files.push(diffMatch[1])
|
|
|
+ commitInfo.diffs.push({
|
|
|
+ file: diffMatch[1],
|
|
|
+ changes: diffMatch[2].trim().split('\n')
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ res.json({
|
|
|
+ ...BaseStdResponse.OK,
|
|
|
+ data: commitInfo
|
|
|
+ })
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ this.logger.error('获取提交详情失败!' + error.stack)
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '获取提交详情失败!'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports.GetCommitDetail = GetCommitDetail
|