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
|
package repository
import (
"context"
"errors"
"os"
"path/filepath"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)
func (s *server) RenameRepository(ctx context.Context, in *gitalypb.RenameRepositoryRequest) (*gitalypb.RenameRepositoryResponse, error) {
if err := validateRenameRepositoryRequest(in); err != nil {
return nil, helper.ErrInvalidArgument(err)
}
fromFullPath, err := s.locator.GetRepoPath(in.GetRepository())
if err != nil {
return nil, helper.ErrInvalidArgument(err)
}
toFullPath, err := s.locator.GetPath(&gitalypb.Repository{StorageName: in.GetRepository().GetStorageName(), RelativePath: in.GetRelativePath()})
if err != nil {
return nil, helper.ErrInvalidArgument(err)
}
if _, err = os.Stat(toFullPath); !os.IsNotExist(err) {
return nil, helper.ErrPreconditionFailed(errors.New("destination already exists"))
}
if err = os.MkdirAll(filepath.Dir(toFullPath), 0755); err != nil {
return nil, helper.ErrInternal(err)
}
if err = os.Rename(fromFullPath, toFullPath); err != nil {
return nil, helper.ErrInternal(err)
}
return &gitalypb.RenameRepositoryResponse{}, nil
}
func validateRenameRepositoryRequest(in *gitalypb.RenameRepositoryRequest) error {
if in.GetRepository() == nil {
return errors.New("from repository is empty")
}
if in.GetRelativePath() == "" {
return errors.New("destination relative path is empty")
}
return nil
}
|