diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-10-21 17:22:43 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-21 17:22:43 +0800 |
commit | 83df0caf15c4a8c3b9336987f329501507c6d527 (patch) | |
tree | 34fa87145972771c6d3ea417bba79028e3186b70 /modules/repository | |
parent | 053b2f4dce2c404bcd7cb828147deb4b99ab71e6 (diff) | |
download | gitea-83df0caf15c4a8c3b9336987f329501507c6d527.tar.gz gitea-83df0caf15c4a8c3b9336987f329501507c6d527.zip |
Sync gitea app path for git hooks and authorized keys when starting (#17335)
Gitea writes its own AppPath into git hook scripts. If Gitea's AppPath changes, then the git push will fail.
This PR:
* Introduce an AppState module, it can persist app states into database
* During GlobalInit, Gitea will check if the current AppPath is the same as last one. If they don't match, Gitea will sync git hooks.
* Refactor some code to make them more clear.
* Also, "Detect if gitea binary's name changed" #11341 is related, we call models.RewriteAllPublicKeys to update ssh authorized_keys file
Diffstat (limited to 'modules/repository')
-rw-r--r-- | modules/repository/hooks.go | 58 |
1 files changed, 42 insertions, 16 deletions
diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go index 6072dda016..63f00b8f80 100644 --- a/modules/repository/hooks.go +++ b/modules/repository/hooks.go @@ -23,64 +23,90 @@ import ( func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) { hookNames = []string{"pre-receive", "update", "post-receive"} hookTpls = []string{ + // for pre-receive fmt.Sprintf(`#!/usr/bin/env %s +# AUTO GENERATED BY GITEA, DO NOT MODIFY data=$(cat) exitcodes="" hookname=$(basename $0) GIT_DIR=${GIT_DIR:-$(dirname $0)/..} for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do -test -x "${hook}" && test -f "${hook}" || continue -echo "${data}" | "${hook}" -exitcodes="${exitcodes} $?" + test -x "${hook}" && test -f "${hook}" || continue + echo "${data}" | "${hook}" + exitcodes="${exitcodes} $?" done for i in ${exitcodes}; do -[ ${i} -eq 0 ] || exit ${i} + [ ${i} -eq 0 ] || exit ${i} done `, setting.ScriptType), + + // for update fmt.Sprintf(`#!/usr/bin/env %s +# AUTO GENERATED BY GITEA, DO NOT MODIFY exitcodes="" hookname=$(basename $0) GIT_DIR=${GIT_DIR:-$(dirname $0/..)} for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do -test -x "${hook}" && test -f "${hook}" || continue -"${hook}" $1 $2 $3 -exitcodes="${exitcodes} $?" + test -x "${hook}" && test -f "${hook}" || continue + "${hook}" $1 $2 $3 + exitcodes="${exitcodes} $?" done for i in ${exitcodes}; do -[ ${i} -eq 0 ] || exit ${i} + [ ${i} -eq 0 ] || exit ${i} done `, setting.ScriptType), + + // for post-receive fmt.Sprintf(`#!/usr/bin/env %s +# AUTO GENERATED BY GITEA, DO NOT MODIFY data=$(cat) exitcodes="" hookname=$(basename $0) GIT_DIR=${GIT_DIR:-$(dirname $0)/..} for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do -test -x "${hook}" && test -f "${hook}" || continue -echo "${data}" | "${hook}" -exitcodes="${exitcodes} $?" + test -x "${hook}" && test -f "${hook}" || continue + echo "${data}" | "${hook}" + exitcodes="${exitcodes} $?" done for i in ${exitcodes}; do -[ ${i} -eq 0 ] || exit ${i} + [ ${i} -eq 0 ] || exit ${i} done `, setting.ScriptType), } + giteaHookTpls = []string{ - fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s pre-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), - fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s update $1 $2 $3\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), - fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s post-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), + // for pre-receive + fmt.Sprintf(`#!/usr/bin/env %s +# AUTO GENERATED BY GITEA, DO NOT MODIFY +%s hook --config=%s pre-receive +`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)), + + // for update + 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)), } if git.SupportProcReceive { hookNames = append(hookNames, "proc-receive") hookTpls = append(hookTpls, - fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s proc-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf))) + 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, "") } |