Maybe made the commit info work in prod?

This commit is contained in:
HangerThem 2026-08-03 17:54:08 +02:00
parent 3941d77d10
commit 2c660bae02
6 changed files with 58 additions and 56 deletions

View file

@ -6,11 +6,7 @@ import { usePathname } from 'next/navigation'
import { ExternalLink } from '@/components/ui/ExternalLink'
import { navItems } from '@/components/layout/navItems'
type NavLinksProps = {
gitOriginUrl: string | null
}
export function NavLinks({ gitOriginUrl }: NavLinksProps) {
export function NavLinks() {
const pathname = usePathname()
const isActive = (href: string) => {
@ -30,11 +26,7 @@ export function NavLinks({ gitOriginUrl }: NavLinksProps) {
>
{item.isExternal ? (
<ExternalLink
href={
item.href.includes('{gitOriginUrl}')
? item.href.replace('{gitOriginUrl}', gitOriginUrl ?? '')
: item.href
}
href={item.href}
className="flex items-center gap-2 px-2 py-1 hover:no-underline focus:no-underline"
>
{item.icon}

View file

@ -42,7 +42,7 @@ export async function Sidebar() {
<span>{metadata.title!.toString()}</span>
</h2>
<ul className="space-y-2">
<NavLinks gitOriginUrl={gitOriginUrl} />
<NavLinks />
</ul>
</div>
<div className="mt-auto flex gap-4 mx-auto">

View file

@ -20,10 +20,14 @@ export const navItems: NavItem[] = [
icon: <Rss className={navIconsClassName} />,
isExternal: true,
},
{
href: '{gitOriginUrl}',
label: 'Source Code',
icon: <GitBranch className={navIconsClassName} />,
isExternal: true,
},
...(process.env.GIT_ORIGIN_URL
? [
{
href: process.env.GIT_ORIGIN_URL,
label: 'Source Code',
icon: <GitBranch className={navIconsClassName} />,
isExternal: true,
},
]
: []),
]

View file

@ -4,6 +4,10 @@ const nextConfig: NextConfig = {
images: {
qualities: [75, 100],
},
env: {
GIT_COMMIT_HASH: process.env.GIT_COMMIT_HASH,
GIT_ORIGIN_URL: process.env.GIT_ORIGIN_URL,
},
}
export default nextConfig

View file

@ -1,2 +1,2 @@
[phases.setup]
aptPkgs = ["libnss3", "libatk1.0-0", "libatk-bridge2.0-0", "libcups2", "libgbm1", "libasound2t64", "libpangocairo-1.0-0", "libxss1", "libgtk-3-0", "libxshmfence1", "libglu1", "chromium", "unzip", "curl", "wget", "git"]
aptPkgs = ["libnss3", "libatk1.0-0", "libatk-bridge2.0-0", "libcups2", "libgbm1", "libasound2t64", "libpangocairo-1.0-0", "libxss1", "libgtk-3-0", "libxshmfence1", "libglu1", "chromium", "unzip", "curl", "wget"]

View file

@ -1,53 +1,55 @@
import { exec } from 'child_process'
/**
* Get the current Git commit hash.
* @returns The current Git commit hash as a string, or null if an error occurs.
* Get the current git commit hash.
* @returns The current git commit hash, or null if it cannot be determined.
*/
export async function getGitCommitHash(): Promise<string | null> {
try {
const commitHash = (await new Promise((resolve, reject) => {
exec('git rev-parse HEAD', (error, stdout) => {
if (error) {
reject(error)
} else {
resolve(stdout.toString().trim())
}
})
})) as string
if (process.env.GIT_COMMIT_HASH) {
return process.env.GIT_COMMIT_HASH
}
return commitHash
} catch (error) {
console.error('Error getting Git commit hash:', error)
try {
const { exec } = await import('child_process')
const hash = await new Promise<string>((res, rej) => {
exec('git rev-parse HEAD', (err, stdout) => (err ? rej(err) : res(stdout.trim())))
})
return hash
} catch {
return null
}
}
/**
* Get the Git origin URL.
* @returns The Git origin URL as a string, or null if an error occurs.
* 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 originUrl = (await new Promise((resolve, reject) => {
exec('git config --get remote.origin.url', (error, stdout) => {
if (error) {
reject(error)
} else {
resolve(stdout.toString().trim())
}
})
})) as string
if (originUrl.startsWith('ssh://')) {
const httpsUrl = originUrl.replace(/^ssh:\/\/git@/, 'https://').replace(/\.git$/, '')
const urlWithoutPort = httpsUrl.replace(/:\d+/, '')
return urlWithoutPort
}
return originUrl
} catch (error) {
console.error('Error getting Git origin URL:', error)
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
}