From f189ccd2d60231485f045ce70a523fe12e401912 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Sun, 2 Jul 2017 16:50:57 +0300 Subject: [PATCH] Fix git hooks update to receive required arguments. Fixes #2090 (#2095) * Changed migration calling so that migrations can use models package --- models/migrations/migrations.go | 2 ++ models/migrations/v36.go | 15 +++++++++++++++ models/models.go | 5 ++--- models/repo.go | 10 +++++++--- routers/init.go | 3 ++- 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 models/migrations/v36.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index ae15c9f47..437b63f60 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -120,6 +120,8 @@ var migrations = []Migration{ NewMigration("give all units to owner teams", giveAllUnitsToOwnerTeams), // v35 -> v36 NewMigration("adds comment to an action", addCommentIDToAction), + // v36 -> v37 + NewMigration("regenerate git hooks", regenerateGitHooks36), } // Migrate database to current version diff --git a/models/migrations/v36.go b/models/migrations/v36.go new file mode 100644 index 000000000..06f76a26d --- /dev/null +++ b/models/migrations/v36.go @@ -0,0 +1,15 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "code.gitea.io/gitea/models" + + "github.com/go-xorm/xorm" +) + +func regenerateGitHooks36(x *xorm.Engine) (err error) { + return models.SyncRepositoryHooks() +} diff --git a/models/models.go b/models/models.go index 6a8fbfe62..1688a6364 100644 --- a/models/models.go +++ b/models/models.go @@ -24,7 +24,6 @@ import ( // Needed for the MSSSQL driver _ "github.com/denisenkom/go-mssqldb" - "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -259,7 +258,7 @@ func SetEngine() (err error) { } // NewEngine initializes a new xorm.Engine -func NewEngine() (err error) { +func NewEngine(migrateFunc func(*xorm.Engine) error) (err error) { if err = SetEngine(); err != nil { return err } @@ -268,7 +267,7 @@ func NewEngine() (err error) { return err } - if err = migrations.Migrate(x); err != nil { + if err = migrateFunc(x); err != nil { return fmt.Errorf("migrate: %v", err) } diff --git a/models/repo.go b/models/repo.go index 8d9713842..cb6d6d5c3 100644 --- a/models/repo.go +++ b/models/repo.go @@ -948,8 +948,12 @@ func cleanUpMigrateGitConfig(configPath 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"} - hookTpl = 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}\" || continue\necho \"${data}\" | \"${hook}\"\nexitcodes=\"${exitcodes} $?\"\ndone\n\nfor i in ${exitcodes}; do\n[ ${i} -eq 0 ] || exit ${i}\ndone\n", setting.ScriptType) + 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}\" || 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}\" || 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}\" || 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), @@ -968,7 +972,7 @@ func createDelegateHooks(repoPath string) (err error) { } // WARNING: This will override all old server-side hooks - if err = ioutil.WriteFile(oldHookPath, []byte(hookTpl), 0777); err != nil { + if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil { return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err) } diff --git a/routers/init.go b/routers/init.go index dec7f1818..ad624d3b7 100644 --- a/routers/init.go +++ b/routers/init.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/cron" "code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/indexer" @@ -50,7 +51,7 @@ func GlobalInit() { if setting.InstallLock { highlight.NewContext() markdown.NewSanitizer() - if err := models.NewEngine(); err != nil { + if err := models.NewEngine(migrations.Migrate); err != nil { log.Fatal(4, "Failed to initialize ORM engine: %v", err) } models.HasEngine = true