From 3cd46c63adc183f8bbcbbd4003b6ac6131d3eeed Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Sun, 6 Apr 2025 16:34:57 +0100 Subject: [PATCH] Update git-utils.ts --- lib/git-utils.ts | 55 +++--------------------------------------------- 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/lib/git-utils.ts b/lib/git-utils.ts index 72fc3d0..fa9eb05 100644 --- a/lib/git-utils.ts +++ b/lib/git-utils.ts @@ -1,3 +1,5 @@ +import gitInfo from "../public/git-info.json"; + /** * Git utility to load commit hash in both development and production environments */ @@ -16,56 +18,5 @@ let cachedGitInfo: GitInfo | null = null; * - Returns "dev" if nothing is available */ export async function getGitCommitInfo(): Promise { - // Return cached value if available - if (cachedGitInfo) { - return cachedGitInfo; - } - - // First, check if we have the env variable (from Docker) - if (process.env.GIT_COMMIT_SHA) { - const gitInfo: GitInfo = { - commitHash: process.env.GIT_COMMIT_SHA, - buildTime: new Date().toISOString(), - }; - cachedGitInfo = gitInfo; - return gitInfo; - } - - // In the browser, fetch from the public directory - if (typeof window !== 'undefined') { - try { - const response = await fetch('/git-info.json'); - if (response.ok) { - const gitInfo: GitInfo = await response.json(); - cachedGitInfo = gitInfo; - return gitInfo; - } - } catch (error) { - console.warn('Could not fetch git info from public directory', error); - } - } else { - // On the server (during local development), try to load from the static directory - try { - // We use dynamic import to avoid bundling this in the client build - const fs = require('fs'); - const path = require('path'); - - const gitInfoPath = path.join(process.cwd(), 'public', 'git-info.json'); - - if (fs.existsSync(gitInfoPath)) { - const gitInfo: GitInfo = JSON.parse(fs.readFileSync(gitInfoPath, 'utf8')); - cachedGitInfo = gitInfo; - return gitInfo; - } - } catch (error) { - console.warn('Could not read git info from file system', error); - } - } - - // Default if nothing else works - const defaultGitInfo: GitInfo = { - commitHash: 'development', - buildTime: new Date().toISOString(), - }; - return defaultGitInfo; + return gitInfo; } \ No newline at end of file