Skip to content

Commit f286ace

Browse files
committed
Merge branch 'main' into lunny/refactor_transfer
2 parents 6bd16bd + fffc855 commit f286ace

File tree

128 files changed

+2049
-1737
lines changed

Some content is hidden

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

128 files changed

+2049
-1737
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ module.exports = {
403403
'github/a11y-svg-has-accessible-name': [0],
404404
'github/array-foreach': [0],
405405
'github/async-currenttarget': [2],
406-
'github/async-preventdefault': [2],
406+
'github/async-preventdefault': [0], // https://github.com/github/eslint-plugin-github/issues/599
407407
'github/authenticity-token': [0],
408408
'github/get-attribute': [0],
409409
'github/js-class-name': [0],

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
poetry
3030

3131
# backend
32+
go_1_23
3233
gofumpt
3334
sqlite
3435
];
36+
shellHook = ''
37+
export GO="${pkgs.go_1_23}/bin/go"
38+
export GOROOT="${pkgs.go_1_23}/share/go"
39+
'';
3540
};
3641
}
3742
);

models/fixtures/access.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,9 @@
171171
user_id: 40
172172
repo_id: 61
173173
mode: 4
174+
175+
-
176+
id: 30
177+
user_id: 40
178+
repo_id: 1
179+
mode: 2

models/git/branch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ type FindRecentlyPushedNewBranchesOptions struct {
440440
}
441441

442442
type RecentlyPushedNewBranch struct {
443+
BranchRepo *repo_model.Repository
444+
BranchName string
443445
BranchDisplayName string
444446
BranchLink string
445447
BranchCompareURL string
@@ -540,7 +542,9 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o
540542
branchDisplayName = fmt.Sprintf("%s:%s", branch.Repo.FullName(), branchDisplayName)
541543
}
542544
newBranches = append(newBranches, &RecentlyPushedNewBranch{
545+
BranchRepo: branch.Repo,
543546
BranchDisplayName: branchDisplayName,
547+
BranchName: branch.Name,
544548
BranchLink: fmt.Sprintf("%s/src/branch/%s", branch.Repo.Link(), util.PathEscapeSegments(branch.Name)),
545549
BranchCompareURL: branch.Repo.ComposeBranchCompareURL(opts.BaseRepo, branch.Name),
546550
CommitTime: branch.CommitTime,

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/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

modules/repository/branch.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package repository
66
import (
77
"context"
88
"fmt"
9-
"strings"
109

1110
"code.gitea.io/gitea/models/db"
1211
git_model "code.gitea.io/gitea/models/git"
@@ -52,9 +51,6 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository,
5251
{
5352
branches, _, err := gitRepo.GetBranchNames(0, 0)
5453
if err != nil {
55-
if strings.Contains(err.Error(), "ref file is empty") {
56-
return 0, nil
57-
}
5854
return 0, err
5955
}
6056
log.Trace("SyncRepoBranches[%s]: branches[%d]: %v", repo.FullName(), len(branches), branches)

0 commit comments

Comments
 (0)