From 6be2f3412e5bebdd368ba29a4904ef5268f6d453 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Wed, 15 Mar 2017 00:12:35 +0100 Subject: [PATCH 1/7] issue #1250, replace {pre,post}-receive and update hooks with a single shell script that does not require custom hooks to be a sh-script --- models/repo.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/models/repo.go b/models/repo.go index d44f4ba4899c9..878347613e4a3 100644 --- a/models/repo.go +++ b/models/repo.go @@ -845,11 +845,7 @@ func cleanUpMigrateGitConfig(configPath string) error { func createDelegateHooks(repoPath string) (err error) { var ( hookNames = []string{"pre-receive", "update", "post-receive"} - hookTpls = []string{ - fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/pre-receive.d\"`; do\n sh \"$SHELL_FOLDER/pre-receive.d/$i\"\ndone", setting.ScriptType), - fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/update.d\"`; do\n sh \"$SHELL_FOLDER/update.d/$i\" $1 $2 $3\ndone", setting.ScriptType), - fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/post-receive.d\"`; do\n sh \"$SHELL_FOLDER/post-receive.d/$i\"\ndone", setting.ScriptType), - } + 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+=($?)\ndone\n\nfor i in \"${exitcodes[@]}\"; do\n[ \"${i}\" == 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), @@ -868,7 +864,7 @@ func createDelegateHooks(repoPath string) (err error) { } // WARNING: This will override all old server-side hooks - if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil { + if err = ioutil.WriteFile(oldHookPath, []byte(hookTpl), 0777); err != nil { return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err) } From 11082ae246f992c799b815ca109a2ea6988b32a1 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Wed, 15 Mar 2017 10:21:29 +0100 Subject: [PATCH 2/7] issue #1250, make script posix compilant --- models/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/repo.go b/models/repo.go index 878347613e4a3..d3a7f0f03427b 100644 --- a/models/repo.go +++ b/models/repo.go @@ -845,7 +845,7 @@ func cleanUpMigrateGitConfig(configPath string) error { 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+=($?)\ndone\n\nfor i in \"${exitcodes[@]}\"; do\n[ \"${i}\" == 0 ] || exit ${i}\ndone\n", setting.ScriptType) + 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) 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), From d79048285aa91f6e0f6a49e1f36022de3d5a6d41 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Tue, 21 Mar 2017 16:36:36 +0100 Subject: [PATCH 3/7] v23, add migration script to update {pre,post}-receive and update hooks --- models/migrations/migrations.go | 2 + models/migrations/v23.go | 83 +++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 models/migrations/v23.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 1425dbffcff47..a4a4ba9246dd6 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -94,6 +94,8 @@ var migrations = []Migration{ NewMigration("rewrite authorized_keys file via new format", useNewPublickeyFormat), // v21 -> v22 NewMigration("generate and migrate wiki Git hooks", generateAndMigrateWikiGitHooks), + // v23 -> v24 + NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHooksV23), } // Migrate database to current version diff --git a/models/migrations/v23.go b/models/migrations/v23.go new file mode 100644 index 0000000000000..5e72bcd935ef3 --- /dev/null +++ b/models/migrations/v23.go @@ -0,0 +1,83 @@ +package migrations + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" + "io" + "crypto/md5" + "encoding/hex" + + "code.gitea.io/gitea/modules/setting" + + "github.com/Unknwon/com" + "github.com/go-xorm/xorm" +) + +func generateAndMigrateGitHooksV23(x *xorm.Engine) (err error) { + type Repository struct { + ID int64 + OwnerID int64 + Name string + } + type User struct { + ID int64 + Name string + } + + 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) + ) + + return x.Where("id > 0").Iterate(new(Repository), + func(idx int, bean interface{}) error { + repo := bean.(*Repository) + user := new(User) + has, err := x.Where("id = ?", repo.OwnerID).Get(user) + if err != nil { + return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err) + } else if !has { + return nil + } + + repoPaths := []string{ + filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git", + filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".wiki.git", + } + + for _, repoPath := range repoPaths { + if com.IsExist(repoPath) { + hookDir := filepath.Join(repoPath, "hooks") + + for _, hookName := range hookNames { + oldHookPath := filepath.Join(hookDir, hookName) + + // compare md5sums of hooks + if com.IsExist(oldHookPath) { + + f, err := os.Open(oldHookPath) + if err != nil { + return fmt.Errorf("cannot open old hook file '%s': %v", oldHookPath, err) + } + defer f.Close() + h := md5.New() + if _,err := io.Copy(h,f); err != nil { + return fmt.Errorf("cannot read old hook file '%s': %v", oldHookPath, err) + } + if hex.EncodeToString(h.Sum(nil)) == "6718ef67d0834e0a7908259acd566e3f" { + return nil + } + } + + if err = ioutil.WriteFile(oldHookPath, []byte(hookTpl), 0777); err != nil { + return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err) + } + } + } + } + return nil + }) +} From e5ff83272b9a8c305c2524d15fec35bb4bf2e4be Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Tue, 21 Mar 2017 16:46:34 +0100 Subject: [PATCH 4/7] migration: use a more common name and rename v23 to v26 to avoid conflicts --- models/migrations/migrations.go | 2 +- models/migrations/{v23.go => v26.go} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename models/migrations/{v23.go => v26.go} (97%) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index a4a4ba9246dd6..57111a0a397c7 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -95,7 +95,7 @@ var migrations = []Migration{ // v21 -> v22 NewMigration("generate and migrate wiki Git hooks", generateAndMigrateWikiGitHooks), // v23 -> v24 - NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHooksV23), + NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains), } // Migrate database to current version diff --git a/models/migrations/v23.go b/models/migrations/v26.go similarity index 97% rename from models/migrations/v23.go rename to models/migrations/v26.go index 5e72bcd935ef3..8f5a932539cc5 100644 --- a/models/migrations/v23.go +++ b/models/migrations/v26.go @@ -16,7 +16,7 @@ import ( "github.com/go-xorm/xorm" ) -func generateAndMigrateGitHooksV23(x *xorm.Engine) (err error) { +func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) { type Repository struct { ID int64 OwnerID int64 From f62e8e253e65ab556defebbaa0c6ad77946c3876 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Wed, 22 Mar 2017 10:35:58 +0100 Subject: [PATCH 5/7] gofmt'ed and added copyright header --- models/migrations/v26.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/models/migrations/v26.go b/models/migrations/v26.go index 8f5a932539cc5..8b1c9a6326f44 100644 --- a/models/migrations/v26.go +++ b/models/migrations/v26.go @@ -1,14 +1,18 @@ +// 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 ( + "crypto/md5" + "encoding/hex" "fmt" + "io" "io/ioutil" "os" "path/filepath" "strings" - "io" - "crypto/md5" - "encoding/hex" "code.gitea.io/gitea/modules/setting" @@ -29,7 +33,7 @@ func generateAndMigrateGitHookChains(x *xorm.Engine) (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) + 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) ) return x.Where("id > 0").Iterate(new(Repository), @@ -64,7 +68,7 @@ func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) { } defer f.Close() h := md5.New() - if _,err := io.Copy(h,f); err != nil { + if _, err := io.Copy(h, f); err != nil { return fmt.Errorf("cannot read old hook file '%s': %v", oldHookPath, err) } if hex.EncodeToString(h.Sum(nil)) == "6718ef67d0834e0a7908259acd566e3f" { From e01bf52fb9e85da3539ae565f765972c8df8f660 Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Wed, 22 Mar 2017 10:59:23 +0100 Subject: [PATCH 6/7] fix SyncRepositoryHooks to also sync wiki repos --- models/repo.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/models/repo.go b/models/repo.go index d3a7f0f03427b..ecde6cca2175d 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1905,6 +1905,11 @@ func SyncRepositoryHooks() error { if err := createDelegateHooks(bean.(*Repository).RepoPath()); err != nil { return fmt.Errorf("SyncRepositoryHook: %v", err) } + if bean.(*Repository).HasWiki() { + if err := createDelegateHooks(bean.(*Repository).WikiPath()); err != nil { + return fmt.Errorf("SyncRepositoryHook: %v", err) + } + } return nil }) } From 7e52f013a35615abe67a8d50383233943fe347ae Mon Sep 17 00:00:00 2001 From: Philippe Kueck Date: Thu, 23 Mar 2017 18:22:16 +0100 Subject: [PATCH 7/7] no migration for you. --- models/migrations/migrations.go | 2 - models/migrations/v26.go | 87 --------------------------------- 2 files changed, 89 deletions(-) delete mode 100644 models/migrations/v26.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 57111a0a397c7..1425dbffcff47 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -94,8 +94,6 @@ var migrations = []Migration{ NewMigration("rewrite authorized_keys file via new format", useNewPublickeyFormat), // v21 -> v22 NewMigration("generate and migrate wiki Git hooks", generateAndMigrateWikiGitHooks), - // v23 -> v24 - NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains), } // Migrate database to current version diff --git a/models/migrations/v26.go b/models/migrations/v26.go deleted file mode 100644 index 8b1c9a6326f44..0000000000000 --- a/models/migrations/v26.go +++ /dev/null @@ -1,87 +0,0 @@ -// 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 ( - "crypto/md5" - "encoding/hex" - "fmt" - "io" - "io/ioutil" - "os" - "path/filepath" - "strings" - - "code.gitea.io/gitea/modules/setting" - - "github.com/Unknwon/com" - "github.com/go-xorm/xorm" -) - -func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) { - type Repository struct { - ID int64 - OwnerID int64 - Name string - } - type User struct { - ID int64 - Name string - } - - 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) - ) - - return x.Where("id > 0").Iterate(new(Repository), - func(idx int, bean interface{}) error { - repo := bean.(*Repository) - user := new(User) - has, err := x.Where("id = ?", repo.OwnerID).Get(user) - if err != nil { - return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err) - } else if !has { - return nil - } - - repoPaths := []string{ - filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git", - filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".wiki.git", - } - - for _, repoPath := range repoPaths { - if com.IsExist(repoPath) { - hookDir := filepath.Join(repoPath, "hooks") - - for _, hookName := range hookNames { - oldHookPath := filepath.Join(hookDir, hookName) - - // compare md5sums of hooks - if com.IsExist(oldHookPath) { - - f, err := os.Open(oldHookPath) - if err != nil { - return fmt.Errorf("cannot open old hook file '%s': %v", oldHookPath, err) - } - defer f.Close() - h := md5.New() - if _, err := io.Copy(h, f); err != nil { - return fmt.Errorf("cannot read old hook file '%s': %v", oldHookPath, err) - } - if hex.EncodeToString(h.Sum(nil)) == "6718ef67d0834e0a7908259acd566e3f" { - return nil - } - } - - if err = ioutil.WriteFile(oldHookPath, []byte(hookTpl), 0777); err != nil { - return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err) - } - } - } - } - return nil - }) -}