@@ -185,6 +185,50 @@ function _getPackageJson(projectPath = process.cwd()): { packageJsonContent: any
185185 return { packageJsonContent : JSON . parse ( packageJsonDataRaw ) , packageJsonPath : targetPackageJson }
186186}
187187
188+ /**
189+ * Checks if git hooks are already installed and up-to-date
190+ */
191+ export function areHooksInstalled ( projectRootPath : string = process . cwd ( ) ) : boolean {
192+ const gitRoot = getGitProjectRoot ( projectRootPath )
193+ if ( ! gitRoot ) {
194+ return false
195+ }
196+
197+ if ( ! config || Object . keys ( config ) . length === 0 ) {
198+ return false
199+ }
200+
201+ // Check if at least one configured hook exists and contains our script marker
202+ const configuredHooks = Object . keys ( config ) . filter ( key =>
203+ VALID_GIT_HOOKS . includes ( key as typeof VALID_GIT_HOOKS [ number ] )
204+ )
205+
206+ if ( configuredHooks . length === 0 ) {
207+ return false
208+ }
209+
210+ // Check if all configured hooks exist
211+ for ( const hook of configuredHooks ) {
212+ const hookPath = path . normalize ( path . join ( gitRoot , 'hooks' , hook ) )
213+ if ( ! fs . existsSync ( hookPath ) ) {
214+ return false
215+ }
216+
217+ // Verify the hook contains our prepend script (marker of our installation)
218+ try {
219+ const hookContent = fs . readFileSync ( hookPath , 'utf-8' )
220+ if ( ! hookContent . includes ( 'SKIP_BUN_GIT_HOOKS' ) ) {
221+ return false
222+ }
223+ }
224+ catch {
225+ return false
226+ }
227+ }
228+
229+ return true
230+ }
231+
188232/**
189233 * Parses the config and sets git hooks
190234 */
@@ -524,6 +568,7 @@ const gitHooks: {
524568 getGitProjectRoot : typeof getGitProjectRoot
525569 runStagedLint : typeof runStagedLint
526570 getStagedFiles : typeof getStagedFiles
571+ areHooksInstalled : typeof areHooksInstalled
527572} = {
528573 PREPEND_SCRIPT ,
529574 setHooksFromConfig,
@@ -532,7 +577,8 @@ const gitHooks: {
532577 getProjectRootDirectoryFromNodeModules,
533578 getGitProjectRoot,
534579 runStagedLint,
535- getStagedFiles
580+ getStagedFiles,
581+ areHooksInstalled
536582}
537583
538584export default gitHooks
0 commit comments