Hopefully finally correct pass through for git info

This commit is contained in:
HangerThem 2026-08-03 18:10:01 +02:00
parent 2c660bae02
commit ee48b991e1
3 changed files with 8 additions and 44 deletions

View file

@ -3,7 +3,7 @@ import { NotepadText } from 'lucide-react'
import { NavLinks } from '@/components/layout/NavLinks' import { NavLinks } from '@/components/layout/NavLinks'
import { siBluesky, siGithub, siMatrix, siTelegram, type SimpleIcon } from 'simple-icons' import { siBluesky, siGithub, siMatrix, siTelegram, type SimpleIcon } from 'simple-icons'
import { ExternalLink } from '@/components/ui/ExternalLink' import { ExternalLink } from '@/components/ui/ExternalLink'
import { getGitCommitHash, getGitOriginUrl } from '@/utils/git' import { getGitCommitHash } from '@/utils/git'
type SocialLink = { type SocialLink = {
icon: SimpleIcon icon: SimpleIcon
@ -29,10 +29,10 @@ const socialLinks: SocialLink[] = [
}, },
] ]
export async function Sidebar() { const gitCommitHash = await getGitCommitHash()
const gitCommitHash = (await getGitCommitHash())?.substring(0, 7) const gitOriginUrl = process.env.GIT_ORIGIN_URL
const gitOriginUrl = await getGitOriginUrl()
export async function Sidebar() {
return ( return (
<aside className="hidden lg:block w-64 shrink-0 px-4 bg-fg/5"> <aside className="hidden lg:block w-64 shrink-0 px-4 bg-fg/5">
<div className="sticky top-0 flex flex-col h-screen gap-4 py-4"> <div className="sticky top-0 flex flex-col h-screen gap-4 py-4">
@ -60,7 +60,7 @@ export async function Sidebar() {
<div className="text-xs text-fg-dim mt-2 mx-auto"> <div className="text-xs text-fg-dim mt-2 mx-auto">
<span>Commit: </span> <span>Commit: </span>
<ExternalLink href={gitOriginUrl + '/commit/' + gitCommitHash}> <ExternalLink href={gitOriginUrl + '/commit/' + gitCommitHash}>
{gitCommitHash} {gitCommitHash.slice(0, 7)}
</ExternalLink> </ExternalLink>
</div> </div>
)} )}

View file

@ -5,7 +5,7 @@ const nextConfig: NextConfig = {
qualities: [75, 100], qualities: [75, 100],
}, },
env: { env: {
GIT_COMMIT_HASH: process.env.GIT_COMMIT_HASH, GIT_COMMIT_HASH: process.env.SOURCE_COMMIT,
GIT_ORIGIN_URL: process.env.GIT_ORIGIN_URL, GIT_ORIGIN_URL: process.env.GIT_ORIGIN_URL,
}, },
} }

View file

@ -1,6 +1,6 @@
/** /**
* Get the current git commit hash. * Get the current Git commit hash.
* @returns The current git commit hash, or null if it cannot be determined. * @returns The current Git commit hash, or null if it cannot be determined.
*/ */
export async function getGitCommitHash(): Promise<string | null> { export async function getGitCommitHash(): Promise<string | null> {
if (process.env.GIT_COMMIT_HASH) { if (process.env.GIT_COMMIT_HASH) {
@ -17,39 +17,3 @@ export async function getGitCommitHash(): Promise<string | null> {
return null return null
} }
} }
/**
* Get the git origin URL.
* @returns The git origin URL, or null if it cannot be determined.
*/
export async function getGitOriginUrl(): Promise<string | null> {
const envUrl = process.env.GIT_ORIGIN_URL
if (envUrl) return normalizeGitUrl(envUrl)
try {
const { exec } = await import('child_process')
const url = await new Promise<string>((res, rej) => {
exec('git config --get remote.origin.url', (err, stdout) =>
err ? rej(err) : res(stdout.trim()),
)
})
return normalizeGitUrl(url)
} catch {
return null
}
}
/**
* Normalize a git URL to a standard format.
* @param url The git URL to normalize.
* @returns The normalized git URL.
*/
function normalizeGitUrl(url: string): string {
if (url.startsWith('ssh://')) {
return url
.replace(/^ssh:\/\/git@/, 'https://')
.replace(/\.git$/, '')
.replace(/:\d+/, '')
}
return url
}