Skip to content

Commit 10ef557

Browse files
committed
sync git hook path
1 parent 8af5450 commit 10ef557

File tree

8 files changed

+216
-55
lines changed

8 files changed

+216
-55
lines changed

contrib/pr/checkout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func runPR() {
9191
dbCfg.NewKey("DB_TYPE", "sqlite3")
9292
dbCfg.NewKey("PATH", ":memory:")
9393

94-
routers.NewServices()
94+
routers.InitGitServices()
9595
setting.Database.LogSQL = true
9696
//x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
9797

modules/appstate/appstate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 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 appstate
6+
7+
// StateStore is the interface to get/set app state items
8+
type StateStore interface {
9+
Get(item StateItem) error
10+
Set(item StateItem) error
11+
Delete(item StateItem) error
12+
}
13+
14+
// StateItem provides the name for a state item. the name will be used to generate filenames, etc
15+
type StateItem interface {
16+
Name() string
17+
}

modules/appstate/file.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 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 appstate
6+
7+
import (
8+
"io/fs"
9+
"io/ioutil"
10+
"os"
11+
"path"
12+
13+
"code.gitea.io/gitea/modules/json"
14+
)
15+
16+
// FileStore can be used to store app state items in local filesystem
17+
type FileStore struct {
18+
path string
19+
}
20+
21+
func (f *FileStore) genFilePath(item StateItem) string {
22+
return path.Join(f.path, item.Name())
23+
}
24+
25+
// Get reads the state item
26+
func (f *FileStore) Get(item StateItem) error {
27+
b, e := ioutil.ReadFile(f.genFilePath(item))
28+
if os.IsNotExist(e) {
29+
return nil
30+
}
31+
if e != nil {
32+
return e
33+
}
34+
e = json.Unmarshal(b, item)
35+
return e
36+
}
37+
38+
//Set saves the state item
39+
func (f *FileStore) Set(item StateItem) error {
40+
b, e := json.Marshal(item)
41+
if e != nil {
42+
return e
43+
}
44+
return ioutil.WriteFile(f.genFilePath(item), b, fs.FileMode(0644))
45+
}
46+
47+
//Delete removes the state item
48+
func (f *FileStore) Delete(item StateItem) error {
49+
return os.Remove(f.genFilePath(item))
50+
}
51+
52+
//NewFileStore returns a new file store
53+
func NewFileStore(path string) (*FileStore, error) {
54+
_ = os.Mkdir(path, fs.FileMode(0755))
55+
if _, err := os.Stat(path); err != nil {
56+
return nil, err
57+
}
58+
return &FileStore{path: path}, nil
59+
}

modules/appstate/item_runtime.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2021 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 appstate
6+
7+
// RuntimeState contains app state for runtime, and we can save remote version for update checker here in future
8+
type RuntimeState struct {
9+
LastAppPath string `json:"last_app_path"`
10+
}
11+
12+
//Name returns the item name
13+
func (a RuntimeState) Name() string {
14+
return "runtime-state"
15+
}

modules/repository/hooks.go

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,64 +23,90 @@ import (
2323
func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) {
2424
hookNames = []string{"pre-receive", "update", "post-receive"}
2525
hookTpls = []string{
26+
// for pre-receive
2627
fmt.Sprintf(`#!/usr/bin/env %s
28+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
2729
data=$(cat)
2830
exitcodes=""
2931
hookname=$(basename $0)
3032
GIT_DIR=${GIT_DIR:-$(dirname $0)/..}
3133
3234
for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
33-
test -x "${hook}" && test -f "${hook}" || continue
34-
echo "${data}" | "${hook}"
35-
exitcodes="${exitcodes} $?"
35+
test -x "${hook}" && test -f "${hook}" || continue
36+
echo "${data}" | "${hook}"
37+
exitcodes="${exitcodes} $?"
3638
done
3739
3840
for i in ${exitcodes}; do
39-
[ ${i} -eq 0 ] || exit ${i}
41+
[ ${i} -eq 0 ] || exit ${i}
4042
done
4143
`, setting.ScriptType),
44+
45+
// for update
4246
fmt.Sprintf(`#!/usr/bin/env %s
47+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
4348
exitcodes=""
4449
hookname=$(basename $0)
4550
GIT_DIR=${GIT_DIR:-$(dirname $0/..)}
4651
4752
for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
48-
test -x "${hook}" && test -f "${hook}" || continue
49-
"${hook}" $1 $2 $3
50-
exitcodes="${exitcodes} $?"
53+
test -x "${hook}" && test -f "${hook}" || continue
54+
"${hook}" $1 $2 $3
55+
exitcodes="${exitcodes} $?"
5156
done
5257
5358
for i in ${exitcodes}; do
54-
[ ${i} -eq 0 ] || exit ${i}
59+
[ ${i} -eq 0 ] || exit ${i}
5560
done
5661
`, setting.ScriptType),
62+
63+
// for post-receive
5764
fmt.Sprintf(`#!/usr/bin/env %s
65+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
5866
data=$(cat)
5967
exitcodes=""
6068
hookname=$(basename $0)
6169
GIT_DIR=${GIT_DIR:-$(dirname $0)/..}
6270
6371
for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
64-
test -x "${hook}" && test -f "${hook}" || continue
65-
echo "${data}" | "${hook}"
66-
exitcodes="${exitcodes} $?"
72+
test -x "${hook}" && test -f "${hook}" || continue
73+
echo "${data}" | "${hook}"
74+
exitcodes="${exitcodes} $?"
6775
done
6876
6977
for i in ${exitcodes}; do
70-
[ ${i} -eq 0 ] || exit ${i}
78+
[ ${i} -eq 0 ] || exit ${i}
7179
done
7280
`, setting.ScriptType),
7381
}
82+
7483
giteaHookTpls = []string{
75-
fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s pre-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
76-
fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s update $1 $2 $3\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
77-
fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s post-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
84+
// for pre-receive
85+
fmt.Sprintf(`#!/usr/bin/env %s
86+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
87+
%s hook --config=%s pre-receive
88+
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
89+
90+
// for update
91+
fmt.Sprintf(`#!/usr/bin/env %s
92+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
93+
%s hook --config=%s update $1 $2 $3
94+
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
95+
96+
// for post-receive
97+
fmt.Sprintf(`#!/usr/bin/env %s
98+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
99+
%s hook --config=%s post-receive
100+
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)),
78101
}
79102

80103
if git.SupportProcReceive {
81104
hookNames = append(hookNames, "proc-receive")
82105
hookTpls = append(hookTpls,
83-
fmt.Sprintf("#!/usr/bin/env %s\n%s hook --config=%s proc-receive\n", setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)))
106+
fmt.Sprintf(`#!/usr/bin/env %s
107+
# AUTO GENERATED BY GITEA, DO NOT MODIFY
108+
%s hook --config=%s proc-receive
109+
`, setting.ScriptType, util.ShellEscape(setting.AppPath), util.ShellEscape(setting.CustomConf)))
84110
giteaHookTpls = append(giteaHookTpls, "")
85111
}
86112

modules/setting/appstate.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 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 setting
6+
7+
import (
8+
"path"
9+
10+
"code.gitea.io/gitea/modules/appstate"
11+
"code.gitea.io/gitea/modules/log"
12+
)
13+
14+
// AppState contains the state items for the app
15+
var AppState appstate.StateStore
16+
17+
func newAppState() {
18+
var err error
19+
appStatePath := path.Join(AppDataPath, "appstate")
20+
AppState, err = appstate.NewFileStore(appStatePath)
21+
if err != nil {
22+
log.Fatal("failed to init AppState, err = %v", err)
23+
}
24+
}

modules/setting/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ func CreateOrAppendToCustomConf(callback func(cfg *ini.File)) {
11881188
func NewServices() {
11891189
InitDBConfig()
11901190
newService()
1191+
newAppState()
11911192
newOAuth2Client()
11921193
NewLogServices(false)
11931194
newCacheService()

0 commit comments

Comments
 (0)