aboutsummaryrefslogtreecommitdiff
path: root/receivepack/internal/service/execute.go
blob: 8febfc79d6844d4b6862b14165e982693af8f2ea (about) (plain) (blame)
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
package service

import (
	"context"
	"os"

	"codeberg.org/lindenii/furgit/format/pack/ingest"
)

// Execute validates one receive-pack request, optionally ingests its pack into
// quarantine, runs the optional hook, and applies allowed ref updates.
func (service *Service) Execute(ctx context.Context, req *Request) (*Result, error) {
	result := &Result{
		Commands: make([]CommandResult, 0, len(req.Commands)),
	}
	var (
		quarantineName string
		quarantineRoot *os.Root
		err            error
	)

	if req.PackExpected {
		if req.Pack == nil {
			result.UnpackError = "missing pack stream"
			fillCommandErrors(result, req.Commands, "missing pack stream")

			return result, nil
		}

		if service.opts.ObjectsRoot == nil {
			result.UnpackError = "objects root not configured"
			fillCommandErrors(result, req.Commands, "objects root not configured")

			return result, nil
		}

		quarantineName, quarantineRoot, err = service.createQuarantineRoot()
		if err != nil {
			result.UnpackError = err.Error()
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}

		defer func() {
			_ = quarantineRoot.Close()
			_ = service.opts.ObjectsRoot.RemoveAll(quarantineName)
		}()

		quarantinePackRoot, err := service.openQuarantinePackRoot(quarantineRoot)
		if err != nil {
			result.UnpackError = err.Error()
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}

		defer func() {
			_ = quarantinePackRoot.Close()
		}()

		ingested, err := ingest.Ingest(
			req.Pack,
			quarantinePackRoot,
			service.opts.Algorithm,
			true,
			true,
			service.opts.ExistingObjects,
		)
		if err != nil {
			result.UnpackError = err.Error()
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}

		result.Ingest = &ingested
	}

	for _, command := range req.Commands {
		result.Planned = append(result.Planned, PlannedUpdate{
			Name:   command.Name,
			OldID:  command.OldID,
			NewID:  command.NewID,
			Delete: isDelete(command),
		})
	}

	if len(req.Commands) == 0 {
		return result, nil
	}

	allowedCommands := append([]Command(nil), req.Commands...)
	allowedIndices := make([]int, 0, len(req.Commands))
	for index := range req.Commands {
		allowedIndices = append(allowedIndices, index)
	}
	rejected := make(map[int]string)

	if service.opts.Hook != nil {
		quarantinedObjects, err := service.openQuarantinedObjects(quarantineName)
		if err != nil {
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}

		defer func() {
			_ = quarantinedObjects.Close()
		}()

		decisions, err := service.opts.Hook(ctx, HookRequest{
			Refs:               service.opts.Refs,
			ExistingObjects:    service.opts.ExistingObjects,
			QuarantinedObjects: quarantinedObjects,
			Updates:            buildHookUpdates(req.Commands),
			PushOptions:        append([]string(nil), req.PushOptions...),
		})
		if err != nil {
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}

		if len(decisions) != len(req.Commands) {
			fillCommandErrors(result, req.Commands, "hook returned wrong number of update decisions")

			return result, nil
		}

		allowedCommands = allowedCommands[:0]
		allowedIndices = allowedIndices[:0]
		for index, decision := range decisions {
			if decision.Accept {
				allowedCommands = append(allowedCommands, req.Commands[index])
				allowedIndices = append(allowedIndices, index)

				continue
			}

			message := decision.Message
			if message == "" {
				message = "rejected by hook"
			}

			rejected[index] = message
		}

		if req.Atomic && len(rejected) != 0 {
			result.Commands = make([]CommandResult, 0, len(req.Commands))
			for index, command := range req.Commands {
				message := rejected[index]
				if message == "" {
					message = "atomic push rejected by hook"
				}

				result.Commands = append(result.Commands, resultForHookRejection(command, message))
			}

			return result, nil
		}
	}

	if len(allowedCommands) == 0 {
		result.Commands = mergeCommandResults(req.Commands, rejected, nil, nil)

		return result, nil
	}

	if req.PackExpected {
		// Git migrates quarantined objects into permanent storage immediately
		// before starting ref updates.
		err = service.promoteQuarantine(quarantineName, quarantineRoot)
		if err != nil {
			result.UnpackError = err.Error()
			fillCommandErrors(result, req.Commands, err.Error())

			return result, nil
		}
	}

	if req.Atomic {
		subresult := &Result{}
		err := service.applyAtomic(subresult, allowedCommands)
		if err != nil {
			return result, err
		}

		result.Commands = mergeCommandResults(req.Commands, rejected, subresult.Commands, allowedIndices)
		result.Applied = subresult.Applied

		return result, nil
	}

	subresult := &Result{}
	err = service.applyBatch(subresult, allowedCommands)
	if err != nil {
		return result, err
	}

	result.Commands = mergeCommandResults(req.Commands, rejected, subresult.Commands, allowedIndices)
	result.Applied = subresult.Applied

	return result, nil
}