Skip to content

Commit 6feba52

Browse files
authored
Merge branch 'go-gitea:main' into telackey/sort
2 parents 7b4563c + 5eff19a commit 6feba52

File tree

112 files changed

+1307
-1298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1307
-1298
lines changed

models/actions/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (run *ActionRun) RefLink() string {
8888
if refName.IsPull() {
8989
return run.Repo.Link() + "/pulls/" + refName.ShortName()
9090
}
91-
return git.RefURL(run.Repo.Link(), run.Ref)
91+
return run.Repo.Link() + "/src/" + refName.RefWebLinkPath()
9292
}
9393

9494
// PrettyRef return #id for pull ref or ShortName for others

models/activities/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (a *Action) GetBranch() string {
355355

356356
// GetRefLink returns the action's ref link.
357357
func (a *Action) GetRefLink(ctx context.Context) string {
358-
return git.RefURL(a.GetRepoLink(ctx), a.RefName)
358+
return a.GetRepoLink(ctx) + "/src/" + git.RefName(a.RefName).RefWebLinkPath()
359359
}
360360

361361
// GetTag returns the action's repository tag.

models/fixtures/webhook.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@
2222
content_type: 1 # json
2323
events: '{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}'
2424
is_active: true
25+
2526
-
2627
id: 4
2728
repo_id: 2
2829
url: www.example.com/url4
2930
content_type: 1 # json
3031
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
3132
is_active: true
33+
34+
-
35+
id: 5
36+
repo_id: 0
37+
owner_id: 0
38+
url: www.example.com/url5
39+
content_type: 1 # json
40+
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
41+
is_active: true
42+
is_system_webhook: true
43+
44+
-
45+
id: 6
46+
repo_id: 0
47+
owner_id: 0
48+
url: www.example.com/url6
49+
content_type: 1 # json
50+
events: '{"push_only":true,"branch_filter":"{master,feature*}"}'
51+
is_active: true
52+
is_system_webhook: false

models/issues/comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ const (
112112
CommentTypePRScheduledToAutoMerge // 34 pr was scheduled to auto merge when checks succeed
113113
CommentTypePRUnScheduledToAutoMerge // 35 pr was un scheduled to auto merge when checks succeed
114114

115-
CommentTypePin // 36 pin Issue
116-
CommentTypeUnpin // 37 unpin Issue
115+
CommentTypePin // 36 pin Issue/PullRequest
116+
CommentTypeUnpin // 37 unpin Issue/PullRequest
117117

118118
CommentTypeChangeTimeEstimate // 38 Change time estimate
119119
)

models/perm/access/repo_permission.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,14 @@ func (p *Permission) LogString() string {
175175
return fmt.Sprintf(format, args...)
176176
}
177177

178-
func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
178+
func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
179179
if user == nil || user.ID <= 0 {
180+
// for anonymous access, it could be:
181+
// AccessMode is None or Read, units has repo units, unitModes is nil
180182
return
181183
}
184+
185+
// apply everyone access permissions
182186
for _, u := range perm.units {
183187
if u.EveryoneAccessMode >= perm_model.AccessModeRead && u.EveryoneAccessMode > perm.everyoneAccessMode[u.Type] {
184188
if perm.everyoneAccessMode == nil {
@@ -187,17 +191,40 @@ func applyEveryoneRepoPermission(user *user_model.User, perm *Permission) {
187191
perm.everyoneAccessMode[u.Type] = u.EveryoneAccessMode
188192
}
189193
}
194+
195+
if perm.unitsMode == nil {
196+
// if unitsMode is not set, then it means that the default p.AccessMode applies to all units
197+
return
198+
}
199+
200+
// remove no permission units
201+
origPermUnits := perm.units
202+
perm.units = make([]*repo_model.RepoUnit, 0, len(perm.units))
203+
for _, u := range origPermUnits {
204+
shouldKeep := false
205+
for t := range perm.unitsMode {
206+
if shouldKeep = u.Type == t; shouldKeep {
207+
break
208+
}
209+
}
210+
for t := range perm.everyoneAccessMode {
211+
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
212+
break
213+
}
214+
}
215+
if shouldKeep {
216+
perm.units = append(perm.units, u)
217+
}
218+
}
190219
}
191220

192221
// GetUserRepoPermission returns the user permissions to the repository
193222
func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (perm Permission, err error) {
194223
defer func() {
195224
if err == nil {
196-
applyEveryoneRepoPermission(user, &perm)
197-
}
198-
if log.IsTrace() {
199-
log.Trace("Permission Loaded for user %-v in repo %-v, permissions: %-+v", user, repo, perm)
225+
finalProcessRepoUnitPermission(user, &perm)
200226
}
227+
log.Trace("Permission Loaded for user %-v in repo %-v, permissions: %-+v", user, repo, perm)
201228
}()
202229

203230
if err = repo.LoadUnits(ctx); err != nil {
@@ -294,16 +321,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
294321
}
295322
}
296323

297-
// remove no permission units
298-
perm.units = make([]*repo_model.RepoUnit, 0, len(repo.Units))
299-
for t := range perm.unitsMode {
300-
for _, u := range repo.Units {
301-
if u.Type == t {
302-
perm.units = append(perm.units, u)
303-
}
304-
}
305-
}
306-
307324
return perm, err
308325
}
309326

models/perm/access/repo_permission_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
5050
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
5151
},
5252
}
53-
applyEveryoneRepoPermission(nil, &perm)
53+
finalProcessRepoUnitPermission(nil, &perm)
5454
assert.False(t, perm.CanRead(unit.TypeWiki))
5555

5656
perm = Permission{
@@ -59,7 +59,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
5959
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
6060
},
6161
}
62-
applyEveryoneRepoPermission(&user_model.User{ID: 0}, &perm)
62+
finalProcessRepoUnitPermission(&user_model.User{ID: 0}, &perm)
6363
assert.False(t, perm.CanRead(unit.TypeWiki))
6464

6565
perm = Permission{
@@ -68,7 +68,7 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
6868
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
6969
},
7070
}
71-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
71+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
7272
assert.True(t, perm.CanRead(unit.TypeWiki))
7373

7474
perm = Permission{
@@ -77,20 +77,22 @@ func TestApplyEveryoneRepoPermission(t *testing.T) {
7777
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
7878
},
7979
}
80-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
80+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
8181
// it should work the same as "EveryoneAccessMode: none" because the default AccessMode should be applied to units
8282
assert.True(t, perm.CanWrite(unit.TypeWiki))
8383

8484
perm = Permission{
8585
units: []*repo_model.RepoUnit{
86+
{Type: unit.TypeCode}, // will be removed
8687
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeRead},
8788
},
8889
unitsMode: map[unit.Type]perm_model.AccessMode{
8990
unit.TypeWiki: perm_model.AccessModeWrite,
9091
},
9192
}
92-
applyEveryoneRepoPermission(&user_model.User{ID: 1}, &perm)
93+
finalProcessRepoUnitPermission(&user_model.User{ID: 1}, &perm)
9394
assert.True(t, perm.CanWrite(unit.TypeWiki))
95+
assert.Len(t, perm.units, 1)
9496
}
9597

9698
func TestUnitAccessMode(t *testing.T) {

models/repo/archiver.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
5656
if err != nil {
5757
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
5858
}
59-
nameExts := strings.SplitN(parts[2], ".", 2)
60-
if len(nameExts) != 2 {
59+
commitID, archiveType := git.SplitArchiveNameType(parts[2])
60+
if archiveType == git.ArchiveUnknown {
6161
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
6262
}
63-
64-
return &RepoArchiver{
65-
RepoID: repoID,
66-
CommitID: parts[1] + nameExts[0],
67-
Type: git.ToArchiveType(nameExts[1]),
68-
}, nil
63+
return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
6964
}
7065

7166
// GetRepoArchiver get an archiver

models/repo/license.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func UpdateRepoLicenses(ctx context.Context, repo *Repository, commitID string,
5454
for _, o := range oldLicenses {
5555
// Update already existing license
5656
if o.License == license {
57+
o.CommitID = commitID
5758
if _, err := db.GetEngine(ctx).ID(o.ID).Cols("`commit_id`").Update(o); err != nil {
5859
return err
5960
}

models/unittest/fscopy.go

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,13 @@ import (
1111
"code.gitea.io/gitea/modules/util"
1212
)
1313

14-
// Copy copies file from source to target path.
15-
func Copy(src, dest string) error {
16-
// Gather file information to set back later.
17-
si, err := os.Lstat(src)
18-
if err != nil {
19-
return err
20-
}
21-
22-
// Handle symbolic link.
23-
if si.Mode()&os.ModeSymlink != 0 {
24-
target, err := os.Readlink(src)
25-
if err != nil {
26-
return err
27-
}
28-
// NOTE: os.Chmod and os.Chtimes don't recognize symbolic link,
29-
// which will lead "no such file or directory" error.
30-
return os.Symlink(target, dest)
31-
}
32-
33-
return util.CopyFile(src, dest)
34-
}
35-
36-
// Sync synchronizes the two files. This is skipped if both files
14+
// SyncFile synchronizes the two files. This is skipped if both files
3715
// exist and the size, modtime, and mode match.
38-
func Sync(srcPath, destPath string) error {
16+
func SyncFile(srcPath, destPath string) error {
3917
dest, err := os.Stat(destPath)
4018
if err != nil {
4119
if os.IsNotExist(err) {
42-
return Copy(srcPath, destPath)
20+
return util.CopyFile(srcPath, destPath)
4321
}
4422
return err
4523
}
@@ -55,7 +33,7 @@ func Sync(srcPath, destPath string) error {
5533
return nil
5634
}
5735

58-
return Copy(srcPath, destPath)
36+
return util.CopyFile(srcPath, destPath)
5937
}
6038

6139
// SyncDirs synchronizes files recursively from source to target directory.
@@ -66,23 +44,31 @@ func SyncDirs(srcPath, destPath string) error {
6644
return err
6745
}
6846

47+
// the keep file is used to keep the directory in a git repository, it doesn't need to be synced
48+
// and go-git doesn't work with the ".keep" file (it would report errors like "ref is empty")
49+
const keepFile = ".keep"
50+
6951
// find and delete all untracked files
7052
destFiles, err := util.ListDirRecursively(destPath, &util.ListDirOptions{IncludeDir: true})
7153
if err != nil {
7254
return err
7355
}
7456
for _, destFile := range destFiles {
7557
destFilePath := filepath.Join(destPath, destFile)
58+
shouldRemove := filepath.Base(destFilePath) == keepFile
7659
if _, err = os.Stat(filepath.Join(srcPath, destFile)); err != nil {
7760
if os.IsNotExist(err) {
78-
// if src file does not exist, remove dest file
79-
if err = os.RemoveAll(destFilePath); err != nil {
80-
return err
81-
}
61+
shouldRemove = true
8262
} else {
8363
return err
8464
}
8565
}
66+
// if src file does not exist, remove dest file
67+
if shouldRemove {
68+
if err = os.RemoveAll(destFilePath); err != nil {
69+
return err
70+
}
71+
}
8672
}
8773

8874
// sync src files to dest
@@ -95,8 +81,8 @@ func SyncDirs(srcPath, destPath string) error {
9581
// util.ListDirRecursively appends a slash to the directory name
9682
if strings.HasSuffix(srcFile, "/") {
9783
err = os.MkdirAll(destFilePath, os.ModePerm)
98-
} else {
99-
err = Sync(filepath.Join(srcPath, srcFile), destFilePath)
84+
} else if filepath.Base(destFilePath) != keepFile {
85+
err = SyncFile(filepath.Join(srcPath, srcFile), destFilePath)
10086
}
10187
if err != nil {
10288
return err

models/webhook/webhook_system.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import (
1111
"code.gitea.io/gitea/modules/optional"
1212
)
1313

14+
// GetSystemOrDefaultWebhooks returns webhooks by given argument or all if argument is missing.
15+
func GetSystemOrDefaultWebhooks(ctx context.Context, isSystemWebhook optional.Option[bool]) ([]*Webhook, error) {
16+
webhooks := make([]*Webhook, 0, 5)
17+
if !isSystemWebhook.Has() {
18+
return webhooks, db.GetEngine(ctx).Where("repo_id=? AND owner_id=?", 0, 0).
19+
Find(&webhooks)
20+
}
21+
22+
return webhooks, db.GetEngine(ctx).
23+
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook.Value()).
24+
Find(&webhooks)
25+
}
26+
1427
// GetDefaultWebhooks returns all admin-default webhooks.
1528
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
1629
webhooks := make([]*Webhook, 0, 5)

0 commit comments

Comments
 (0)