Skip to content

Commit

Permalink
Simplifies hookz deleter
Browse files Browse the repository at this point in the history
  • Loading branch information
djschleen authored Feb 28, 2024
1 parent 9f569f9 commit e46f0de
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions lib/hookdeleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package lib

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -11,42 +10,45 @@ import (
"github.com/spf13/afero"
)

// RemoveHooks purges all hooks from the filesystem that Hookz has created
// and deletes any generated scripts
func RemoveHooks(afs *afero.Afero, verbose bool) (err error) {
path, _ := os.Getwd()
// RemoveHooks removes hooks with a specific extension and their corresponding files in the Git hooks directory.
// It also optionally prints information about deleted hooks if verbose is set to true.
func RemoveHooks(afs *afero.Afero, verbose bool) error {
path, err := os.Getwd()
if err != nil {
return err

Check warning on line 18 in lib/hookdeleter.go

View check run for this annotation

Codecov / codecov/patch

lib/hookdeleter.go#L18

Added line #L18 was not covered by tests
}

ext := ".hookz"
p := fmt.Sprintf("%s/%s", path, ".git/hooks")

dirFiles, _ := afs.ReadDir(p)
hooksPath := filepath.Join(path, ".git/hooks")

for index := range dirFiles {
file := dirFiles[index]
dirFiles, err := afs.ReadDir(hooksPath)
if err != nil {
return err

Check warning on line 26 in lib/hookdeleter.go

View check run for this annotation

Codecov / codecov/patch

lib/hookdeleter.go#L26

Added line #L26 was not covered by tests
}

name := file.Name()
fullPath := fmt.Sprintf("%s/%s", p, name)
info, _ := afs.Stat(fullPath)
isHookzFile := strings.Contains(info.Name(), ext)
if isHookzFile {
var hookName = fullPath[0 : len(fullPath)-len(ext)]
if removeErr := afs.Fs.Remove(fullPath); removeErr != nil {
for _, file := range dirFiles {
fullPath := filepath.Join(hooksPath, file.Name())
if strings.Contains(file.Name(), ext) {
if removeErr := afs.Remove(fullPath); removeErr != nil {
return removeErr
}
if removeErr := afs.Fs.Remove(hookName); removeErr != nil {

correspondingFile := fullPath[:len(fullPath)-len(ext)]
if removeErr := afs.Remove(correspondingFile); removeErr != nil {
return removeErr
}
parts := strings.Split(hookName, "/")

util.DoIf(verbose, func() {
util.PrintTabbedf("Deleted %s\n", parts[len(parts)-1])
util.PrintTabbedf("Deleted %s\n", filepath.Base(correspondingFile))
})
}
}

removeShasum(afs)
return
return nil
}

// removeShasum removes the shasum file from the Git hooks directory.
func removeShasum(afs *afero.Afero) {
filename, _ := filepath.Abs(".git/hooks/hookz.shasum")
_ = afs.Remove(filename)
Expand Down

0 comments on commit e46f0de

Please sign in to comment.