|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// removeLint is the callback function for the uninstall command. |
| 11 | +func removeLint(isGlobal bool) error { |
| 12 | + return gitRemoveHook(isGlobal) |
| 13 | +} |
| 14 | + |
| 15 | +// gitRemoveHook removes commitlint from git config by unsetting core.hooksPath. |
| 16 | +// It prompts for confirmation before making any changes. |
| 17 | +// Hook files are left intact - the user can remove them manually if needed. |
| 18 | +func gitRemoveHook(isGlobal bool) error { |
| 19 | + scope := "local" |
| 20 | + if isGlobal { |
| 21 | + scope = "global" |
| 22 | + } |
| 23 | + confirmed, err := promptConfirm(fmt.Sprintf("Unset core.hooksPath from %s git config?", scope)) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + if !confirmed { |
| 28 | + fmt.Println("aborted") |
| 29 | + return nil |
| 30 | + } |
| 31 | + |
| 32 | + if err := unsetGitConf(isGlobal); err != nil { |
| 33 | + return fmt.Errorf("could not unset git core.hooksPath: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + fmt.Println("commitlint hook removed successfully") |
| 37 | + fmt.Println("note: hook files were not removed - delete them manually if no longer needed") |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// unsetGitConf removes the core.hooksPath entry from git config (local or global). |
| 42 | +func unsetGitConf(isGlobal bool) error { |
| 43 | + args := []string{"config"} |
| 44 | + if isGlobal { |
| 45 | + args = append(args, "--global") |
| 46 | + } |
| 47 | + args = append(args, "--unset", "core.hooksPath") |
| 48 | + |
| 49 | + cmd := exec.Command("git", args...) |
| 50 | + cmd.Stderr = os.Stderr |
| 51 | + return cmd.Run() |
| 52 | +} |
| 53 | + |
| 54 | +// promptConfirm prints prompt and waits for the user to type y/yes or anything else. |
| 55 | +// Returns true only when the user confirms with "y" or "yes" (case-insensitive). |
| 56 | +func promptConfirm(prompt string) (bool, error) { |
| 57 | + fmt.Printf("%s [y/N]: ", prompt) |
| 58 | + var response string |
| 59 | + _, err := fmt.Scanln(&response) |
| 60 | + if err != nil { |
| 61 | + // empty Enter is reported by fmt.Scanln as "unexpected newline" |
| 62 | + if err.Error() == "unexpected newline" { |
| 63 | + return false, nil |
| 64 | + } |
| 65 | + return false, err |
| 66 | + } |
| 67 | + response = strings.TrimSpace(strings.ToLower(response)) |
| 68 | + return response == "y" || response == "yes", nil |
| 69 | +} |
0 commit comments