2021-07-13 12:44:14 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-13 12:44:14 +05:30
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-05-21 07:20:53 +05:30
|
|
|
gocontext "context"
|
2022-04-30 20:02:01 +05:30
|
|
|
"fmt"
|
2021-07-13 12:44:14 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2022-07-25 21:09:42 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-07-13 12:44:14 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// 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 21:09:42 +05:30
|
|
|
sha := ref
|
2021-07-13 12:44:14 +05:30
|
|
|
// 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 21:09:42 +05:30
|
|
|
sha = refSHA
|
|
|
|
break
|
2021-07-13 12:44:14 +05:30
|
|
|
}
|
|
|
|
}
|
2022-07-25 21:09:42 +05:30
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
sha = MustConvertToSHA1(ctx, ctx.Repo, sha)
|
2022-12-27 18:42:49 +05:30
|
|
|
|
2022-07-30 13:39:04 +05:30
|
|
|
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 21:09:42 +05:30
|
|
|
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 12:44:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 20:02:01 +05:30
|
|
|
return nil, "", fmt.Errorf("no open git repo found in context")
|
2021-07-13 12:44:14 +05:30
|
|
|
}
|
|
|
|
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 23:16:10 +05:30
|
|
|
refs, lastMethodName, err := GetGitRefs(ctx, refType+"/"+filter) // Search by type
|
2021-07-13 12:44:14 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", lastMethodName, err
|
|
|
|
}
|
|
|
|
if len(refs) > 0 {
|
2022-01-20 23:16:10 +05:30
|
|
|
return refs[0].Object.String(), "", nil // Return found SHA
|
2021-07-13 12:44:14 +05:30
|
|
|
}
|
|
|
|
return "", "", nil
|
|
|
|
}
|
2022-12-27 18:42:49 +05:30
|
|
|
|
2023-12-14 02:32:00 +05:30
|
|
|
// ConvertToObjectID returns a full-length SHA1 from a potential ID string
|
|
|
|
func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID string) (git.ObjectID, error) {
|
2024-02-24 12:25:19 +05:30
|
|
|
objectFormat := repo.GetObjectFormat()
|
2023-12-14 02:32:00 +05:30
|
|
|
if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
|
2023-12-19 12:50:47 +05:30
|
|
|
sha, err := git.NewIDFromString(commitID)
|
2022-12-27 18:42:49 +05:30
|
|
|
if err == nil {
|
2023-12-14 02:32:00 +05:30
|
|
|
return sha, nil
|
2022-12-27 18:42:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-28 01:39:51 +05:30
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo.Repository)
|
2022-12-27 18:42:49 +05:30
|
|
|
if err != nil {
|
2023-12-17 17:26:08 +05:30
|
|
|
return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err)
|
2022-12-27 18:42:49 +05:30
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
2023-12-14 02:32:00 +05:30
|
|
|
return gitRepo.ConvertToGitID(commitID)
|
2022-12-27 18:42:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 07:20:53 +05:30
|
|
|
func MustConvertToSHA1(ctx gocontext.Context, repo *context.Repository, commitID string) string {
|
2023-12-14 02:32:00 +05:30
|
|
|
sha, err := ConvertToObjectID(ctx, repo, commitID)
|
2022-12-27 18:42:49 +05:30
|
|
|
if err != nil {
|
|
|
|
return commitID
|
|
|
|
}
|
|
|
|
return sha.String()
|
|
|
|
}
|