Skip to content

Commit 6968190

Browse files
authored
Merge branch 'main' into bugfix/svg_side_by_side_comparison
2 parents d5d0d22 + 83df0ca commit 6968190

File tree

11 files changed

+338
-55
lines changed

11 files changed

+338
-55
lines changed

contrib/pr/checkout.go

+1-1
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

models/appstate/appstate.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"context"
9+
10+
"code.gitea.io/gitea/models/db"
11+
)
12+
13+
// AppState represents a state record in database
14+
// if one day we would make Gitea run as a cluster,
15+
// we can introduce a new field `Scope` here to store different states for different nodes
16+
type AppState struct {
17+
ID string `xorm:"pk varchar(200)"`
18+
Revision int64
19+
Content string `xorm:"LONGTEXT"`
20+
}
21+
22+
func init() {
23+
db.RegisterModel(new(AppState))
24+
}
25+
26+
// SaveAppStateContent saves the app state item to database
27+
func SaveAppStateContent(key, content string) error {
28+
return db.WithTx(func(ctx context.Context) error {
29+
eng := db.GetEngine(ctx)
30+
// try to update existing row
31+
res, err := eng.Exec("UPDATE app_state SET revision=revision+1, content=? WHERE id=?", content, key)
32+
if err != nil {
33+
return err
34+
}
35+
rows, _ := res.RowsAffected()
36+
if rows != 0 {
37+
// the existing row is updated, so we can return
38+
return nil
39+
}
40+
// if no existing row, insert a new row
41+
_, err = eng.Insert(&AppState{ID: key, Content: content})
42+
return err
43+
})
44+
}
45+
46+
// GetAppStateContent gets an app state from database
47+
func GetAppStateContent(key string) (content string, err error) {
48+
e := db.GetEngine(db.DefaultContext)
49+
appState := &AppState{ID: key}
50+
has, err := e.Get(appState)
51+
if err != nil {
52+
return "", err
53+
} else if !has {
54+
return "", nil
55+
}
56+
return appState.Content, nil
57+
}

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ var migrations = []Migration{
352352
NewMigration("Add issue content history table", addTableIssueContentHistory),
353353
// v199 -> v200
354354
NewMigration("Add remote version table", addRemoteVersionTable),
355+
// v200 -> v201
356+
NewMigration("Add table app_state", addTableAppState),
355357
}
356358

357359
// GetCurrentDBVersion returns the current db version

models/migrations/v200.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 migrations
6+
7+
import (
8+
"fmt"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addTableAppState(x *xorm.Engine) error {
14+
type AppState struct {
15+
ID string `xorm:"pk varchar(200)"`
16+
Revision int64
17+
Content string `xorm:"LONGTEXT"`
18+
}
19+
if err := x.Sync2(new(AppState)); err != nil {
20+
return fmt.Errorf("Sync2: %v", err)
21+
}
22+
return nil
23+
}

modules/appstate/appstate.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
12+
13+
// StateItem provides the name for a state item. the name will be used to generate filenames, etc
14+
type StateItem interface {
15+
Name() string
16+
}
17+
18+
// AppState contains the state items for the app
19+
var AppState StateStore
20+
21+
// Init initialize AppState interface
22+
func Init() error {
23+
AppState = &DBStore{}
24+
return nil
25+
}

modules/appstate/appstate_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"path/filepath"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models/db"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestMain(m *testing.M) {
17+
db.MainTest(m, filepath.Join("..", ".."), "")
18+
}
19+
20+
type testItem1 struct {
21+
Val1 string
22+
Val2 int
23+
}
24+
25+
func (*testItem1) Name() string {
26+
return "test-item1"
27+
}
28+
29+
type testItem2 struct {
30+
K string
31+
}
32+
33+
func (*testItem2) Name() string {
34+
return "test-item2"
35+
}
36+
37+
func TestAppStateDB(t *testing.T) {
38+
assert.NoError(t, db.PrepareTestDatabase())
39+
40+
as := &DBStore{}
41+
42+
item1 := new(testItem1)
43+
assert.NoError(t, as.Get(item1))
44+
assert.Equal(t, "", item1.Val1)
45+
assert.EqualValues(t, 0, item1.Val2)
46+
47+
item1 = new(testItem1)
48+
item1.Val1 = "a"
49+
item1.Val2 = 2
50+
assert.NoError(t, as.Set(item1))
51+
52+
item2 := new(testItem2)
53+
item2.K = "V"
54+
assert.NoError(t, as.Set(item2))
55+
56+
item1 = new(testItem1)
57+
assert.NoError(t, as.Get(item1))
58+
assert.Equal(t, "a", item1.Val1)
59+
assert.EqualValues(t, 2, item1.Val2)
60+
61+
item2 = new(testItem2)
62+
assert.NoError(t, as.Get(item2))
63+
assert.Equal(t, "V", item2.K)
64+
}

modules/appstate/db.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"code.gitea.io/gitea/models/appstate"
9+
"code.gitea.io/gitea/modules/json"
10+
11+
"github.com/yuin/goldmark/util"
12+
)
13+
14+
// DBStore can be used to store app state items in local filesystem
15+
type DBStore struct {
16+
}
17+
18+
// Get reads the state item
19+
func (f *DBStore) Get(item StateItem) error {
20+
content, err := appstate.GetAppStateContent(item.Name())
21+
if err != nil {
22+
return err
23+
}
24+
if content == "" {
25+
return nil
26+
}
27+
return json.Unmarshal(util.StringToReadOnlyBytes(content), item)
28+
}
29+
30+
// Set saves the state item
31+
func (f *DBStore) Set(item StateItem) error {
32+
b, err := json.Marshal(item)
33+
if err != nil {
34+
return err
35+
}
36+
return appstate.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b))
37+
}

modules/appstate/item_runtime.go

+15
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

+42-16
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/setting.go

+12
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,18 @@ func NewContext() {
683683
StaticRootPath = sec.Key("STATIC_ROOT_PATH").MustString(StaticRootPath)
684684
StaticCacheTime = sec.Key("STATIC_CACHE_TIME").MustDuration(6 * time.Hour)
685685
AppDataPath = sec.Key("APP_DATA_PATH").MustString(path.Join(AppWorkPath, "data"))
686+
if _, err = os.Stat(AppDataPath); err != nil {
687+
// FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
688+
// For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs,
689+
// then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem.
690+
// The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories.
691+
// For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK).
692+
// Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future.
693+
err = os.MkdirAll(AppDataPath, os.ModePerm)
694+
if err != nil {
695+
log.Fatal("Failed to create the directory for app data path '%s'", AppDataPath)
696+
}
697+
}
686698
EnableGzip = sec.Key("ENABLE_GZIP").MustBool()
687699
EnablePprof = sec.Key("ENABLE_PPROF").MustBool(false)
688700
PprofDataPath = sec.Key("PPROF_DATA_PATH").MustString(path.Join(AppWorkPath, "data/tmp/pprof"))

0 commit comments

Comments
 (0)