|
@@ -94,45 +94,51 @@ class gitCodeStats extends API {
|
|
|
...BaseStdResponse.ACCESS_DENIED
|
|
|
})
|
|
|
|
|
|
- let sql = 'SELECT path FROM repos WHERE create_user = ? AND id = ?'
|
|
|
+ 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)
|
|
|
+ if (!r || r.length === 0)
|
|
|
return res.json({
|
|
|
...BaseStdResponse.ERR,
|
|
|
msg: '未找到仓库'
|
|
|
})
|
|
|
|
|
|
- if (r.state !== 1 || !r[0].path === 0)
|
|
|
+ if (r[0].state !== 1 || !r[0].path)
|
|
|
return res.json({
|
|
|
...BaseStdResponse.ERR,
|
|
|
msg: '仓库未成功克隆!'
|
|
|
})
|
|
|
|
|
|
- const git = simpleGit(r[0].path)
|
|
|
- const files = await git.raw(["ls-files"]) // 获取所有 Git 跟踪的文件
|
|
|
- const fileList = files.split("\n").map(f => f.trim()).filter(f => f)
|
|
|
-
|
|
|
let totalLines = 0
|
|
|
let languageStats = {}
|
|
|
-
|
|
|
- for (const file of fileList) {
|
|
|
- const fullPath = path.join(r[0].path, file)
|
|
|
- const ext = path.extname(file)
|
|
|
- const language = Object.keys(this.languageExtensions).find(lang =>
|
|
|
- this.languageExtensions[lang].includes(ext)
|
|
|
- );
|
|
|
-
|
|
|
- if (language) {
|
|
|
- const lines = await countLines(fullPath)
|
|
|
- totalLines += lines
|
|
|
- languageStats[language] = (languageStats[language] || 0) + lines
|
|
|
- } else {
|
|
|
- // 如果文件不属于任何已知语言,归类为其他
|
|
|
- const lines = await countLines(fullPath)
|
|
|
- totalLines += lines
|
|
|
- languageStats["Other"] = (languageStats["Other"] || 0) + lines
|
|
|
+ try {
|
|
|
+ const git = simpleGit(r[0].path)
|
|
|
+ const files = await git.raw(["ls-files"]) // 获取所有 Git 跟踪的文件
|
|
|
+ const fileList = files.split("\n").map(f => f.trim()).filter(f => f)
|
|
|
+
|
|
|
+ for (const file of fileList) {
|
|
|
+ const fullPath = path.join(r[0].path, file)
|
|
|
+ const ext = path.extname(file)
|
|
|
+ const language = Object.keys(this.languageExtensions).find(lang =>
|
|
|
+ this.languageExtensions[lang].includes(ext)
|
|
|
+ )
|
|
|
+
|
|
|
+ if (language) {
|
|
|
+ const lines = await countLines(fullPath)
|
|
|
+ totalLines += lines
|
|
|
+ languageStats[language] = (languageStats[language] || 0) + lines
|
|
|
+ } else {
|
|
|
+ // 如果文件不属于任何已知语言,归类为其他
|
|
|
+ const lines = await countLines(fullPath)
|
|
|
+ totalLines += lines
|
|
|
+ languageStats["Other"] = (languageStats["Other"] || 0) + lines
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (error) {
|
|
|
+ return res.json({
|
|
|
+ ...BaseStdResponse.ERR,
|
|
|
+ msg: '获取仓库信息失败!'
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
res.json({
|