-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommit_executor.go
More file actions
197 lines (177 loc) · 5.4 KB
/
Copy pathcommit_executor.go
File metadata and controls
197 lines (177 loc) · 5.4 KB
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
// SPDX-License-Identifier: Apache-2.0
package git
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func (w *BranchWorker) executePendingWrites(
ctx context.Context,
repo *gogit.Repository,
pendingWrites []PendingWrite,
) (int, error) {
worktree, err := repo.Worktree()
if err != nil {
return 0, fmt.Errorf("failed to get worktree: %w", err)
}
commitsCreated := 0
// Index over the slice so the per-write commit hash is written back onto the
// caller's PendingWrite (§6.5): a CommitRequest riding a write resolves to this
// SHA on push, and a rebase-replay (which re-runs this loop on the retained
// writes) refreshes it to the post-rebase hash.
for i := range pendingWrites {
created, hash, err := w.executePendingWrite(ctx, repo, worktree, pendingWrites[i])
if err != nil {
return commitsCreated, err
}
pendingWrites[i].CommitSHA = hash
commitsCreated += created
}
return commitsCreated, nil
}
func (p PendingWrite) path() string {
if targetPath := p.Target().Path; targetPath != "" {
return targetPath
}
for _, event := range p.Events {
if event.Path != "" {
return event.Path
}
}
return ""
}
func (p PendingWrite) commitMetadata() (string, *gogit.CommitOptions, error) {
when := time.Now()
// An explicit literal message (e.g. from a CommitRequest's spec.message)
// is used verbatim, bypassing the configured templates.
if message := strings.TrimSpace(p.CommitMessage); message != "" {
return p.CommitMessage, commitOptionsFor(p, p.CommitConfig, p.Signer, when), nil
}
switch p.MessageKind() {
case CommitMessagePerEvent:
if len(p.Events) != 1 {
return "", nil, errors.New("per-event pending write requires exactly one event")
}
message, err := renderEventCommitMessage(p.Events[0], p.CommitConfig)
if err != nil {
return "", nil, err
}
return message, commitOptionsFor(p, p.CommitConfig, p.Signer, when), nil
case CommitMessageReconcile:
message, err := renderReconcileCommitMessageFromEvents(
p.Events,
p.CommitMessage,
p.Target().Name,
p.CommitConfig,
)
if err != nil {
return "", nil, err
}
return message, commitOptionsFor(p, p.CommitConfig, p.Signer, when), nil
case CommitMessageGrouped:
message, err := renderGroupCommitMessage(p, p.CommitConfig)
if err != nil {
return "", nil, err
}
return message, commitOptionsFor(p, p.CommitConfig, p.Signer, when), nil
default:
return "", nil, fmt.Errorf("unsupported commit message kind %q", p.MessageKind())
}
}
func (w *BranchWorker) executePendingWrite(
ctx context.Context,
repo *gogit.Repository,
worktree *gogit.Worktree,
pendingWrite PendingWrite,
) (int, plumbing.Hash, error) {
switch pendingWrite.Kind {
case PendingWriteResync:
// Resync writes never carry a CommitRequest, so their commit hash is unused;
// report ZeroHash to keep the per-write SHA bookkeeping uniform.
created, err := w.executeResyncPendingWrite(ctx, repo, worktree, pendingWrite)
return created, plumbing.ZeroHash, err
case PendingWriteCommit, PendingWriteAtomic:
default:
return 0, plumbing.ZeroHash, fmt.Errorf("unsupported pending write kind %q", pendingWrite.Kind)
}
if len(pendingWrite.Events) == 0 {
return 0, plumbing.ZeroHash, nil
}
target := pendingWrite.Target()
encryptionPath := filepath.Join(worktree.Filesystem.Root(), sanitizePath(pendingWrite.path()))
if err := configureSecretEncryptionWriter(
w.contentWriter,
encryptionPath,
target.EncryptionConfig,
); err != nil {
return 0, plumbing.ZeroHash, fmt.Errorf("configure secret encryptor: %w", err)
}
anyChanges, err := w.applyPendingWriteEvents(ctx, repo, worktree, pendingWrite.Events, pendingWrite.Targets)
if err != nil {
return 0, plumbing.ZeroHash, err
}
if !anyChanges {
return 0, plumbing.ZeroHash, nil
}
commitMessage, commitOptions, err := pendingWrite.commitMetadata()
if err != nil {
return 0, plumbing.ZeroHash, err
}
hash, err := worktree.Commit(commitMessage, commitOptions)
if err != nil {
return 0, plumbing.ZeroHash, fmt.Errorf("failed to create commit: %w", err)
}
log.FromContext(ctx).Info(
"git commit created",
"messageKind",
pendingWrite.MessageKind(),
"events",
len(pendingWrite.Events),
"message",
commitMessage,
)
return 1, hash, nil
}
func (w *BranchWorker) applyPendingWriteEvents(
ctx context.Context,
repo *gogit.Repository,
worktree *gogit.Worktree,
events []Event,
targets map[pendingTargetKey]ResolvedTargetMetadata,
) (bool, error) {
// Stage path-scoped bootstrap files first, before any resource write, exactly as
// the per-event path did.
for _, event := range events {
if err := ensureBootstrapTemplateInPath(repo, sanitizePath(event.Path), event.BootstrapOptions); err != nil {
return false, err
}
}
// Plan-then-flush each GitTarget subtree once: build the structure model, resolve
// every event to a single-identity action, apply to hydrated file buffers, and
// flush dirty/deleted files. A grouped window is single-target, so this is usually
// one base path.
byBase := groupEventsByBase(events)
anyChanges := false
for _, base := range sortedBaseKeys(byBase) {
changed, err := w.flushEventsToWorktree(
ctx,
worktree,
base,
byBase[base],
placementPolicyForBase(targets, base),
)
if err != nil {
return false, err
}
if changed {
anyChanges = true
}
}
return anyChanges, nil
}