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
|
package updateref
import (
"bytes"
"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"
)
// 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
stderr *bytes.Buffer
}
// 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, conf config.Cfg, gitCmdFactory git.CommandFactory, repo repository.GitRepo, opts ...UpdaterOpt) (*Updater, error) {
var cfg updaterConfig
for _, opt := range opts {
opt(&cfg)
}
txOption := git.WithRefTxHook(ctx, repo, conf)
if cfg.disableTransactions {
txOption = git.WithDisabledHooks()
}
var stderr bytes.Buffer
cmd, err := gitCmdFactory.New(ctx, repo,
git.SubCmd{
Name: "update-ref",
Flags: []git.Option{git.Flag{Name: "-z"}, git.Flag{Name: "--stdin"}},
},
txOption,
git.WithStdin(command.SetupStdin),
git.WithStderr(&stderr),
)
if err != nil {
return nil, err
}
// By writing an explicit "start" to the command, we enable
// transactional behaviour. Which effectively means that without an
// explicit "commit", no changes will be inadvertently committed to
// disk.
if _, err := cmd.Write([]byte("start\x00")); err != nil {
return nil, err
}
return &Updater{repo: repo, cmd: cmd, stderr: &stderr}, nil
}
// Create commands the reference to be created with the sha specified in value
func (u *Updater) Create(reference git.ReferenceName, value string) error {
_, err := fmt.Fprintf(u.cmd, "create %s\x00%s\x00", reference.String(), value)
return err
}
// Update commands the reference to be updated to point at the sha specified in
// newvalue
func (u *Updater) Update(reference git.ReferenceName, newvalue, oldvalue string) error {
_, err := fmt.Fprintf(u.cmd, "update %s\x00%s\x00%s\x00", reference.String(), newvalue, oldvalue)
return err
}
// Delete commands the reference to be removed from the repository
func (u *Updater) Delete(reference git.ReferenceName) error {
_, err := fmt.Fprintf(u.cmd, "delete %s\x00\x00", reference.String())
return err
}
// Prepare prepares the reference transaction by locking all references and determining their
// current values. The updates are not yet committed and will be rolled back in case there is no
// call to `Wait()`. This call is optional.
func (u *Updater) Prepare() error {
_, err := fmt.Fprintf(u.cmd, "prepare\x00")
return err
}
// Wait applies the commands specified in other calls to the Updater
func (u *Updater) Wait() error {
if _, err := u.cmd.Write([]byte("commit\x00")); err != nil {
return err
}
if err := u.cmd.Wait(); err != nil {
return fmt.Errorf("git update-ref: %v, stderr: %q", err, u.stderr)
}
return nil
}
|