Skip to content

Commit 837b280

Browse files
committed
fix forget migration for wiki hooks (#1227)
1 parent 2b5e4b4 commit 837b280

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ var migrations = []Migration{
9292
NewMigration("use new avatar path name for security reason", useNewNameAvatars),
9393
// v21 -> v22
9494
NewMigration("rewrite authorized_keys file via new format", useNewPublickeyFormat),
95+
// v21 -> v22
96+
NewMigration("generate and migrate wiki Git hooks", generateAndMigrateWikiGitHooks),
9597
}
9698

9799
// Migrate database to current version

models/migrations/v22.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
"fmt"
9+
"io/ioutil"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
14+
"code.gitea.io/gitea/modules/setting"
15+
16+
"github.com/Unknwon/com"
17+
"github.com/go-xorm/xorm"
18+
)
19+
20+
func generateAndMigrateWikiGitHooks(x *xorm.Engine) (err error) {
21+
type Repository struct {
22+
ID int64
23+
OwnerID int64
24+
Name string
25+
}
26+
type User struct {
27+
ID int64
28+
Name string
29+
}
30+
31+
var (
32+
hookNames = []string{"pre-receive", "update", "post-receive"}
33+
hookTpls = []string{
34+
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),
35+
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),
36+
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),
37+
}
38+
giteaHookTpls = []string{
39+
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
40+
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
41+
fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
42+
}
43+
)
44+
45+
return x.Where("id > 0").Iterate(new(Repository),
46+
func(idx int, bean interface{}) error {
47+
repo := bean.(*Repository)
48+
user := new(User)
49+
has, err := x.Where("id = ?", repo.OwnerID).Get(user)
50+
if err != nil {
51+
return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
52+
} else if !has {
53+
return nil
54+
}
55+
56+
repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".wiki.git"
57+
if !com.IsExist(repoPath) {
58+
return nil
59+
}
60+
hookDir := filepath.Join(repoPath, "hooks")
61+
62+
for i, hookName := range hookNames {
63+
oldHookPath := filepath.Join(hookDir, hookName)
64+
newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")
65+
66+
customHooksDir := filepath.Join(hookDir, hookName+".d")
67+
// if it's exist, that means you have upgraded ever
68+
if com.IsExist(customHooksDir) {
69+
continue
70+
}
71+
72+
if err = os.MkdirAll(customHooksDir, os.ModePerm); err != nil {
73+
return fmt.Errorf("create hooks dir '%s': %v", customHooksDir, err)
74+
}
75+
76+
// WARNING: Old server-side hooks will be moved to sub directory with the same name
77+
if hookName != "update" && com.IsExist(oldHookPath) {
78+
newPlace := filepath.Join(hookDir, hookName+".d", hookName)
79+
if err = os.Rename(oldHookPath, newPlace); err != nil {
80+
return fmt.Errorf("Remove old hook file '%s' to '%s': %v", oldHookPath, newPlace, err)
81+
}
82+
}
83+
84+
if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil {
85+
return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err)
86+
}
87+
88+
if err = ioutil.WriteFile(newHookPath, []byte(giteaHookTpls[i]), 0777); err != nil {
89+
return fmt.Errorf("write new hook file '%s': %v", oldHookPath, err)
90+
}
91+
}
92+
return nil
93+
})
94+
}

0 commit comments

Comments
 (0)