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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
//go:build !gitaly_test_sha256
package repository
import (
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/v15/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v15/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestCreateBundleFromRefList_success(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, repo, repoPath, client := setupRepositoryService(t, ctx)
// Create a work tree with a HEAD pointing to a commit that is missing. CreateBundle should
// clean this up before creating the bundle.
sha := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("branch"))
require.NoError(t, os.MkdirAll(filepath.Join(repoPath, "gitlab-worktree"), 0o755))
gittest.Exec(t, cfg, "-C", repoPath, "worktree", "add", "gitlab-worktree/worktree1", sha.String())
require.NoError(t, os.Chtimes(filepath.Join(repoPath, "gitlab-worktree", "worktree1"), time.Now().Add(-7*time.Hour), time.Now().Add(-7*time.Hour)))
gittest.Exec(t, cfg, "-C", repoPath, "branch", "-D", "branch")
require.NoError(t, os.Remove(filepath.Join(repoPath, "objects", sha.String()[0:2], sha.String()[2:])))
masterOID := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/master"))
c, err := client.CreateBundleFromRefList(ctx)
require.NoError(t, err)
require.NoError(t, c.Send(&gitalypb.CreateBundleFromRefListRequest{
Repository: repo,
Patterns: [][]byte{
[]byte("master"),
[]byte("^master~1"),
},
}))
require.NoError(t, c.CloseSend())
reader := streamio.NewReader(func() ([]byte, error) {
response, err := c.Recv()
return response.GetData(), err
})
bundle, err := os.Create(filepath.Join(testhelper.TempDir(t), "bundle"))
require.NoError(t, err)
_, err = io.Copy(bundle, reader)
require.NoError(t, err)
require.NoError(t, bundle.Close())
output := gittest.Exec(t, cfg, "-C", repoPath, "bundle", "verify", bundle.Name())
require.Contains(t, string(output), fmt.Sprintf("The bundle contains this ref:\n%s refs/heads/master", masterOID))
}
func TestCreateBundleFromRefList_missing_ref(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
cfg, repo, repoPath, client := setupRepositoryService(t, ctx)
masterOID := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/master"))
c, err := client.CreateBundleFromRefList(ctx)
require.NoError(t, err)
require.NoError(t, c.Send(&gitalypb.CreateBundleFromRefListRequest{
Repository: repo,
Patterns: [][]byte{
[]byte("refs/heads/master"),
[]byte("refs/heads/totally_missing"),
},
}))
require.NoError(t, c.CloseSend())
reader := streamio.NewReader(func() ([]byte, error) {
response, err := c.Recv()
return response.GetData(), err
})
bundle, err := os.Create(filepath.Join(testhelper.TempDir(t), "bundle"))
require.NoError(t, err)
_, err = io.Copy(bundle, reader)
require.NoError(t, err)
require.NoError(t, bundle.Close())
output := gittest.Exec(t, cfg, "-C", repoPath, "bundle", "verify", bundle.Name())
require.Contains(t, string(output), fmt.Sprintf("The bundle contains this ref:\n%s refs/heads/master", masterOID))
}
func TestCreateBundleFromRefList_validations(t *testing.T) {
t.Parallel()
ctx := testhelper.Context(t)
_, repo, _, client := setupRepositoryService(t, ctx)
testCases := []struct {
desc string
request *gitalypb.CreateBundleFromRefListRequest
expectedErr error
}{
{
desc: "empty repository",
request: &gitalypb.CreateBundleFromRefListRequest{
Patterns: [][]byte{[]byte("master")},
},
expectedErr: status.Error(codes.InvalidArgument, testhelper.GitalyOrPraefect(
"empty Repository",
"repo scoped: empty Repository",
)),
},
{
desc: "empty bundle",
request: &gitalypb.CreateBundleFromRefListRequest{
Repository: repo,
Patterns: [][]byte{[]byte("master"), []byte("^master")},
},
expectedErr: status.Error(codes.FailedPrecondition, "cmd wait failed: refusing to create empty bundle"),
},
}
for _, testCase := range testCases {
t.Run(testCase.desc, func(t *testing.T) {
stream, err := client.CreateBundleFromRefList(ctx)
require.NoError(t, err)
require.NoError(t, stream.Send(testCase.request))
require.NoError(t, stream.CloseSend())
for _, err = stream.Recv(); err == nil; _, err = stream.Recv() {
}
testhelper.RequireGrpcError(t, testCase.expectedErr, err)
})
}
}
|