From 4e5d4d0073df7bffba95107ea969f1fdeb165830 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 1 Dec 2022 17:02:04 +0800 Subject: [PATCH 01/15] Skip initing LFS storage if disabled (#21996) A complement to #21985. I overlooked it because the name of the switch is `StartServer`, not `Enabled`. I believe the weird name is a legacy, but renaming is out of scope. --- modules/storage/storage.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/storage/storage.go b/modules/storage/storage.go index 35f152717238..671e0ce56529 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -174,6 +174,10 @@ func initAttachments() (err error) { } func initLFS() (err error) { + if !setting.LFS.StartServer { + LFS = discardStorage("LFS isn't enabled") + return nil + } log.Info("Initialising LFS storage with type: %s", setting.LFS.Storage.Type) LFS, err = NewStorage(setting.LFS.Storage.Type, &setting.LFS.Storage) return err From f9cbf5a1bcd38810432e9ca16270e1f7ccc63605 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 1 Dec 2022 19:56:04 +0800 Subject: [PATCH 02/15] Util type to parse ref name (#21969) Provide a new type to make it easier to parse a ref name. Actually, it's picked up from #21937, to make the origin PR lighter. Co-authored-by: Lunny Xiao --- modules/git/ref.go | 57 ++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/modules/git/ref.go b/modules/git/ref.go index cd8d26818495..47cc04b7fbdb 100644 --- a/modules/git/ref.go +++ b/modules/git/ref.go @@ -55,40 +55,57 @@ func (ref *Reference) Commit() (*Commit, error) { // ShortName returns the short name of the reference func (ref *Reference) ShortName() string { - if ref == nil { - return "" - } - if strings.HasPrefix(ref.Name, BranchPrefix) { - return strings.TrimPrefix(ref.Name, BranchPrefix) + return RefName(ref.Name).ShortName() +} + +// RefGroup returns the group type of the reference +func (ref *Reference) RefGroup() string { + return RefName(ref.Name).RefGroup() +} + +// RefName represents a git reference name +type RefName string + +func (ref RefName) IsBranch() bool { + return strings.HasPrefix(string(ref), BranchPrefix) +} + +func (ref RefName) IsTag() bool { + return strings.HasPrefix(string(ref), TagPrefix) +} + +// ShortName returns the short name of the reference name +func (ref RefName) ShortName() string { + refName := string(ref) + if strings.HasPrefix(refName, BranchPrefix) { + return strings.TrimPrefix(refName, BranchPrefix) } - if strings.HasPrefix(ref.Name, TagPrefix) { - return strings.TrimPrefix(ref.Name, TagPrefix) + if strings.HasPrefix(refName, TagPrefix) { + return strings.TrimPrefix(refName, TagPrefix) } - if strings.HasPrefix(ref.Name, RemotePrefix) { - return strings.TrimPrefix(ref.Name, RemotePrefix) + if strings.HasPrefix(refName, RemotePrefix) { + return strings.TrimPrefix(refName, RemotePrefix) } - if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 { - return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen] + if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 { + return refName[pullLen : strings.IndexByte(refName[pullLen:], '/')+pullLen] } - return ref.Name + return refName } // RefGroup returns the group type of the reference -func (ref *Reference) RefGroup() string { - if ref == nil { - return "" - } - if strings.HasPrefix(ref.Name, BranchPrefix) { +func (ref RefName) RefGroup() string { + refName := string(ref) + if strings.HasPrefix(refName, BranchPrefix) { return "heads" } - if strings.HasPrefix(ref.Name, TagPrefix) { + if strings.HasPrefix(refName, TagPrefix) { return "tags" } - if strings.HasPrefix(ref.Name, RemotePrefix) { + if strings.HasPrefix(refName, RemotePrefix) { return "remotes" } - if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 { + if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 { return "pull" } return "" From f0bd219a5e153ddcf6806334da700aac46fcefad Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 1 Dec 2022 22:44:38 +0100 Subject: [PATCH 03/15] Update chroma to v2.4.0 (#22000) Did a few cursory tests, seems to work well. --- go.mod | 2 +- go.sum | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ca8c79c68900..5e090a98b4d0 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/42wim/sshsig v0.0.0-20211121163825-841cf5bbc121 github.com/NYTimes/gziphandler v1.1.1 github.com/PuerkitoBio/goquery v1.8.0 - github.com/alecthomas/chroma/v2 v2.3.0 + github.com/alecthomas/chroma/v2 v2.4.0 github.com/blevesearch/bleve/v2 v2.3.4 github.com/buildkite/terminal-to-html/v3 v3.7.0 github.com/caddyserver/certmagic v0.17.2 diff --git a/go.sum b/go.sum index d6748eae34de..097d03780cdb 100644 --- a/go.sum +++ b/go.sum @@ -160,9 +160,10 @@ github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= +github.com/alecthomas/assert/v2 v2.2.0 h1:f6L/b7KE2bfA+9O4FL3CM/xJccDEwPVYd5fALBiuwvw= github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs= -github.com/alecthomas/chroma/v2 v2.3.0 h1:83xfxrnjv8eK+Cf8qZDzNo3PPF9IbTWHs7z28GY6D0U= -github.com/alecthomas/chroma/v2 v2.3.0/go.mod h1:mZxeWZlxP2Dy+/8cBob2PYd8O2DwNAzave5AY7A2eQw= +github.com/alecthomas/chroma/v2 v2.4.0 h1:Loe2ZjT5x3q1bcWwemqyqEi8p11/IV/ncFCeLYDpWC4= +github.com/alecthomas/chroma/v2 v2.4.0/go.mod h1:6kHzqF5O6FUSJzBXW7fXELjb+e+7OXW4UpoPqMO7IBQ= github.com/alecthomas/kingpin v2.2.6+incompatible/go.mod h1:59OFYbFVLKQKq+mqrL6Rw5bR0c3ACQaawgXx0QYndlE= github.com/alecthomas/repr v0.0.0-20220113201626-b1b626ac65ae/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= @@ -830,6 +831,7 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.0.0/go.mod h1:4qWG/gcEcfX4z/mBDHJ++3ReCw9ibxbsNJbcucJdbSo= github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4= From 64973cf18fcd21179fb7612b15914691ee24c657 Mon Sep 17 00:00:00 2001 From: zeripath Date: Thu, 1 Dec 2022 23:56:51 +0000 Subject: [PATCH 04/15] Use path not filepath in template filenames (#21993) Paths in git are always separated by `/` not `\` - therefore we should `path` and not `filepath` Fix #21987 Signed-off-by: Andrew Thornton Co-authored-by: Lunny Xiao Co-authored-by: Lauris BH --- modules/issue/template/unmarshal.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/issue/template/unmarshal.go b/modules/issue/template/unmarshal.go index f43a83fb62b8..8cae8d4c4299 100644 --- a/modules/issue/template/unmarshal.go +++ b/modules/issue/template/unmarshal.go @@ -6,7 +6,7 @@ package template import ( "fmt" "io" - "path/filepath" + "path" "strconv" "code.gitea.io/gitea/modules/git" @@ -43,7 +43,7 @@ func Unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { // UnmarshalFromEntry parses out a valid template from the blob in entry func UnmarshalFromEntry(entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) { - return unmarshalFromEntry(entry, filepath.Join(dir, entry.Name())) + return unmarshalFromEntry(entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here } // UnmarshalFromCommit parses out a valid template from the commit @@ -108,7 +108,7 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) { // It could be a valid markdown with two horizontal lines, or an invalid markdown with wrong metadata. it.Content = string(content) - it.Name = filepath.Base(it.FileName) + it.Name = path.Base(it.FileName) // paths in Git are always '/' separated - do not use filepath! it.About, _ = util.SplitStringAtByteN(it.Content, 80) } else { it.Content = templateBody From f7ade6de7c1c4991c650d6b96e6407053e6bdae7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 2 Dec 2022 11:15:36 +0800 Subject: [PATCH 05/15] Fix generate index failure possibility on postgres (#21998) @wxiaoguang Please review Co-authored-by: silverwind --- models/db/index.go | 20 ++++++++++++++++++++ models/git/commit_status.go | 18 ++++++++++++++++++ tests/integration/repo_commits_test.go | 4 ++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/models/db/index.go b/models/db/index.go index ca664c9cf125..f840a62c8913 100644 --- a/models/db/index.go +++ b/models/db/index.go @@ -7,6 +7,9 @@ import ( "context" "errors" "fmt" + "strconv" + + "code.gitea.io/gitea/modules/setting" ) // ResourceIndex represents a resource index which could be used as issue/release and others @@ -55,8 +58,25 @@ func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxInd return nil } +func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) { + res, err := GetEngine(ctx).Query(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+ + "VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1 RETURNING max_index", + tableName, tableName), groupID) + if err != nil { + return 0, err + } + if len(res) == 0 { + return 0, ErrGetResourceIndexFailed + } + return strconv.ParseInt(string(res[0]["max_index"]), 10, 64) +} + // GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) { + if setting.Database.UsePostgreSQL { + return postgresGetNextResourceIndex(ctx, tableName, groupID) + } + e := GetEngine(ctx) // try to update the max_index to next value, and acquire the write-lock for the record diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 6353bb2fa9f9..928f1771fe93 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/url" + "strconv" "strings" "time" @@ -49,8 +50,25 @@ func init() { db.RegisterModel(new(CommitStatusIndex)) } +func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) { + res, err := db.GetEngine(ctx).Query("INSERT INTO `commit_status_index` (repo_id, sha, max_index) "+ + "VALUES (?,?,1) ON CONFLICT (repo_id, sha) DO UPDATE SET max_index = `commit_status_index`.max_index+1 RETURNING max_index", + repoID, sha) + if err != nil { + return 0, err + } + if len(res) == 0 { + return 0, db.ErrGetResourceIndexFailed + } + return strconv.ParseInt(string(res[0]["max_index"]), 10, 64) +} + // GetNextCommitStatusIndex retried 3 times to generate a resource index func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) { + if setting.Database.UsePostgreSQL { + return postgresGetCommitStatusIndex(ctx, repoID, sha) + } + e := db.GetEngine(ctx) // try to update the max_index to next value, and acquire the write-lock for the record diff --git a/tests/integration/repo_commits_test.go b/tests/integration/repo_commits_test.go index 3840c508dceb..0d794cec757b 100644 --- a/tests/integration/repo_commits_test.go +++ b/tests/integration/repo_commits_test.go @@ -135,8 +135,8 @@ func TestRepoCommitsStatusParallel(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) - go func(t *testing.T, i int) { - t.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) { + go func(parentT *testing.T, i int) { + parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) { runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending")) runBody(t) wg.Done() From 665d02efaf6ebaadf3ffb5b412ea77c4502f0baf Mon Sep 17 00:00:00 2001 From: Mark Ormesher Date: Fri, 2 Dec 2022 03:39:19 +0000 Subject: [PATCH 06/15] Remove duplicate "Actions" label in mobile view (#21974) Closes #21973. The "Actions" button on the commit view page is labelled twice in mobile view. No other buttons on the page have a `mobile-only` extra label, so this PR removes it. Before: ![before](https://user-images.githubusercontent.com/6496999/204540002-75baa08a-6c06-4b39-847b-34272e09d71e.PNG) After: ![after](https://user-images.githubusercontent.com/6496999/204539991-a0607765-d5e2-4b1a-84c9-a3e16cbc674e.PNG) Co-authored-by: Lunny Xiao --- templates/repo/commit_page.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl index 2dcd02ad98df..d4839e8930e8 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -27,7 +27,7 @@ {{if and ($.Permission.CanWrite $.UnitTypeCode) (not $.Repository.IsArchived) (not .IsDeleted)}}{{- /* */ -}}