2021-07-13 09:14:14 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-13 09:14:14 +02:00
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-05-21 03:50:53 +02:00
|
|
|
gocontext "context"
|
2022-04-30 16:32:01 +02:00
|
|
|
"fmt"
|
2021-07-13 09:14:14 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-07-25 17:39:42 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-07-13 09:14:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 ""
|
|
|
|
}
|
|
|
|
|
2022-07-25 17:39:42 +02:00
|
|
|
sha := ref
|
2021-07-13 09:14:14 +02:00
|
|
|
// 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 != "" {
|
2022-07-25 17:39:42 +02:00
|
|
|
sha = refSHA
|
|
|
|
break
|
2021-07-13 09:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-25 17:39:42 +02:00
|
|
|
|
2023-05-21 03:50:53 +02:00
|
|
|
sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
|
2022-12-27 14:12:49 +01:00
|
|
|
|
2022-07-30 10:09:04 +02:00
|
|
|
if ctx.Repo.GitRepo != nil {
|
|
|
|
err := ctx.Repo.GitRepo.AddLastCommitCache(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, ref != sha), ctx.Repo.Repository.FullName(), sha)
|
2022-07-25 17:39:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sha
|
2021-07-13 09:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGitRefs return git references based on filter
|
|
|
|
func GetGitRefs(ctx *context.APIContext, filter string) ([]*git.Reference, string, error) {
|
|
|
|
if ctx.Repo.GitRepo == nil {
|
2022-04-30 16:32:01 +02:00
|
|
|
return nil, "", fmt.Errorf("no open git repo found in context")
|
2021-07-13 09:14:14 +02:00
|
|
|
}
|
|
|
|
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) {
|
2022-01-20 18:46:10 +01:00
|
|
|
refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) // Search by type
|
2021-07-13 09:14:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", lastMethodName, err
|
|
|
|
}
|
|
|
|
if len(refs) > 0 {
|
2022-01-20 18:46:10 +01:00
|
|
|
return refs[0].Object.String(), "", nil // Return found SHA
|
2021-07-13 09:14:14 +02:00
|
|
|
}
|
|
|
|
return "", "", nil
|
|
|
|
}
|
2022-12-27 14:12:49 +01:00
|
|
|
|
2023-12-13 22:02:00 +01:00
|
|
|
// 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.GitRepo.GetObjectFormat()
|
|
|
|
if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
|
|
|
|
sha, err := objectFormat.NewIDFromString(commitID)
|
2022-12-27 14:12:49 +01:00
|
|
|
if err == nil {
|
2023-12-13 22:02:00 +01:00
|
|
|
return sha, nil
|
2022-12-27 14:12:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 03:50:53 +02:00
|
|
|
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.Repository.RepoPath())
|
2022-12-27 14:12:49 +01:00
|
|
|
if err != nil {
|
2023-12-17 12:56:08 +01:00
|
|
|
return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err)
|
2022-12-27 14:12:49 +01:00
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
2023-12-13 22:02:00 +01:00
|
|
|
return gitRepo.ConvertToGitID(commitID)
|
2022-12-27 14:12:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MustConvertToSHA1 returns a full-length SHA1 string from a potential ID string, or returns origin input if it can't convert to SHA1
|
2023-05-21 03:50:53 +02:00
|
|
|
func MustConvertToSHA1(ctx gocontext.Context, repo *context.Repository, commitID string) string {
|
2023-12-13 22:02:00 +01:00
|
|
|
sha, err := ConvertToObjectID(ctx, repo, commitID)
|
2022-12-27 14:12:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return commitID
|
|
|
|
}
|
|
|
|
return sha.String()
|
|
|
|
}
|