From 5895f9e79459fed65556ec8669c73786774dd311 Mon Sep 17 00:00:00 2001 From: Gsh <15170702455@163.com> Date: Sun, 18 Jan 2026 23:46:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=89=8D=E7=AB=AF=E6=89=93=E5=8C=85?= =?UTF-8?q?=E6=97=B6=E5=A2=9E=E5=8A=A0git=20hash=20=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Yi.Ai.Vue3/.build/plugins/git-hash.ts | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Yi.Ai.Vue3/.build/plugins/git-hash.ts diff --git a/Yi.Ai.Vue3/.build/plugins/git-hash.ts b/Yi.Ai.Vue3/.build/plugins/git-hash.ts new file mode 100644 index 00000000..f1845b0f --- /dev/null +++ b/Yi.Ai.Vue3/.build/plugins/git-hash.ts @@ -0,0 +1,60 @@ +import type { Plugin } from 'vite'; +import { execSync } from 'node:child_process'; +import path from 'node:path'; + +/** + * 获取 Git 提交哈希值插件 + * Git 仓库在上一级目录 + */ +export default function gitHashPlugin(): Plugin { + let gitHash = 'unknown'; + let gitBranch = 'unknown'; + let gitDate = 'unknown'; + + try { + // Git 仓库在上一级目录 + const execOptions = { cwd: path.resolve(__dirname, '../../..'), encoding: 'utf-8' as BufferEncoding }; + + // 获取完整的 commit hash + gitHash = execSync('git rev-parse HEAD', execOptions) + .toString() + .trim(); + + // 获取短 hash (前7位) + const shortHash = gitHash.substring(0, 7); + + // 获取分支名 + gitBranch = execSync('git rev-parse --abbrev-ref HEAD', execOptions) + .toString() + .trim(); + + // 获取提交时间 + gitDate = execSync('git log -1 --format=%cd --date=iso', execOptions) + .toString() + .trim(); + + console.log(`\n📦 Git Info:`); + console.log(` Branch: ${gitBranch}`); + console.log(` Commit: ${shortHash}`); + console.log(` Date: ${gitDate}\n`); + + gitHash = shortHash; // 使用短 hash + } catch (error: any) { + console.warn('⚠️ 无法获取 Git 信息:', error?.message || error); + } + + return { + name: 'vite-plugin-git-hash', + config() { + // 在 config 钩子中返回配置 + return { + define: { + __GIT_HASH__: JSON.stringify(gitHash), + __GIT_BRANCH__: JSON.stringify(gitBranch), + __GIT_DATE__: JSON.stringify(gitDate), + __BUILD_TIME__: JSON.stringify(new Date().toISOString()), + }, + }; + }, + }; +}