|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package migrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/md5" |
| 9 | + "encoding/hex" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "io/ioutil" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "code.gitea.io/gitea/modules/setting" |
| 18 | + |
| 19 | + "github.com/Unknwon/com" |
| 20 | + "github.com/go-xorm/xorm" |
| 21 | +) |
| 22 | + |
| 23 | +func generateAndMigrateGitHookChains(x *xorm.Engine) (err error) { |
| 24 | + type Repository struct { |
| 25 | + ID int64 |
| 26 | + OwnerID int64 |
| 27 | + Name string |
| 28 | + } |
| 29 | + type User struct { |
| 30 | + ID int64 |
| 31 | + Name string |
| 32 | + } |
| 33 | + |
| 34 | + var ( |
| 35 | + hookNames = []string{"pre-receive", "update", "post-receive"} |
| 36 | + 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) |
| 37 | + ) |
| 38 | + |
| 39 | + return x.Where("id > 0").Iterate(new(Repository), |
| 40 | + func(idx int, bean interface{}) error { |
| 41 | + repo := bean.(*Repository) |
| 42 | + user := new(User) |
| 43 | + has, err := x.Where("id = ?", repo.OwnerID).Get(user) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err) |
| 46 | + } else if !has { |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + repoPaths := []string{ |
| 51 | + filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git", |
| 52 | + filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".wiki.git", |
| 53 | + } |
| 54 | + |
| 55 | + for _, repoPath := range repoPaths { |
| 56 | + if com.IsExist(repoPath) { |
| 57 | + hookDir := filepath.Join(repoPath, "hooks") |
| 58 | + |
| 59 | + for _, hookName := range hookNames { |
| 60 | + oldHookPath := filepath.Join(hookDir, hookName) |
| 61 | + |
| 62 | + // compare md5sums of hooks |
| 63 | + if com.IsExist(oldHookPath) { |
| 64 | + |
| 65 | + f, err := os.Open(oldHookPath) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("cannot open old hook file '%s': %v", oldHookPath, err) |
| 68 | + } |
| 69 | + defer f.Close() |
| 70 | + h := md5.New() |
| 71 | + if _, err := io.Copy(h, f); err != nil { |
| 72 | + return fmt.Errorf("cannot read old hook file '%s': %v", oldHookPath, err) |
| 73 | + } |
| 74 | + if hex.EncodeToString(h.Sum(nil)) == "6718ef67d0834e0a7908259acd566e3f" { |
| 75 | + return nil |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if err = ioutil.WriteFile(oldHookPath, []byte(hookTpl), 0777); err != nil { |
| 80 | + return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return nil |
| 86 | + }) |
| 87 | +} |
0 commit comments