Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_from_bundle.go « repository « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f8200b790187b9fc110d314a4db61b927fd4660 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package repository

import (
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/git"
	"gitlab.com/gitlab-org/gitaly/internal/helper"
	"gitlab.com/gitlab-org/gitaly/internal/tempdir"
	"gitlab.com/gitlab-org/gitaly/streamio"
)

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")
	}

	firstRead := false
	reader := streamio.NewReader(func() ([]byte, error) {
		if !firstRead {
			firstRead = true
			return firstRequest.GetData(), nil
		}

		request, err := stream.Recv()
		return request.GetData(), err
	})

	ctx := stream.Context()

	tmpDir, err := tempdir.New(ctx, repo)
	if err != nil {
		cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: tmp dir failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	bundlePath := path.Join(tmpDir, "repo.bundle")
	file, err := os.Create(bundlePath)
	if err != nil {
		cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: new bundle file failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	_, err = io.Copy(file, reader)
	if err != nil {
		cleanError := sanitizedError(tmpDir, "CreateRepositoryFromBundle: new bundle file failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	repoPath, err := helper.GetPath(repo)
	if err != nil {
		return err
	}

	args := []string{
		"clone",
		"--bare",
		"--",
		bundlePath,
		repoPath,
	}
	cmd, err := git.CommandWithoutRepo(ctx, args...)
	if err != nil {
		cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd start failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}
	if err := cmd.Wait(); err != nil {
		cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd wait failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	// We do a fetch to get all refs including keep-around refs
	args = []string{
		"-C",
		repoPath,
		"fetch",
		bundlePath,
		"refs/*:refs/*",
	}

	cmd, err = git.CommandWithoutRepo(ctx, args...)
	if err != nil {
		cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd start failed fetching refs: %v", err)
		return status.Error(codes.Internal, cleanError)
	}
	if err := cmd.Wait(); err != nil {
		cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: cmd wait failed fetching refs: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	// CreateRepository is harmless on existing repositories with the side effect that it creates the hook symlink.
	if _, err := s.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{Repository: repo}); err != nil {
		cleanError := sanitizedError(repoPath, "CreateRepositoryFromBundle: create hooks failed: %v", err)
		return status.Error(codes.Internal, cleanError)
	}

	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)
}