diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bf29ba4f..2b315bc1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,6 +28,7 @@ jobs: - uses: actions/checkout@v4 with: ref: ${{ github.head_ref }} + fetch-depth: 0 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: @@ -35,7 +36,6 @@ jobs: - run: node ./build/build.js - uses: stefanzweifel/git-auto-commit-action@v5 with: - fetch-depth: 0 commit_message: update repo.json upload: diff --git a/build/build.js b/build/build.js index 9b1e93ef..ec8a14ac 100644 --- a/build/build.js +++ b/build/build.js @@ -16,13 +16,23 @@ function calculateSHA1(filePath) { function getGitTimestamp(filePath) { try { const time = execSync(`git log -1 --format="%ai" -- ${filePath}`).toString().trim(); - return time || 'No commit found'; + if (!time) { + console.warn(`未找到文件 ${filePath} 的提交记录`); + return null; + } + return time; } catch (e) { console.warn(`无法通过 Git 获取时间: ${filePath}`, e); return null; } } +function formatTime(timestamp) { + if (!timestamp) return null; + // 将 "2023-01-01 12:00:00 +0800" 格式化为 "20230101120000" + return timestamp.replace(/[-: ]/g, '').split('+')[0]; +} + function convertNewlines(text) { return text.replace(/\\n/g, '\n'); } @@ -37,12 +47,8 @@ function extractInfoFromCombatFile(filePath) { .map(char => char.trim()) .filter(char => char.length > 0 && !char.match(/^[,.]$/)); // 过滤掉单个逗号或句号 - let version = getGitTimestamp(filePath); - if (!version) { - version = calculateSHA1(filePath).substring(0, 7); - } else { - version = formatTime(version); - } + const gitTimestamp = getGitTimestamp(filePath); + const version = gitTimestamp ? formatTime(gitTimestamp) : calculateSHA1(filePath).substring(0, 7); return { author: authorMatch ? authorMatch[1].trim() : '', @@ -91,12 +97,9 @@ function extractInfoFromPathingFile(filePath, parentFolders) { const contentObj = JSON.parse(content); - // 提取版本字段,若不存在则使用上传时间,还不存在就使用 SHA - let version = contentObj.info && contentObj.info.version; - if (!version) { - const gitDate = getGitTimestamp(filePath); - version = gitDate ? formatTime(gitDate) : calculateSHA1(filePath).substring(0, 7); - } + // 优先使用 Git 提交时间作为版本号 + const gitTimestamp = getGitTimestamp(filePath); + const version = gitTimestamp ? formatTime(gitTimestamp) : (contentObj.info?.version || calculateSHA1(filePath).substring(0, 7)); let tags = parentFolders.slice(2) .filter(tag => !tag.includes('@')) @@ -138,12 +141,8 @@ function extractInfoFromTCGFile(filePath, parentFolder) { tags = ['酒馆挑战', ...tags]; } - let version = getGitTimestamp(filePath); - if (!version) { - version = calculateSHA1(filePath).substring(0, 7); - } else { - version = formatTime(version); - } + const gitTimestamp = getGitTimestamp(filePath); + const version = gitTimestamp ? formatTime(gitTimestamp) : calculateSHA1(filePath).substring(0, 7); return { author: authorMatch ? authorMatch[1].trim() : '',