InstallHook installs the prepare-commit-msg hook if it doesn't already exist. It retrieves the hooks directory path, checks if the hook file exists, and writes the hook file with executable permissions.
(ctx context.Context)
| 213 | // InstallHook installs the prepare-commit-msg hook if it doesn't already exist. |
| 214 | // It retrieves the hooks directory path, checks if the hook file exists, and writes the hook file with executable permissions. |
| 215 | func (c *Command) InstallHook(ctx context.Context) error { |
| 216 | hookPath, err := c.hookPath(ctx).Output() |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | |
| 221 | target := path.Join(strings.TrimSpace(string(hookPath)), HookPrepareCommitMessageTemplate) |
| 222 | if exists, err := file.IsFile(target); err != nil { |
| 223 | if !os.IsNotExist(err) { |
| 224 | return err |
| 225 | } |
| 226 | } else if exists { |
| 227 | return errors.New("hook file prepare-commit-msg exist") |
| 228 | } |
| 229 | |
| 230 | content, err := util.GetTemplateByBytes(HookPrepareCommitMessageTemplate, nil) |
| 231 | if err != nil { |
| 232 | return err |
| 233 | } |
| 234 | |
| 235 | // Write the hook file with executable permissions (0o755) |
| 236 | return os.WriteFile( |
| 237 | target, |
| 238 | content, |
| 239 | 0o755, |
| 240 | ) //nolint:gosec // hook file needs executable permissions |
| 241 | } |
| 242 | |
| 243 | // UninstallHook removes the prepare-commit-msg hook if it exists. |
| 244 | // It retrieves the hooks directory path, checks if the hook file exists, and removes the hook file. |
no test coverage detected