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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package repository
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/internal/tempdir"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v14/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *server) CreateRepositoryFromBundle(stream gitalypb.RepositoryService_CreateRepositoryFromBundleServer) error {
firstRequest, err := stream.Recv()
if err != nil {
return status.Errorf(codes.Internal, "CreateRepositoryFromBundle: first request failed: %v", err)
}
repo := firstRequest.GetRepository()
if repo == nil {
return status.Errorf(codes.InvalidArgument, "CreateRepositoryFromBundle: empty Repository")
}
ctx := stream.Context()
firstRead := false
bundleReader := streamio.NewReader(func() ([]byte, error) {
if !firstRead {
firstRead = true
return firstRequest.GetData(), nil
}
request, err := stream.Recv()
return request.GetData(), err
})
bundleDir, err := tempdir.New(ctx, repo.GetStorageName(), s.locator)
if err != nil {
return helper.ErrInternalf("creating bundle directory: %w", err)
}
bundlePath := filepath.Join(bundleDir.Path(), "repo.bundle")
bundleFile, err := os.Create(bundlePath)
if err != nil {
return helper.ErrInternalf("creating bundle file: %w", err)
}
if _, err := io.Copy(bundleFile, bundleReader); err != nil {
return helper.ErrInternalf("writing bundle file: %w", err)
}
if err := s.createRepository(ctx, repo, func(repo *gitalypb.Repository) error {
var stderr bytes.Buffer
cmd, err := s.gitCmdFactory.New(ctx, repo, git.SubCmd{
Name: "fetch",
Flags: []git.Option{
git.Flag{Name: "--quiet"},
git.Flag{Name: "--atomic"},
},
Args: []string{bundlePath, "refs/*:refs/*"},
}, git.WithStderr(&stderr), git.WithRefTxHook(repo))
if err != nil {
return helper.ErrInternalf("spawning fetch: %w", err)
}
if err := cmd.Wait(); err != nil {
sanitizedStderr := sanitizedError("%s", stderr.String())
return helper.ErrInternalf("fetch from bundle: %w, stderr: %q", err, sanitizedStderr)
}
if err := s.updateHeadFromBundle(ctx, s.localrepo(repo), bundlePath); err != nil {
return helper.ErrInternal(err)
}
return nil
}); err != nil {
return helper.ErrInternalf("creating repository: %w", err)
}
return stream.SendAndClose(&gitalypb.CreateRepositoryFromBundleResponse{})
}
func sanitizedError(path, format string, a ...interface{}) string {
str := fmt.Sprintf(format, a...)
return strings.Replace(str, path, "[REPO PATH]", -1)
}
|