Browse Source

✨ feat: 获取文件内容时支持传入hash

Pchen. 2 months ago
parent
commit
dcf24c0769
1 changed files with 7 additions and 7 deletions
  1. 7 7
      apis/Repos/GetFileDetail.js

+ 7 - 7
apis/Repos/GetFileDetail.js

@@ -9,7 +9,6 @@ const simpleGit = require('simple-git')
 class GetFileDetail extends API {
     constructor() {
         super()
-
         this.setMethod("GET")
         this.setPath("/Repos/GetFileDetail")
     }
@@ -23,9 +22,9 @@ class GetFileDetail extends API {
     }
 
     async onRequest(req, res) {
-        let { uuid, session, id, filePath } = req.query
+        let { uuid, session, id, filePath, hash } = req.query
 
-        if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
+        if ([uuid, session, id, filePath].some(value => value === '' || value === null || value === undefined))
             return res.json({
                 ...BaseStdResponse.MISSING_PARAMETER
             })
@@ -53,6 +52,7 @@ class GetFileDetail extends API {
         try {
             const git = simpleGit()
             await git.cwd(r[0].path)
+            const targetHash = hash || 'HEAD'
             const log = await git.log({
                 file: filePath,
                 n: 1,
@@ -63,18 +63,18 @@ class GetFileDetail extends API {
                     message: '%B',
                     author_name: '%aN',
                     email: '%aE',
-                    date: '%ad',
+                    date: '%ad'
                 }
             })
 
-            if(this.isBinaryFile(path.join(r[0].path, filePath))) 
+            if (this.isBinaryFile(path.join(r[0].path, filePath)))
                 return res.json({
                     ...BaseStdResponse.OK,
                     type: 'binary',
                     commit: log
                 })
 
-            const content = await git.show([`HEAD:${filePath}`])
+            const content = await git.show([`${targetHash}:${filePath}`])
             res.json({
                 ...BaseStdResponse.OK,
                 type: 'text',
@@ -91,4 +91,4 @@ class GetFileDetail extends API {
     }
 }
 
-module.exports.GetFileDetail = GetFileDetail
+module.exports.GetFileDetail = GetFileDetail