11import { execSync , execFile } from "child_process" ;
22import { promisify } from "util" ;
3- import type { BuildInfo , BuildCommitInfo } from "@/lib/buildInfo" ;
3+ import { BuildInfo , CommitEntry } from "@/lib/buildInfo" ;
4+ import { fromISODateString , Serialized , toISODateString } from "../utils" ;
45const execFileAsync = promisify ( execFile ) ;
56
67export async function getBuildInfo ( ) : Promise < BuildInfo > {
7- return {
8- commitInfo : await getBuildCommitInfo ( ) ,
9- buildTimestamp : new Date ( ) . toLocaleDateString ( "en-US" , {
10- month : "short" ,
11- day : "numeric" ,
12- year : "numeric" ,
13- hour : "numeric" ,
14- minute : "2-digit" ,
15- hour12 : true ,
16- timeZoneName : "short" ,
17- timeZone : "America/Chicago" ,
18- } ) ,
19- } ;
8+ // NOTE: We assume our build environment has our git repo
9+ // and git history configured, which getCommitEntryForFile already assumes.
10+ // So lets just use the git cli to get the info.
11+ const branch =
12+ launchShellCmd ( "git branch --show-current" ) ?. toString ( ) . trim ( ) || null ;
13+ if ( branch == null ) {
14+ throw "Could not compute branch for the build build, are you sure you set up git" ;
15+ }
16+ const buildCommitEntry = getCommitEntry ( null , true ) ;
17+ if ( buildCommitEntry == null ) {
18+ throw "Could not compute commit entry for build, are you sure you set up git" ;
19+ }
20+ const buildTimestamp = new Date ( ) ;
21+ return { branch, buildTimestamp, buildCommitEntry } ;
2022}
2123
22- export function getCommitEntryForFile ( filepath : string , head : boolean = true ) {
24+ export function getCommitEntry (
25+ forFilepath : string | null = null ,
26+ head : boolean = true ,
27+ ) : CommitEntry | null {
2328 // Prior style for reference: "%H - %an, %at : %s"
2429 // Example: "8fd2212f... - Michael Fortunato, 1739958235 : tighten static props typing"
2530 const commitEntryFormat = "%H%x1f%an%x1f%aI%x1f%s" ;
2631 const cmd = ! head
27- ? `git log --reverse --pretty=format:"${ commitEntryFormat } " -- ${ filepath } | head -1`
28- : `git log -1 --pretty=format:"${ commitEntryFormat } " -- ${ filepath } ` ;
32+ ? `git log --reverse --pretty=format:"${ commitEntryFormat } " ${ forFilepath != null ? " -- " + forFilepath : "" } | head -1`
33+ : `git log -1 --pretty=format:"${ commitEntryFormat } " ${ forFilepath != null ? " -- " + forFilepath : "" } ` ;
2934 const commitEntry = launchShellCmd ( cmd ) ?. toString ( ) . trim ( ) ;
3035 if ( commitEntry == null ) return null ;
3136 const [ commitHash , author , authorDateISO , message ] =
@@ -37,6 +42,7 @@ export function getCommitEntryForFile(filepath: string, head: boolean = true) {
3742 if ( Number . isNaN ( timestamp . valueOf ( ) ) ) {
3843 return null ;
3944 }
45+
4046 return {
4147 commitHash,
4248 get shortCommitHash ( ) {
@@ -60,28 +66,6 @@ export function isDirty(filepath?: string) {
6066 }
6167}
6268
63- /// Returns the commit info for this build
64- async function getBuildCommitInfo ( ) : Promise < BuildCommitInfo > {
65- // NOTE: We assume our build environment has our git repo
66- // and git history configured, which getCommitEntryForFile already assumes.
67- // So lets just use the git cli to get the info.
68- const branch = launchShellCmd ( "git branch --show-current" ) ?. toString ( ) . trim ( ) ;
69- if ( ! branch )
70- throw "Could not compute build branch using git cli. Are you sure the build environment has git set up?" ;
71- const hash = launchShellCmd ( `git log -1 --pretty=format:"%H"` )
72- ?. toString ( )
73- . trim ( ) ;
74- if ( ! hash )
75- throw "Could not compute build commit hash using git cli. Are you sure the build environment has git set up?" ;
76- const repo = "personal-website" ; // This should never change.
77-
78- return {
79- repo,
80- hash,
81- branch,
82- } ;
83- }
84-
8569let _gitDir : string | null = null ;
8670export async function getGitDir ( ) : Promise < string > {
8771 if ( _gitDir ) return _gitDir ;
0 commit comments