aboutsummaryrefslogtreecommitdiff
path: root/protocol/v0v1/server/receivepack/report_status.go
blob: f6ac498513cf4bd44681e5bda098c50c982336a3 (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
package receivepack

import (
	"fmt"

	"codeberg.org/lindenii/furgit/protocol/pktline"
)

// WriteReportStatus writes one classic report-status response.
func (session *Session) WriteReportStatus(result ReportStatusResult) error {
	unpackResult := "ok"
	if result.UnpackError != "" {
		unpackResult = result.UnpackError
	}

	if !session.negotiated.SideBand64K {
		err := session.base.WriteData(fmt.Appendf(nil, "unpack %s\n", unpackResult))
		if err != nil {
			return err
		}

		for _, command := range result.Commands {
			line := fmt.Sprintf("ok %s\n", command.Name)
			if command.Error != "" {
				line = fmt.Sprintf("ng %s %s\n", command.Name, command.Error)
			}

			err = session.base.WriteData([]byte(line))
			if err != nil {
				return err
			}
		}

		return session.base.WriteFlush()
	}

	buf, err := pktline.AppendData(nil, fmt.Appendf(nil, "unpack %s\n", unpackResult))
	if err != nil {
		return err
	}

	for _, command := range result.Commands {
		line := fmt.Sprintf("ok %s\n", command.Name)
		if command.Error != "" {
			line = fmt.Sprintf("ng %s %s\n", command.Name, command.Error)
		}

		buf, err = pktline.AppendData(buf, []byte(line))
		if err != nil {
			return err
		}
	}

	buf = pktline.AppendFlushPkt(buf)

	w := session.base.PrimaryDataWriter()

	_, err = w.Write(buf)
	if err != nil {
		return err
	}

	return session.base.WriteFlush()
}

// WriteReportStatusV2 writes one report-status-v2 response.
func (session *Session) WriteReportStatusV2(result ReportStatusResult) error {
	unpackResult := "ok"
	if result.UnpackError != "" {
		unpackResult = result.UnpackError
	}

	if !session.negotiated.SideBand64K { //nolint:nestif
		err := session.base.WriteData(fmt.Appendf(nil, "unpack %s\n", unpackResult))
		if err != nil {
			return err
		}

		for _, command := range result.Commands {
			if command.Error != "" {
				err = session.base.WriteData(fmt.Appendf(nil, "ng %s %s\n", command.Name, command.Error))
				if err != nil {
					return err
				}

				continue
			}

			err = session.base.WriteData(fmt.Appendf(nil, "ok %s\n", command.Name))
			if err != nil {
				return err
			}

			if command.RefName != "" {
				err = session.base.WriteData(fmt.Appendf(nil, "option refname %s\n", command.RefName))
				if err != nil {
					return err
				}
			}

			if command.OldID != nil {
				err = session.base.WriteData(fmt.Appendf(nil, "option old-oid %s\n", *command.OldID))
				if err != nil {
					return err
				}
			}

			if command.NewID != nil {
				err = session.base.WriteData(fmt.Appendf(nil, "option new-oid %s\n", *command.NewID))
				if err != nil {
					return err
				}
			}

			if command.ForcedUpdate {
				err = session.base.WriteData([]byte("option forced-update\n"))
				if err != nil {
					return err
				}
			}
		}

		return session.base.WriteFlush()
	}

	buf, err := pktline.AppendData(nil, fmt.Appendf(nil, "unpack %s\n", unpackResult))
	if err != nil {
		return err
	}

	for _, command := range result.Commands {
		if command.Error != "" {
			buf, err = pktline.AppendData(buf, fmt.Appendf(nil, "ng %s %s\n", command.Name, command.Error))
			if err != nil {
				return err
			}

			continue
		}

		buf, err = pktline.AppendData(buf, fmt.Appendf(nil, "ok %s\n", command.Name))
		if err != nil {
			return err
		}

		if command.RefName != "" {
			buf, err = pktline.AppendData(buf, fmt.Appendf(nil, "option refname %s\n", command.RefName))
			if err != nil {
				return err
			}
		}

		if command.OldID != nil {
			buf, err = pktline.AppendData(buf, fmt.Appendf(nil, "option old-oid %s\n", *command.OldID))
			if err != nil {
				return err
			}
		}

		if command.NewID != nil {
			buf, err = pktline.AppendData(buf, fmt.Appendf(nil, "option new-oid %s\n", *command.NewID))
			if err != nil {
				return err
			}
		}

		if command.ForcedUpdate {
			buf, err = pktline.AppendData(buf, []byte("option forced-update\n"))
			if err != nil {
				return err
			}
		}
	}

	buf = pktline.AppendFlushPkt(buf)

	w := session.base.PrimaryDataWriter()

	_, err = w.Write(buf)
	if err != nil {
		return err
	}

	return session.base.WriteFlush()
}