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
|
package updateref
import (
"context"
"fmt"
"gitlab.com/gitlab-org/gitaly/internal/command"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/internal/helper"
)
// Updater wraps a `git update-ref --stdin` process, presenting an interface
// that allows references to be easily updated in bulk. It is not suitable for
// concurrent use.
type Updater struct {
repo repository.GitRepo
cmd *command.Command
}
// UpdaterOpt is a type representing options for the Updater.
type UpdaterOpt func(*updaterConfig)
type updaterConfig struct {
disableTransactions bool
}
// WithDisabledTransactions disables hooks such that no reference-transactions
// are used for the updater.
func WithDisabledTransactions() UpdaterOpt {
return func(cfg *updaterConfig) {
cfg.disableTransactions = true
}
}
// New returns a new bulk updater, wrapping a `git update-ref` process. Call the
// various methods to enqueue updates, then call Wait() to attempt to apply all
// the updates at once.
//
// It is important that ctx gets canceled somewhere. If it doesn't, the process
// spawned by New() may never terminate.
func New(ctx context.Context, repo repository.GitRepo, opts ...UpdaterOpt) (*Updater, error) {
var cfg updaterConfig
for _, opt := range opts {
opt(&cfg)
}
txOption := git.WithRefTxHook(ctx, helper.ProtoRepoFromRepo(repo), config.Config)
if cfg.disableTransactions {
txOption = git.WithDisabledHooks()
}
cmd, err := git.SafeStdinCmd(ctx, repo, nil,
git.SubCmd{
Name: "update-ref",
Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--stdin"}},
},
txOption,
)
if err != nil {
return nil, err
}
return &Updater{repo: repo, cmd: cmd}, nil
}
// Create commands the reference to be created with the sha specified in value
func (u *Updater) Create(ref, value string) error {
_, err := fmt.Fprintf(u.cmd, "create %s\x00%s\x00", ref, value)
return err
}
// Update commands the reference to be updated to point at the sha specified in
// newvalue
func (u *Updater) Update(ref, newvalue, oldvalue string) error {
_, err := fmt.Fprintf(u.cmd, "update %s\x00%s\x00%s\x00", ref, newvalue, oldvalue)
return err
}
// Delete commands the reference to be removed from the repository
func (u *Updater) Delete(ref string, oldvalue string) error {
_, err := fmt.Fprintf(u.cmd, "delete %s\x00%s\x00", ref, oldvalue)
return err
}
// Wait applies the commands specified in other calls to the Updater
func (u *Updater) Wait() error {
if err := u.cmd.Wait(); err != nil {
return fmt.Errorf("git update-ref: %v", err)
}
return nil
}
|