git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
62cb8aeacdb91441788930f2f0fbeefe478581bc blob 8745 bytes (raw)

  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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
 
#include "cache.h"
#include "repository.h"
#include "config.h"
#include "pkt-line.h"
#include "version.h"
#include "strvec.h"
#include "ls-refs.h"
#include "protocol-caps.h"
#include "serve.h"
#include "upload-pack.h"

static int advertise_sid;

static int always_advertise(struct repository *r,
			    struct strbuf *value)
{
	return 1;
}

static int agent_advertise(struct repository *r,
			   struct strbuf *value)
{
	if (value)
		strbuf_addstr(value, git_user_agent_sanitized());
	return 1;
}

static int object_format_advertise(struct repository *r,
				   struct strbuf *value)
{
	if (value)
		strbuf_addstr(value, r->hash_algo->name);
	return 1;
}

static int session_id_configure(const char *var, const char *value, void *data)
{
	if (!strcmp(var, "transfer.advertisesid"))
		advertise_sid = git_config_bool(var, value);
	return 0;
}

static int session_id_advertise(struct repository *r, struct strbuf *value)
{
	if (!advertise_sid)
		return 0;
	if (value)
		strbuf_addstr(value, trace2_session_id());
	return 1;
}

struct protocol_capability {
	/*
	 * The name of the capability.  The server uses this name when
	 * advertising this capability, and the client uses this name to
	 * specify this capability.
	 */
	const char *name;

	/*
	 * A git_config() callback. If defined it'll be dispatched too
	 * sometime before the other functions are called, giving the
	 * capability a chance to configure itself.
	 */
	int (*configure)(const char *var, const char *value, void *data);

	/*
	 * Function queried to see if a capability should be advertised.
	 * Optionally a value can be specified by adding it to 'value'.
	 * If a value is added to 'value', the server will advertise this
	 * capability as "<name>=<value>" instead of "<name>".
	 */
	int (*advertise)(struct repository *r, struct strbuf *value);

	/*
	 * Function called when a client requests the capability as a command.
	 * The function will be provided the capabilities requested via 'keys'
	 * as well as a struct packet_reader 'request' which the command should
	 * use to read the command specific part of the request.  Every command
	 * MUST read until a flush packet is seen before sending a response.
	 *
	 * This field should be NULL for capabilities which are not commands.
	 */
	int (*command)(struct repository *r,
		       struct strvec *keys,
		       struct packet_reader *request);
};

static struct protocol_capability capabilities[] = {
	{
		.name = "agent",
		.advertise = agent_advertise,
	},
	{
		.name = "ls-refs",
		.configure = ls_refs_configure,
		.advertise = ls_refs_advertise,
		.command = ls_refs,
	},
	{
		.name = "fetch",
		.advertise = upload_pack_advertise,
		.command = upload_pack_v2,
	},
	{
		.name = "server-option",
		.advertise = always_advertise,
	},
	{
		.name = "object-format",
		.advertise = object_format_advertise,
	},
	{
		.name = "session-id",
		.configure = session_id_configure,
		.advertise = session_id_advertise,
	},
	{
		.name = "object-info",
		.advertise = always_advertise,
		.command = cap_object_info,
	},
};

static int git_serve_config(const char *var, const char *value, void *data)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
		struct protocol_capability *c = &capabilities[i];
		config_fn_t fn = c->configure;
		int ret;
		if (!fn)
			continue;
		ret = fn(var, value, data);
		if (ret)
			return ret;
	}
	return 0;
}

static void advertise_capabilities(void)
{
	struct strbuf capability = STRBUF_INIT;
	struct strbuf value = STRBUF_INIT;
	int i;

	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
		struct protocol_capability *c = &capabilities[i];

		if (c->advertise(the_repository, &value)) {
			strbuf_addstr(&capability, c->name);

			if (value.len) {
				strbuf_addch(&capability, '=');
				strbuf_addbuf(&capability, &value);
			}

			strbuf_addch(&capability, '\n');
			packet_write(1, capability.buf, capability.len);
		}

		strbuf_reset(&capability);
		strbuf_reset(&value);
	}

	packet_flush(1);
	strbuf_release(&capability);
	strbuf_release(&value);
}

static struct protocol_capability *get_capability(const char *key)
{
	int i;

	if (!key)
		return NULL;

	for (i = 0; i < ARRAY_SIZE(capabilities); i++) {
		struct protocol_capability *c = &capabilities[i];
		const char *out;
		if (skip_prefix(key, c->name, &out) && (!*out || *out == '='))
			return c;
	}

	return NULL;
}

static int is_valid_capability(const char *key)
{
	const struct protocol_capability *c = get_capability(key);

	return c && c->advertise(the_repository, NULL);
}

static int is_command(const char *key, struct protocol_capability **command)
{
	const char *out;

	if (skip_prefix(key, "command=", &out)) {
		struct protocol_capability *cmd = get_capability(out);

		if (*command)
			die("command '%s' requested after already requesting command '%s'",
			    out, (*command)->name);
		if (!cmd || !cmd->advertise(the_repository, NULL) || !cmd->command)
			die("invalid command '%s'", out);

		*command = cmd;
		return 1;
	}

	return 0;
}

static int has_capability(const struct strvec *keys, const char *capability,
			  const char **value)
{
	int i;
	for (i = 0; i < keys->nr; i++) {
		const char *out;
		if (skip_prefix(keys->v[i], capability, &out) &&
		    (!*out || *out == '=')) {
			if (value) {
				if (*out == '=')
					out++;
				*value = out;
			}
			return 1;
		}
	}

	return 0;
}

static void check_algorithm(struct repository *r, struct strvec *keys)
{
	int client = GIT_HASH_SHA1, server = hash_algo_by_ptr(r->hash_algo);
	const char *algo_name;

	if (has_capability(keys, "object-format", &algo_name)) {
		client = hash_algo_by_name(algo_name);
		if (client == GIT_HASH_UNKNOWN)
			die("unknown object format '%s'", algo_name);
	}

	if (client != server)
		die("mismatched object format: server %s; client %s\n",
		    r->hash_algo->name, hash_algos[client].name);
}

enum request_state {
	PROCESS_REQUEST_KEYS,
	PROCESS_REQUEST_DONE,
};

static int process_request(void)
{
	enum request_state state = PROCESS_REQUEST_KEYS;
	struct packet_reader reader;
	struct strvec keys = STRVEC_INIT;
	struct protocol_capability *command = NULL;
	const char *client_sid;

	packet_reader_init(&reader, 0, NULL, 0,
			   PACKET_READ_CHOMP_NEWLINE |
			   PACKET_READ_GENTLE_ON_EOF |
			   PACKET_READ_DIE_ON_ERR_PACKET);

	/*
	 * Check to see if the client closed their end before sending another
	 * request.  If so we can terminate the connection.
	 */
	if (packet_reader_peek(&reader) == PACKET_READ_EOF)
		return 1;
	reader.options &= ~PACKET_READ_GENTLE_ON_EOF;

	while (state != PROCESS_REQUEST_DONE) {
		switch (packet_reader_peek(&reader)) {
		case PACKET_READ_EOF:
			BUG("Should have already died when seeing EOF");
		case PACKET_READ_NORMAL:
			/* collect request; a sequence of keys and values */
			if (is_command(reader.line, &command) ||
			    is_valid_capability(reader.line))
				strvec_push(&keys, reader.line);
			else
				die("unknown capability '%s'", reader.line);

			/* Consume the peeked line */
			packet_reader_read(&reader);
			break;
		case PACKET_READ_FLUSH:
			/*
			 * If no command and no keys were given then the client
			 * wanted to terminate the connection.
			 */
			if (!keys.nr)
				return 1;

			/*
			 * The flush packet isn't consume here like it is in
			 * the other parts of this switch statement.  This is
			 * so that the command can read the flush packet and
			 * see the end of the request in the same way it would
			 * if command specific arguments were provided after a
			 * delim packet.
			 */
			state = PROCESS_REQUEST_DONE;
			break;
		case PACKET_READ_DELIM:
			/* Consume the peeked line */
			packet_reader_read(&reader);

			state = PROCESS_REQUEST_DONE;
			break;
		case PACKET_READ_RESPONSE_END:
			BUG("unexpected stateless separator packet");
		}
	}

	if (!command)
		die("no command requested");

	check_algorithm(the_repository, &keys);

	if (has_capability(&keys, "session-id", &client_sid))
		trace2_data_string("transfer", NULL, "client-sid", client_sid);

	command->command(the_repository, &keys, &reader);

	strvec_clear(&keys);
	return 0;
}

/* Main serve loop for protocol version 2 */
void serve(struct serve_options *options)
{
	git_config(git_serve_config, NULL);

	if (options->advertise_capabilities || !options->stateless_rpc) {
		/* serve by default supports v2 */
		packet_write_fmt(1, "version 2\n");

		advertise_capabilities();
		/*
		 * If only the list of capabilities was requested exit
		 * immediately after advertising capabilities
		 */
		if (options->advertise_capabilities)
			return;
	}

	/*
	 * If stateless-rpc was requested then exit after
	 * a single request/response exchange
	 */
	if (options->stateless_rpc) {
		process_request();
	} else {
		for (;;)
			if (process_request())
				break;
	}
}
debug log:

solving 62cb8aeacdb ...
found 62cb8aeacdb in https://public-inbox.org/git/patch-5.5-c8625025125-20210616T141332Z-avarab@gmail.com/
found 49ea9fc8fd5 in https://public-inbox.org/git/patch-4.5-8e97412d584-20210616T141332Z-avarab@gmail.com/ ||
	https://public-inbox.org/git/patch-04.12-13f1a8d8325-20210721T233307Z-avarab@gmail.com/ ||
	https://public-inbox.org/git/patch-4.8-bd920de1629-20210628T191634Z-avarab@gmail.com/
found 6748c590b74 in https://public-inbox.org/git/patch-1.5-4f74d7d34c4-20210616T141332Z-avarab@gmail.com/ ||
	https://public-inbox.org/git/patch-01.12-192fb64ef82-20210721T233307Z-avarab@gmail.com/ ||
	https://public-inbox.org/git/patch-1.8-fdb0c5f4df1-20210628T191634Z-avarab@gmail.com/
found aa8209f147e in git.git.git
preparing index
index prepared:
100644 aa8209f147ef25c674502e663906779b09a237f8	serve.c

applying [1/7] https://public-inbox.org/git/patch-1.5-4f74d7d34c4-20210616T141332Z-avarab@gmail.com/
diff --git a/serve.c b/serve.c
index aa8209f147e..6748c590b74 100644

Checking patch serve.c...
Applied patch serve.c cleanly.

skipping https://public-inbox.org/git/patch-01.12-192fb64ef82-20210721T233307Z-avarab@gmail.com/ for 6748c590b74
skipping https://public-inbox.org/git/patch-1.8-fdb0c5f4df1-20210628T191634Z-avarab@gmail.com/ for 6748c590b74
index at:
100644 6748c590b74833ac1469a41252555e0ca65dc6f3	serve.c

applying [2/7] https://public-inbox.org/git/patch-4.5-8e97412d584-20210616T141332Z-avarab@gmail.com/
diff --git a/serve.c b/serve.c
index 6748c590b74..49ea9fc8fd5 100644

Checking patch serve.c...
Applied patch serve.c cleanly.

skipping https://public-inbox.org/git/patch-04.12-13f1a8d8325-20210721T233307Z-avarab@gmail.com/ for 49ea9fc8fd5
skipping https://public-inbox.org/git/patch-4.8-bd920de1629-20210628T191634Z-avarab@gmail.com/ for 49ea9fc8fd5
index at:
100644 49ea9fc8fd5dceff9b3da76ba8bd839573ace61a	serve.c

applying [3/7] https://public-inbox.org/git/patch-5.5-c8625025125-20210616T141332Z-avarab@gmail.com/
diff --git a/serve.c b/serve.c
index 49ea9fc8fd5..62cb8aeacdb 100644

Checking patch serve.c...
Applied patch serve.c cleanly.

index at:
100644 62cb8aeacdb91441788930f2f0fbeefe478581bc	serve.c

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).