MCPcopy Create free account
hub / github.com/appleboy/CodeGPT / InstallHook

Method InstallHook

git/git.go:215–241  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
215func (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.

Callers 1

hook.goFile · 0.80

Calls 2

hookPathMethod · 0.95
GetTemplateByBytesFunction · 0.92

Tested by

no test coverage detected