mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-10-30 17:34:13 +01:00
894d9b2836
Since `modules/context` has to depend on `models` and many other packages, it should be moved from `modules/context` to `services/context` according to design principles. There is no logic code change on this PR, only move packages. - Move `code.gitea.io/gitea/modules/context` to `code.gitea.io/gitea/services/context` - Move `code.gitea.io/gitea/modules/contexttest` to `code.gitea.io/gitea/services/contexttest` because of depending on context - Move `code.gitea.io/gitea/modules/upload` to `code.gitea.io/gitea/services/context/upload` because of depending on context (cherry picked from commit 29f149bd9f517225a3c9f1ca3fb0a7b5325af696) Conflicts: routers/api/packages/alpine/alpine.go routers/api/v1/repo/issue_reaction.go routers/install/install.go routers/web/admin/config.go routers/web/passkey.go routers/web/repo/search.go routers/web/repo/setting/default_branch.go routers/web/user/home.go routers/web/user/profile.go tests/integration/editor_test.go tests/integration/integration_test.go tests/integration/mirror_push_test.go trivial context conflicts also modified all other occurrences in Forgejo specific files
99 lines
2.9 KiB
Go
99 lines
2.9 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package utils
|
|
|
|
import (
|
|
gocontext "context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/services/context"
|
|
)
|
|
|
|
// ResolveRefOrSha resolve ref to sha if exist
|
|
func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
|
|
if len(ref) == 0 {
|
|
ctx.Error(http.StatusBadRequest, "ref not given", nil)
|
|
return ""
|
|
}
|
|
|
|
sha := ref
|
|
// Search branches and tags
|
|
for _, refType := range []string{"heads", "tags"} {
|
|
refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, lastMethodName, err)
|
|
return ""
|
|
}
|
|
if refSHA != "" {
|
|
sha = refSHA
|
|
break
|
|
}
|
|
}
|
|
|
|
sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
|
|
|
|
if ctx.Repo.GitRepo != nil {
|
|
err := ctx.Repo.GitRepo.AddLastCommitCache(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, ref != sha), ctx.Repo.Repository.FullName(), sha)
|
|
if err != nil {
|
|
log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err)
|
|
}
|
|
}
|
|
|
|
return sha
|
|
}
|
|
|
|
// GetGitRefs return git references based on filter
|
|
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
|
|
if ctx.Repo.GitRepo == nil {
|
|
return nil, "", fmt.Errorf("no open git repo found in context")
|
|
}
|
|
if len(filter) > 0 {
|
|
filter = "refs/" + filter
|
|
}
|
|
refs, err := ctx.Repo.GitRepo.GetRefsFiltered(filter)
|
|
return refs, "GetRefsFiltered", err
|
|
}
|
|
|
|
func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (string, string, error) {
|
|
refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) // Search by type
|
|
if err != nil {
|
|
return "", lastMethodName, err
|
|
}
|
|
if len(refs) > 0 {
|
|
return refs[0].Object.String(), "", nil // Return found SHA
|
|
}
|
|
return "", "", nil
|
|
}
|
|
|
|
// ConvertToObjectID returns a full-length SHA1 from a potential ID string
|
|
func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID string) (git.ObjectID, error) {
|
|
objectFormat := repo.GetObjectFormat()
|
|
if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
|
|
sha, err := git.NewIDFromString(commitID)
|
|
if err == nil {
|
|
return sha, nil
|
|
}
|
|
}
|
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo.Repository)
|
|
if err != nil {
|
|
return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err)
|
|
}
|
|
defer closer.Close()
|
|
|
|
return gitRepo.ConvertToGitID(commitID)
|
|
}
|
|
|
|
// MustConvertToSHA1 returns a full-length SHA1 string from a potential ID string, or returns origin input if it can't convert to SHA1
|
|
func MustConvertToSHA1(ctx gocontext.Context, repo *context.Repository, commitID string) string {
|
|
sha, err := ConvertToObjectID(ctx, repo, commitID)
|
|
if err != nil {
|
|
return commitID
|
|
}
|
|
return sha.String()
|
|
}
|