diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 3d7fc7d..8a73e1c 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -14,6 +14,8 @@ jobs: uses: actions/checkout@v4 - name: Build Frontend + env: + CI: "true" run: | npm install rm -rf dist/ diff --git a/test_version.js b/test_version.js new file mode 100644 index 0000000..c64adf0 --- /dev/null +++ b/test_version.js @@ -0,0 +1,29 @@ +const { execSync } = require('child_process'); +function getGitVersion() { + try { + try { + execSync('git fetch --tags', { stdio: 'ignore' }); + } catch (e) {} + + const raw = execSync('git describe --tags --always', { stdio: ['pipe', 'pipe', 'ignore'] }) + .toString() + .trim(); + + // Check if it's just a commit hash (no dashes, just hex) + if (/^[0-9a-f]{7,}$/.test(raw)) { + return `v0.0.0-${raw}`; + } + + // It might be tag-commits-hash, e.g., v0.1.1-5-g4a9f6b6 + // Let's shorten it to v0.1.1-4a9f6b6 for a cleaner look, or just return as-is + const match = raw.match(/^(.*)-\d+-g([0-9a-f]+)$/); + if (match) { + return `${match[1]}-${match[2]}`; + } + + return raw; + } catch (e) { + return 'dev'; + } +} +console.log(getGitVersion()); diff --git a/vite.config.js b/vite.config.js index 56eb311..7a3577d 100644 --- a/vite.config.js +++ b/vite.config.js @@ -6,12 +6,33 @@ import { execSync } from 'child_process' // Derive version from git tags; fall back to commit hash function getGitVersion() { try { - // If there's a tag pointing at HEAD, use it exactly (e.g. "v1.2.0") - // Otherwise use "v0.0.0-[-dirty]" - const raw = execSync('git describe --tags --always --dirty', { stdio: ['pipe', 'pipe', 'ignore'] }) + // Attempt to fetch tags in case of a shallow clone (e.g. CI/CD) + try { + execSync('git fetch --tags', { stdio: 'ignore' }); + } catch (e) { + // Ignore if fetch fails + } + + // Only include --dirty locally (development), omit in production to prevent false "dirty" flags + const isProd = process.env.NODE_ENV === 'production' || process.env.CI; + const describeCmd = isProd ? 'git describe --tags --always' : 'git describe --tags --always --dirty'; + const raw = execSync(describeCmd, { stdio: ['pipe', 'pipe', 'ignore'] }) .toString() .trim(); - // If it looks like a pure tag (no dashes after the tag part) return as-is + + // If it's just a commit hash (no tags found) + if (/^[0-9a-f]{7,}$/.test(raw)) { + return `v0.0.0-${raw}`; + } + + // If it's a tag + commits + hash (e.g., v0.1.1-5-g4a9f6b6) + // Format it cleanly to v0.1.1-4a9f6b6 + const match = raw.match(/^(.*)-\d+-g([0-9a-f]+)$/); + if (match) { + return `${match[1]}-${match[2]}`; + } + + // Exact tag (e.g., v1.2.0) return raw; } catch { return 'dev';