Repository: go-gitea/gitea
File: modules/gitrepo/hooks.go (Go)

Code before:
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s update $1 $2 $3
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),

		// for post-receive
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s post-receive
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
	}

	// although only new git (>=2.29) supports proc-receive, it's still good to create its hook, in case the user upgrades git
	hookNames = append(hookNames, "proc-receive")
	hookTpls = append(hookTpls,
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s proc-receive
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)))
	giteaHookTpls = append(giteaHookTpls, "")

	return hookNames, hookTpls, giteaHookTpls
}

// CreateDelegateHooks creates all the hooks scripts for the repo
func CreateDelegateHooks(ctx context.Context, repo Repository, isWiki bool) (err error) {
	hookNames, hookTpls, giteaHookTpls := getHookTemplates()
	var hookDir string
	if isWiki {
		hookDir = filepath.Join(wikiPath(repo), "hooks")
	} else {
		hookDir = filepath.Join(repoPath(repo), "hooks")
	}

	for i, hookName := range hookNames {
		oldHookPath := filepath.Join(hookDir, hookName)
		newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")

		if err := os.MkdirAll(filepath.Join(hookDir, hookName+".d"), os.ModePerm); err != nil {
			return fmt.Errorf("create hooks dir '%s': %w", filepath.Join(hookDir, hookName+".d"), err)
		}

		// WARNING: This will override all old server-side hooks
		if err = util.Remove(oldHookPath); err != nil && !os.IsNotExist(err) {
			return fmt.Errorf("unable to pre-remove old hook file '%s' prior to rewriting: %w ", oldHookPath, err)
		}
		if err = os.WriteFile(oldHookPath, []byte(hookTpls[i]), 0o777); err != nil {
			return fmt.Errorf("write old hook file '%s': %w", oldHookPath, err)
		}

		if err = ensureExecutable(oldHookPath); err != nil {

Review comment (suggestion):
Please introduce 2 functions: 


```
CreateDelegateHooksForRepo
CreateDelegateHooksForWiki
```


Code after:
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s update $1 $2 $3
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),

		// for post-receive
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s post-receive
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
	}

	// although only new git (>=2.29) supports proc-receive, it's still good to create its hook, in case the user upgrades git
	hookNames = append(hookNames, "proc-receive")
	hookTpls = append(hookTpls,
		fmt.Sprintf(`#!/usr/bin/env %s
# AUTO GENERATED BY GITEA, DO NOT MODIFY
%s hook --config=%s proc-receive
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)))
	giteaHookTpls = append(giteaHookTpls, "")

	return hookNames, hookTpls, giteaHookTpls
}

// CreateDelegateHooksForRepo creates all the hooks scripts for the repo
func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) {
	return createDelegateHooks(filepath.Join(repoPath(repo), "hooks"))
}

// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo
func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) {
	return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks"))
}

func createDelegateHooks(hookDir string) (err error) {
	hookNames, hookTpls, giteaHookTpls := getHookTemplates()

	for i, hookName := range hookNames {
		oldHookPath := filepath.Join(hookDir, hookName)
		newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")

		if err := os.MkdirAll(filepath.Join(hookDir, hookName+".d"), os.ModePerm); err != nil {
			return fmt.Errorf("create hooks dir '%s': %w", filepath.Join(hookDir, hookName+".d"), err)
		}

		// WARNING: This will override all old server-side hooks
		if err = util.Remove(oldHookPath); err != nil && !os.IsNotExist(err) {
			return fmt.Errorf("unable to pre-remove old hook file '%s' prior to rewriting: %w ", oldHookPath, err)
		}
		if err = os.WriteFile(oldHookPath, []byte(hookTpls[i]), 0o777); err != nil {
			return fmt.Errorf("write old hook file '%s': %w", oldHookPath, err)