summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-04-06 11:44:47 +0100
committerGitHub <noreply@github.com>2020-04-06 11:44:47 +0100
commitd26885e2bf0a53af1c5c97c9d062f329250d8d20 (patch)
tree7dfe7646b121c0c77bed52ebe3bb9c41a1af5250 /modules
parent1648bf2b60126f7959d92d176bf7f3cf3ae1cfe6 (diff)
downloadgitea-d26885e2bf0a53af1c5c97c9d062f329250d8d20.tar.gz
gitea-d26885e2bf0a53af1c5c97c9d062f329250d8d20.zip
Mulitple Gitea Doctor improvements (#10943)
* Add `gitea doctor --list` flag to list the checks that will be run, including those by default * Add `gitea doctor --run` to run specific checks * Add `gitea doctor --all` to run all checks * Add db version checker * Add non-default recalculate merge bases check/fixer to doctor * Add hook checker (Fix #9878) and ensure hooks are executable (Fix #6319) * Fix authorized_keys checker - slight change of functionality here because parsing the command is fragile and we should just check if the authorized_keys file is essentially the same as what gitea would produce. (This is still not perfect as order matters - we should probably just md5sum the two files.) * Add SCRIPT_TYPE check (Fix #10977) * Add `gitea doctor --fix` to attempt to fix what is possible to easily fix * Add `gitea doctor --log-file` to set the log-file, be it a file, stdout or to switch off completely. (Fixes previously undetected bug with certain xorm logging configurations - see @6543 comment.) Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules')
-rw-r--r--modules/repository/hooks.go111
1 files changed, 96 insertions, 15 deletions
diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go
index 404c897715..94f570bf3a 100644
--- a/modules/repository/hooks.go
+++ b/modules/repository/hooks.go
@@ -14,10 +14,26 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
+ "github.com/unknwon/com"
"xorm.io/builder"
)
+func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) {
+ hookNames = []string{"pre-receive", "update", "post-receive"}
+ hookTpls = []string{
+ fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
+ fmt.Sprintf("#!/usr/bin/env %s\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\n\"${hook}\" $1 $2 $3\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
+ fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
+ }
+ giteaHookTpls = []string{
+ fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
+ fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
+ fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
+ }
+ return
+}
+
// CreateDelegateHooks creates all the hooks scripts for the repo
func CreateDelegateHooks(repoPath string) error {
return createDelegateHooks(repoPath)
@@ -25,21 +41,7 @@ func CreateDelegateHooks(repoPath string) error {
// createDelegateHooks creates all the hooks scripts for the repo
func createDelegateHooks(repoPath string) (err error) {
-
- var (
- hookNames = []string{"pre-receive", "update", "post-receive"}
- hookTpls = []string{
- fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
- fmt.Sprintf("#!/usr/bin/env %s\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\n\"${hook}\" $1 $2 $3\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
- fmt.Sprintf("#!/usr/bin/env %s\ndata=$(cat)\nexitcodes=\"\"\nhookname=$(basename $0)\nGIT_DIR=${GIT_DIR:-$(dirname $0)}\n\nfor hook in ${GIT_DIR}/hooks/${hookname}.d/*; do\ntest -x \"${hook}\" && test -f \"${hook}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType),
- }
- giteaHookTpls = []string{
- fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
- fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
- fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
- }
- )
-
+ hookNames, hookTpls, giteaHookTpls := getHookTemplates()
hookDir := filepath.Join(repoPath, "hooks")
for i, hookName := range hookNames {
@@ -58,17 +60,96 @@ func createDelegateHooks(repoPath string) (err error) {
return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err)
}
+ if err = ensureExecutable(oldHookPath); err != nil {
+ return fmt.Errorf("Unable to set %s executable. Error %v", oldHookPath, err)
+ }
+
if err = os.Remove(newHookPath); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unable to pre-remove new hook file '%s' prior to rewriting: %v", newHookPath, err)
}
if err = ioutil.WriteFile(newHookPath, []byte(giteaHookTpls[i]), 0777); err != nil {
return fmt.Errorf("write new hook file '%s': %v", newHookPath, err)
}
+
+ if err = ensureExecutable(newHookPath); err != nil {
+ return fmt.Errorf("Unable to set %s executable. Error %v", oldHookPath, err)
+ }
}
return nil
}
+func checkExecutable(filename string) bool {
+ fileInfo, err := os.Stat(filename)
+ if err != nil {
+ return false
+ }
+ return (fileInfo.Mode() & 0100) > 0
+}
+
+func ensureExecutable(filename string) error {
+ fileInfo, err := os.Stat(filename)
+ if err != nil {
+ return err
+ }
+ if (fileInfo.Mode() & 0100) > 0 {
+ return nil
+ }
+ mode := fileInfo.Mode() | 0100
+ return os.Chmod(filename, mode)
+}
+
+// CheckDelegateHooks checks the hooks scripts for the repo
+func CheckDelegateHooks(repoPath string) ([]string, error) {
+ hookNames, hookTpls, giteaHookTpls := getHookTemplates()
+
+ hookDir := filepath.Join(repoPath, "hooks")
+ results := make([]string, 0, 10)
+
+ for i, hookName := range hookNames {
+ oldHookPath := filepath.Join(hookDir, hookName)
+ newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")
+
+ cont := false
+ if !com.IsExist(oldHookPath) {
+ results = append(results, fmt.Sprintf("old hook file %s does not exist", oldHookPath))
+ cont = true
+ }
+ if !com.IsExist(oldHookPath + ".d") {
+ results = append(results, fmt.Sprintf("hooks directory %s does not exist", oldHookPath+".d"))
+ cont = true
+ }
+ if !com.IsExist(newHookPath) {
+ results = append(results, fmt.Sprintf("new hook file %s does not exist", newHookPath))
+ cont = true
+ }
+ if cont {
+ continue
+ }
+ contents, err := ioutil.ReadFile(oldHookPath)
+ if err != nil {
+ return results, err
+ }
+ if string(contents) != hookTpls[i] {
+ results = append(results, fmt.Sprintf("old hook file %s is out of date", oldHookPath))
+ }
+ if !checkExecutable(oldHookPath) {
+ results = append(results, fmt.Sprintf("old hook file %s is not executable", oldHookPath))
+ }
+ contents, err = ioutil.ReadFile(newHookPath)
+ if err != nil {
+ return results, err
+ }
+ if string(contents) != giteaHookTpls[i] {
+ results = append(results, fmt.Sprintf("new hook file %s is out of date", newHookPath))
+ }
+ if !checkExecutable(newHookPath) {
+ results = append(results, fmt.Sprintf("new hook file %s is not executable", newHookPath))
+ }
+ }
+ return results, nil
+}
+
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
// to make sure the binary and custom conf path are up-to-date.
func SyncRepositoryHooks(ctx context.Context) error {