GitCodeStats.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 redis = require('../../plugin/DataBase/Redis')
  6. const simpleGit = require('simple-git')
  7. const fs = require("fs")
  8. const path = require("path")
  9. const { isBinaryFileSync } = require('isbinaryfile')
  10. class gitCodeStats extends API {
  11. constructor() {
  12. super()
  13. this.setMethod("GET")
  14. this.setPath("/Repos/CodeStats")
  15. }
  16. // 定义支持的语言后缀
  17. languageExtensions = {
  18. "JavaScript": [".js", ".jsx", ".mjs", ".cjs"],
  19. "TypeScript": [".ts", ".tsx"],
  20. "Python": [".py"],
  21. "Java": [".java"],
  22. "C": [".c"],
  23. "C++": [".cpp", ".cc", ".cxx", ".h", ".hpp"],
  24. "C#": [".cs"],
  25. "Go": [".go"],
  26. "PHP": [".php"],
  27. "HTML": [".html", ".htm"],
  28. "CSS": [".css"],
  29. "SCSS": [".scss", ".sass"],
  30. "JSON": [".json"],
  31. "YAML": [".yml", ".yaml"],
  32. "Markdown": [".md"],
  33. "Shell": [".sh"],
  34. "Bash": [".bash"],
  35. "PowerShell": [".ps1"],
  36. "Ruby": [".rb"],
  37. "Vue": [".vue"], // Vue文件
  38. "LESS": [".less"], // LESS样式文件
  39. "Sass": [".sass"], // Sass文件
  40. "Haml": [".haml"], // Haml文件
  41. "Elixir": [".ex", ".exs"], // Elixir语言文件
  42. "Erlang": [".erl", ".hrl"], // Erlang语言文件
  43. "Swift": [".swift"], // Swift语言文件
  44. "Kotlin": [".kt", ".kts"], // Kotlin语言文件
  45. "Rust": [".rs"], // Rust语言文件
  46. "Dart": [".dart"], // Dart语言文件
  47. "Lua": [".lua"], // Lua语言文件
  48. "R": [".r"], // R语言文件
  49. "Perl": [".pl"], // Perl语言文件
  50. "SQL": [".sql"], // SQL文件
  51. "Dockerfile": [".dockerfile", "Dockerfile"], // Dockerfile
  52. "GraphQL": [".graphql", ".gql"], // GraphQL文件
  53. "JSON5": [".json5"], // JSON5文件
  54. "TOML": [".toml"], // TOML文件
  55. "Text": [".txt", ".text"], // 文本文件
  56. "Assembly": [".asm", ".s"],
  57. "Objective-C": [".m", ".mm"],
  58. "F#": [".fs", ".fsi", ".fsx", ".fsscript"],
  59. "Haskell": [".hs", ".lhs"],
  60. "OCaml": [".ml", ".mli"],
  61. "Nim": [".nim", ".nims"],
  62. "Julia": [".jl"],
  63. "Fortran": [".f", ".for", ".f90", ".f95"],
  64. "COBOL": [".cbl", ".cob"],
  65. "VHDL": [".vhd", ".vhdl"],
  66. "Verilog": [".v", ".vh"],
  67. "Ada": [".adb", ".ads"],
  68. "Matlab": [".m"], // 注意 Objective-C 也使用 .m,可根据上下文判断
  69. "Scala": [".scala"],
  70. "VB.NET": [".vb"],
  71. "Groovy": [".groovy", ".gvy", ".gy", ".gsh"],
  72. "Makefile": ["Makefile", "makefile", ".mk"],
  73. "Clojure": [".clj", ".cljs", ".cljc", ".edn"],
  74. "Common Lisp": [".lisp", ".lsp"],
  75. "Scheme": [".scm", ".ss"],
  76. "Prolog": [".pl", ".pro", ".P"],
  77. "Smalltalk": [".st", ".squeak"],
  78. "Tcl": [".tcl"],
  79. "Crystal": [".cr"],
  80. "Solidity": [".sol"],
  81. "Vim script": [".vim"],
  82. "ReScript": [".res", ".resi"],
  83. "ReasonML": [".re", ".rei"],
  84. "Pug": [".pug", ".jade"],
  85. "Handlebars": [".hbs", ".handlebars"],
  86. "XML": [".xml"],
  87. "INI": [".ini", ".cfg", ".conf"],
  88. "Log": [".log"],
  89. "ENV": [".env"],
  90. "ReStructuredText": [".rst"],
  91. "AsciiDoc": [".adoc", ".asciidoc"],
  92. "Racket": [".rkt"],
  93. "Zig": [".zig"],
  94. "Haxe": [".hx"],
  95. "Dotenv": [".env"],
  96. "Config": [".config"],
  97. "PlantUML": [".puml", ".plantuml"],
  98. "Visual Basic": [".bas", ".vbs"],
  99. "Batch": [".bat", ".cmd"],
  100. "BibTeX": [".bib"],
  101. "TeX/LaTeX": [".tex", ".ltx", ".sty", ".cls"],
  102. "Apache": [".htaccess", "httpd.conf"],
  103. "NGINX": ["nginx.conf"],
  104. "Terraform": [".tf", ".tfvars"],
  105. "HCL": [".hcl"],
  106. "QML": [".qml"],
  107. "Cue": [".cue"],
  108. "GDScript": [".gd"], // 用于 Godot 引擎
  109. "ANTLR": [".g4"],
  110. "Pascal": [".pas"],
  111. "Logtalk": [".lgt"],
  112. "Awk": [".awk"],
  113. "Sed": [".sed"],
  114. "ConfigScript": [".conf", ".cfg"],
  115. "YANG": [".yang"],
  116. "NetLogo": [".nlogo"],
  117. "Other": [".bin", ".dat", ".exe", ".dll", ".obj", ".so", ".class"] // 二进制及无法识别的文件
  118. }
  119. // 判断文件是否为二进制文件
  120. isBinaryFile(filePath) {
  121. try {
  122. return isBinaryFileSync(filePath)
  123. } catch (err) {
  124. return false
  125. }
  126. }
  127. // 统计单个文件的行数
  128. countLines(filePath) {
  129. return new Promise((resolve) => {
  130. if (this.isBinaryFile(filePath)) {
  131. return resolve(0) // 二进制文件直接跳过
  132. }
  133. let lineCount = 0;
  134. const stream = fs.createReadStream(filePath, 'utf8')
  135. stream.on("data", (chunk) => {
  136. lineCount += chunk.toString().split("\n").length
  137. });
  138. stream.on("end", () => resolve(lineCount))
  139. stream.on("error", () => resolve(0)) // 读取错误默认返回 0
  140. })
  141. }
  142. async onRequest(req, res) {
  143. let { uuid, session, id } = req.query
  144. if ([uuid, session, id].some(value => value === '' || value === null || value === undefined))
  145. return res.json({
  146. ...BaseStdResponse.MISSING_PARAMETER
  147. })
  148. // 检查 session
  149. if (!await AccessControl.checkSession(uuid, session))
  150. return res.status(401).json({
  151. ...BaseStdResponse.ACCESS_DENIED
  152. })
  153. let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
  154. let r = await db.query(sql, [uuid, id])
  155. if (!r || r.length === 0)
  156. return res.json({
  157. ...BaseStdResponse.ERR,
  158. msg: '未找到仓库'
  159. })
  160. if (r[0].state !== 1 || !r[0].path)
  161. return res.json({
  162. ...BaseStdResponse.ERR,
  163. msg: '仓库未成功克隆!'
  164. })
  165. const cachedCodeStats = await redis.get(`codeStats:${r[0].path}`)
  166. if(cachedCodeStats)
  167. return res.json({
  168. ...BaseStdResponse.OK,
  169. data: JSON.parse(cachedCodeStats)
  170. })
  171. let totalLines = 0
  172. let languageStats = {}
  173. try {
  174. const git = simpleGit(r[0].path)
  175. const files = await git.raw(["ls-files"]) // 获取所有 Git 跟踪的文件
  176. const fileList = files.split("\n").map(f => f.trim()).filter(f => f)
  177. for (const file of fileList) {
  178. const fullPath = path.join(r[0].path, file)
  179. const ext = path.extname(file)
  180. const language = Object.keys(this.languageExtensions).find(lang =>
  181. this.languageExtensions[lang].includes(ext)
  182. )
  183. if (language) {
  184. const lines = await this.countLines(fullPath)
  185. totalLines += lines
  186. languageStats[language] = (languageStats[language] || 0) + lines
  187. } else {
  188. // 如果文件不属于任何已知语言,归类为其他
  189. const lines = await this.countLines(fullPath)
  190. totalLines += lines
  191. languageStats["Other"] = (languageStats["Other"] || 0) + lines
  192. }
  193. }
  194. } catch (error) {
  195. this.logger.error('获取仓库代码信息失败!' + error.stack)
  196. return res.json({
  197. ...BaseStdResponse.ERR,
  198. msg: '获取仓库代码信息失败!'
  199. })
  200. }
  201. const data = { totalLines, languageStats }
  202. res.json({
  203. ...BaseStdResponse.OK,
  204. data
  205. })
  206. redis.set(`codeStats:${r[0].path}`, JSON.stringify(data), {
  207. EX: 172800
  208. })
  209. }
  210. }
  211. module.exports.gitCodeStats = gitCodeStats