Browse Source

🐞 fix: 修复无法获取第一个修改的文件的bug

Pchen. 2 months ago
parent
commit
6acb6e2b4b
1 changed files with 45 additions and 47 deletions
  1. 45 47
      apis/Repos/GetCommitDetail.js

+ 45 - 47
apis/Repos/GetCommitDetail.js

@@ -7,7 +7,6 @@ const simpleGit = require('simple-git')
 class GetCommitDetail extends API {
     constructor() {
         super()
-
         this.setMethod("GET")
         this.setPath("/Repos/GetCommitDetail")
     }
@@ -44,57 +43,56 @@ class GetCommitDetail extends API {
             const git = simpleGit()
             await git.cwd(r[0].path)
 
-            git.show([hash], (err, result) => {
-                if (err)
-                    return res.json({
-                        ...BaseStdResponse.ERR,
-                        msg: '获取提交详情失败!'
+            const showCommitDetails = (hash) => {
+                return new Promise((resolve, reject) => {
+                    git.show([hash], (err, result) => {
+                        if (err) reject(err)
+                        resolve(result)
                     })
+                })
+            }
 
-                console.log(result)
-
-                // 修改正则表达式,捕获多行 commit message(包括 body)
-                const commitRegex = /^commit (\w+)\nAuthor: (.+) <(.+)>\nDate:\s+(.+)\n\n([\s\S]+?)(?=diff --git|$)/;
-                const match = result.match(commitRegex)
-                if (!match)
-                    return res.json({
-                        ...BaseStdResponse.ERR,
-                        msg: '解析提交详情失败!'
-                    })
-
-                // 拆分 message 和 body
-                const fullMessage = match[5].trim().split("\n");
-                const message = fullMessage[0]; // 第一行短描述
-                const body = fullMessage.slice(1).join("\n").trim(); // 多行 body
-
-                const commitInfo = {
-                    hash: match[1],
-                    author: match[2],
-                    email: match[3],
-                    date: match[4],
-                    message,
-                    body,
-                    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')
-                    })
-                }
+            const result = await showCommitDetails(hash)
 
-                console.log(commitInfo)
+            // 更新正则表达式,捕获多行 commit message(包括 body)
+            const commitRegex = /^commit (\w+)\nAuthor: (.+) <(.+)>\nDate:\s+(.+)\n\n([\s\S]+?)(?=\ndiff --git|$)/
+            const match = result.match(commitRegex)
+            if (!match)
+                return res.json({
+                    ...BaseStdResponse.ERR,
+                    msg: '解析提交详情失败!'
+                })
 
-                res.json({
-                    ...BaseStdResponse.OK,
-                    data: commitInfo
+            // 拆分 message 和 body
+            const fullMessage = match[5].trim().split("\n")
+            const message = fullMessage[0] // 第一行短描述
+            const body = fullMessage.slice(1).join("\n").trim() // 多行 body
+
+            const commitInfo = {
+                hash: match[1],
+                author: match[2],
+                email: match[3],
+                date: match[4],
+                message,
+                body,
+                files: [],
+                diffs: []
+            }
+
+            // 解析 diff 信息
+            const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=(diff --git a\/|$))/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)