blog.hangerthem.com/utils/git.ts
HangerThem cb960e268a Maybe?
2026-08-03 18:15:21 +02:00

19 lines
529 B
TypeScript

/**
* 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> {
if (process.env.SOURCE_COMMIT) {
return process.env.SOURCE_COMMIT
}
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
}
}