mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-16 14:47:48 +02:00
move of sizerelated CountObjects, CountObjectsWithEnv to modules/gitrepo/size.go and to use Repository interface
This commit is contained in:
parent
d2beb1d0d4
commit
3dcfd233a8
@ -248,26 +248,11 @@ const (
|
||||
statSizeGarbage = "size-garbage: "
|
||||
)
|
||||
|
||||
// CountObjects returns the results of git count-objects on the repoPath
|
||||
func CountObjects(ctx context.Context, repoPath string) (*CountObject, error) {
|
||||
return CountObjectsWithEnv(ctx, repoPath, nil)
|
||||
}
|
||||
|
||||
// CountObjectsWithEnv returns the results of git count-objects on the repoPath with custom env setup
|
||||
func CountObjectsWithEnv(ctx context.Context, repoPath string, env []string) (*CountObject, error) {
|
||||
cmd := gitcmd.NewCommand("count-objects", "-v")
|
||||
stdout, _, err := cmd.WithDir(repoPath).WithEnv(env).RunStdString(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return parseSize(stdout), nil
|
||||
}
|
||||
|
||||
// parseSize parses the output from count-objects and return a CountObject
|
||||
func parseSize(objects string) *CountObject {
|
||||
// ParseCountObjectsResult parses the output from git count-objects -v
|
||||
// and returns a CountObject struct with the parsed values
|
||||
func ParseCountObjectsResult(output string) *CountObject {
|
||||
repoSize := new(CountObject)
|
||||
for line := range strings.SplitSeq(objects, "\n") {
|
||||
for line := range strings.SplitSeq(output, "\n") {
|
||||
switch {
|
||||
case strings.HasPrefix(line, statCount):
|
||||
repoSize.Count, _ = strconv.ParseInt(line[7:], 10, 64)
|
||||
|
||||
@ -4,8 +4,12 @@
|
||||
package gitrepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/git/gitcmd"
|
||||
)
|
||||
|
||||
const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
|
||||
@ -35,3 +39,20 @@ func CalcRepositorySize(repo Repository) (int64, error) {
|
||||
})
|
||||
return size, err
|
||||
}
|
||||
|
||||
// CountObjects returns the results of git count-objects on the repository
|
||||
func CountObjects(ctx context.Context, repo Repository) (*git.CountObject, error) {
|
||||
return CountObjectsWithEnv(ctx, repo, nil)
|
||||
}
|
||||
|
||||
// CountObjectsWithEnv returns the results of git count-objects on the repository
|
||||
// with custom environment variables (e.g., GIT_QUARANTINE_PATH for pre-receive hooks)
|
||||
func CountObjectsWithEnv(ctx context.Context, repo Repository, env []string) (*git.CountObject, error) {
|
||||
cmd := gitcmd.NewCommand("count-objects", "-v")
|
||||
stdout, _, err := cmd.WithDir(repoPath(repo)).WithEnv(env).RunStdString(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return git.ParseCountObjectsResult(stdout), nil
|
||||
}
|
||||
|
||||
@ -547,7 +547,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
|
||||
|
||||
// Only do CountObjects (push/repo) when we're doing the repo-size limit at all
|
||||
if needGitDelta {
|
||||
repoSize, err = git.CountObjects(ctx, repo.RepoPath())
|
||||
repoSize, err = gitrepo.CountObjects(ctx, repo)
|
||||
if err != nil {
|
||||
log.Error("Unable to get repository size with env %v: %s Error: %v", repo.RepoPath(), ourCtx.env, err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]any{
|
||||
@ -556,7 +556,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
|
||||
return
|
||||
}
|
||||
|
||||
pushSize, err = git.CountObjectsWithEnv(ctx, repo.RepoPath(), ourCtx.env)
|
||||
pushSize, err = gitrepo.CountObjectsWithEnv(ctx, repo, ourCtx.env)
|
||||
if err != nil {
|
||||
log.Error("Unable to get push size with env %v: %s Error: %v", repo.RepoPath(), ourCtx.env, err)
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]any{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user