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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
package objectpool
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gitlab.com/gitlab-org/gitaly/internal/git/remote"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/internal/helper/text"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
// Link will write the relative path to the object pool from the repository that
// is to join the pool. This does not trigger deduplication, which is the
// responsibility of the caller.
func (o *ObjectPool) Link(ctx context.Context, repo *gitalypb.Repository) error {
altPath, err := git.InfoAlternatesPath(repo)
if err != nil {
return err
}
relPath, err := o.getRelativeObjectPath(repo)
if err != nil {
return err
}
expectedContent := filepath.Join(relPath, "objects")
actualContentBytes, err := ioutil.ReadFile(altPath)
if err == nil {
actualContent := text.ChompBytes(actualContentBytes)
if actualContent == expectedContent {
return nil
}
if filepath.Clean(actualContent) != filepath.Join(o.FullPath(), "objects") {
return fmt.Errorf("unexpected alternates content: %q", actualContent)
}
}
if err != nil && !os.IsNotExist(err) {
return err
}
tmp, err := ioutil.TempFile(filepath.Dir(altPath), "alternates")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
if _, err := io.WriteString(tmp, expectedContent); err != nil {
return err
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Rename(tmp.Name(), altPath); err != nil {
return err
}
return o.removeMemberBitmaps(repo)
}
// removeMemberBitmaps removes packfile bitmaps from the member
// repository that just joined the pool. If Git finds two packfiles with
// bitmaps it will print a warning, which is visible to the end user
// during a Git clone. Our goal is to avoid that warning. In normal
// operation, the next 'git gc' or 'git repack -ad' on the member
// repository will remove its local bitmap file. In other words the
// situation we eventually converge to is that the pool may have a bitmap
// but none of its members will. With removeMemberBitmaps we try to
// change "eventually" to "immediately", so that users won't see the
// warning. https://gitlab.com/gitlab-org/gitaly/issues/1728
func (o *ObjectPool) removeMemberBitmaps(repo repository.GitRepo) error {
poolBitmaps, err := getBitmaps(o)
if err != nil {
return err
}
if len(poolBitmaps) == 0 {
return nil
}
memberBitmaps, err := getBitmaps(repo)
if err != nil {
return err
}
if len(memberBitmaps) == 0 {
return nil
}
for _, bitmap := range memberBitmaps {
if err := os.Remove(bitmap); err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}
func getBitmaps(repo repository.GitRepo) ([]string, error) {
repoPath, err := helper.GetPath(repo)
if err != nil {
return nil, err
}
packDir := filepath.Join(repoPath, "objects/pack")
entries, err := ioutil.ReadDir(packDir)
if err != nil {
return nil, err
}
var bitmaps []string
for _, entry := range entries {
if name := entry.Name(); strings.HasSuffix(name, ".bitmap") && strings.HasPrefix(name, "pack-") {
bitmaps = append(bitmaps, filepath.Join(packDir, name))
}
}
return bitmaps, nil
}
func (o *ObjectPool) getRelativeObjectPath(repo *gitalypb.Repository) (string, error) {
repoPath, err := helper.GetRepoPath(repo)
if err != nil {
return "", err
}
relPath, err := filepath.Rel(filepath.Join(repoPath, "objects"), o.FullPath())
if err != nil {
return "", err
}
return relPath, nil
}
// LinkedToRepository tests if a repository is linked to an object pool
func (o *ObjectPool) LinkedToRepository(repo *gitalypb.Repository) (bool, error) {
altPath, err := git.InfoAlternatesPath(repo)
if err != nil {
return false, err
}
relPath, err := o.getRelativeObjectPath(repo)
if err != nil {
return false, err
}
if stat, err := os.Stat(altPath); err == nil && stat.Size() > 0 {
alternatesFile, err := os.Open(altPath)
if err != nil {
return false, err
}
defer alternatesFile.Close()
r := bufio.NewReader(alternatesFile)
b, err := r.ReadBytes('\n')
if err != nil && err != io.EOF {
return false, fmt.Errorf("reading alternates file: %v", err)
}
return string(b) == filepath.Join(relPath, "objects"), nil
}
return false, nil
}
// Unlink removes the alternates file, so Git won't look there anymore
// It removes the remote from the object pool too,
func (o *ObjectPool) Unlink(ctx context.Context, repo *gitalypb.Repository) error {
if !o.Exists() {
return errors.New("pool does not exist")
}
// We need to use removeRemote, and can't leverage `git config --remove-section`
// as the latter doesn't clean up refs
remoteName := repo.GetGlRepository()
if err := remote.Remove(ctx, o, remoteName); err != nil {
if present, err2 := remote.Exists(ctx, o, remoteName); err2 != nil || present {
return err
}
}
return nil
}
// Config options setting will leak the key value pairs in the logs. This makes
// this function not suitable for general usage, and scoped to this package.
// To be corrected in: https://gitlab.com/gitlab-org/gitaly/issues/1430
func (o *ObjectPool) setConfig(ctx context.Context, key, value string) error {
cmd, err := git.SafeCmd(ctx, o, nil, git.SubCmd{
Name: "config",
Flags: []git.Option{git.ConfigPair{Key: key, Value: value}},
})
if err != nil {
return err
}
return cmd.Wait()
}
|