Skip to content

Commit d105211

Browse files
authored
Merge branch 'main' into store-team-read-access
2 parents e83db34 + 36353e2 commit d105211

File tree

28 files changed

+301
-156
lines changed

28 files changed

+301
-156
lines changed

custom/conf/app.example.ini

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,10 @@ ROUTER = console
603603
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
604604
;;
605605
;; The path of git executable. If empty, Gitea searches through the PATH environment.
606-
PATH =
606+
;PATH =
607+
;;
608+
;; The HOME directory for Git
609+
;HOME_PATH = %(APP_DATA_PATH)/home
607610
;;
608611
;; Disables highlight of added and removed changes
609612
;DISABLE_DIFF_HIGHLIGHT = false
@@ -1231,7 +1234,7 @@ PATH =
12311234
;; Define allowed algorithms and their minimum key length (use -1 to disable a type)
12321235
;ED25519 = 256
12331236
;ECDSA = 256
1234-
;RSA = 2048
1237+
;RSA = 2047 ; we allow 2047 here because an otherwise valid 2048 bit RSA key can be reported as having 2047 bit length
12351238
;DSA = -1 ; set to 1024 to switch on
12361239

12371240
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
621621

622622
- `ED25519`: **256**
623623
- `ECDSA`: **256**
624-
- `RSA`: **2048**
624+
- `RSA`: **2047**: We set 2047 here because an otherwise valid 2048 RSA key can be reported as 2047 length.
625625
- `DSA`: **-1**: DSA is now disabled by default. Set to **1024** to re-enable but ensure you may need to reconfigure your SSHD provider
626626

627627
## Webhook (`webhook`)
@@ -948,6 +948,8 @@ Default templates for project boards:
948948
## Git (`git`)
949949

950950
- `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment.
951+
- `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git.
952+
This directory will be used to contain the `.gitconfig` and possible `.gnupg` directories that Gitea's git calls will use. If you can confirm Gitea is the only application running in this environment, you can set it to the normal home directory for Gitea user.
951953
- `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes.
952954
- `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view.
953955
- `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view.

docs/content/doc/advanced/signing.en-us.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ repositories, `SIGNING_KEY=default` could be used to provide different
9797
signing keys on a per-repository basis. However, this is clearly not an
9898
ideal UI and therefore subject to change.
9999

100-
**Since 1.17**, Gitea runs git in its own home directory `[repository].ROOT` and uses its own config `{[repository].ROOT}/.gitconfig`.
100+
**Since 1.17**, Gitea runs git in its own home directory `[git].HOME_PATH` (default to `%(APP_DATA_PATH)/home`)
101+
and uses its own config `{[git].HOME_PATH}/.gitconfig`.
101102
If you have your own customized git config for Gitea, you should set these configs in system git config (aka `/etc/gitconfig`)
102-
or the Gitea internal git config `{[repository].ROOT}/.gitconfig`.
103-
Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[repository].ROOT`.
103+
or the Gitea internal git config `{[git].HOME_PATH}/.gitconfig`.
104+
Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[git].HOME_PATH`.
104105

105106

106107
### `INITIAL_COMMIT`

models/issues/milestone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (opts GetMilestonesOption) toCond() builder.Cond {
361361
}
362362

363363
if len(opts.Name) != 0 {
364-
cond = cond.And(builder.Like{"name", opts.Name})
364+
cond = cond.And(builder.Like{"UPPER(name)", strings.ToUpper(opts.Name)})
365365
}
366366

367367
return cond

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ var migrations = []Migration{
396396
NewMigration("Alter hook_task table TEXT fields to LONGTEXT", alterHookTaskTextFieldsToLongText),
397397
// v218 -> v219
398398
NewMigration("Improve Action table indices v2", improveActionTableIndices),
399+
// v219 -> v220
400+
NewMigration("Add sync_on_commit column to push_mirror table", addSyncOnCommitColForPushMirror),
399401
}
400402

401403
// GetCurrentDBVersion returns the current db version

models/migrations/v219.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2022 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+
"time"
9+
10+
"code.gitea.io/gitea/models/repo"
11+
"code.gitea.io/gitea/modules/timeutil"
12+
"xorm.io/xorm"
13+
)
14+
15+
func addSyncOnCommitColForPushMirror(x *xorm.Engine) error {
16+
type PushMirror struct {
17+
ID int64 `xorm:"pk autoincr"`
18+
RepoID int64 `xorm:"INDEX"`
19+
Repo *repo.Repository `xorm:"-"`
20+
RemoteName string
21+
22+
SyncOnCommit bool `xorm:"NOT NULL DEFAULT true"`
23+
Interval time.Duration
24+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
25+
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
26+
LastError string `xorm:"text"`
27+
}
28+
29+
return x.Sync2(new(PushMirror))
30+
}

models/repo/pushmirror.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type PushMirror struct {
2323
Repo *Repository `xorm:"-"`
2424
RemoteName string
2525

26+
SyncOnCommit bool `xorm:"NOT NULL DEFAULT true"`
2627
Interval time.Duration
2728
CreatedUnix timeutil.TimeStamp `xorm:"created"`
2829
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"`
@@ -93,6 +94,14 @@ func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
9394
return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors)
9495
}
9596

97+
// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits
98+
func GetPushMirrorsSyncedOnCommit(repoID int64) ([]*PushMirror, error) {
99+
mirrors := make([]*PushMirror, 0, 10)
100+
return mirrors, db.GetEngine(db.DefaultContext).
101+
Where("repo_id=? AND sync_on_commit=?", repoID, true).
102+
Find(&mirrors)
103+
}
104+
96105
// PushMirrorsIterate iterates all push-mirror repositories.
97106
func PushMirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
98107
return db.GetEngine(db.DefaultContext).

models/unittest/testdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
107107

108108
setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
109109

110+
setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home")
111+
110112
if err = storage.Init(); err != nil {
111113
fatalTestError("storage.Init: %v\n", err)
112114
}

modules/git/command.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,36 @@ type RunOpts struct {
105105
PipelineFunc func(context.Context, context.CancelFunc) error
106106
}
107107

108-
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
109-
func CommonGitCmdEnvs() []string {
108+
func commonBaseEnvs() []string {
110109
// at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it
111-
return []string{
112-
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
113-
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
114-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
110+
envs := []string{
115111
"HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config
112+
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
113+
}
114+
115+
// some environment variables should be passed to git command
116+
passThroughEnvKeys := []string{
117+
"GNUPGHOME", // git may call gnupg to do commit signing
118+
}
119+
for _, key := range passThroughEnvKeys {
120+
if val, ok := os.LookupEnv(key); ok {
121+
envs = append(envs, key+"="+val)
122+
}
116123
}
124+
return envs
125+
}
126+
127+
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
128+
func CommonGitCmdEnvs() []string {
129+
return append(commonBaseEnvs(), []string{
130+
"LC_ALL=" + DefaultLocale,
131+
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
132+
}...)
117133
}
118134

119135
// CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command
120136
func CommonCmdServEnvs() []string {
121-
return []string{
122-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
123-
"HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config
124-
}
137+
return commonBaseEnvs()
125138
}
126139

127140
// Run runs the command with the RunOpts

modules/git/git.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"regexp"
1516
"runtime"
1617
"strings"
@@ -19,7 +20,6 @@ import (
1920

2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/setting"
22-
2323
"github.com/hashicorp/go-version"
2424
)
2525

@@ -126,8 +126,8 @@ func VersionInfo() string {
126126
}
127127

128128
func checkInit() error {
129-
if setting.RepoRootPath == "" {
130-
return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
129+
if setting.Git.HomePath == "" {
130+
return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
131131
}
132132
if DefaultContext != nil {
133133
log.Warn("git module has been initialized already, duplicate init should be fixed")
@@ -137,14 +137,14 @@ func checkInit() error {
137137

138138
// HomeDir is the home dir for git to store the global config file used by Gitea internally
139139
func HomeDir() string {
140-
if setting.RepoRootPath == "" {
140+
if setting.Git.HomePath == "" {
141141
// strict check, make sure the git module is initialized correctly.
142142
// attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users.
143143
// for example: if there is gitea git hook code calling git.NewCommand before git.InitXxx, the integration test won't show the real failure reasons.
144-
log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
144+
log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
145145
return ""
146146
}
147-
return setting.RepoRootPath
147+
return setting.Git.HomePath
148148
}
149149

150150
// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
@@ -175,11 +175,15 @@ func InitOnceWithSync(ctx context.Context) (err error) {
175175
}
176176

177177
initOnce.Do(func() {
178-
err = InitSimple(ctx)
179-
if err != nil {
178+
if err = InitSimple(ctx); err != nil {
180179
return
181180
}
182181

182+
// when git works with gnupg (commit signing), there should be a stable home for gnupg commands
183+
if _, ok := os.LookupEnv("GNUPGHOME"); !ok {
184+
_ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg"))
185+
}
186+
183187
// Since git wire protocol has been released from git v2.18
184188
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
185189
globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2")
@@ -206,7 +210,7 @@ func InitOnceWithSync(ctx context.Context) (err error) {
206210
// syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem)
207211
func syncGitConfig() (err error) {
208212
if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil {
209-
return fmt.Errorf("unable to create directory %s, err: %w", setting.RepoRootPath, err)
213+
return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err)
210214
}
211215

212216
// Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults"

modules/git/git_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
func testRun(m *testing.M) error {
2222
_ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`)
2323

24-
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
24+
gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home")
2525
if err != nil {
2626
return fmt.Errorf("unable to create temp dir: %w", err)
2727
}
28-
defer util.RemoveAll(repoRootPath)
29-
setting.RepoRootPath = repoRootPath
28+
defer util.RemoveAll(gitHomePath)
29+
setting.Git.HomePath = gitHomePath
3030

3131
if err = InitOnceWithSync(context.Background()); err != nil {
3232
return fmt.Errorf("failed to call Init: %w", err)

modules/markup/html.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,10 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
841841

842842
// Repos with external issue trackers might still need to reference local PRs
843843
// We need to concern with the first one that shows up in the text, whichever it is
844-
if hasExtTrackFormat && !isNumericStyle {
844+
if hasExtTrackFormat && !isNumericStyle && refNumeric != nil {
845845
// If numeric (PR) was found, and it was BEFORE the non-numeric pattern, use that
846-
if foundNumeric && refNumeric.RefLocation.Start < ref.RefLocation.Start {
846+
// Allow a free-pass when non-numeric pattern wasn't found.
847+
if found && (ref == nil || refNumeric.RefLocation.Start < ref.RefLocation.Start) {
847848
found = foundNumeric
848849
ref = refNumeric
849850
}

modules/mirror/mirror.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2022 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 mirror
6+
7+
import (
8+
"code.gitea.io/gitea/modules/graceful"
9+
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/queue"
11+
"code.gitea.io/gitea/modules/setting"
12+
)
13+
14+
var mirrorQueue queue.UniqueQueue
15+
16+
// SyncType type of sync request
17+
type SyncType int
18+
19+
const (
20+
// PullMirrorType for pull mirrors
21+
PullMirrorType SyncType = iota
22+
// PushMirrorType for push mirrors
23+
PushMirrorType
24+
)
25+
26+
// SyncRequest for the mirror queue
27+
type SyncRequest struct {
28+
Type SyncType
29+
ReferenceID int64 // RepoID for pull mirror, MirrorID for push mirror
30+
}
31+
32+
// StartSyncMirrors starts a go routine to sync the mirrors
33+
func StartSyncMirrors(queueHandle func(data ...queue.Data) []queue.Data) {
34+
if !setting.Mirror.Enabled {
35+
return
36+
}
37+
mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest))
38+
39+
go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run)
40+
}
41+
42+
// AddPullMirrorToQueue adds repoID to mirror queue
43+
func AddPullMirrorToQueue(repoID int64) {
44+
addMirrorToQueue(PullMirrorType, repoID)
45+
}
46+
47+
// AddPushMirrorToQueue adds the push mirror to the queue
48+
func AddPushMirrorToQueue(mirrorID int64) {
49+
addMirrorToQueue(PushMirrorType, mirrorID)
50+
}
51+
52+
func addMirrorToQueue(syncType SyncType, referenceID int64) {
53+
if !setting.Mirror.Enabled {
54+
return
55+
}
56+
go func() {
57+
if err := PushToQueue(syncType, referenceID); err != nil {
58+
log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]. Error: %v", referenceID, err)
59+
}
60+
}()
61+
}
62+
63+
// PushToQueue adds the sync request to the queue
64+
func PushToQueue(mirrorType SyncType, referenceID int64) error {
65+
return mirrorQueue.Push(&SyncRequest{
66+
Type: mirrorType,
67+
ReferenceID: referenceID,
68+
})
69+
}

modules/notification/mirror/mirror.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2022 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 mirror
6+
7+
import (
8+
repo_model "code.gitea.io/gitea/models/repo"
9+
user_model "code.gitea.io/gitea/models/user"
10+
"code.gitea.io/gitea/modules/log"
11+
mirror_module "code.gitea.io/gitea/modules/mirror"
12+
"code.gitea.io/gitea/modules/notification/base"
13+
"code.gitea.io/gitea/modules/repository"
14+
)
15+
16+
type mirrorNotifier struct {
17+
base.NullNotifier
18+
}
19+
20+
var _ base.Notifier = &mirrorNotifier{}
21+
22+
// NewNotifier create a new mirrorNotifier notifier
23+
func NewNotifier() base.Notifier {
24+
return &mirrorNotifier{}
25+
}
26+
27+
func (m *mirrorNotifier) NotifyPushCommits(_ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
28+
syncPushMirrorWithSyncOnCommit(repo.ID)
29+
}
30+
31+
func (m *mirrorNotifier) NotifySyncPushCommits(_ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
32+
syncPushMirrorWithSyncOnCommit(repo.ID)
33+
}
34+
35+
func syncPushMirrorWithSyncOnCommit(repoID int64) {
36+
pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(repoID)
37+
if err != nil {
38+
log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
39+
return
40+
}
41+
42+
for _, mirror := range pushMirrors {
43+
mirror_module.AddPushMirrorToQueue(mirror.ID)
44+
}
45+
}

0 commit comments

Comments
 (0)