@@ -180,13 +180,22 @@ async function setHooksFromConfig(projectRootPath=process.cwd(), argv=process.ar
180180
181181 const preserveUnused = Array . isArray ( config . preserveUnused ) ? config . preserveUnused : config . preserveUnused ? VALID_GIT_HOOKS : [ ]
182182
183+ let isHookChanged = false
184+
183185 for ( let hook of VALID_GIT_HOOKS ) {
184186 if ( Object . prototype . hasOwnProperty . call ( config , hook ) ) {
185- _setHook ( hook , config [ hook ] , projectRootPath )
187+ const isHookUpdated = _setHook ( hook , config [ hook ] , projectRootPath ) . hookChanged
188+ isHookChanged = isHookChanged || isHookUpdated
186189 } else if ( ! preserveUnused . includes ( hook ) ) {
187- _removeHook ( hook , projectRootPath )
190+ const isHookDeleted = _removeHook ( hook , projectRootPath )
191+ isHookChanged = isHookChanged || isHookDeleted
188192 }
189193 }
194+
195+
196+ return {
197+ isHookChanged,
198+ }
190199}
191200
192201/**
@@ -230,7 +239,10 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
230239
231240 if ( ! gitRoot ) {
232241 console . info ( '[INFO] No `.git` root folder found, skipping' )
233- return
242+ return {
243+ hookChanged : false ,
244+ success : false
245+ }
234246 }
235247
236248 const hookCommand = PREPEND_SCRIPT + command
@@ -242,10 +254,23 @@ function _setHook(hook, command, projectRoot=process.cwd()) {
242254 fs . mkdirSync ( normalizedHookDirectory , { recursive : true } )
243255 }
244256
257+ if ( fs . existsSync ( hookPath ) ) {
258+ const existingHook = fs . readFileSync ( hookPath , { encoding : 'utf-8' } )
259+ if ( existingHook === hookCommand ) {
260+ return {
261+ hookChanged : false ,
262+ success : true
263+ }
264+ }
265+ }
245266 fs . writeFileSync ( hookPath , hookCommand )
246267 fs . chmodSync ( hookPath , 0o0755 )
247268
248269 console . info ( `[INFO] Successfully set the ${ hook } with command: ${ command } ` )
270+ return {
271+ hookChanged : true ,
272+ success : true
273+ }
249274}
250275
251276/**
@@ -272,6 +297,7 @@ async function removeHooks(projectRoot = process.cwd()) {
272297 * Removes the pre-commit hook
273298 * @param {string } hook
274299 * @param {string } projectRoot
300+ * @returns {boolean } whether the hook was removed
275301 * @private
276302 */
277303function _removeHook ( hook , projectRoot = process . cwd ( ) ) {
@@ -280,7 +306,9 @@ function _removeHook(hook, projectRoot=process.cwd()) {
280306
281307 if ( fs . existsSync ( hookPath ) ) {
282308 fs . unlinkSync ( hookPath )
309+ return true
283310 }
311+ return false
284312}
285313
286314/** Reads package.json file, returns package.json content and path
0 commit comments