1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package repository
import (
"context"
"fmt"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/housekeeping"
"gitlab.com/gitlab-org/gitaly/v16/internal/git/stats"
"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
)
func (s *server) OptimizeRepository(ctx context.Context, in *gitalypb.OptimizeRepositoryRequest) (*gitalypb.OptimizeRepositoryResponse, error) {
if err := s.validateOptimizeRepositoryRequest(in); err != nil {
return nil, err
}
repo := s.localrepo(in.GetRepository())
gitVersion, err := repo.GitVersion(ctx)
if err != nil {
return nil, fmt.Errorf("detecting Git version: %w", err)
}
var strategyConstructor housekeeping.OptimizationStrategyConstructor
switch in.GetStrategy() {
case gitalypb.OptimizeRepositoryRequest_STRATEGY_UNSPECIFIED, gitalypb.OptimizeRepositoryRequest_STRATEGY_HEURISTICAL:
strategyConstructor = func(info stats.RepositoryInfo) housekeeping.OptimizationStrategy {
return housekeeping.NewHeuristicalOptimizationStrategy(gitVersion, info)
}
case gitalypb.OptimizeRepositoryRequest_STRATEGY_EAGER:
strategyConstructor = func(info stats.RepositoryInfo) housekeeping.OptimizationStrategy {
return housekeeping.NewEagerOptimizationStrategy(info)
}
default:
return nil, structerr.NewInvalidArgument("unsupported optimization strategy %d", in.GetStrategy())
}
if err := s.housekeepingManager.OptimizeRepository(ctx, ctxlogrus.Extract(ctx), repo,
housekeeping.WithOptimizationStrategyConstructor(strategyConstructor),
); err != nil {
return nil, structerr.NewInternal("%w", err)
}
return &gitalypb.OptimizeRepositoryResponse{}, nil
}
func (s *server) validateOptimizeRepositoryRequest(in *gitalypb.OptimizeRepositoryRequest) error {
repository := in.GetRepository()
if err := s.locator.ValidateRepository(repository); err != nil {
return structerr.NewInvalidArgument("%w", err)
}
_, err := s.locator.GetRepoPath(repository)
if err != nil {
return err
}
return nil
}
|