Skip to contentSkip to conversation

cat-file: add remote-object-info to batch-command

PATCH Superseded Eric Ju 13 participants 397 messages Jun 28, 2024 – Jul 24 Page 4 of 4

Subsystems (3)
lore
Share & tools
lore
#339 lore raw

PATCH v18 12/13 cat-file: validate remote atoms with an allow-list

In reply to Pablo Sabater

strstr() is not enough to validate the format placeholders in remote-object-info causing two errors:

1. Atoms recognized by expand_atom() but the remote doesn't returns 1, but data->type contains garbage causing segfault.

2. expand_atom() returns 0 for unknown atoms, calling strbuf_expand_bad_format() which ends up dying, blocking local queries if the same format is shared.

Add an allow-list with the supported atoms at the top of expand_atom(). In remote mode, unsupported atoms return 1 leaving the buffer empty, honoring how for-each-ref handles known but inapplicable atoms.

As extra safety, initialize data->type to OBJ_BAD and add a NULL check for type_name() so uninitialized data doesn't cause segfault.

Update tests that expect previous die() behavior to expect an empty string and add an explicit test for empty string return on unknown placeholder.

Update cat-file command documentation regarding remote-object-info.

---
 Documentation/git-cat-file.adoc        |  2 +-
 builtin/cat-file.c                     | 41 +++++++++++++++++++++++++++-------
 t/t1017-cat-file-remote-object-info.sh | 27 ++++++++++++++++++----
 3 files changed, 57 insertions(+), 13 deletions(-)
Documentation/git-cat-file.adoc +1 -1
index a7fa6674c3..643eac9245 100644
@@ -451,7 +451,7 @@ CAVEATS
 
 Note that since only `%(objectname)` and `%(objectsize)` are currently
 supported by the `remote-object-info` command. Using any other placeholder in
-the format string will raise an error.
+the format string will return an empty string in its position.
 
 Note that the sizes of objects on disk are reported accurately, but care
 should be taken in drawing conclusions about which refs or objects are
builtin/cat-file.c +33 -8

72 lines not on this page · raw message

t/t1017-cat-file-remote-object-info.sh +23 -4
index 49b6660934..6bc863c391 100755
@@ -236,6 +236,21 @@ test_expect_success 'remote-object-info does not die on missing oid like info' '
 	)
 '
 
+# This tests depends on %(objecttype) not being supported yet, once supported
+# it needs to be updated.
+test_expect_success 'unsupported placeholder on remote returns empty string' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo "" >expect &&
+		git cat-file --batch-command="%(objecttype)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
+		EOF
+		test_cmp expect actual
+	)
+'
+
 # Test --batch-command remote-object-info with 'git://' and
 # transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
 test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
@@ -575,10 +590,12 @@ test_expect_success 'remote-object-info fails on unsupported filter option (obje
 		set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
 		cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
 
-		test_must_fail git cat-file --batch-command="%(objectsize:disk)" 2>err <<-EOF &&
+		echo "$hello_oid " >expect &&
+
+		git cat-file --batch-command="%(objectname) %(objectsize:disk)" >actual <<-EOF &&
 		remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
 		EOF
-		test_grep "%(objectsize:disk) is currently not supported with remote-object-info" err
+		test_cmp expect actual
 	)
 '
 
@@ -587,10 +604,12 @@ test_expect_success 'remote-object-info fails on unsupported filter option (delt
 		set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
 		cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
 
-		test_must_fail git cat-file --batch-command="%(deltabase)" 2>err <<-EOF &&
+		echo "" >expect &&
+
+		git cat-file --batch-command="%(deltabase)" >actual <<-EOF &&
 		remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
 		EOF
-		test_grep "%(deltabase) is currently not supported with remote-object-info" err
+		test_cmp expect actual
 	)
 '
 
Signature · 1 line
2.54.0
#340 lore raw

PATCH v18 13/13 cat-file: make remote-object-info allow-list dynamic

In reply to Pablo Sabater

The static allow-list in expand_atom() is hardcoded to only allow "objectname" and "objectsize" for remote queries. This works because up to this point all servers will either support object-info with name and size or they do not support them at all, but we cannot expect that in a future different servers with different git versions to have the same object-info capabilities. Therefore, the allow_list needs to be dynamic depending on what the server advertises.

The client will now:

1. Request the protocol option that the placeholder refers to (i.e. "size" when "%(objectsize)").

2. Filters the request in fetch_object_info() dropping any option that the server does not advertise.

3. After the fetching, the options that haven't been dropped are the ones fetched and supported by the server, these supported options are mapped and remote_allowed_atoms is populated with the placeholders.

4. expand_atom() checks remote_allowed_atoms with the same behaviour as the static allow_list had.

Move object_info_options out of get_remote_info so the caller which has data can select what options will be requested instead of requesting always size.
Move batch_object_write() out so there will always be an output even if all the placeholders are not supported by the server (returns an empty line).

Include "type" in the object_info_options so once the server supports it, the clients know already how to request it.

---
 builtin/cat-file.c  | 97 +++++++++++++++++++++++++++++++++++------------------
 fetch-object-info.c | 20 +++++++++++
 2 files changed, 84 insertions(+), 33 deletions(-)
builtin/cat-file.c +64 -33
index af388b6238..20d9705a7f 100644
@@ -338,13 +338,11 @@ struct expand_data {
 	 * Flags about when an object info is being fetched from remote.
 	 */
 	unsigned is_remote:1;
-};
-#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
 
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	struct string_list remote_allowed_atoms;
 };
+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
 
 static int is_atom(const char *atom, const char *s, int slen)
 {
@@ -356,17 +354,11 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
 		       struct expand_data *data)
 {
 	if (data->is_remote) {
-		size_t i, allowed_nr = ARRAY_SIZE(remote_object_info_atoms);
-		for (i = 0; i < allowed_nr; i++)
-			if (is_atom(remote_object_info_atoms[i], atom, len))
+		size_t i;
+		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
 				break;
-
-		/*
-		 * On remote, skip unsupported atoms returning an empty sb,
-		 * honoring how for-each-ref handles known but inapplicable
-		 * atoms (e.g. %(tagger)).
-		 */
-		if (i == allowed_nr)
+		if (i == data->remote_allowed_atoms.nr)
 			return 1;
 	}
 
@@ -683,12 +675,12 @@ static int get_remote_info(struct batch_options *opt,
 			   int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	/*
@@ -736,15 +728,12 @@ static int get_remote_info(struct batch_options *opt,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;
 
-	string_list_append(&object_info_options, "size");
-
-	if (object_info_options.nr > 0) {
-		gtransport->smart_options->object_info_options = &object_info_options;
+	if (object_info_options->nr > 0) {
+		gtransport->smart_options->object_info_options = object_info_options;
 		gtransport->smart_options->object_info_data = *remote_object_info;
 		retval = transport_fetch_object_info(gtransport);
 	}
 cleanup:
-	string_list_clear(&object_info_options, 0);
 	transport_disconnect(gtransport);
 	return retval;
 }
@@ -830,6 +819,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
 		load_mailmap();
 }
 
+struct protocol_placeholder_entry {
+	const char *option;
+	const char *atom;
+};
+
+static const struct protocol_placeholder_entry remote_atom_map[] = {
+	{"size", "objectsize"},
+	{"type", "objecttype"},
+	/*
+	 * Add new protocol options here. Even if the server doesn't support
+	 * them the allow_list will drop them if the server doesn't advertise
+	 * them.
+	 */
+};
+
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -839,6 +843,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
 		die(_("remote-object-info command too long"));
@@ -851,32 +856,57 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");
+
 	if (get_remote_info(opt, count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		goto cleanup;
 
+	string_list_clear(&data->remote_allowed_atoms, 0);
+	string_list_append(&data->remote_allowed_atoms, "objectname");
+	for (size_t i = 0; i < ARRAY_SIZE(remote_atom_map); i++)
+		if (unsorted_string_list_has_string(&object_info_options, remote_atom_map[i].option))
+			string_list_append(&data->remote_allowed_atoms,
+					   remote_atom_map[i].atom);
+
 	data->skip_object_info = 1;
 	for (size_t i = 0; i < object_info_oids.nr; i++) {
+		int found = 0;
 		data->oid = object_info_oids.oid[i];
+		/*
+		 * When reaching here, it means remote-object-info can retrieve
+		 * information from server without downloading them.
+		 */
 		if (remote_object_info[i].sizep) {
-			/*
-			 * When reaching here, it means remote-object-info can retrieve
-			 * information from server without downloading them.
-			 */
 			data->size = *remote_object_info[i].sizep;
-			opt->batch_mode = BATCH_MODE_INFO;
-			data->is_remote = 1;
-			batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
-			data->is_remote = 0;
-		} else {
-			report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing");
+			found = 1;
 		}
+
+		if (remote_object_info[i].typep) {
+			data->type = *remote_object_info[i].typep;
+			found = 1;
+		}
+
+		if (!found && object_info_options.nr > 0) {
+			report_object_status(opt, oid_to_hex(&data->oid),
+					     &data->oid, "missing");
+			continue;
+		}
+
+		opt->batch_mode = BATCH_MODE_INFO;
+		data->is_remote = 1;
+		batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
+		data->is_remote = 0;
 	}
 	data->skip_object_info = 0;
 
 cleanup:
 	for (size_t i = 0; i < object_info_oids.nr; i++)
 		free_object_info_contents(&remote_object_info[i]);
+	string_list_clear(&object_info_options, 0);
 	free(line_to_split);
 	free(argv);
 	free(remote_object_info);
@@ -1192,6 +1222,7 @@ static int batch_objects(struct batch_options *opt)
  cleanup:
 	strbuf_release(&input);
 	strbuf_release(&output);
+	string_list_clear(&data.remote_allowed_atoms, 0);
 	cfg->warn_on_object_refname_ambiguity = save_warning;
 	return retval;
 }
fetch-object-info.c +20 -0
index 5f98840c7c..160c29507a 100644
@@ -55,6 +55,26 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 	case protocol_v2:
 		if (!server_supports_v2("object-info"))
 			die(_("object-info capability is not enabled on the server"));
+		/*
+		 * When removing an element from the list it gets swapped by the
+		 * last element, iterate backwards to prevent elements skipping
+		 * evaluation.
+		 *
+		 * object_info_options->nr can be safely casted without overflow
+		 * beacuse the number of options is a small known number (the
+		 * supported placeholders which currently are size and type).
+		 */
+		for (int i = (int)args->object_info_options->nr - 1; i >= 0; i--)
+			if (!server_supports_feature("object-info",
+						     args->object_info_options->items[i].string, 0))
+				unsorted_string_list_delete_item(args->object_info_options, i, 0);
+		/*
+		 * If no options are left after the filtering, avoid unnecessary
+		 * request to the server.
+		 */
+		if (!args->object_info_options->nr)
+			return 0;
+
 		send_object_info_request(fd_out, args);
 		break;
 	case protocol_v1:
Signature · 1 line
2.54.0
#341 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 8 lines · Pablo Sabater

The static allow-list in expand_atom() is hardcoded to only allow "objectname" and "objectsize" for remote queries. This works because up to this point all servers will either support object-info with name and size or they do not support them at all, but we cannot expect that in a future different servers with different git versions to have the same object-info capabilities. Therefore, the allow_list needs to be dynamic depending on what the server advertises.

Quoted 4 lines

The client will now:

1. Request the protocol option that the placeholder refers to (i.e. "size" when "%(objectsize)").

"when" -> "for"?

Quoted 10 lines

2. Filters the request in fetch_object_info() dropping any option that the server does not advertise.

3. After the fetching, the options that haven't been dropped are the ones fetched and supported by the server, these supported options are mapped and remote_allowed_atoms is populated with the placeholders.

4. expand_atom() checks remote_allowed_atoms with the same behaviour as the static allow_list had.

I am not sure I follow the above entirely. Could you add a concrete example to the commit message?

For instance, if the client wants "%(objectsize) %(objectcolor)" and the server only supports 'size' but not 'color', the filtering in step (2) prevents the client from asking about the color, requesting only the size instead. When the server says the size is 42, step (3) uses that to substitute '%(objectsize)'. Would the end result then be "42 %(objectcolor)"?

builtin/cat-file.c struct expand_data { · line 345

Quoted 7 lines
builtin/cat-file.c +3 -3
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	struct string_list remote_allowed_atoms;
 };
+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

Hmph, is this list expected to change over time? One-line-per-item format would be more suited for updates if it is the case.

Quoted 18 lines
builtin/cat-file.c +2 -2
@@ -683,12 +675,12 @@ static int get_remote_info(struct batch_options *opt,
 			   int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	/*
@@ -736,15 +728,12 @@ static int get_remote_info(struct batch_options *opt,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;

builtin/cat-file.c static int get_remote_info(struct batch_options *opt, · line 735

Quoted 10 lines
builtin/cat-file.c +2 -4
 
-	string_list_append(&object_info_options, "size");
-
-	if (object_info_options.nr > 0) {
-		gtransport->smart_options->object_info_options = &object_info_options;
+	if (object_info_options->nr > 0) {
+		gtransport->smart_options->object_info_options = object_info_options;
 		gtransport->smart_options->object_info_data = *remote_object_info;
 		retval = transport_fetch_object_info(gtransport);
 	}

This is not a new issue, but if the caller does not ask for anything in object_info_options, no call to transport_fetch_object_info() is made here. This is so even though we went through quite a lot of work, including the connection establishment and teardown below.

By failing to contact the remote side, we wouldn't even know if the objects being queried actually exist there, which is probably even worse.

Quoted 12 lines
builtin/cat-file.c +1 -0
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -839,6 +843,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
 		die(_("remote-object-info command too long"));

builtin/cat-file.c static void parse_cmd_remote_object_info(struct batch_options *opt, · line 862

Quoted 7 lines
builtin/cat-file.c +4 -0
@@ -851,32 +856,57 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");

And if the request is only for "%(objectname)", an empty object_info_options is given to get_remote_info().

Quoted 4 lines
builtin/cat-file.c +1 -1
 	if (get_remote_info(opt, count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		goto cleanup;
#342 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Pablo Sabater

The static allow-list in expand_atom() is hardcoded to only allow "objectname" and "objectsize" for remote queries. This works because up to this point all servers will either support object-info with name and size or they do not support them at all, but we cannot expect that in a future different servers with different git versions to have the same object-info capabilities. Therefore, the allow_list needs to be dynamic depending on what the server advertises.

Quoted 6 lines
Quoted 4 lines

The client will now:

1. Request the protocol option that the placeholder refers to (i.e. "size" when "%(objectsize)").

"when" -> "for"?

Will change.

Quoted 15 lines
Quoted 11 lines

2. Filters the request in fetch_object_info() dropping any option that the server does not advertise.

3. After the fetching, the options that haven't been dropped are the ones fetched and supported by the server, these supported options are mapped and remote_allowed_atoms is populated with the placeholders.

4. expand_atom() checks remote_allowed_atoms with the same behaviour as the static allow_list had.

I am not sure I follow the above entirely. Could you add a concrete example to the commit message?

Quoted 6 lines

For instance, if the client wants "%(objectsize) %(objectcolor)" and the server only supports 'size' but not 'color', the filtering in step (2) prevents the client from asking about the color, requesting only the size instead. When the server says the size is 42, step (3) uses that to substitute '%(objectsize)'. Would the end result then be "42 %(objectcolor)"?

You've gotten everything right until the last step, because we have only size from the server there is no data to match %(objectcolor) and the end result is an empty string for %(objeccolor):

"42 "

Note that %(objectcolor) doesn't exists and it would have die(), the empty string is only for known but unsupported placeholders.

This is what for-each-ref does for known but unaplicable placeholders (atoms).

I'll add an example to the commmit log so it is clearer.

Quoted 11 lines

builtin/cat-file.c struct expand_data { · line 345

Quoted 8 lines
builtin/cat-file.c +3 -3
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	struct string_list remote_allowed_atoms;
 };
+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

Hmph, is this list expected to change over time? One-line-per-item format would be more suited for updates if it is the case.

Will format like so.

Quoted 36 lines
Quoted 19 lines
builtin/cat-file.c +2 -2
@@ -683,12 +675,12 @@ static int get_remote_info(struct batch_options *opt,
 			   int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	/*
@@ -736,15 +728,12 @@ static int get_remote_info(struct batch_options *opt,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;

builtin/cat-file.c static int get_remote_info(struct batch_options *opt, · line 735

Quoted 10 lines
builtin/cat-file.c +2 -4
 
-	string_list_append(&object_info_options, "size");
-
-	if (object_info_options.nr > 0) {
-		gtransport->smart_options->object_info_options = &object_info_options;
+	if (object_info_options->nr > 0) {
+		gtransport->smart_options->object_info_options = object_info_options;
 		gtransport->smart_options->object_info_data = *remote_object_info;
 		retval = transport_fetch_object_info(gtransport);
 	}

This is not a new issue, but if the caller does not ask for anything in object_info_options, no call to transport_fetch_object_info() is made here. This is so even though we went through quite a lot of work, including the connection establishment and teardown below.

Quoted 3 lines

By failing to contact the remote side, we wouldn't even know if the objects being queried actually exist there, which is probably even worse.

[Answered below]

Quoted 16 lines
Quoted 16 lines
builtin/cat-file.c +1 -0
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -839,6 +843,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
 		die(_("remote-object-info command too long"));
@@ -851,32 +856,57 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
Quoted 7 lines

builtin/cat-file.c static void parse_cmd_remote_object_info(struct batch_options *opt, · line 862

Quoted 4 lines
builtin/cat-file.c +4 -0
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");

And if the request is only for "%(objectname)", an empty object_info_options is given to get_remote_info().

Right now 'name' is not part of the protocol as 'type' or 'size' are, 'objectname' is always allowed but only shown if it's present on the format.
If the format is only "%(objectname)" then there's nothing to ask the server for.

The current code avoids making the request if there's only objectname or nothing supported, but still goes through the connection work. I will add an early return to just output the oid back without any connection.

Returning the oid is what the client expects for which we don't need a connection, but this means that we skip the existence check that we would get if we asked for size.

The server is not ready to support a bare oid for existence check, it could be done but that work belongs to a future series.

Quoted 5 lines

builtin/cat-file.c static void parse_cmd_remote_object_info(struct batch_options *opt, · line 866

Quoted 5 lines
builtin/cat-file.c +1 -1
 	if (get_remote_info(opt, count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		goto cleanup;

Regards,
Pablo

#343 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 20 lines · Pablo Sabater
Quoted 13 lines
Quoted 9 lines

2. Filters the request in fetch_object_info() dropping any option that the server does not advertise.

3. After the fetching, the options that haven't been dropped are the ones fetched and supported by the server, these supported options are mapped and remote_allowed_atoms is populated with the placeholders.

4. expand_atom() checks remote_allowed_atoms with the same behaviour as the static allow_list had.

I am not sure I follow the above entirely. Could you add a concrete example to the commit message?

Quoted 6 lines

For instance, if the client wants "%(objectsize) %(objectcolor)" and the server only supports 'size' but not 'color', the filtering in step (2) prevents the client from asking about the color, requesting only the size instead. When the server says the size is 42, step (3) uses that to substitute '%(objectsize)'. Would the end result then be "42 %(objectcolor)"?

Quoted 8 lines

You've gotten everything right until the last step, because we have only size from the server there is no data to match %(objectcolor) and the end result is an empty string for %(objeccolor):

"42 "

Note that %(objectcolor) doesn't exists and it would have die(), the empty string is only for known but unsupported placeholders.

It was not clear there is a distinction between "unknown" and "known but unsupported". The proposed log message needs to be clarified to make this distinction obvious.

Quoted 9 lines
Quoted 2 lines

And if the request is only for "%(objectname)", an empty object_info_options is given to get_remote_info().

Right now 'name' is not part of the protocol as 'type' or 'size' are, 'objectname' is always allowed but only shown if it's present on the format.
If the format is only "%(objectname)" then there's nothing to ask the server for.

Quoted 3 lines

The current code avoids making the request if there's only objectname or nothing supported, but still goes through the connection work. I will add an early return to just output the oid back without any connection.

I think you are heading in the opposite direction. Rather, when only the object name is requested, I was hoping we would pick something cheap to retrieve and ask the remote side for it, if only to catch a bogus or missing object name.

Thanks.

#344 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 26 lines · Junio C Hamano
Quoted 24 lines · Pablo Sabater
Quoted 13 lines
Quoted 9 lines

2. Filters the request in fetch_object_info() dropping any option that the server does not advertise.

3. After the fetching, the options that haven't been dropped are the ones fetched and supported by the server, these supported options are mapped and remote_allowed_atoms is populated with the placeholders.

4. expand_atom() checks remote_allowed_atoms with the same behaviour as the static allow_list had.

I am not sure I follow the above entirely. Could you add a concrete example to the commit message?

Quoted 6 lines

For instance, if the client wants "%(objectsize) %(objectcolor)" and the server only supports 'size' but not 'color', the filtering in step (2) prevents the client from asking about the color, requesting only the size instead. When the server says the size is 42, step (3) uses that to substitute '%(objectsize)'. Would the end result then be "42 %(objectcolor)"?

You've gotten everything right until the last step, because we have only size from the server there is no data to match %(objectcolor) and the end result is an empty string for %(objeccolor):

Quoted 8 lines
Quoted 4 lines

"42 "

Note that %(objectcolor) doesn't exists and it would have die(), the empty string is only for known but unsupported placeholders.

It was not clear there is a distinction between "unknown" and "known but unsupported". The proposed log message needs to be clarified to make this distinction obvious.

Sorry, I'll make sure to write it clear next reroll.

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines

And if the request is only for "%(objectname)", an empty object_info_options is given to get_remote_info().

Right now 'name' is not part of the protocol as 'type' or 'size' are, 'objectname' is always allowed but only shown if it's present on the format.
If the format is only "%(objectname)" then there's nothing to ask the server for.

Quoted 8 lines
Quoted 3 lines

The current code avoids making the request if there's only objectname or nothing supported, but still goes through the connection work. I will add an early return to just output the oid back without any connection.

I think you are heading in the opposite direction. Rather, when only the object name is requested, I was hoping we would pick something cheap to retrieve and ask the remote side for it, if only to catch a bogus or missing object name.

Looking at send_info() in 'protocol-caps.c', the server only calls odb_read_object_info() inside the if (info->size). A bare OID without any requested attribute gets echoed back with no existence check.

We can force "size" when only %(objectname) is requested so the server validates the OID, and discard the size on the client side.

Because this is a cheap fix, I'll add a NEEDSWORK for the existence check to be done regardless of the attributes requested.

This can be done on a future series focused on the server side.

Quoted 2 lines

Thanks.

Regards,
Pablo

#345 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 5 lines · Pablo Sabater

We can force "size" when only %(objectname) is requested so the server validates the OID, and discard the size on the client side.

Because this is a cheap fix, I'll add a NEEDSWORK for the existence check to be done regardless of the attributes requested.

Hmph, why NEEDSWORK? Not doing so would mean that the result lacks correctness. Why should the first version of this series deliberately produce an incorrect result?

#346 lore raw

Re: [PATCH GSoC v18 13/13] cat-file: make remote-object-info allow-list dynamic

Quoted 11 lines · Junio C Hamano
Quoted 5 lines · Pablo Sabater

We can force "size" when only %(objectname) is requested so the server validates the OID, and discard the size on the client side.

Because this is a cheap fix, I'll add a NEEDSWORK for the existence check to be done regardless of the attributes requested.

Hmph, why NEEDSWORK? Not doing so would mean that the result lacks correctness. Why should the first version of this series deliberately produce an incorrect result?

I was trying to keep the scope of this series to client-side only.

But you are right, I'll add a prep commit in this series that fixes the behavior. no NEEDSWORK.

Thanks,
Pablo

#347 lore raw
In reply to Pablo Sabater
Quoted 46 lines · Pablo Sabater

This patch series is a continuation of Eric Ju's ([email protected]) and Calvin Wan's ([email protected]) patch series [1] and [2] respectively.

Sometimes it is beneficial to retrieve information about an object without having to download it completely. The server logic for retrieving size has already been implemented and merged in a2ba162cda (object-info: support for retrieving object info, 2021-04-20) [3]. This patch series implement the client option for it.

Eric's series adds the remote-object-info command to cat-file --batch-command. This command allows the client to make an object-info command request to a server that supports protocol v2.

If the server uses protocol v2 but does not support the object-info capability, cat-file --batch-command will die.

If a user attempts to use remote-object-info with protocol v1, cat-file --batch-command will die.

Currently, only the size (%(objectsize)) is supported end to end in this implementation. The type (%(objecttype)) is known by the client's allow-list and request path but is not supported on the server side nor the response parsing. A follow up series will add full end-to-end support for %(objecttype).

The default format for remote-object-info is set to "%(objectname) %(objectsize)". Once %(objecttype) is supported, the default format will be unified accordingly.

If the batch command format includes unsupported fields such as %(objecttype), %(objectsize:disk), or %(deltabase), the command will return empty strings for each unsupported field.

This series completes Eric's work mainly with the refactor of the validation of the placeholder with an allow-list that filters what the client asks with what the server is capable of provide following Jeff King's idea [4].

GitHub CI: https://github.com/pabloosabaterr/git/actions/runs/29404390713

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/#t
[3]: https://git.kernel.org/pub/scm/git/git.git/commit/?id=a2ba162cda2acc171c3e36acbbc854792b093cb7
[4]: https://lore.kernel.org/git/[email protected]/

Quoted 8 lines

Changes since v17:

At 10th commit: transport: add client support for object-info
- style
- enforced the server response handling, not allowing bare "<oid>" or responses with a different number of attributes different from the number asked.

Some small comments/nits from my side, mostly looks good otherwise :)

#348 lore raw

v19 cover cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

This patch series is a continuation of Eric Ju's ([email protected]) and Calvin Wan's ([email protected]) patch series [1] and [2] respectively.

Sometimes it is beneficial to retrieve information about an object without having to download it completely. The server logic for retrieving size has already been implemented and merged in a2ba162cda (object-info: support for retrieving object info, 2021-04-20) [3]. This patch series implements the client option for it.

Eric's series adds the remote-object-info command to cat-file --batch-command. This command allows the client to make an object-info command request to a server that supports protocol v2.

If the server uses protocol v2 but does not support the object-info capability, cat-file --batch-command will die.
If a user attempts to use remote-object-info with protocol v1, cat-file --batch-command will die.

Currently, only the size (%(objectsize)) is supported end to end in this implementation. The type (%(objecttype)) is known by the client's allow-list and request path but is not supported on the server side nor the response parsing. A follow up series will add full end-to-end support for %(objecttype).

The default format for remote-object-info is set to "%(objectname) %(objectsize)". Once %(objecttype) is supported, the default format will be unified accordingly.

If the batch command format includes unsupported fields such as %(objecttype), %(objectsize:disk), or %(deltabase), the command will return empty strings for each unsupported field.

This series completes Eric's work mainly with the refactor of the validation of the placeholders with an allow-list that filters what the client asks with what the server is capable of providing, following Jeff King's idea [4].

Github CI: https://github.com/pabloosabaterr/git/actions/runs/29586719871

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/#t
[3]: https://git.kernel.org/pub/scm/git/git.git/commit/?id=a2ba162cda2acc171c3e36acbbc854792b093cb7
[4]: https://lore.kernel.org/git/[email protected]/

Changes in v19:
- Changed the commit structure:
- squashed v18 10th and 11th commits into:
cat-file: add remote-object-info to batch-command
- Moved after the refactor and renamed:
connect: use unsigned int for hash_algo_by_name() calls.
- Added a new commit:
protocol-caps: ...

- protocol-caps: check object existence regardless of the attributes requested; a bare OID request now gets an existence check

Don't lazy-fetch on the server to answer object-info requests (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK)

- gitprotocol-v2: correct the object-info response grammar to match the implementations

- fetch-object-info: validate that each response line echoes the requested object ID at that position

- cat-file: the remote-object-info default format no longer leaks into subsequent info commands in the same session

- commit message and documentation fixes

---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info

Eric Ju (3):
cat-file: declare loop counter inside for()
t1006: extract helper functions into new 'lib-cat-file.sh'
cat-file: add remote-object-info to batch-command

Pablo Sabater (7):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: drop the static advertise_sid variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: use unsigned int for hash_algo_by_name() calls
connect: make write_fetch_command_and_capabilities() more generic
protocol-caps: check object existence regardless of the attributes requested
cat-file: make remote-object-info allow-list adapt to the server

 Documentation/git-cat-file.adoc        |  28 +-
 Documentation/gitprotocol-v2.adoc      |  21 +-
 Makefile                               |   1 +
 builtin/cat-file.c                     | 214 +++++++++-
 connect.c                              |  38 +-
 connect.h                              |   8 +
 fetch-object-info.c                    | 156 +++++++
 fetch-object-info.h                    |  25 ++
 fetch-pack.c                           |  58 +--
 meson.build                            |   1 +
 object-file.c                          |  10 +
 odb.h                                  |   9 +
 protocol-caps.c                        |  45 +-
 serve.c                                |   5 +-
 t/lib-cat-file.sh                      |  16 +
 t/meson.build                          |   1 +
 t/t1006-cat-file.sh                    |  15 +-
 t/t1017-cat-file-remote-object-info.sh | 747 +++++++++++++++++++++++++++++++++
 t/t5701-git-serve.sh                   |  63 +++
 transport-helper.c                     |  12 +-
 transport-internal.h                   |   8 +
 transport.c                            |  44 ++
 transport.h                            |   9 +
 23 files changed, 1437 insertions(+), 97 deletions(-)

---
base-commit: 44de1520f08d1dfebc3ab2d9f644208eaa5ac925

#349 lore raw

PATCH v19 1/13 transport-helper: fix memory leak of helper on disconnect

disconnect_helper() only frees data inside of the if(data->helper) block [1]. When the transport is disconnected without the helper being fully started, data->name allocated in transport_helper_init()
is never freed.

Move FREE_AND_NULL(data->name) outside the conditional block so it's always freed on disconnect.

[1]: https://lore.kernel.org/git/05fbadbae2184479c87c37675dde7bd79b3e32ab.1716465556.git.ps@pks.im/

---
 transport-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
transport-helper.c +1 -1
index 80f90eb7ba..f195070788 100644
@@ -266,9 +266,9 @@ static int disconnect_helper(struct transport *transport)
 		close(data->helper->out);
 		fclose(data->out);
 		res = finish_command(data->helper);
-		FREE_AND_NULL(data->name);
 		FREE_AND_NULL(data->helper);
 	}
+	FREE_AND_NULL(data->name);
 	return res;
 }
 
Signature · 1 line
2.54.0
#350 lore raw

PATCH v19 2/13 cat-file: declare loop counter inside for()

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Declare loop counters in the for statement when they are only used within the loop body, limiting their scope and improving readability.

While updating the loop counters, use size_t instead of int for counters that iterate over object counts.

Update the 'nr' parameter of dispatch_calls() to size_t as all callers already pass a value of that type.

---
 builtin/cat-file.c | 13 ++++---------
 fetch-pack.c       |  3 +--
 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
index b4b99a73da..03afc44c5e 100644
@@ -721,14 +721,12 @@ static void dispatch_calls(struct batch_options *opt,
 		struct strbuf *output,
 		struct expand_data *data,
 		struct queued_cmd *cmd,
-		int nr)
+		size_t nr)
 {
-	int i;
-
 	if (!opt->buffer_output)
 		die(_("flush is only for --buffer mode"));
 
-	for (i = 0; i < nr; i++)
+	for (size_t i = 0; i < nr; i++)
 		cmd[i].fn(opt, cmd[i].line, output, data);
 
 	fflush(stdout);
@@ -736,9 +734,7 @@ static void dispatch_calls(struct batch_options *opt,
 
 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
 {
-	size_t i;
-
-	for (i = 0; i < *nr; i++)
+	for (size_t i = 0; i < *nr; i++)
 		FREE_AND_NULL(cmd[i].line);
 
 	*nr = 0;
@@ -765,7 +761,6 @@ static void batch_objects_command(struct batch_options *opt,
 	size_t alloc = 0, nr = 0;
 
 	while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
-		int i;
 		const struct parse_cmd *cmd = NULL;
 		const char *p = NULL, *cmd_end;
 		struct queued_cmd call = {0};
@@ -775,7 +770,7 @@ static void batch_objects_command(struct batch_options *opt,
 		if (isspace(*input.buf))
 			die(_("whitespace before command: '%s'"), input.buf);
 
-		for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
 			if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
 				continue;
 
fetch-pack.c +1 -2
index 29c41132ee..9eb8fc5399 100644
@@ -1388,9 +1388,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	if (advertise_sid && server_supports_v2("session-id"))
 		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
 	if (server_options && server_options->nr) {
-		int i;
 		ensure_server_supports_v2("server-option");
-		for (i = 0; i < server_options->nr; i++)
+		for (size_t i = 0; i < server_options->nr; i++)
 			packet_buf_write(req_buf, "server-option=%s",
 					 server_options->items[i].string);
 	}
Signature · 1 line
2.54.0
#351 lore raw

PATCH v19 3/13 t1006: extract helper functions into new 'lib-cat-file.sh'

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Extract utility functions from the cat-file's test script 't1006-cat-file.sh' into a new 'lib-cat-file.sh' dedicated library file.

A subsequent commit will need these functions. This improves the code reuse and readability, enabling future cat-file tests to share these helpers without duplicating code.

While at it update the style of this line to follow coding guidelines:

. "$TEST_DIRECTORY/lib-loose.sh"

to

. "$TEST_DIRECTORY"/lib-loose.sh

---
 t/lib-cat-file.sh   | 16 ++++++++++++++++
 t/t1006-cat-file.sh | 15 ++-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
t/lib-cat-file.sh new file +16 -0
new file mode 100644
index 0000000000..7c2e877016
@@ -0,0 +1,16 @@
+# Library of git-cat-file related test functions.
+
+# Print a string without a trailing newline.
+echo_without_newline () {
+	printf '%s' "$*"
+}
+
+# Print a string without newlines and replace them with a NUL character (\0).
+echo_without_newline_nul () {
+	echo_without_newline "$@" | tr '\n' '\0'
+}
+
+# Calculate the length of a string.
+strlen () {
+	echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
t/t1006-cat-file.sh +2 -13
index 8e2c52652c..cf65bfc88f 100755
@@ -3,7 +3,8 @@
 test_description='git cat-file'
 
 . ./test-lib.sh
-. "$TEST_DIRECTORY/lib-loose.sh"
+. "$TEST_DIRECTORY"/lib-loose.sh
+. "$TEST_DIRECTORY"/lib-cat-file.sh
 
 test_cmdmode_usage () {
 	test_expect_code 129 "$@" 2>err &&
@@ -99,18 +100,6 @@ do
 	'
 done
 
-echo_without_newline () {
-    printf '%s' "$*"
-}
-
-echo_without_newline_nul () {
-	echo_without_newline "$@" | tr '\n' '\0'
-}
-
-strlen () {
-    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
-}
-
 run_tests () {
     type=$1
     object_name="$2"
Signature · 1 line
2.54.0
#352 lore raw

PATCH v19 4/13 fetch-pack: drop the static advertise_sid variable

In reply to Pablo Sabater

write_fetch_command_and_capabilities() is moved to 'connect.c' in a subsequent commit. To prepare for that, drop the static variable usage of advertise_sid.

Currently advertise_sid is set in fetch_pack_config() by reading "transfer.advertisesid". It is used in three places:

1. In do_fetch_pack(), to clear it when the server lacks support:

        if (!server_supports("session-id"))
               advertise_sid = 0;

2. In find_common(), to advertise the session id over protocol v0/v1:

        if (advertise_sid)
                strbuf_addf(&c, " session-id=%s", trace2_session_id());

3. In write_fetch_command_and_capabilities(), to advertise it over protocol v2:

        if (advertise_sid && server_supports_v2("session-id"))
                packet_buf_write(req_buf, "session-id=%s", trace2_session_id());

About 1, the check only guards the v0/v1 path, and the v2 path already checks server support inline in its condition. Follow the same pattern and fold the check into the condition in find_common().

About 2 and 3, replace the static variable with a local read via repo_config_get_bool() in each function.

Because repo_config_get_bool() leaves advertise_sid as is if it is not set, initialize it to 0, matching its default.

---
 fetch-pack.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fetch-pack.c +7 -6
index 9eb8fc5399..65ebfec09f 100644
@@ -49,7 +49,6 @@ static int fetch_fsck_objects = -1;
 static int transfer_fsck_objects = -1;
 static int agent_supported;
 static int server_supports_filtering;
-static int advertise_sid;
 static struct shallow_lock shallow_lock;
 static const char *alternate_shallow_file;
 static struct strbuf fsck_msg_types = STRBUF_INIT;
@@ -363,6 +362,9 @@ static int find_common(struct fetch_negotiator *negotiator,
 	size_t state_len = 0;
 	struct packet_reader reader;
 	struct oidset negotiation_include_oids = OIDSET_INIT;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	if (args->stateless_rpc && multi_ack == 1)
 		die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
@@ -414,7 +416,7 @@ static int find_common(struct fetch_negotiator *negotiator,
 			if (deepen_not_ok)      strbuf_addstr(&c, " deepen-not");
 			if (agent_supported)    strbuf_addf(&c, " agent=%s",
 							    git_user_agent_sanitized());
-			if (advertise_sid)
+			if (advertise_sid && server_supports("session-id"))
 				strbuf_addf(&c, " session-id=%s", trace2_session_id());
 			if (args->filter_options.choice)
 				strbuf_addstr(&c, " filter");
@@ -1160,9 +1162,6 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 				      (int)agent_len, agent_feature);
 	}
 
-	if (!server_supports("session-id"))
-		advertise_sid = 0;
-
 	if (server_supports("shallow"))
 		print_verbose(args, _("Server supports %s"), "shallow");
 	else if (args->depth > 0 || is_repository_shallow(r))
@@ -1380,6 +1379,9 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 						 const struct string_list *server_options)
 {
 	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	ensure_server_supports_v2("fetch");
 	packet_buf_write(req_buf, "command=fetch");
@@ -1998,7 +2000,6 @@ static void fetch_pack_config(void)
 	repo_config_get_bool(the_repository, "repack.usedeltabaseoffset", &prefer_ofs_delta);
 	repo_config_get_bool(the_repository, "fetch.fsckobjects", &fetch_fsck_objects);
 	repo_config_get_bool(the_repository, "transfer.fsckobjects", &transfer_fsck_objects);
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 	if (!uri_protocols.nr) {
 		char *str;
 
Signature · 1 line
2.54.0
#353 lore raw

PATCH v19 5/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c

In reply to Pablo Sabater

In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future.

Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions.

Because string_list is only used as a pointer, use a forward declaration [1].

[1]: https://lore.kernel.org/git/[email protected]/

---
 connect.c    | 34 ++++++++++++++++++++++++++++++++++
 connect.h    |  4 ++++
 fetch-pack.c | 34 ----------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
connect.c +34 -0
index 47e39d2a73..31e5ab958b 100644
@@ -700,6 +700,40 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options)
+{
+	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
+
+	ensure_server_supports_v2("fetch");
+	packet_buf_write(req_buf, "command=fetch");
+	if (server_supports_v2("agent"))
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
+	if (advertise_sid && server_supports_v2("session-id"))
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+	if (server_options && server_options->nr) {
+		ensure_server_supports_v2("server-option");
+		for (size_t i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
+	}
+
+	if (server_feature_v2("object-format", &hash_name)) {
+		int hash_algo = hash_algo_by_name(hash_name);
+		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
+			die(_("mismatched algorithms: client %s; server %s"),
+			    the_hash_algo->name, hash_name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
+	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
+		die(_("the server does not support algorithm '%s'"),
+		    the_hash_algo->name);
+	}
+	packet_buf_delim(req_buf);
+}
+
 static const char *url_scheme_name(enum url_scheme scheme)
 {
 	switch (scheme) {
connect.h +4 -0
index aa482a37fb..c4f6ea4b0a 100644
@@ -34,4 +34,8 @@ void check_stateless_delimiter(int stateless_rpc,
 			       struct packet_reader *reader,
 			       const char *error);
 
+struct string_list;
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options);
+
 #endif
fetch-pack.c +0 -34
index 65ebfec09f..f7789e8456 100644
@@ -1375,40 +1375,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-						 const struct string_list *server_options)
-{
-	const char *hash_name;
-	int advertise_sid = 0;
-
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
-
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
-	if (server_supports_v2("agent"))
-		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
-	if (advertise_sid && server_supports_v2("session-id"))
-		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
-	if (server_options && server_options->nr) {
-		ensure_server_supports_v2("server-option");
-		for (size_t i = 0; i < server_options->nr; i++)
-			packet_buf_write(req_buf, "server-option=%s",
-					 server_options->items[i].string);
-	}
-
-	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
-		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
-			die(_("mismatched algorithms: client %s; server %s"),
-			    the_hash_algo->name, hash_name);
-		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
-	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
-		die(_("the server does not support algorithm '%s'"),
-		    the_hash_algo->name);
-	}
-	packet_buf_delim(req_buf);
-}
-
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      struct fetch_pack_args *args,
 			      const struct ref *wants, struct oidset *common,
Signature · 1 line
2.54.0
#354 lore raw

Re: [PATCH GSoC v19 05/13] fetch-pack: move write_fetch_command_and_capabilities() to connect.c

Quoted 45 lines · Pablo Sabater

In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future.

Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions.

Because string_list is only used as a pointer, use a forward declaration [1].

[1]: https://lore.kernel.org/git/[email protected]/

---
 connect.c    | 34 ++++++++++++++++++++++++++++++++++
 connect.h    |  4 ++++
 fetch-pack.c | 34 ----------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
connect.c +14 -0
index 47e39d2a73..31e5ab958b 100644
@@ -700,6 +700,40 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options)
+{
+	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
+
+	ensure_server_supports_v2("fetch");
+	packet_buf_write(req_buf, "command=fetch");
+	if (server_supports_v2("agent"))
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
+	if (advertise_sid && server_supports_v2("session-id"))
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());

connect.c int server_supports(const char *feature) · line 726

Quoted 10 lines
connect.c +10 -0
+	if (server_options && server_options->nr) {
+		ensure_server_supports_v2("server-option");
+		for (size_t i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
+	}
+
+	if (server_feature_v2("object-format", &hash_name)) {
+		int hash_algo = hash_algo_by_name(hash_name);
+		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)

hash_algo_by_ptr() returns an unsigned integer, and comparing that to a signed integer results in:

connect.c: In function ‘write_fetch_command_and_capabilities’:
connect.c:726:53: error: comparison of integer expressions of different signedness: ‘uint32_t’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]

    726 |                 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
        |                                                     ^~

cc1: all warnings being treated as errors
make: *** [Makefile:2924: connect.o] Error 1

This was not an issue while the function lived in "fetch-pack.c", because that file uses #define DISABLE_SIGN_COMPARE_WARNINGS, but "connect.c" doesn't.

I think as a preparatory step the type of the 'hash_algo' variable should be changed to unsigned int while the function is still in "fetch-pack.c" to avoid this; you'll convert it in the next patch anyway.

Quoted 74 lines
connect.c +10 -0
+			die(_("mismatched algorithms: client %s; server %s"),
+			    the_hash_algo->name, hash_name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
+	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
+		die(_("the server does not support algorithm '%s'"),
+		    the_hash_algo->name);
+	}
+	packet_buf_delim(req_buf);
+}
+
 static const char *url_scheme_name(enum url_scheme scheme)
 {
 	switch (scheme) {
connect.h +4 -0
index aa482a37fb..c4f6ea4b0a 100644
@@ -34,4 +34,8 @@ void check_stateless_delimiter(int stateless_rpc,
 			       struct packet_reader *reader,
 			       const char *error);
 
+struct string_list;
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options);
+
 #endif
fetch-pack.c +0 -34
index 65ebfec09f..f7789e8456 100644
@@ -1375,40 +1375,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-						 const struct string_list *server_options)
-{
-	const char *hash_name;
-	int advertise_sid = 0;
-
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
-
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
-	if (server_supports_v2("agent"))
-		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
-	if (advertise_sid && server_supports_v2("session-id"))
-		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
-	if (server_options && server_options->nr) {
-		ensure_server_supports_v2("server-option");
-		for (size_t i = 0; i < server_options->nr; i++)
-			packet_buf_write(req_buf, "server-option=%s",
-					 server_options->items[i].string);
-	}
-
-	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
-		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
-			die(_("mismatched algorithms: client %s; server %s"),
-			    the_hash_algo->name, hash_name);
-		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
-	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
-		die(_("the server does not support algorithm '%s'"),
-		    the_hash_algo->name);
-	}
-	packet_buf_delim(req_buf);
-}
-
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      struct fetch_pack_args *args,
 			      const struct ref *wants, struct oidset *common,
Signature · 2 lines
-- 
2.54.0
#355 lore raw

Re: [PATCH GSoC v19 05/13] fetch-pack: move write_fetch_command_and_capabilities() to connect.c

Quoted 67 lines · SZEDER Gábor
Quoted 45 lines · Pablo Sabater

In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future.

Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions.

Because string_list is only used as a pointer, use a forward declaration [1].

[1]: https://lore.kernel.org/git/[email protected]/

---
 connect.c    | 34 ++++++++++++++++++++++++++++++++++
 connect.h    |  4 ++++
 fetch-pack.c | 34 ----------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
connect.c +14 -0
index 47e39d2a73..31e5ab958b 100644
@@ -700,6 +700,40 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options)
+{
+	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
+
+	ensure_server_supports_v2("fetch");
+	packet_buf_write(req_buf, "command=fetch");
+	if (server_supports_v2("agent"))
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
+	if (advertise_sid && server_supports_v2("session-id"))
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());

connect.c int server_supports(const char *feature) · line 726

Quoted 10 lines
connect.c +10 -0
+	if (server_options && server_options->nr) {
+		ensure_server_supports_v2("server-option");
+		for (size_t i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
+	}
+
+	if (server_feature_v2("object-format", &hash_name)) {
+		int hash_algo = hash_algo_by_name(hash_name);
+		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)

hash_algo_by_ptr() returns an unsigned integer, and comparing that to a signed integer results in:

connect.c: In function ‘write_fetch_command_and_capabilities’:
connect.c:726:53: error: comparison of integer expressions of different signedness: ‘uint32_t’ {aka ‘unsigned int’} and ‘int’ [-Werror=sign-compare]

    726 |                 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
        |                                                     ^~

cc1: all warnings being treated as errors
make: *** [Makefile:2924: connect.o] Error 1

Quoted 8 lines

This was not an issue while the function lived in "fetch-pack.c", because that file uses #define DISABLE_SIGN_COMPARE_WARNINGS, but "connect.c" doesn't.

I think as a preparatory step the type of the 'hash_algo' variable should be changed to unsigned int while the function is still in "fetch-pack.c" to avoid this; you'll convert it in the next patch anyway.

Thanks for noticing it.

Yes, then the prep patch needs to be done in fetch-pack.c before moving it to connect.c.

I had it after because 2 hunks of that patch fixes calls for connect.c so this way there were all for connect.c instead of mixing.

[snip]

Thanks,
Pablo

#356 lore raw

PATCH v19 6/13 connect: use unsigned int for hash_algo_by_name() calls

In reply to Pablo Sabater

hash_algo_by_name() returns "unsigned int", but multiple variables that store its return are assigned as "int".

Change hash_algo_by_name() variables type to match its return type, also make it const because they are never modified.

---
 connect.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
connect.c +3 -3
index 31e5ab958b..9d236e7bba 100644
@@ -248,7 +248,7 @@ static void process_capabilities(struct packet_reader *reader, size_t *linelen)
 	feat_val = server_feature_value("object-format", &feat_len);
 	if (feat_val) {
 		char *hash_name = xstrndup(feat_val, feat_len);
-		int hash_algo = hash_algo_by_name(hash_name);
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
 		if (hash_algo != GIT_HASH_UNKNOWN)
 			reader->hash_algo = &hash_algos[hash_algo];
 		free(hash_name);
@@ -496,7 +496,7 @@ static void send_capabilities(int fd_out, struct packet_reader *reader)
 		packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized());
 
 	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
 		if (hash_algo == GIT_HASH_UNKNOWN)
 			die(_("unknown object format '%s' specified by server"), hash_name);
 		reader->hash_algo = &hash_algos[hash_algo];
@@ -722,7 +722,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	}
 
 	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
Signature · 1 line
2.54.0
#357 lore raw

PATCH v19 7/13 connect: make write_fetch_command_and_capabilities() more generic

In reply to Pablo Sabater

Refactor write_fetch_command_and_capabilities(), enabling it to serve both fetch and additional commands.

In this context, "command" refers to the "operations" supported by Git's wire protocol Documentation/gitprotocol-v2.adoc, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162cda (object-info: support for retrieving object info, 2021-04-20).

Refactor the function signature to accept a command instead of the hardcoded "fetch".

---
 connect.c    | 8 ++++----
 connect.h    | 8 ++++++--
 fetch-pack.c | 4 ++--
 3 files changed, 12 insertions(+), 8 deletions(-)
connect.c +4 -4
index 9d236e7bba..9d54d40770 100644
@@ -700,16 +700,16 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options)
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options)
 {
 	const char *hash_name;
 	int advertise_sid = 0;
 
 	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
+	ensure_server_supports_v2(command);
+	packet_buf_write(req_buf, "command=%s", command);
 	if (server_supports_v2("agent"))
 		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
 	if (advertise_sid && server_supports_v2("session-id"))
connect.h +6 -2
index c4f6ea4b0a..957e5fe2b9 100644
@@ -35,7 +35,11 @@ void check_stateless_delimiter(int stateless_rpc,
 			       const char *error);
 
 struct string_list;
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options);
+/*
+ * Write a protocol v2 command request, along with the capability
+ * advertisements, into req_buf.
+ */
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options);
 
 #endif
fetch-pack.c +2 -2
index f7789e8456..3695059cd5 100644
@@ -1386,7 +1386,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 	int done_sent = 0;
 	struct strbuf req_buf = STRBUF_INIT;
 
-	write_fetch_command_and_capabilities(&req_buf, args->server_options);
+	write_command_and_capabilities(&req_buf, "fetch", args->server_options);
 
 	if (args->use_thin_pack)
 		packet_buf_write(&req_buf, "thin-pack");
@@ -2253,7 +2253,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
 					   the_repository, "%d",
 					   negotiation_round);
 		strbuf_reset(&req_buf);
-		write_fetch_command_and_capabilities(&req_buf, server_options);
+		write_command_and_capabilities(&req_buf, "fetch", server_options);
 
 		packet_buf_write(&req_buf, "wait-for-done");
 
Signature · 1 line
2.54.0
#358 lore raw

PATCH v19 8/13 fetch-pack: move fetch initialization

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

There are some variables initialized at the start of the do_fetch_pack_v2() state machine. Currently, they are initialized in FETCH_CHECK_LOCAL, which is the initial state set at the beginning of the function.

However, a subsequent patch will allow for another initial state, while still requiring these initialized variables.
Move the initialization to be before the state machine, so that they are set regardless of the initial state.

Note that there is no change in behavior, because we're moving code from the beginning of the first state to just before the execution of the state machine.

---
 fetch-pack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fetch-pack.c +6 -6
index 3695059cd5..922a9b2581 100644
@@ -1735,18 +1735,18 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 		reader.me = "fetch-pack";
 	}
 
+	/* v2 supports these by default */
+	allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
+	use_sideband = 2;
+	if (args->depth > 0 || args->deepen_since || args->deepen_not)
+		args->deepen = 1;
+
 	while (state != FETCH_DONE) {
 		switch (state) {
 		case FETCH_CHECK_LOCAL:
 			sort_ref_list(&ref, ref_compare_name);
 			QSORT(sought, nr_sought, cmp_ref_by_name);
 
-			/* v2 supports these by default */
-			allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
-			use_sideband = 2;
-			if (args->depth > 0 || args->deepen_since || args->deepen_not)
-				args->deepen = 1;
-
 			/* Filter 'ref' by 'sought' and those that aren't local */
 			mark_complete_and_common_ref(negotiator, args, &ref);
 			filter_refs(args, &ref, sought, nr_sought);
Signature · 1 line
2.54.0
#359 lore raw

PATCH v19 9/13 protocol-caps: check object existence regardless of the attributes requested

In reply to Pablo Sabater

Currently, send_info() only checks for existence when the attribute 'size' is also requested. Requesting a bare OID, without attributes only echoes back the OID.

Extract the existence check to be done regardless of the number of attributes requested.

While at it, introduce a wrapper called get_object_info() similar to odb_read_object_info() that returns OBJ_BAD on fail and adds OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK flags. OBJECT_INFO_SKIP_FETCH_OBJECT is so a server with a partial clone doesn't trigger fetching objects when it gets an object-info request with an OID that is not available locally. A server should only report what it has locally.

Tighten the condition used to determine whether an object is recognized. get_object_info() returns OBJ_BAD for unknown objects, but OBJ_NONE (0) can also mean "not found". Change the check from '< 0' to '<= OBJ_NONE' to cover both as unrecognized.

With this patch, a bare OID has two possible responses:

1. Recognized OID: the server answers with "<OID>"

2. Unrecognized OID: the server answers with "<OID> SP"

Update the object-info section in 'gitprotocol-v2.adoc':
- Require full obj-oid explicitly.
- Fix parentheses.
- Define obj-size explicitly.
- Make obj-size optional in obj-info and document the behavior for unrecognized object IDs.
- Describe the attr header as zero or more pkt-lines, one per attribute, matching what the server implements. A request with no attributes gets no header.

---
 Documentation/gitprotocol-v2.adoc | 21 ++++++++-----
 protocol-caps.c                   | 45 ++++++++++++++++++++++++----
 t/t5701-git-serve.sh              | 63 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 14 deletions(-)
Documentation/gitprotocol-v2.adoc +13 -8
index 2beb70595f..7bf62014c3 100644
@@ -568,21 +568,26 @@ An `object-info` request takes the following arguments:
 
 	oid <oid>
 	Indicates to the server an object which the client wants to obtain
-	information for.
+	information for. They must be full OIDs.
 
-The response of `object-info` is a list of the requested object ids
-and associated requested information, each separated by a single space.
+The response of `object-info` consists of one pkt-line per requested attribute,
+echoing the attributes the server will report, followed by one pkt-line per
+requested object id with its information, each field separated by a single
+space.
 
 	output = info flush-pkt
 
-	info = PKT-LINE(attrs) LF)
-		*PKT-LINE(obj-info LF)
-
-	attrs = attr | attrs SP attrs
+	info = *PKT-LINE(attr LF)
+	       *PKT-LINE(obj-info LF)
 
 	attr = "size"
 
-	obj-info = obj-id SP obj-size
+	obj-size = 1*DIGIT
+
+	obj-info = obj-id [SP [obj-size]]
+
+If the server does not recognize the OID, the response will be `<oid> SP`
+regardless of the number of attributes requested.
 
 bundle-uri
 ~~~~~~~~~~
protocol-caps.c +39 -6

60 lines not on this page · raw message

t/t5701-git-serve.sh +63 -0

75 lines not on this page · raw message

Signature · 1 line
2.54.0
#360 lore raw

PATCH v19 10/13 serve: advertise object-info feature

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

In order for a client to know what object-info components a server can provide, advertise supported object-info features. This allows a client to decide whether to query the server for object-info or fetch as a fallback.

---
 serve.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
serve.c +4 -1
index 49a6e39b1d..2b07d922b3 100644
@@ -89,7 +89,7 @@ static void session_id_receive(struct repository *r UNUSED,
 	trace2_data_string("transfer", NULL, "client-sid", client_sid);
 }
 
-static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
+static int object_info_advertise(struct repository *r, struct strbuf *value)
 {
 	if (advertise_object_info == -1 &&
 	    repo_config_get_bool(r, "transfer.advertiseobjectinfo",
@@ -97,6 +97,9 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS
 		/* disabled by default */
 		advertise_object_info = 0;
 	}
+	/* Currently only size is supported */
+	if (value && advertise_object_info)
+		strbuf_addstr(value, "size");
 	return advertise_object_info;
 }
 
Signature · 1 line
2.54.0
#361 lore raw

PATCH v19 11/13 transport: add client support for object-info

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

Sometimes, it is beneficial to retrieve information about an object without downloading it entirely. The server-side logic for this functionality was implemented in commit "a2ba162cda (object-info:
support for retrieving object info, 2021-04-20)." And the wire format is documented at
https://git-scm.com/docs/protocol-v2#_object_info.

Introduce client-side support for the object-info capability.

Add its own function for object-info separate from existing fetch infrastructure.

Currently, the client supports requesting a list of OIDs with the size attribute from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client returns an error and exits.

Note that:

1. The entire request is written into req_buf before being sent to the remote. This approach follows the pattern used in the send_fetch_request() logic within 'fetch-pack.c'. Streaming the request is not addressed in this patch.

2. A new field 'unrecognized' has been added to object_info. This new field is set at fetch_object_info() when the object is unrecognized by the server.

---
 Makefile             |   1 +
 fetch-object-info.c  | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fetch-object-info.h  |  22 ++++++++
 meson.build          |   1 +
 odb.h                |   6 +++
 transport-helper.c   |  10 ++++
 transport-internal.h |   8 +++
 transport.c          |  45 +++++++++++++++++
 transport.h          |   9 ++++
 9 files changed, 240 insertions(+)
Makefile +1 -0
index 1f3f099f5c..d450e0277e 100644
@@ -1158,6 +1158,7 @@ LIB_OBJS += ewah/ewah_io.o
 LIB_OBJS += ewah/ewah_rlw.o
 LIB_OBJS += exec-cmd.o
 LIB_OBJS += fetch-negotiator.o
+LIB_OBJS += fetch-object-info.o
 LIB_OBJS += fetch-pack.o
 LIB_OBJS += fmt-merge-msg.o
 LIB_OBJS += fsck.o
fetch-object-info.c new file +138 -0

138 lines not on this page · raw message

fetch-object-info.h new file +22 -0
new file mode 100644
index 0000000000..31aad98408
@@ -0,0 +1,22 @@
+#ifndef FETCH_OBJECT_INFO_H
+#define FETCH_OBJECT_INFO_H
+
+#include "pkt-line.h"
+#include "protocol.h"
+
+struct object_info_args {
+	struct string_list *object_info_options;
+	const struct string_list *server_options;
+	struct oid_array *oids;
+};
+
+struct object_info;
+/*
+ * Sends git-cat-file object-info command into the request buf and read the
+ * results from packets.
+ */
+int fetch_object_info(enum protocol_version version, struct object_info_args *args,
+		      struct packet_reader *reader, struct object_info *object_info_data,
+		      int stateless_rpc, int fd_out);
+
+#endif /* FETCH_OBJECT_INFO_H */
meson.build +1 -0
index 9434b56960..dfefcd3475 100644
@@ -359,6 +359,7 @@ libgit_sources = [
   'ewah/ewah_rlw.c',
   'exec-cmd.c',
   'fetch-negotiator.c',
+  'fetch-object-info.c',
   'fetch-pack.c',
   'fmt-merge-msg.c',
   'fsck.c',
odb.h +6 -0
index 94754643d2..88a37febbf 100644
@@ -339,6 +339,12 @@ struct object_info {
 	 * or multiple times in the same source.
 	 */
 	struct odb_source_info *source_infop;
+
+	/*
+	 * object-info protocol specific. Set by the protocol when the remote
+	 * does not recognize the requested object.
+	 */
+	unsigned int unrecognized:1;
 };
 
 /*
transport-helper.c +10 -0
index f195070788..623463dcea 100644
@@ -784,6 +784,15 @@ static int fetch_refs(struct transport *transport,
 	return -1;
 }
 
+static int fetch_object_info_helper(struct transport *transport)
+{
+	get_helper(transport);
+	if (process_connect(transport, 0))
+		return transport->vtable->fetch_object_info(transport);
+
+	die(_("object-info requires protocol v2"));
+}
+
 struct push_update_ref_state {
 	struct ref *hint;
 	struct ref_push_report *report;
@@ -1330,6 +1339,7 @@ static struct transport_vtable vtable = {
 	.get_refs_list	= get_refs_list,
 	.get_bundle_uri = get_bundle_uri,
 	.fetch_refs	= fetch_refs,
+	.fetch_object_info = fetch_object_info_helper,
 	.push_refs	= push_refs,
 	.connect	= connect_helper,
 	.disconnect	= release_helper
transport-internal.h +8 -0
index 051f3ab0dc..60db0bedcd 100644
@@ -45,6 +45,14 @@ struct transport_vtable {
 	 **/
 	int (*fetch_refs)(struct transport *transport, int refs_nr, struct ref **refs);
 
+	/*
+	 * Fetch object info (only size currently) from remote without
+	 * downloading the objects.
+	 *
+	 * Uses object-info capability of v2 protocol.
+	 */
+	int (*fetch_object_info)(struct transport *transport);
+
 	/**
 	 * Push the objects and refs. Send the necessary objects, and
 	 * then, for any refs where peer_ref is set and
transport.c +45 -0

69 lines not on this page · raw message

transport.h +9 -0
index 7e5867cffa..a7869d18e0 100644
@@ -55,6 +55,10 @@ struct git_transport_options {
 	 * common commits to this oidset instead of fetching any packfiles.
 	 */
 	struct oidset *acked_commits;
+
+	struct oid_array *object_info_oids;
+	struct object_info *object_info_data;
+	struct string_list *object_info_options;
 };
 
 enum transport_family {
@@ -309,6 +313,11 @@ int transport_get_remote_bundle_uri(struct transport *transport);
 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport);
 int transport_fetch_refs(struct transport *transport, struct ref *refs);
 
+/*
+ * Fetch the object info from remote
+ */
+int transport_fetch_object_info(struct transport *transport);
+
 /*
  * If this flag is set, unlocking will avoid to call non-async-signal-safe
  * functions. This will necessarily leave behind some data structures which
Signature · 1 line
2.54.0
#362 lore raw

PATCH v19 12/13 cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Since the info command in cat-file --batch-command prints object info for a given object, it is natural to add another command in cat-file --batch-command to print object info for a given object from a remote.

Add remote-object-info command to cat-file --batch-command.

While info takes object ids one at a time, this creates overhead when making requests to a server. So remote-object-info instead can take multiple object ids at once.

The cat-file --batch-command command is generally implemented in the following manner:

- Receive and parse input from user
- Call respective function attached to command
- Get object info, print object info

In --buffer mode, this changes to:

- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue
- Call respective function attached to command
- Get object info, print object info

Notice how the getting and printing of object info is accomplished one at a time. As described above, this creates a problem for making requests to a server. Therefore, remote-object-info is implemented in the following manner:

- Receive and parse input from user
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Parse input, get object info, print object info

And finally for --buffer mode remote-object-info:
- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue:
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Get object info, print object info

To summarize, remote-object-info gets object info from the remote and then loops through the object info passed in, printing the info.

In order for remote-object-info to avoid remote communication overhead in the non-buffer mode, the objects are passed in as such:

remote-object-info <remote> <oid> <oid> ... <oid>

rather than

remote-object-info <remote> <oid>
remote-object-info <remote> <oid>
...
remote-object-info <remote> <oid>

Placeholders in the format are validated against an allow-list of the atoms the remote path supports: "objectname" and "objectsize". Unsupported atoms expand to an empty string, honoring how for-each-ref handles known but inapplicable atoms.
Without this, atoms like %(objecttype) would mark data->info.typep and because the server only sends size, type_name() would later crash. As extra safety, even outside of the remote path, initialize expand_data's type to OBJ_BAD and handle type_name() returning NULL.

---
 Documentation/git-cat-file.adoc        |  28 +-
 builtin/cat-file.c                     | 179 +++++++-
 object-file.c                          |  10 +
 odb.h                                  |   3 +
 t/meson.build                          |   1 +
 t/t1017-cat-file-remote-object-info.sh | 719 +++++++++++++++++++++++++++++++++
 6 files changed, 929 insertions(+), 11 deletions(-)
Documentation/git-cat-file.adoc +22 -6
index 86b9181599..7522e24874 100644
@@ -169,6 +169,13 @@ info <object>::
 	Print object info for object reference `<object>`. This corresponds to the
 	output of `--batch-check`.
 
+remote-object-info <remote> <object>...::
+	Print object info for object references `<object>` at specified
+	`<remote>` without downloading objects from the remote.
+	Raise an error when the `object-info` capability is not supported by the remote.
+	Raise an error when no object references are provided.
+	This command may be combined with `--buffer`.
+
 flush::
 	Used with `--buffer` to execute all preceding commands that were issued
 	since the beginning or since the last flush was issued. When `--buffer`
@@ -301,7 +308,8 @@ one per line, and print information based on the command given. With
 `--batch-command`, the `info` command followed by an object will print
 information about the object the same way `--batch-check` would, and the
 `contents` command followed by an object prints contents in the same way
-`--batch` would.
+`--batch` would. The `remote-object-info` command followed by a remote and
+objects IDs prints object info from the remote without downloading the objects.
 
 You can specify the information shown for each object by using a custom
 `<format>`. The `<format>` is copied literally to stdout for each
@@ -324,14 +332,12 @@ newline. The available atoms are:
 	reports).
 
 `objectsize:disk`::
-	The size, in bytes, that the object takes up on disk. See the
-	note about on-disk sizes in the `CAVEATS` section below.
+	The size, in bytes, that the object takes up on disk.
 
 `deltabase`::
 	If the object is stored as a delta on-disk, this expands to the
 	full hex representation of the delta base object name.
-	Otherwise, expands to the null OID (all zeroes). See `CAVEATS`
-	below.
+	Otherwise, expands to the null OID (all zeroes).
 
 `rest`::
 	If this atom is used in the output string, input lines are split
@@ -340,8 +346,14 @@ newline. The available atoms are:
 	after that first run of whitespace (i.e., the "rest" of the
 	line) are output in place of the `%(rest)` atom.
 
+The command `remote-object-info` only supports the `%(objectname)` and
+`%(objectsize)` placeholders. See `CAVEATS` below for more information.
+
 If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize)`, except for `remote-object-info` commands which
+use `%(objectname) %(objectsize)` because "%(objecttype)" is not supported yet.
+WARNING: When "%(objecttype)" is supported, the default format WILL be unified,
+so DO NOT RELY on the current default format to stay the same!!!
 
 If `--batch` is specified, or if `--batch-command` is used with the `contents`
 command, the object information is followed by the object contents (consisting
@@ -438,6 +450,10 @@ scripting purposes.
 CAVEATS
 -------
 
+Note that only `%(objectname)` and `%(objectsize)` are currently
+supported by the `remote-object-info` command. Using any other placeholder in
+the format string will return an empty string in its position.
+
 Note that the sizes of objects on disk are reported accurately, but care
 should be taken in drawing conclusions about which refs or objects are
 responsible for disk usage. The size of a packed non-delta object may be
builtin/cat-file.c +174 -5

223 lines not on this page · raw message

object-file.c +10 -0
index 6453b1d6fa..07f019a0f6 100644
@@ -1694,3 +1694,13 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
 
 	return &transaction->base;
 }
+
+void free_object_info_contents(struct object_info *object_info)
+{
+	if (!object_info)
+		return;
+	free(object_info->typep);
+	free(object_info->sizep);
+	free(object_info->disk_sizep);
+	free(object_info->delta_base_oid);
+}
odb.h +3 -0
index 88a37febbf..92fa414e2c 100644
@@ -623,4 +623,7 @@ void parse_alternates(const char *string,
 		      const char *relative_base,
 		      struct strvec *out);
 
+/* Free pointers inside of object_info, but not object_info itself */
+void free_object_info_contents(struct object_info *object_info);
+
 #endif /* ODB_H */
t/meson.build +1 -0
index 8ae6ab6c5f..10241e3dcc 100644
@@ -171,6 +171,7 @@ integration_tests = [
   't1014-read-tree-confusing.sh',
   't1015-read-index-unmerged.sh',
   't1016-compatObjectFormat.sh',
+  't1017-cat-file-remote-object-info.sh',
   't1020-subdirectory.sh',
   't1022-read-tree-partial-clone.sh',
   't1050-large.sh',
t/t1017-cat-file-remote-object-info.sh new file +719 -0

719 lines not on this page · raw message

Signature · 1 line
2.54.0
#363 lore raw

PATCH v19 13/13 cat-file: make remote-object-info allow-list adapt to the server

In reply to Pablo Sabater

The static allow-list in expand_atom() is hardcoded to allow only "objectname" and "objectsize" for remote queries. This works because, up to this point, servers will either support object-info with name and size or they do not support them at all.

As object-info gains new capabilities, we cannot expect different servers with different Git versions to have the same object-info capabilities. Therefore, the client needs to adapt its allow-list to what the server advertises.

The client now:

1. Requests the protocol option that the placeholder refers to (i.e. "size" for "%(objectsize)").

2. Drops any requested option that the server does not advertise in fetch_object_info().

3. Maps the remaining advertised options back to their placeholders and populates remote_allowed_atoms.

4. Uses remote_allowed_atoms in expand_atom(), preserving the previous behavior for supported placeholders.

For example, if the client requests "%(objectsize) %(objecttype)" and the server only supports 'size', then the client only requests 'size'. The server returns the size (i.e "42") "%(objectsize)" is expanded normally while "%(objecttype)" expands to an empty string:

	"42 "

Note that the empty string expansion is only for known but unsupported placeholders. "%(objectcolor)" which doesn't exist would die().

This honors what for-each-ref does for known but inapplicable atoms (placeholders).

Move object_info_options out of get_remote_info() so the caller which has data can select what options will be requested instead of requesting always size.

Move batch_object_write() out so output is always produced. If there are no supported attributes, the output is a blank line.

Include "type" in the object_info_options even though the client does not yet know how to parse the server's "type" capability.

As a result, "type" is always filtered out, allowing the tests to verify that known but unsupported placeholders expand to an empty string.

Since the filter removes options by swapping with the last element, the list is no longer kept sorted. Drop the pre-sort in fetch_object_info_via_pack() and use the unsorted string_list lookup for the response header. This has no effect in performance as the list can only be two entries long ('size' and 'type').

---
 builtin/cat-file.c                     | 86 +++++++++++++++++++++-------------
 fetch-object-info.c                    | 20 +++++++-
 fetch-object-info.h                    |  3 ++
 t/t1017-cat-file-remote-object-info.sh | 28 +++++++++++
 transport.c                            |  1 -
 5 files changed, 104 insertions(+), 34 deletions(-)
builtin/cat-file.c +54 -32
index f5e5528a21..7a0431bf2b 100644
@@ -338,13 +338,11 @@ struct expand_data {
 	 * Flags about when an object info is being fetched from remote.
 	 */
 	unsigned is_remote:1;
-};
-#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
 
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	struct string_list remote_allowed_atoms;
 };
+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
 
 static int is_atom(const char *atom, const char *s, int slen)
 {
@@ -356,17 +354,11 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
 		       struct expand_data *data)
 {
 	if (data->is_remote) {
-		size_t i, allowed_nr = ARRAY_SIZE(remote_object_info_atoms);
-		for (i = 0; i < allowed_nr; i++)
-			if (is_atom(remote_object_info_atoms[i], atom, len))
+		size_t i;
+		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
 				break;
-
-		/*
-		 * On remote, skip unsupported atoms returning an empty sb,
-		 * honoring how for-each-ref handles known but inapplicable
-		 * atoms (e.g. %(tagger)).
-		 */
-		if (i == allowed_nr)
+		if (i == data->remote_allowed_atoms.nr)
 			return 1;
 	}
 
@@ -682,12 +674,12 @@ static void batch_one_object(const char *obj_name,
 static int get_remote_info(int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	remote = remote_get(argv[0]);
@@ -727,13 +719,10 @@ static int get_remote_info(int argc,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;
 
-	string_list_append(&object_info_options, "size");
-
-	gtransport->smart_options->object_info_options = &object_info_options;
+	gtransport->smart_options->object_info_options = object_info_options;
 	gtransport->smart_options->object_info_data = *remote_object_info;
 	retval = transport_fetch_object_info(gtransport);
 cleanup:
-	string_list_clear(&object_info_options, 0);
 	transport_disconnect(gtransport);
 	return retval;
 }
@@ -819,6 +808,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
 		load_mailmap();
 }
 
+struct protocol_placeholder_entry {
+	const char *option;
+	const char *atom;
+};
+
+static const struct protocol_placeholder_entry remote_atom_map[] = {
+	{"size", "objectsize"},
+	{"type", "objecttype"},
+	/*
+	 * Add new protocol options here. Even if the server doesn't support
+	 * them the allow_list will drop them if the server doesn't advertise
+	 * them.
+	 */
+};
+
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -828,6 +832,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	const char *saved_format = opt->format;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
@@ -846,10 +851,22 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");
+
 	if (get_remote_info(count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		die(_("failed to get object info from the remote: %s"), argv[0]);
 
+	string_list_clear(&data->remote_allowed_atoms, 0);
+	string_list_append(&data->remote_allowed_atoms, "objectname");
+	for (size_t i = 0; i < ARRAY_SIZE(remote_atom_map); i++)
+		if (unsorted_string_list_has_string(&object_info_options, remote_atom_map[i].option))
+			string_list_append(&data->remote_allowed_atoms,
+					   remote_atom_map[i].atom);
+
 	data->skip_object_info = 1;
 	for (size_t i = 0; i < object_info_oids.nr; i++) {
 		data->oid = object_info_oids.oid[i];
@@ -860,25 +877,29 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 			continue;
 		}
 
+		/*
+		 * When reaching here, it means remote-object-info can retrieve
+		 * information from server without downloading them.
+		 */
 		if (remote_object_info[i].sizep) {
-			/*
-			 * When reaching here, it means remote-object-info can retrieve
-			 * information from server without downloading them.
-			 */
 			data->size = *remote_object_info[i].sizep;
-			opt->batch_mode = BATCH_MODE_INFO;
-			data->is_remote = 1;
-			batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
-			data->is_remote = 0;
-		} else {
-			report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing");
 		}
+
+		if (remote_object_info[i].typep) {
+			data->type = *remote_object_info[i].typep;
+		}
+
+		opt->batch_mode = BATCH_MODE_INFO;
+		data->is_remote = 1;
+		batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
+		data->is_remote = 0;
 	}
 	data->skip_object_info = 0;
 	opt->format = saved_format;
 
 	for (size_t i = 0; i < object_info_oids.nr; i++)
 		free_object_info_contents(&remote_object_info[i]);
+	string_list_clear(&object_info_options, 0);
 	free(line_to_split);
 	free(argv);
 	free(remote_object_info);
@@ -1198,6 +1219,7 @@ static int batch_objects(struct batch_options *opt)
  cleanup:
 	strbuf_release(&input);
 	strbuf_release(&output);
+	string_list_clear(&data.remote_allowed_atoms, 0);
 	cfg->warn_on_object_refname_ambiguity = save_warning;
 	return retval;
 }
fetch-object-info.c +19 -1
index 30475a1e87..ba7e179c44 100644
@@ -55,6 +55,24 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 	case protocol_v2:
 		if (!server_supports_v2("object-info"))
 			die(_("object-info capability is not enabled on the server"));
+		/*
+		 * When removing an element from the list it gets swapped by the
+		 * last element, iterate backwards to prevent elements skipping
+		 * evaluation.
+		 *
+		 * object_info_options->nr can be safely casted without overflow
+		 * because the number of options is a small known number (the
+		 * supported placeholders which currently are size and type).
+		 */
+		for (int i = (int)args->object_info_options->nr - 1; i >= 0; i--)
+			if (!server_supports_feature("object-info",
+						     args->object_info_options->items[i].string, 0))
+				unsorted_string_list_delete_item(args->object_info_options, i, 0);
+
+		/*
+		 * Even if no options are left, we still send the oid so we get
+		 * at least an existence check.
+		 */
 		send_object_info_request(fd_out, args);
 		break;
 	case protocol_v1:
@@ -71,7 +89,7 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 			return -1;
 		}
 
-		if (!string_list_has_string(args->object_info_options, reader->line))
+		if (!unsorted_string_list_has_string(args->object_info_options, reader->line))
 			return -1;
 
 		if (!strcmp(reader->line, "size")) {
fetch-object-info.h +3 -0
index 31aad98408..269cebb3f7 100644
@@ -14,6 +14,9 @@ struct object_info;
 /*
  * Sends git-cat-file object-info command into the request buf and read the
  * results from packets.
+ *
+ * Modifies args->object_info_options, on return it contains only the supported
+ * options by the server.
  */
 int fetch_object_info(enum protocol_version version, struct object_info_args *args,
 		      struct packet_reader *reader, struct object_info *object_info_data,
t/t1017-cat-file-remote-object-info.sh +28 -0
index edc20394d8..116862f9d0 100755
@@ -271,6 +271,34 @@ test_expect_success 'unsupported placeholder on remote returns empty string' '
 	)
 '
 
+test_expect_success 'requesting only objectname echoes back' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo $hello_oid >expect &&
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'objectname goes through existence check' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo "$unstored_oid missing" >expect &&
+
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $unstored_oid
+		EOF
+
+		test_cmp expect actual
+	)
+'
+
 # Test --batch-command remote-object-info with 'git://' and
 # transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
 test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
transport.c +0 -1
index 9342680531..f0a6a45547 100644
@@ -443,7 +443,6 @@ static int fetch_object_info_via_pack(struct transport *transport)
 	args.server_options = transport->server_options;
 	args.oids = transport->smart_options->object_info_oids;
 	args.object_info_options = transport->smart_options->object_info_options;
-	string_list_sort(args.object_info_options);
 
 	connect_setup(transport, 0);
 	packet_reader_init(&reader, data->fd[0], NULL, 0,
Signature · 1 line
2.54.0
#364 lore raw

v20 cover cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

This patch series is a continuation of Eric Ju's ([email protected]) and Calvin Wan's ([email protected]) patch series [1] and [2] respectively.

Sometimes it is beneficial to retrieve information about an object without having to download it completely. The server logic for retrieving size has already been implemented and merged in a2ba162cda (object-info: support for retrieving object info, 2021-04-20) [3]. This patch series implements the client option for it.

Eric's series adds the remote-object-info command to cat-file --batch-command. This command allows the client to make an object-info command request to a server that supports protocol v2.

If the server uses protocol v2 but does not support the object-info capability, cat-file --batch-command will die.
If a user attempts to use remote-object-info with protocol v1, cat-file --batch-command will die.

Currently, only the size (%(objectsize)) is supported end to end in this implementation. The type (%(objecttype)) is known by the client's allow-list and request path but is not supported on the server side nor the response parsing. A follow up series will add full end-to-end support for %(objecttype).

The default format for remote-object-info is set to "%(objectname) %(objectsize)". Once %(objecttype) is supported, the default format will be unified accordingly.

If the batch command format includes unsupported fields such as %(objecttype), %(objectsize:disk), or %(deltabase), the command will return empty strings for each unsupported field.

This series completes Eric's work mainly with the refactor of the validation of the placeholders with an allow-list that filters what the client asks with what the server is capable of providing, following Jeff King's idea [4].

GitHub CI: https://github.com/pabloosabaterr/git/actions/runs/29660677279

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/#t
[3]: https://git.kernel.org/pub/scm/git/git.git/commit/?id=a2ba162cda2acc171c3e36acbbc854792b093cb7
[4]: https://lore.kernel.org/git/[email protected]/

Changes in v20:
- Moved the prep patch that fixes hash_algo's type to be before write_fetch_command_and_capabilities() being moved to 'connect.c'
- Reverted git-cat-file.adoc documentation comments about CAVEATS
- Fixed style for EXPAND_DATA_INIT
- Added more context for comman line die()

---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info

Eric Ju (3):
cat-file: declare loop counter inside for()
t1006: extract helper functions into new 'lib-cat-file.sh'
cat-file: add remote-object-info to batch-command

Pablo Sabater (7):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: drop the static advertise_sid variable
fetch-pack: use unsigned int for hash_algo variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
protocol-caps: check object existence regardless of the attributes requested
cat-file: make remote-object-info allow-list adapt to the server

 Documentation/git-cat-file.adoc        |  24 +-
 Documentation/gitprotocol-v2.adoc      |  21 +-
 Makefile                               |   1 +
 builtin/cat-file.c                     | 222 +++++++++-
 connect.c                              |  34 ++
 connect.h                              |   8 +
 fetch-object-info.c                    | 156 +++++++
 fetch-object-info.h                    |  25 ++
 fetch-pack.c                           |  58 +--
 meson.build                            |   1 +
 object-file.c                          |  10 +
 odb.h                                  |   9 +
 protocol-caps.c                        |  45 +-
 serve.c                                |   5 +-
 t/lib-cat-file.sh                      |  16 +
 t/meson.build                          |   1 +
 t/t1006-cat-file.sh                    |  15 +-
 t/t1017-cat-file-remote-object-info.sh | 747 +++++++++++++++++++++++++++++++++
 t/t5701-git-serve.sh                   |  63 +++
 transport-helper.c                     |  12 +-
 transport-internal.h                   |   8 +
 transport.c                            |  44 ++
 transport.h                            |   9 +
 23 files changed, 1442 insertions(+), 92 deletions(-)

Range-diff versus v19:

 1:  6df5bcb3bc =  1:  31b02f10b5 transport-helper: fix memory leak of helper on disconnect
 2:  141d85a76f =  2:  7d62e0586f cat-file: declare loop counter inside for()
 3:  87457a1fe3 =  3:  082d593fb7 t1006: extract helper functions into new 'lib-cat-file.sh'
 4:  c26d378931 =  4:  8646b9d7cf fetch-pack: drop the static advertise_sid variable
 -:  ---------- >  5:  5531a3c399 fetch-pack: use unsigned int for hash_algo variable
 5:  ee16e17228 !  6:  92b4122642 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
    @@ connect.c: int server_supports(const char *feature)
     +	}
     +
     +	if (server_feature_v2("object-format", &hash_name)) {
    -+		int hash_algo = hash_algo_by_name(hash_name);
    ++		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     +		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     +			die(_("mismatched algorithms: client %s; server %s"),
     +			    the_hash_algo->name, hash_name);
    @@ fetch-pack.c: static int add_haves(struct fetch_negotiator *negotiator,
     -	}
     -
     -	if (server_feature_v2("object-format", &hash_name)) {
    --		int hash_algo = hash_algo_by_name(hash_name);
    +-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     -		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     -			die(_("mismatched algorithms: client %s; server %s"),
     -			    the_hash_algo->name, hash_name);
 6:  bb915f57ad <  -:  ---------- connect: use unsigned int for hash_algo_by_name() calls
 7:  b3b06b0cae =  7:  b954a5994a connect: make write_fetch_command_and_capabilities() more generic
 8:  79a87c7011 =  8:  627242a7a1 fetch-pack: move fetch initialization
 9:  37c3f93975 =  9:  0578594733 protocol-caps: check object existence regardless of the attributes requested
10:  c5062ecaf2 = 10:  816bfa9162 serve: advertise object-info feature
11:  22d72168bc = 11:  2323f45cb2 transport: add client support for object-info
12:  2cf3b24a35 ! 12:  a39975766b cat-file: add remote-object-info to batch-command

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- reports).
-
- objectsize:disk::
-- The size, in bytes, that the object takes up on disk. See the
-- note about on-disk sizes in the CAVEATS section below.
-+ The size, in bytes, that the object takes up on disk.
-
deltabase::
If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name.
- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-- below.
-+ Otherwise, expands to the null OID (all zeroes).
++ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
+ below.

rest::
- If this atom is used in the output string, input lines are split
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
after that first run of whitespace (i.e., the "rest" of the
line) are output in place of the %(rest) atom.
@@ builtin/cat-file.c: struct expand_data {
+ */
+ unsigned is_remote:1;
+};
++
+#define EXPAND_DATA_INIT { .mode = S_IFINVALID, .type = OBJ_BAD }
+
+static const char *remote_object_info_atoms[] = {
@@ builtin/cat-file.c: static void parse_cmd_mailmap(struct batch_options *opt UNUS
+ line_to_split = xstrdup(line);
+ count = split_cmdline(line_to_split, &argv);
+ if (count < 0)
-+ die(_("remote-object-info: %s"), split_cmdline_strerror(count));
++ die(_("remote-object-info: failed to parse command line: %s"),
++ split_cmdline_strerror(count));
+ if (count - 1 > MAX_ALLOWED_OBJ_LIMIT)
+ die(_("remote-object-info supports at most %d objects"),
+ MAX_ALLOWED_OBJ_LIMIT);
13: ebdfc22fb7 ! 13: 70a11d2aea cat-file: make remote-object-info allow-list adapt to the server

    @@ builtin/cat-file.c: struct expand_data {
      	 */
      	unsigned is_remote:1;
     -};
    +-
     -#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }

     -static const char *remote_object_info_atoms[] = {
     -	"objectname",
     -	"objectsize",
    ++	/*
    ++	 * List of atoms (i.e. "objectsize") that the server supports. Built
    ++	 * from the server's object-info advertised capabilities.
    ++	 */
     +	struct string_list remote_allowed_atoms;
      };
    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

    ++#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    ++			    .type = OBJ_BAD, \
    ++			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++
      static int is_atom(const char *atom, const char *s, int slen)
      {
    + 	int alen = strlen(atom);
     @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom, int len,
      		       struct expand_data *data)
      {
    @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom,
     -			if (is_atom(remote_object_info_atoms[i], atom, len))
     +		size_t i;
     +		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
    -+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
    ++			if (is_atom(data->remote_allowed_atoms.items[i].string,
    ++				    atom, len))
      				break;
     -
     -		/*

---
base-commit: 44de1520f08d1dfebc3ab2d9f644208eaa5ac925

#365 lore raw

PATCH v20 1/13 transport-helper: fix memory leak of helper on disconnect

disconnect_helper() only frees data inside of the if(data->helper) block [1]. When the transport is disconnected without the helper being fully started, data->name allocated in transport_helper_init()
is never freed.

Move FREE_AND_NULL(data->name) outside the conditional block so it's always freed on disconnect.

[1]: https://lore.kernel.org/git/05fbadbae2184479c87c37675dde7bd79b3e32ab.1716465556.git.ps@pks.im/

---
 transport-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
transport-helper.c +1 -1
index 80f90eb7ba..f195070788 100644
@@ -266,9 +266,9 @@ static int disconnect_helper(struct transport *transport)
 		close(data->helper->out);
 		fclose(data->out);
 		res = finish_command(data->helper);
-		FREE_AND_NULL(data->name);
 		FREE_AND_NULL(data->helper);
 	}
+	FREE_AND_NULL(data->name);
 	return res;
 }
 
Signature · 1 line
2.54.0
#366 lore raw

PATCH v20 2/13 cat-file: declare loop counter inside for()

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Declare loop counters in the for statement when they are only used within the loop body, limiting their scope and improving readability.

While updating the loop counters, use size_t instead of int for counters that iterate over object counts.

Update the 'nr' parameter of dispatch_calls() to size_t as all callers already pass a value of that type.

---
 builtin/cat-file.c | 13 ++++---------
 fetch-pack.c       |  3 +--
 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
index b4b99a73da..03afc44c5e 100644
@@ -721,14 +721,12 @@ static void dispatch_calls(struct batch_options *opt,
 		struct strbuf *output,
 		struct expand_data *data,
 		struct queued_cmd *cmd,
-		int nr)
+		size_t nr)
 {
-	int i;
-
 	if (!opt->buffer_output)
 		die(_("flush is only for --buffer mode"));
 
-	for (i = 0; i < nr; i++)
+	for (size_t i = 0; i < nr; i++)
 		cmd[i].fn(opt, cmd[i].line, output, data);
 
 	fflush(stdout);
@@ -736,9 +734,7 @@ static void dispatch_calls(struct batch_options *opt,
 
 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
 {
-	size_t i;
-
-	for (i = 0; i < *nr; i++)
+	for (size_t i = 0; i < *nr; i++)
 		FREE_AND_NULL(cmd[i].line);
 
 	*nr = 0;
@@ -765,7 +761,6 @@ static void batch_objects_command(struct batch_options *opt,
 	size_t alloc = 0, nr = 0;
 
 	while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
-		int i;
 		const struct parse_cmd *cmd = NULL;
 		const char *p = NULL, *cmd_end;
 		struct queued_cmd call = {0};
@@ -775,7 +770,7 @@ static void batch_objects_command(struct batch_options *opt,
 		if (isspace(*input.buf))
 			die(_("whitespace before command: '%s'"), input.buf);
 
-		for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
 			if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
 				continue;
 
fetch-pack.c +1 -2
index 29c41132ee..9eb8fc5399 100644
@@ -1388,9 +1388,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	if (advertise_sid && server_supports_v2("session-id"))
 		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
 	if (server_options && server_options->nr) {
-		int i;
 		ensure_server_supports_v2("server-option");
-		for (i = 0; i < server_options->nr; i++)
+		for (size_t i = 0; i < server_options->nr; i++)
 			packet_buf_write(req_buf, "server-option=%s",
 					 server_options->items[i].string);
 	}
Signature · 1 line
2.54.0
#367 lore raw

PATCH v20 3/13 t1006: extract helper functions into new 'lib-cat-file.sh'

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Extract utility functions from the cat-file's test script 't1006-cat-file.sh' into a new 'lib-cat-file.sh' dedicated library file.

A subsequent commit will need these functions. This improves the code reuse and readability, enabling future cat-file tests to share these helpers without duplicating code.

While at it update the style of this line to follow coding guidelines:

. "$TEST_DIRECTORY/lib-loose.sh"

to

. "$TEST_DIRECTORY"/lib-loose.sh

---
 t/lib-cat-file.sh   | 16 ++++++++++++++++
 t/t1006-cat-file.sh | 15 ++-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
t/lib-cat-file.sh new file +16 -0
new file mode 100644
index 0000000000..7c2e877016
@@ -0,0 +1,16 @@
+# Library of git-cat-file related test functions.
+
+# Print a string without a trailing newline.
+echo_without_newline () {
+	printf '%s' "$*"
+}
+
+# Print a string without newlines and replace them with a NUL character (\0).
+echo_without_newline_nul () {
+	echo_without_newline "$@" | tr '\n' '\0'
+}
+
+# Calculate the length of a string.
+strlen () {
+	echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
t/t1006-cat-file.sh +2 -13
index 8e2c52652c..cf65bfc88f 100755
@@ -3,7 +3,8 @@
 test_description='git cat-file'
 
 . ./test-lib.sh
-. "$TEST_DIRECTORY/lib-loose.sh"
+. "$TEST_DIRECTORY"/lib-loose.sh
+. "$TEST_DIRECTORY"/lib-cat-file.sh
 
 test_cmdmode_usage () {
 	test_expect_code 129 "$@" 2>err &&
@@ -99,18 +100,6 @@ do
 	'
 done
 
-echo_without_newline () {
-    printf '%s' "$*"
-}
-
-echo_without_newline_nul () {
-	echo_without_newline "$@" | tr '\n' '\0'
-}
-
-strlen () {
-    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
-}
-
 run_tests () {
     type=$1
     object_name="$2"
Signature · 1 line
2.54.0
#368 lore raw

PATCH v20 4/13 fetch-pack: drop the static advertise_sid variable

In reply to Pablo Sabater

write_fetch_command_and_capabilities() is moved to 'connect.c' in a subsequent commit. To prepare for that, drop the static variable usage of advertise_sid.

Currently advertise_sid is set in fetch_pack_config() by reading "transfer.advertisesid". It is used in three places:

1. In do_fetch_pack(), to clear it when the server lacks support:

        if (!server_supports("session-id"))
               advertise_sid = 0;

2. In find_common(), to advertise the session id over protocol v0/v1:

        if (advertise_sid)
                strbuf_addf(&c, " session-id=%s", trace2_session_id());

3. In write_fetch_command_and_capabilities(), to advertise it over protocol v2:

        if (advertise_sid && server_supports_v2("session-id"))
                packet_buf_write(req_buf, "session-id=%s", trace2_session_id());

About 1, the check only guards the v0/v1 path, and the v2 path already checks server support inline in its condition. Follow the same pattern and fold the check into the condition in find_common().

About 2 and 3, replace the static variable with a local read via repo_config_get_bool() in each function.

Because repo_config_get_bool() leaves advertise_sid as is if it is not set, initialize it to 0, matching its default.

---
 fetch-pack.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fetch-pack.c +7 -6
index 9eb8fc5399..65ebfec09f 100644
@@ -49,7 +49,6 @@ static int fetch_fsck_objects = -1;
 static int transfer_fsck_objects = -1;
 static int agent_supported;
 static int server_supports_filtering;
-static int advertise_sid;
 static struct shallow_lock shallow_lock;
 static const char *alternate_shallow_file;
 static struct strbuf fsck_msg_types = STRBUF_INIT;
@@ -363,6 +362,9 @@ static int find_common(struct fetch_negotiator *negotiator,
 	size_t state_len = 0;
 	struct packet_reader reader;
 	struct oidset negotiation_include_oids = OIDSET_INIT;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	if (args->stateless_rpc && multi_ack == 1)
 		die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
@@ -414,7 +416,7 @@ static int find_common(struct fetch_negotiator *negotiator,
 			if (deepen_not_ok)      strbuf_addstr(&c, " deepen-not");
 			if (agent_supported)    strbuf_addf(&c, " agent=%s",
 							    git_user_agent_sanitized());
-			if (advertise_sid)
+			if (advertise_sid && server_supports("session-id"))
 				strbuf_addf(&c, " session-id=%s", trace2_session_id());
 			if (args->filter_options.choice)
 				strbuf_addstr(&c, " filter");
@@ -1160,9 +1162,6 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 				      (int)agent_len, agent_feature);
 	}
 
-	if (!server_supports("session-id"))
-		advertise_sid = 0;
-
 	if (server_supports("shallow"))
 		print_verbose(args, _("Server supports %s"), "shallow");
 	else if (args->depth > 0 || is_repository_shallow(r))
@@ -1380,6 +1379,9 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 						 const struct string_list *server_options)
 {
 	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	ensure_server_supports_v2("fetch");
 	packet_buf_write(req_buf, "command=fetch");
@@ -1998,7 +2000,6 @@ static void fetch_pack_config(void)
 	repo_config_get_bool(the_repository, "repack.usedeltabaseoffset", &prefer_ofs_delta);
 	repo_config_get_bool(the_repository, "fetch.fsckobjects", &fetch_fsck_objects);
 	repo_config_get_bool(the_repository, "transfer.fsckobjects", &transfer_fsck_objects);
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 	if (!uri_protocols.nr) {
 		char *str;
 
Signature · 1 line
2.54.0
#369 lore raw

PATCH v20 5/13 fetch-pack: use unsigned int for hash_algo variable

In reply to Pablo Sabater

hash_algo_by_name() returns unsigned int, but it is stored in hash_algo variable as int. This goes unnoticed because of:

DISABLE_SIGN_COMPARE_WARNINGS

On 'fetch-pack.c'

On a subsequent commit this function will be moved to 'connect.c' that would notice this.

Change hash_algo variable type to match its return type, also make it const because they are never modified.

---
 fetch-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
index 65ebfec09f..f1e64160fc 100644
@@ -1397,7 +1397,7 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	}
 
 	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
Signature · 1 line
2.54.0
#370 lore raw

PATCH v20 6/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c

In reply to Pablo Sabater

In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future.

Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions.

Because string_list is only used as a pointer, use a forward declaration [1].

[1]: https://lore.kernel.org/git/[email protected]/

---
 connect.c    | 34 ++++++++++++++++++++++++++++++++++
 connect.h    |  4 ++++
 fetch-pack.c | 34 ----------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
connect.c +34 -0
index 47e39d2a73..c09947cc56 100644
@@ -700,6 +700,40 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options)
+{
+	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
+
+	ensure_server_supports_v2("fetch");
+	packet_buf_write(req_buf, "command=fetch");
+	if (server_supports_v2("agent"))
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
+	if (advertise_sid && server_supports_v2("session-id"))
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+	if (server_options && server_options->nr) {
+		ensure_server_supports_v2("server-option");
+		for (size_t i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
+	}
+
+	if (server_feature_v2("object-format", &hash_name)) {
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
+		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
+			die(_("mismatched algorithms: client %s; server %s"),
+			    the_hash_algo->name, hash_name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
+	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
+		die(_("the server does not support algorithm '%s'"),
+		    the_hash_algo->name);
+	}
+	packet_buf_delim(req_buf);
+}
+
 static const char *url_scheme_name(enum url_scheme scheme)
 {
 	switch (scheme) {
connect.h +4 -0
index aa482a37fb..c4f6ea4b0a 100644
@@ -34,4 +34,8 @@ void check_stateless_delimiter(int stateless_rpc,
 			       struct packet_reader *reader,
 			       const char *error);
 
+struct string_list;
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options);
+
 #endif
fetch-pack.c +0 -34
index f1e64160fc..f7789e8456 100644
@@ -1375,40 +1375,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-						 const struct string_list *server_options)
-{
-	const char *hash_name;
-	int advertise_sid = 0;
-
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
-
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
-	if (server_supports_v2("agent"))
-		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
-	if (advertise_sid && server_supports_v2("session-id"))
-		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
-	if (server_options && server_options->nr) {
-		ensure_server_supports_v2("server-option");
-		for (size_t i = 0; i < server_options->nr; i++)
-			packet_buf_write(req_buf, "server-option=%s",
-					 server_options->items[i].string);
-	}
-
-	if (server_feature_v2("object-format", &hash_name)) {
-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
-		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
-			die(_("mismatched algorithms: client %s; server %s"),
-			    the_hash_algo->name, hash_name);
-		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
-	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
-		die(_("the server does not support algorithm '%s'"),
-		    the_hash_algo->name);
-	}
-	packet_buf_delim(req_buf);
-}
-
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      struct fetch_pack_args *args,
 			      const struct ref *wants, struct oidset *common,
Signature · 1 line
2.54.0
#371 lore raw

PATCH v20 7/13 connect: make write_fetch_command_and_capabilities() more generic

In reply to Pablo Sabater

Refactor write_fetch_command_and_capabilities(), enabling it to serve both fetch and additional commands.

In this context, "command" refers to the "operations" supported by Git's wire protocol Documentation/gitprotocol-v2.adoc, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162cda (object-info: support for retrieving object info, 2021-04-20).

Refactor the function signature to accept a command instead of the hardcoded "fetch".

---
 connect.c    | 8 ++++----
 connect.h    | 8 ++++++--
 fetch-pack.c | 4 ++--
 3 files changed, 12 insertions(+), 8 deletions(-)
connect.c +4 -4
index c09947cc56..127ed4a2e6 100644
@@ -700,16 +700,16 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options)
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options)
 {
 	const char *hash_name;
 	int advertise_sid = 0;
 
 	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
+	ensure_server_supports_v2(command);
+	packet_buf_write(req_buf, "command=%s", command);
 	if (server_supports_v2("agent"))
 		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
 	if (advertise_sid && server_supports_v2("session-id"))
connect.h +6 -2
index c4f6ea4b0a..957e5fe2b9 100644
@@ -35,7 +35,11 @@ void check_stateless_delimiter(int stateless_rpc,
 			       const char *error);
 
 struct string_list;
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options);
+/*
+ * Write a protocol v2 command request, along with the capability
+ * advertisements, into req_buf.
+ */
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options);
 
 #endif
fetch-pack.c +2 -2
index f7789e8456..3695059cd5 100644
@@ -1386,7 +1386,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 	int done_sent = 0;
 	struct strbuf req_buf = STRBUF_INIT;
 
-	write_fetch_command_and_capabilities(&req_buf, args->server_options);
+	write_command_and_capabilities(&req_buf, "fetch", args->server_options);
 
 	if (args->use_thin_pack)
 		packet_buf_write(&req_buf, "thin-pack");
@@ -2253,7 +2253,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
 					   the_repository, "%d",
 					   negotiation_round);
 		strbuf_reset(&req_buf);
-		write_fetch_command_and_capabilities(&req_buf, server_options);
+		write_command_and_capabilities(&req_buf, "fetch", server_options);
 
 		packet_buf_write(&req_buf, "wait-for-done");
 
Signature · 1 line
2.54.0
#372 lore raw

PATCH v20 8/13 fetch-pack: move fetch initialization

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

There are some variables initialized at the start of the do_fetch_pack_v2() state machine. Currently, they are initialized in FETCH_CHECK_LOCAL, which is the initial state set at the beginning of the function.

However, a subsequent patch will allow for another initial state, while still requiring these initialized variables.
Move the initialization to be before the state machine, so that they are set regardless of the initial state.

Note that there is no change in behavior, because we're moving code from the beginning of the first state to just before the execution of the state machine.

---
 fetch-pack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fetch-pack.c +6 -6
index 3695059cd5..922a9b2581 100644
@@ -1735,18 +1735,18 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 		reader.me = "fetch-pack";
 	}
 
+	/* v2 supports these by default */
+	allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
+	use_sideband = 2;
+	if (args->depth > 0 || args->deepen_since || args->deepen_not)
+		args->deepen = 1;
+
 	while (state != FETCH_DONE) {
 		switch (state) {
 		case FETCH_CHECK_LOCAL:
 			sort_ref_list(&ref, ref_compare_name);
 			QSORT(sought, nr_sought, cmp_ref_by_name);
 
-			/* v2 supports these by default */
-			allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
-			use_sideband = 2;
-			if (args->depth > 0 || args->deepen_since || args->deepen_not)
-				args->deepen = 1;
-
 			/* Filter 'ref' by 'sought' and those that aren't local */
 			mark_complete_and_common_ref(negotiator, args, &ref);
 			filter_refs(args, &ref, sought, nr_sought);
Signature · 1 line
2.54.0
#373 lore raw

PATCH v20 9/13 protocol-caps: check object existence regardless of the attributes requested

In reply to Pablo Sabater

Currently, send_info() only checks for existence when the attribute 'size' is also requested. Requesting a bare OID, without attributes only echoes back the OID.

Extract the existence check to be done regardless of the number of attributes requested.

While at it, introduce a wrapper called get_object_info() similar to odb_read_object_info() that returns OBJ_BAD on fail and adds OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK flags. OBJECT_INFO_SKIP_FETCH_OBJECT is so a server with a partial clone doesn't trigger fetching objects when it gets an object-info request with an OID that is not available locally. A server should only report what it has locally.

Tighten the condition used to determine whether an object is recognized. get_object_info() returns OBJ_BAD for unknown objects, but OBJ_NONE (0) can also mean "not found". Change the check from '< 0' to '<= OBJ_NONE' to cover both as unrecognized.

With this patch, a bare OID has two possible responses:

1. Recognized OID: the server answers with "<OID>"

2. Unrecognized OID: the server answers with "<OID> SP"

Update the object-info section in 'gitprotocol-v2.adoc':
- Require full obj-oid explicitly.
- Fix parentheses.
- Define obj-size explicitly.
- Make obj-size optional in obj-info and document the behavior for unrecognized object IDs.
- Describe the attr header as zero or more pkt-lines, one per attribute, matching what the server implements. A request with no attributes gets no header.

---
 Documentation/gitprotocol-v2.adoc | 21 ++++++++-----
 protocol-caps.c                   | 45 ++++++++++++++++++++++++----
 t/t5701-git-serve.sh              | 63 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 14 deletions(-)
Documentation/gitprotocol-v2.adoc +13 -8
index 2beb70595f..7bf62014c3 100644
@@ -568,21 +568,26 @@ An `object-info` request takes the following arguments:
 
 	oid <oid>
 	Indicates to the server an object which the client wants to obtain
-	information for.
+	information for. They must be full OIDs.
 
-The response of `object-info` is a list of the requested object ids
-and associated requested information, each separated by a single space.
+The response of `object-info` consists of one pkt-line per requested attribute,
+echoing the attributes the server will report, followed by one pkt-line per
+requested object id with its information, each field separated by a single
+space.
 
 	output = info flush-pkt
 
-	info = PKT-LINE(attrs) LF)
-		*PKT-LINE(obj-info LF)
-
-	attrs = attr | attrs SP attrs
+	info = *PKT-LINE(attr LF)
+	       *PKT-LINE(obj-info LF)
 
 	attr = "size"
 
-	obj-info = obj-id SP obj-size
+	obj-size = 1*DIGIT
+
+	obj-info = obj-id [SP [obj-size]]
+
+If the server does not recognize the OID, the response will be `<oid> SP`
+regardless of the number of attributes requested.
 
 bundle-uri
 ~~~~~~~~~~
protocol-caps.c +39 -6

60 lines not on this page · raw message

t/t5701-git-serve.sh +63 -0

75 lines not on this page · raw message

Signature · 1 line
2.54.0
#374 lore raw

PATCH v20 10/13 serve: advertise object-info feature

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

In order for a client to know what object-info components a server can provide, advertise supported object-info features. This allows a client to decide whether to query the server for object-info or fetch as a fallback.

---
 serve.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
serve.c +4 -1
index 49a6e39b1d..2b07d922b3 100644
@@ -89,7 +89,7 @@ static void session_id_receive(struct repository *r UNUSED,
 	trace2_data_string("transfer", NULL, "client-sid", client_sid);
 }
 
-static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
+static int object_info_advertise(struct repository *r, struct strbuf *value)
 {
 	if (advertise_object_info == -1 &&
 	    repo_config_get_bool(r, "transfer.advertiseobjectinfo",
@@ -97,6 +97,9 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS
 		/* disabled by default */
 		advertise_object_info = 0;
 	}
+	/* Currently only size is supported */
+	if (value && advertise_object_info)
+		strbuf_addstr(value, "size");
 	return advertise_object_info;
 }
 
Signature · 1 line
2.54.0
#375 lore raw

PATCH v20 11/13 transport: add client support for object-info

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

Sometimes, it is beneficial to retrieve information about an object without downloading it entirely. The server-side logic for this functionality was implemented in commit "a2ba162cda (object-info:
support for retrieving object info, 2021-04-20)." And the wire format is documented at
https://git-scm.com/docs/protocol-v2#_object_info.

Introduce client-side support for the object-info capability.

Add its own function for object-info separate from existing fetch infrastructure.

Currently, the client supports requesting a list of OIDs with the size attribute from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client returns an error and exits.

Note that:

1. The entire request is written into req_buf before being sent to the remote. This approach follows the pattern used in the send_fetch_request() logic within 'fetch-pack.c'. Streaming the request is not addressed in this patch.

2. A new field 'unrecognized' has been added to object_info. This new field is set at fetch_object_info() when the object is unrecognized by the server.

---
 Makefile             |   1 +
 fetch-object-info.c  | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fetch-object-info.h  |  22 ++++++++
 meson.build          |   1 +
 odb.h                |   6 +++
 transport-helper.c   |  10 ++++
 transport-internal.h |   8 +++
 transport.c          |  45 +++++++++++++++++
 transport.h          |   9 ++++
 9 files changed, 240 insertions(+)
Makefile +1 -0
index 1f3f099f5c..d450e0277e 100644
@@ -1158,6 +1158,7 @@ LIB_OBJS += ewah/ewah_io.o
 LIB_OBJS += ewah/ewah_rlw.o
 LIB_OBJS += exec-cmd.o
 LIB_OBJS += fetch-negotiator.o
+LIB_OBJS += fetch-object-info.o
 LIB_OBJS += fetch-pack.o
 LIB_OBJS += fmt-merge-msg.o
 LIB_OBJS += fsck.o
fetch-object-info.c new file +138 -0

138 lines not on this page · raw message

fetch-object-info.h new file +22 -0
new file mode 100644
index 0000000000..31aad98408
@@ -0,0 +1,22 @@
+#ifndef FETCH_OBJECT_INFO_H
+#define FETCH_OBJECT_INFO_H
+
+#include "pkt-line.h"
+#include "protocol.h"
+
+struct object_info_args {
+	struct string_list *object_info_options;
+	const struct string_list *server_options;
+	struct oid_array *oids;
+};
+
+struct object_info;
+/*
+ * Sends git-cat-file object-info command into the request buf and read the
+ * results from packets.
+ */
+int fetch_object_info(enum protocol_version version, struct object_info_args *args,
+		      struct packet_reader *reader, struct object_info *object_info_data,
+		      int stateless_rpc, int fd_out);
+
+#endif /* FETCH_OBJECT_INFO_H */
meson.build +1 -0
index 9434b56960..dfefcd3475 100644
@@ -359,6 +359,7 @@ libgit_sources = [
   'ewah/ewah_rlw.c',
   'exec-cmd.c',
   'fetch-negotiator.c',
+  'fetch-object-info.c',
   'fetch-pack.c',
   'fmt-merge-msg.c',
   'fsck.c',
odb.h +6 -0
index 94754643d2..88a37febbf 100644
@@ -339,6 +339,12 @@ struct object_info {
 	 * or multiple times in the same source.
 	 */
 	struct odb_source_info *source_infop;
+
+	/*
+	 * object-info protocol specific. Set by the protocol when the remote
+	 * does not recognize the requested object.
+	 */
+	unsigned int unrecognized:1;
 };
 
 /*
transport-helper.c +10 -0
index f195070788..623463dcea 100644
@@ -784,6 +784,15 @@ static int fetch_refs(struct transport *transport,
 	return -1;
 }
 
+static int fetch_object_info_helper(struct transport *transport)
+{
+	get_helper(transport);
+	if (process_connect(transport, 0))
+		return transport->vtable->fetch_object_info(transport);
+
+	die(_("object-info requires protocol v2"));
+}
+
 struct push_update_ref_state {
 	struct ref *hint;
 	struct ref_push_report *report;
@@ -1330,6 +1339,7 @@ static struct transport_vtable vtable = {
 	.get_refs_list	= get_refs_list,
 	.get_bundle_uri = get_bundle_uri,
 	.fetch_refs	= fetch_refs,
+	.fetch_object_info = fetch_object_info_helper,
 	.push_refs	= push_refs,
 	.connect	= connect_helper,
 	.disconnect	= release_helper
transport-internal.h +8 -0
index 051f3ab0dc..60db0bedcd 100644
@@ -45,6 +45,14 @@ struct transport_vtable {
 	 **/
 	int (*fetch_refs)(struct transport *transport, int refs_nr, struct ref **refs);
 
+	/*
+	 * Fetch object info (only size currently) from remote without
+	 * downloading the objects.
+	 *
+	 * Uses object-info capability of v2 protocol.
+	 */
+	int (*fetch_object_info)(struct transport *transport);
+
 	/**
 	 * Push the objects and refs. Send the necessary objects, and
 	 * then, for any refs where peer_ref is set and
transport.c +45 -0

69 lines not on this page · raw message

transport.h +9 -0
index 7e5867cffa..a7869d18e0 100644
@@ -55,6 +55,10 @@ struct git_transport_options {
 	 * common commits to this oidset instead of fetching any packfiles.
 	 */
 	struct oidset *acked_commits;
+
+	struct oid_array *object_info_oids;
+	struct object_info *object_info_data;
+	struct string_list *object_info_options;
 };
 
 enum transport_family {
@@ -309,6 +313,11 @@ int transport_get_remote_bundle_uri(struct transport *transport);
 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport);
 int transport_fetch_refs(struct transport *transport, struct ref *refs);
 
+/*
+ * Fetch the object info from remote
+ */
+int transport_fetch_object_info(struct transport *transport);
+
 /*
  * If this flag is set, unlocking will avoid to call non-async-signal-safe
  * functions. This will necessarily leave behind some data structures which
Signature · 1 line
2.54.0
#376 lore raw

PATCH v20 12/13 cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Since the info command in cat-file --batch-command prints object info for a given object, it is natural to add another command in cat-file --batch-command to print object info for a given object from a remote.

Add remote-object-info command to cat-file --batch-command.

While info takes object ids one at a time, this creates overhead when making requests to a server. So remote-object-info instead can take multiple object ids at once.

The cat-file --batch-command command is generally implemented in the following manner:

- Receive and parse input from user
- Call respective function attached to command
- Get object info, print object info

In --buffer mode, this changes to:

- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue
- Call respective function attached to command
- Get object info, print object info

Notice how the getting and printing of object info is accomplished one at a time. As described above, this creates a problem for making requests to a server. Therefore, remote-object-info is implemented in the following manner:

- Receive and parse input from user
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Parse input, get object info, print object info

And finally for --buffer mode remote-object-info:
- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue:
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Get object info, print object info

To summarize, remote-object-info gets object info from the remote and then loops through the object info passed in, printing the info.

In order for remote-object-info to avoid remote communication overhead in the non-buffer mode, the objects are passed in as such:

remote-object-info <remote> <oid> <oid> ... <oid>

rather than

remote-object-info <remote> <oid>
remote-object-info <remote> <oid>
...
remote-object-info <remote> <oid>

Placeholders in the format are validated against an allow-list of the atoms the remote path supports: "objectname" and "objectsize". Unsupported atoms expand to an empty string, honoring how for-each-ref handles known but inapplicable atoms.
Without this, atoms like %(objecttype) would mark data->info.typep and because the server only sends size, type_name() would later crash. As extra safety, even outside of the remote path, initialize expand_data's type to OBJ_BAD and handle type_name() returning NULL.

---
 Documentation/git-cat-file.adoc        |  24 +-
 builtin/cat-file.c                     | 181 ++++++++-
 object-file.c                          |  10 +
 odb.h                                  |   3 +
 t/meson.build                          |   1 +
 t/t1017-cat-file-remote-object-info.sh | 719 +++++++++++++++++++++++++++++++++
 6 files changed, 930 insertions(+), 8 deletions(-)
Documentation/git-cat-file.adoc +21 -3
index 86b9181599..b9cd958e11 100644
@@ -169,6 +169,13 @@ info <object>::
 	Print object info for object reference `<object>`. This corresponds to the
 	output of `--batch-check`.
 
+remote-object-info <remote> <object>...::
+	Print object info for object references `<object>` at specified
+	`<remote>` without downloading objects from the remote.
+	Raise an error when the `object-info` capability is not supported by the remote.
+	Raise an error when no object references are provided.
+	This command may be combined with `--buffer`.
+
 flush::
 	Used with `--buffer` to execute all preceding commands that were issued
 	since the beginning or since the last flush was issued. When `--buffer`
@@ -301,7 +308,8 @@ one per line, and print information based on the command given. With
 `--batch-command`, the `info` command followed by an object will print
 information about the object the same way `--batch-check` would, and the
 `contents` command followed by an object prints contents in the same way
-`--batch` would.
+`--batch` would. The `remote-object-info` command followed by a remote and
+objects IDs prints object info from the remote without downloading the objects.
 
 You can specify the information shown for each object by using a custom
 `<format>`. The `<format>` is copied literally to stdout for each
@@ -330,7 +338,7 @@ newline. The available atoms are:
 `deltabase`::
 	If the object is stored as a delta on-disk, this expands to the
 	full hex representation of the delta base object name.
-	Otherwise, expands to the null OID (all zeroes). See `CAVEATS`
+	Otherwise, expands to the null OID (all zeroes). See `CAVEATS` section
 	below.
 
 `rest`::
@@ -340,8 +348,14 @@ newline. The available atoms are:
 	after that first run of whitespace (i.e., the "rest" of the
 	line) are output in place of the `%(rest)` atom.
 
+The command `remote-object-info` only supports the `%(objectname)` and
+`%(objectsize)` placeholders. See `CAVEATS` below for more information.
+
 If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize)`, except for `remote-object-info` commands which
+use `%(objectname) %(objectsize)` because "%(objecttype)" is not supported yet.
+WARNING: When "%(objecttype)" is supported, the default format WILL be unified,
+so DO NOT RELY on the current default format to stay the same!!!
 
 If `--batch` is specified, or if `--batch-command` is used with the `contents`
 command, the object information is followed by the object contents (consisting
@@ -438,6 +452,10 @@ scripting purposes.
 CAVEATS
 -------
 
+Note that only `%(objectname)` and `%(objectsize)` are currently
+supported by the `remote-object-info` command. Using any other placeholder in
+the format string will return an empty string in its position.
+
 Note that the sizes of objects on disk are reported accurately, but care
 should be taken in drawing conclusions about which refs or objects are
 responsible for disk usage. The size of a packed non-delta object may be
builtin/cat-file.c +176 -5

225 lines not on this page · raw message

object-file.c +10 -0
index 6453b1d6fa..07f019a0f6 100644
@@ -1694,3 +1694,13 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
 
 	return &transaction->base;
 }
+
+void free_object_info_contents(struct object_info *object_info)
+{
+	if (!object_info)
+		return;
+	free(object_info->typep);
+	free(object_info->sizep);
+	free(object_info->disk_sizep);
+	free(object_info->delta_base_oid);
+}
odb.h +3 -0
index 88a37febbf..92fa414e2c 100644
@@ -623,4 +623,7 @@ void parse_alternates(const char *string,
 		      const char *relative_base,
 		      struct strvec *out);
 
+/* Free pointers inside of object_info, but not object_info itself */
+void free_object_info_contents(struct object_info *object_info);
+
 #endif /* ODB_H */
t/meson.build +1 -0
index 8ae6ab6c5f..10241e3dcc 100644
@@ -171,6 +171,7 @@ integration_tests = [
   't1014-read-tree-confusing.sh',
   't1015-read-index-unmerged.sh',
   't1016-compatObjectFormat.sh',
+  't1017-cat-file-remote-object-info.sh',
   't1020-subdirectory.sh',
   't1022-read-tree-partial-clone.sh',
   't1050-large.sh',
t/t1017-cat-file-remote-object-info.sh new file +719 -0

719 lines not on this page · raw message

Signature · 1 line
2.54.0
#377 lore raw

PATCH v20 13/13 cat-file: make remote-object-info allow-list adapt to the server

In reply to Pablo Sabater

The static allow-list in expand_atom() is hardcoded to allow only "objectname" and "objectsize" for remote queries. This works because, up to this point, servers will either support object-info with name and size or they do not support them at all.

As object-info gains new capabilities, we cannot expect different servers with different Git versions to have the same object-info capabilities. Therefore, the client needs to adapt its allow-list to what the server advertises.

The client now:

1. Requests the protocol option that the placeholder refers to (i.e. "size" for "%(objectsize)").

2. Drops any requested option that the server does not advertise in fetch_object_info().

3. Maps the remaining advertised options back to their placeholders and populates remote_allowed_atoms.

4. Uses remote_allowed_atoms in expand_atom(), preserving the previous behavior for supported placeholders.

For example, if the client requests "%(objectsize) %(objecttype)" and the server only supports 'size', then the client only requests 'size'. The server returns the size (i.e "42") "%(objectsize)" is expanded normally while "%(objecttype)" expands to an empty string:

	"42 "

Note that the empty string expansion is only for known but unsupported placeholders. "%(objectcolor)" which doesn't exist would die().

This honors what for-each-ref does for known but inapplicable atoms (placeholders).

Move object_info_options out of get_remote_info() so the caller which has data can select what options will be requested instead of requesting always size.

Move batch_object_write() out so output is always produced. If there are no supported attributes, the output is a blank line.

Include "type" in the object_info_options even though the client does not yet know how to parse the server's "type" capability.

As a result, "type" is always filtered out, allowing the tests to verify that known but unsupported placeholders expand to an empty string.

Since the filter removes options by swapping with the last element, the list is no longer kept sorted. Drop the pre-sort in fetch_object_info_via_pack() and use the unsorted string_list lookup for the response header. This has no effect in performance as the list can only be two entries long ('size' and 'type').

---
 builtin/cat-file.c                     | 94 ++++++++++++++++++++++------------
 fetch-object-info.c                    | 20 +++++++-
 fetch-object-info.h                    |  3 ++
 t/t1017-cat-file-remote-object-info.sh | 28 ++++++++++
 transport.c                            |  1 -
 5 files changed, 111 insertions(+), 35 deletions(-)
builtin/cat-file.c +61 -33
index 8994b04d15..2007b0d4bb 100644
@@ -338,15 +338,18 @@ struct expand_data {
 	 * Flags about when an object info is being fetched from remote.
 	 */
 	unsigned is_remote:1;
-};
-
-#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
 
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	/*
+	 * List of atoms (i.e. "objectsize") that the server supports. Built
+	 * from the server's object-info advertised capabilities.
+	 */
+	struct string_list remote_allowed_atoms;
 };
 
+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
+			    .type = OBJ_BAD, \
+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
+
 static int is_atom(const char *atom, const char *s, int slen)
 {
 	int alen = strlen(atom);
@@ -357,17 +360,12 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
 		       struct expand_data *data)
 {
 	if (data->is_remote) {
-		size_t i, allowed_nr = ARRAY_SIZE(remote_object_info_atoms);
-		for (i = 0; i < allowed_nr; i++)
-			if (is_atom(remote_object_info_atoms[i], atom, len))
+		size_t i;
+		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
+			if (is_atom(data->remote_allowed_atoms.items[i].string,
+				    atom, len))
 				break;
-
-		/*
-		 * On remote, skip unsupported atoms returning an empty sb,
-		 * honoring how for-each-ref handles known but inapplicable
-		 * atoms (e.g. %(tagger)).
-		 */
-		if (i == allowed_nr)
+		if (i == data->remote_allowed_atoms.nr)
 			return 1;
 	}
 
@@ -683,12 +681,12 @@ static void batch_one_object(const char *obj_name,
 static int get_remote_info(int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	remote = remote_get(argv[0]);
@@ -728,13 +726,10 @@ static int get_remote_info(int argc,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;
 
-	string_list_append(&object_info_options, "size");
-
-	gtransport->smart_options->object_info_options = &object_info_options;
+	gtransport->smart_options->object_info_options = object_info_options;
 	gtransport->smart_options->object_info_data = *remote_object_info;
 	retval = transport_fetch_object_info(gtransport);
 cleanup:
-	string_list_clear(&object_info_options, 0);
 	transport_disconnect(gtransport);
 	return retval;
 }
@@ -820,6 +815,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
 		load_mailmap();
 }
 
+struct protocol_placeholder_entry {
+	const char *option;
+	const char *atom;
+};
+
+static const struct protocol_placeholder_entry remote_atom_map[] = {
+	{"size", "objectsize"},
+	{"type", "objecttype"},
+	/*
+	 * Add new protocol options here. Even if the server doesn't support
+	 * them the allow_list will drop them if the server doesn't advertise
+	 * them.
+	 */
+};
+
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -829,6 +839,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	const char *saved_format = opt->format;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
@@ -848,10 +859,22 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");
+
 	if (get_remote_info(count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		die(_("failed to get object info from the remote: %s"), argv[0]);
 
+	string_list_clear(&data->remote_allowed_atoms, 0);
+	string_list_append(&data->remote_allowed_atoms, "objectname");
+	for (size_t i = 0; i < ARRAY_SIZE(remote_atom_map); i++)
+		if (unsorted_string_list_has_string(&object_info_options, remote_atom_map[i].option))
+			string_list_append(&data->remote_allowed_atoms,
+					   remote_atom_map[i].atom);
+
 	data->skip_object_info = 1;
 	for (size_t i = 0; i < object_info_oids.nr; i++) {
 		data->oid = object_info_oids.oid[i];
@@ -862,25 +885,29 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 			continue;
 		}
 
+		/*
+		 * When reaching here, it means remote-object-info can retrieve
+		 * information from server without downloading them.
+		 */
 		if (remote_object_info[i].sizep) {
-			/*
-			 * When reaching here, it means remote-object-info can retrieve
-			 * information from server without downloading them.
-			 */
 			data->size = *remote_object_info[i].sizep;
-			opt->batch_mode = BATCH_MODE_INFO;
-			data->is_remote = 1;
-			batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
-			data->is_remote = 0;
-		} else {
-			report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing");
 		}
+
+		if (remote_object_info[i].typep) {
+			data->type = *remote_object_info[i].typep;
+		}
+
+		opt->batch_mode = BATCH_MODE_INFO;
+		data->is_remote = 1;
+		batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
+		data->is_remote = 0;
 	}
 	data->skip_object_info = 0;
 	opt->format = saved_format;
 
 	for (size_t i = 0; i < object_info_oids.nr; i++)
 		free_object_info_contents(&remote_object_info[i]);
+	string_list_clear(&object_info_options, 0);
 	free(line_to_split);
 	free(argv);
 	free(remote_object_info);
@@ -1200,6 +1227,7 @@ static int batch_objects(struct batch_options *opt)
  cleanup:
 	strbuf_release(&input);
 	strbuf_release(&output);
+	string_list_clear(&data.remote_allowed_atoms, 0);
 	cfg->warn_on_object_refname_ambiguity = save_warning;
 	return retval;
 }
fetch-object-info.c +19 -1
index 30475a1e87..ba7e179c44 100644
@@ -55,6 +55,24 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 	case protocol_v2:
 		if (!server_supports_v2("object-info"))
 			die(_("object-info capability is not enabled on the server"));
+		/*
+		 * When removing an element from the list it gets swapped by the
+		 * last element, iterate backwards to prevent elements skipping
+		 * evaluation.
+		 *
+		 * object_info_options->nr can be safely casted without overflow
+		 * because the number of options is a small known number (the
+		 * supported placeholders which currently are size and type).
+		 */
+		for (int i = (int)args->object_info_options->nr - 1; i >= 0; i--)
+			if (!server_supports_feature("object-info",
+						     args->object_info_options->items[i].string, 0))
+				unsorted_string_list_delete_item(args->object_info_options, i, 0);
+
+		/*
+		 * Even if no options are left, we still send the oid so we get
+		 * at least an existence check.
+		 */
 		send_object_info_request(fd_out, args);
 		break;
 	case protocol_v1:
@@ -71,7 +89,7 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 			return -1;
 		}
 
-		if (!string_list_has_string(args->object_info_options, reader->line))
+		if (!unsorted_string_list_has_string(args->object_info_options, reader->line))
 			return -1;
 
 		if (!strcmp(reader->line, "size")) {
fetch-object-info.h +3 -0
index 31aad98408..269cebb3f7 100644
@@ -14,6 +14,9 @@ struct object_info;
 /*
  * Sends git-cat-file object-info command into the request buf and read the
  * results from packets.
+ *
+ * Modifies args->object_info_options, on return it contains only the supported
+ * options by the server.
  */
 int fetch_object_info(enum protocol_version version, struct object_info_args *args,
 		      struct packet_reader *reader, struct object_info *object_info_data,
t/t1017-cat-file-remote-object-info.sh +28 -0
index edc20394d8..116862f9d0 100755
@@ -271,6 +271,34 @@ test_expect_success 'unsupported placeholder on remote returns empty string' '
 	)
 '
 
+test_expect_success 'requesting only objectname echoes back' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo $hello_oid >expect &&
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'objectname goes through existence check' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo "$unstored_oid missing" >expect &&
+
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $unstored_oid
+		EOF
+
+		test_cmp expect actual
+	)
+'
+
 # Test --batch-command remote-object-info with 'git://' and
 # transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
 test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
transport.c +0 -1
index 9342680531..f0a6a45547 100644
@@ -443,7 +443,6 @@ static int fetch_object_info_via_pack(struct transport *transport)
 	args.server_options = transport->server_options;
 	args.oids = transport->smart_options->object_info_oids;
 	args.object_info_options = transport->smart_options->object_info_options;
-	string_list_sort(args.object_info_options);
 
 	connect_setup(transport, 0);
 	packet_reader_init(&reader, data->fd[0], NULL, 0,
Signature · 1 line
2.54.0
#378 lore raw
In reply to Pablo Sabater

Pablo Sabater <[email protected]> writes:

[snip]

Quoted 7 lines

Changes in v20:
- Moved the prep patch that fixes hash_algo's type to be before write_fetch_command_and_capabilities() being moved to 'connect.c'
- Reverted git-cat-file.adoc documentation comments about CAVEATS
- Fixed style for EXPAND_DATA_INIT
- Added more context for comman line die()

[snip]

Quoted 44 lines

Range-diff versus v19:

 1:  6df5bcb3bc =  1:  31b02f10b5 transport-helper: fix memory leak of helper on disconnect
 2:  141d85a76f =  2:  7d62e0586f cat-file: declare loop counter inside for()
 3:  87457a1fe3 =  3:  082d593fb7 t1006: extract helper functions into new 'lib-cat-file.sh'
 4:  c26d378931 =  4:  8646b9d7cf fetch-pack: drop the static advertise_sid variable
 -:  ---------- >  5:  5531a3c399 fetch-pack: use unsigned int for hash_algo variable
 5:  ee16e17228 !  6:  92b4122642 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
    @@ connect.c: int server_supports(const char *feature)
     +	}
     +
     +	if (server_feature_v2("object-format", &hash_name)) {
    -+		int hash_algo = hash_algo_by_name(hash_name);
    ++		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     +		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     +			die(_("mismatched algorithms: client %s; server %s"),
     +			    the_hash_algo->name, hash_name);
    @@ fetch-pack.c: static int add_haves(struct fetch_negotiator *negotiator,
     -	}
     -
     -	if (server_feature_v2("object-format", &hash_name)) {
    --		int hash_algo = hash_algo_by_name(hash_name);
    +-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     -		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     -			die(_("mismatched algorithms: client %s; server %s"),
     -			    the_hash_algo->name, hash_name);
 6:  bb915f57ad <  -:  ---------- connect: use unsigned int for hash_algo_by_name() calls
 7:  b3b06b0cae =  7:  b954a5994a connect: make write_fetch_command_and_capabilities() more generic
 8:  79a87c7011 =  8:  627242a7a1 fetch-pack: move fetch initialization
 9:  37c3f93975 =  9:  0578594733 protocol-caps: check object existence regardless of the attributes requested
10:  c5062ecaf2 = 10:  816bfa9162 serve: advertise object-info feature
11:  22d72168bc = 11:  2323f45cb2 transport: add client support for object-info
12:  2cf3b24a35 ! 12:  a39975766b cat-file: add remote-object-info to batch-command

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- reports).
-
- objectsize:disk::
-- The size, in bytes, that the object takes up on disk. See the
-- note about on-disk sizes in the CAVEATS section below.
-+ The size, in bytes, that the object takes up on disk.
-

Quoted 9 lines

deltabase::
If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name.
- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-- below.
-+ Otherwise, expands to the null OID (all zeroes).
++ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
+ below.

This chagne is still unnecessary, no?

Quoted 39 lines
      `rest`::
    - 	If this atom is used in the output string, input lines are split
     @@ Documentation/git-cat-file.adoc: newline. The available atoms are:
      	after that first run of whitespace (i.e., the "rest" of the
      	line) are output in place of the `%(rest)` atom.
    @@ builtin/cat-file.c: struct expand_data {
     +	 */
     +	unsigned is_remote:1;
     +};
    ++
     +#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
     +
     +static const char *remote_object_info_atoms[] = {
    @@ builtin/cat-file.c: static void parse_cmd_mailmap(struct batch_options *opt UNUS
     +	line_to_split = xstrdup(line);
     +	count = split_cmdline(line_to_split, &argv);
     +	if (count < 0)
    -+		die(_("remote-object-info: %s"), split_cmdline_strerror(count));
    ++		die(_("remote-object-info: failed to parse command line: %s"),
    ++		    split_cmdline_strerror(count));
     +	if (count - 1 > MAX_ALLOWED_OBJ_LIMIT)
     +		die(_("remote-object-info supports at most %d objects"),
     +		    MAX_ALLOWED_OBJ_LIMIT);
13:  ebdfc22fb7 ! 13:  70a11d2aea cat-file: make remote-object-info allow-list adapt to the server
    @@ builtin/cat-file.c: struct expand_data {
      	 */
      	unsigned is_remote:1;
     -};
    +-
     -#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }

     -static const char *remote_object_info_atoms[] = {
     -	"objectname",
     -	"objectsize",
    ++	/*
    ++	 * List of atoms (i.e. "objectsize") that the server supports. Built
    ++	 * from the server's object-info advertised capabilities.
    ++	 */
     +	struct string_list remote_allowed_atoms;
Quoted 8 lines
      };
    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

    ++#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    ++			    .type = OBJ_BAD, \
    ++			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++

I guess this is output of running the style check script (which I did recommend, but I should've mentioned that you should take it with a pinch of salt, it doesn't always provide the best solutions.)

Quoted 17 lines
      static int is_atom(const char *atom, const char *s, int slen)
      {
    + 	int alen = strlen(atom);
     @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom, int len,
      		       struct expand_data *data)
      {
    @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom,
     -			if (is_atom(remote_object_info_atoms[i], atom, len))
     +		size_t i;
     +		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
    -+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
    ++			if (is_atom(data->remote_allowed_atoms.items[i].string,
    ++				    atom, len))
      				break;
     -
     -		/*
Quoted 2 lines

---
base-commit: 44de1520f08d1dfebc3ab2d9f644208eaa5ac925

I reviewed v18 last, but the diff here looks good to me. Thanks!

#379 lore raw
Quoted 68 lines · Karthik Nayak

Pablo Sabater <[email protected]> writes:

[snip]

Quoted 7 lines

Changes in v20:
- Moved the prep patch that fixes hash_algo's type to be before write_fetch_command_and_capabilities() being moved to 'connect.c'
- Reverted git-cat-file.adoc documentation comments about CAVEATS
- Fixed style for EXPAND_DATA_INIT
- Added more context for comman line die()

[snip]

Quoted 44 lines

Range-diff versus v19:

 1:  6df5bcb3bc =  1:  31b02f10b5 transport-helper: fix memory leak of helper on disconnect
 2:  141d85a76f =  2:  7d62e0586f cat-file: declare loop counter inside for()
 3:  87457a1fe3 =  3:  082d593fb7 t1006: extract helper functions into new 'lib-cat-file.sh'
 4:  c26d378931 =  4:  8646b9d7cf fetch-pack: drop the static advertise_sid variable
 -:  ---------- >  5:  5531a3c399 fetch-pack: use unsigned int for hash_algo variable
 5:  ee16e17228 !  6:  92b4122642 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
    @@ connect.c: int server_supports(const char *feature)
     +	}
     +
     +	if (server_feature_v2("object-format", &hash_name)) {
    -+		int hash_algo = hash_algo_by_name(hash_name);
    ++		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     +		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     +			die(_("mismatched algorithms: client %s; server %s"),
     +			    the_hash_algo->name, hash_name);
    @@ fetch-pack.c: static int add_haves(struct fetch_negotiator *negotiator,
     -	}
     -
     -	if (server_feature_v2("object-format", &hash_name)) {
    --		int hash_algo = hash_algo_by_name(hash_name);
    +-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     -		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     -			die(_("mismatched algorithms: client %s; server %s"),
     -			    the_hash_algo->name, hash_name);
 6:  bb915f57ad <  -:  ---------- connect: use unsigned int for hash_algo_by_name() calls
 7:  b3b06b0cae =  7:  b954a5994a connect: make write_fetch_command_and_capabilities() more generic
 8:  79a87c7011 =  8:  627242a7a1 fetch-pack: move fetch initialization
 9:  37c3f93975 =  9:  0578594733 protocol-caps: check object existence regardless of the attributes requested
10:  c5062ecaf2 = 10:  816bfa9162 serve: advertise object-info feature
11:  22d72168bc = 11:  2323f45cb2 transport: add client support for object-info
12:  2cf3b24a35 ! 12:  a39975766b cat-file: add remote-object-info to batch-command

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- reports).
-
- objectsize:disk::
-- The size, in bytes, that the object takes up on disk. See the
-- note about on-disk sizes in the CAVEATS section below.
-+ The size, in bytes, that the object takes up on disk.
-

Quoted 9 lines

deltabase::
If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name.
- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-- below.
-+ Otherwise, expands to the null OID (all zeroes).
++ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
+ below.

Quoted 1 line

This chagne is still unnecessary, no?

I guess it is unnecessary. I added it because on top, at objectsize:disk it says "CAVEATS section". So I tried to have it consistently.

Is it worth a reroll?

Quoted 44 lines
Quoted 44 lines
      `rest`::
    - 	If this atom is used in the output string, input lines are split
     @@ Documentation/git-cat-file.adoc: newline. The available atoms are:
      	after that first run of whitespace (i.e., the "rest" of the
      	line) are output in place of the `%(rest)` atom.
    @@ builtin/cat-file.c: struct expand_data {
     +	 */
     +	unsigned is_remote:1;
     +};
    ++
     +#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
     +
     +static const char *remote_object_info_atoms[] = {
    @@ builtin/cat-file.c: static void parse_cmd_mailmap(struct batch_options *opt UNUS
     +	line_to_split = xstrdup(line);
     +	count = split_cmdline(line_to_split, &argv);
     +	if (count < 0)
    -+		die(_("remote-object-info: %s"), split_cmdline_strerror(count));
    ++		die(_("remote-object-info: failed to parse command line: %s"),
    ++		    split_cmdline_strerror(count));
     +	if (count - 1 > MAX_ALLOWED_OBJ_LIMIT)
     +		die(_("remote-object-info supports at most %d objects"),
     +		    MAX_ALLOWED_OBJ_LIMIT);
13:  ebdfc22fb7 ! 13:  70a11d2aea cat-file: make remote-object-info allow-list adapt to the server
    @@ builtin/cat-file.c: struct expand_data {
      	 */
      	unsigned is_remote:1;
     -};
    +-
     -#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }

     -static const char *remote_object_info_atoms[] = {
     -	"objectname",
     -	"objectsize",
    ++	/*
    ++	 * List of atoms (i.e. "objectsize") that the server supports. Built
    ++	 * from the server's object-info advertised capabilities.
    ++	 */
     +	struct string_list remote_allowed_atoms;
      };
    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
Quoted 8 lines
Quoted 4 lines
    ++#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    ++			    .type = OBJ_BAD, \
    ++			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++

I guess this is output of running the style check script (which I did recommend, but I should've mentioned that you should take it with a pinch of salt, it doesn't always provide the best solutions.)

It comes in part from Junio's suggestion [1]. Although I don't think that it will receive a lot of new fields, I thought that it would leave the macro clean for that.

Quoted 18 lines
Quoted 18 lines
      static int is_atom(const char *atom, const char *s, int slen)
      {
    + 	int alen = strlen(atom);
     @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom, int len,
      		       struct expand_data *data)
      {
    @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom,
     -			if (is_atom(remote_object_info_atoms[i], atom, len))
     +		size_t i;
     +		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
    -+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
    ++			if (is_atom(data->remote_allowed_atoms.items[i].string,
    ++				    atom, len))
      				break;
     -
     -		/*
Quoted 4 lines
Quoted 2 lines

---
base-commit: 44de1520f08d1dfebc3ab2d9f644208eaa5ac925

I reviewed v18 last, but the diff here looks good to me. Thanks!

[1]: https://lore.kernel.org/git/[email protected]/

Thanks,
Pablo

#380 lore raw
Quoted 69 lines · Pablo Sabater
Quoted 68 lines · Karthik Nayak

Pablo Sabater <[email protected]> writes:

[snip]

Quoted 7 lines

Changes in v20:
- Moved the prep patch that fixes hash_algo's type to be before write_fetch_command_and_capabilities() being moved to 'connect.c'
- Reverted git-cat-file.adoc documentation comments about CAVEATS
- Fixed style for EXPAND_DATA_INIT
- Added more context for comman line die()

[snip]

Quoted 53 lines

Range-diff versus v19:

 1:  6df5bcb3bc =  1:  31b02f10b5 transport-helper: fix memory leak of helper on disconnect
 2:  141d85a76f =  2:  7d62e0586f cat-file: declare loop counter inside for()
 3:  87457a1fe3 =  3:  082d593fb7 t1006: extract helper functions into new 'lib-cat-file.sh'
 4:  c26d378931 =  4:  8646b9d7cf fetch-pack: drop the static advertise_sid variable
 -:  ---------- >  5:  5531a3c399 fetch-pack: use unsigned int for hash_algo variable
 5:  ee16e17228 !  6:  92b4122642 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
    @@ connect.c: int server_supports(const char *feature)
     +	}
     +
     +	if (server_feature_v2("object-format", &hash_name)) {
    -+		int hash_algo = hash_algo_by_name(hash_name);
    ++		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     +		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     +			die(_("mismatched algorithms: client %s; server %s"),
     +			    the_hash_algo->name, hash_name);
    @@ fetch-pack.c: static int add_haves(struct fetch_negotiator *negotiator,
     -	}
     -
     -	if (server_feature_v2("object-format", &hash_name)) {
    --		int hash_algo = hash_algo_by_name(hash_name);
    +-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
     -		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
     -			die(_("mismatched algorithms: client %s; server %s"),
     -			    the_hash_algo->name, hash_name);
 6:  bb915f57ad <  -:  ---------- connect: use unsigned int for hash_algo_by_name() calls
 7:  b3b06b0cae =  7:  b954a5994a connect: make write_fetch_command_and_capabilities() more generic
 8:  79a87c7011 =  8:  627242a7a1 fetch-pack: move fetch initialization
 9:  37c3f93975 =  9:  0578594733 protocol-caps: check object existence regardless of the attributes requested
10:  c5062ecaf2 = 10:  816bfa9162 serve: advertise object-info feature
11:  22d72168bc = 11:  2323f45cb2 transport: add client support for object-info
12:  2cf3b24a35 ! 12:  a39975766b cat-file: add remote-object-info to batch-command

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- reports).
-
- objectsize:disk::
-- The size, in bytes, that the object takes up on disk. See the
-- note about on-disk sizes in the CAVEATS section below.
-+ The size, in bytes, that the object takes up on disk.
-
deltabase::
If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name.
- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-- below.
-+ Otherwise, expands to the null OID (all zeroes).
++ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
+ below.

Quoted 7 lines
Quoted 1 line

This chagne is still unnecessary, no?

I guess it is unnecessary. I added it because on top, at objectsize:disk it says "CAVEATS section". So I tried to have it consistently.

Is it worth a reroll?

No I don't think a re-roll is necessary. But something to avoid next time :)

Quoted 49 lines
Quoted 49 lines
Quoted 48 lines
      `rest`::
    - 	If this atom is used in the output string, input lines are split
     @@ Documentation/git-cat-file.adoc: newline. The available atoms are:
      	after that first run of whitespace (i.e., the "rest" of the
      	line) are output in place of the `%(rest)` atom.
    @@ builtin/cat-file.c: struct expand_data {
     +	 */
     +	unsigned is_remote:1;
     +};
    ++
     +#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
     +
     +static const char *remote_object_info_atoms[] = {
    @@ builtin/cat-file.c: static void parse_cmd_mailmap(struct batch_options *opt UNUS
     +	line_to_split = xstrdup(line);
     +	count = split_cmdline(line_to_split, &argv);
     +	if (count < 0)
    -+		die(_("remote-object-info: %s"), split_cmdline_strerror(count));
    ++		die(_("remote-object-info: failed to parse command line: %s"),
    ++		    split_cmdline_strerror(count));
     +	if (count - 1 > MAX_ALLOWED_OBJ_LIMIT)
     +		die(_("remote-object-info supports at most %d objects"),
     +		    MAX_ALLOWED_OBJ_LIMIT);
13:  ebdfc22fb7 ! 13:  70a11d2aea cat-file: make remote-object-info allow-list adapt to the server
    @@ builtin/cat-file.c: struct expand_data {
      	 */
      	unsigned is_remote:1;
     -};
    +-
     -#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }

     -static const char *remote_object_info_atoms[] = {
     -	"objectname",
     -	"objectsize",
    ++	/*
    ++	 * List of atoms (i.e. "objectsize") that the server supports. Built
    ++	 * from the server's object-info advertised capabilities.
    ++	 */
     +	struct string_list remote_allowed_atoms;
      };
    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

    ++#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    ++			    .type = OBJ_BAD, \
    ++			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++
Quoted 8 lines
Quoted 3 lines

I guess this is output of running the style check script (which I did recommend, but I should've mentioned that you should take it with a pinch of salt, it doesn't always provide the best solutions.)

It comes in part from Junio's suggestion [1]. Although I don't think that it will receive a lot of new fields, I thought that it would leave the macro clean for that.

Then wouldn't something like this be better?

     #define EXPAND_DATA_INIT  {  \
             .mode = S_IFINVALID, \
             .type = OBJ_BAD,     \
             .remote_allowed_atoms = STRING_LIST_INIT_NODUP, \
    }

Note the difference:
1. The first line no longer has the '.mode' field, this would ensure that removal of the field is a diff which only contains the field and not movement of other fields to the top line.
2. Similar for the last time.
3. We also add a comma for the last field, this allows addition of new fields without modifying previous lines by adding a missing comma.

I don't think this too warrants a re-roll, but this is something to definitely fix incase you do re-roll for other reasons :)

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines
      static int is_atom(const char *atom, const char *s, int slen)
      {
    + 	int alen = strlen(atom);
     @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom, int len,
      		       struct expand_data *data)
      {
    @@ builtin/cat-file.c: static int expand_atom(struct strbuf *sb, const char *atom,
     -			if (is_atom(remote_object_info_atoms[i], atom, len))
     +		size_t i;
     +		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
    -+			if (is_atom(data->remote_allowed_atoms.items[i].string, atom, len))
    ++			if (is_atom(data->remote_allowed_atoms.items[i].string,
    ++				    atom, len))
      				break;
     -
     -		/*
Quoted 4 lines
Quoted 2 lines

---
base-commit: 44de1520f08d1dfebc3ab2d9f644208eaa5ac925

I reviewed v18 last, but the diff here looks good to me. Thanks!

[1]: https://lore.kernel.org/git/[email protected]/

Thanks,
Pablo

#381 lore raw
Quoted 18 lines · Karthik Nayak
Quoted 16 lines · Pablo Sabater
Quoted 15 lines · Karthik Nayak

Pablo Sabater <[email protected]> writes:

[snip]

Quoted 7 lines

Changes in v20:
- Moved the prep patch that fixes hash_algo's type to be before write_fetch_command_and_capabilities() being moved to 'connect.c'
- Reverted git-cat-file.adoc documentation comments about CAVEATS
- Fixed style for EXPAND_DATA_INIT
- Added more context for comman line die()

[snip]

Quoted 1 line

Range-diff versus v19:

[snip]

Quoted 33 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- reports).
-
- objectsize:disk::
-- The size, in bytes, that the object takes up on disk. See the
-- note about on-disk sizes in the CAVEATS section below.
-+ The size, in bytes, that the object takes up on disk.
-
deltabase::
If the object is stored as a delta on-disk, this expands to the full hex representation of the delta base object name.
- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-- below.
-+ Otherwise, expands to the null OID (all zeroes).
++ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
+ below.

Quoted 7 lines
Quoted 1 line

This chagne is still unnecessary, no?

I guess it is unnecessary. I added it because on top, at objectsize:disk it says "CAVEATS section". So I tried to have it consistently.

Is it worth a reroll?

No I don't think a re-roll is necessary. But something to avoid next time :)

[snip]

Quoted 51 lines
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines

13: ebdfc22fb7 ! 13: 70a11d2aea cat-file: make remote-object-info allow-list adapt to the server

    @@ builtin/cat-file.c: struct expand_data {
      	 */
      	unsigned is_remote:1;
     -};
    +-
     -#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }

     -static const char *remote_object_info_atoms[] = {
     -	"objectname",
     -	"objectsize",
    ++	/*
    ++	 * List of atoms (i.e. "objectsize") that the server supports. Built
    ++	 * from the server's object-info advertised capabilities.
    ++	 */
     +	struct string_list remote_allowed_atoms;
      };
    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }

    ++#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    ++			    .type = OBJ_BAD, \
    ++			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++
Quoted 8 lines
Quoted 3 lines

I guess this is output of running the style check script (which I did recommend, but I should've mentioned that you should take it with a pinch of salt, it doesn't always provide the best solutions.)

It comes in part from Junio's suggestion [1]. Although I don't think that it will receive a lot of new fields, I thought that it would leave the macro clean for that.

Then wouldn't something like this be better?

     #define EXPAND_DATA_INIT  {  \
             .mode = S_IFINVALID, \
             .type = OBJ_BAD,     \
             .remote_allowed_atoms = STRING_LIST_INIT_NODUP, \
    }

Note the difference:
1. The first line no longer has the '.mode' field, this would ensure that removal of the field is a diff which only contains the field and not movement of other fields to the top line.
2. Similar for the last time.
3. We also add a comma for the last field, this allows addition of new fields without modifying previous lines by adding a missing comma.

Quoted 2 lines

I don't think this too warrants a re-roll, but this is something to definitely fix incase you do re-roll for other reasons :)

Yeah, this one is better formatted than what I did. Thanks for the explanation. I'll definetely fix this two hunks if I have to reroll.

[snip]

Thanks,
Pablo

#382 lore raw

v21 cover cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

This patch series is a continuation of Eric Ju's ([email protected]) and Calvin Wan's ([email protected]) patch series [1] and [2] respectively.

Sometimes it is beneficial to retrieve information about an object without having to download it completely. The server logic for retrieving size has already been implemented and merged in a2ba162cda (object-info: support for retrieving object info, 2021-04-20) [3]. This patch series implements the client option for it.

Eric's series adds the remote-object-info command to cat-file --batch-command. This command allows the client to make an object-info command request to a server that supports protocol v2.

If the server uses protocol v2 but does not support the object-info capability, cat-file --batch-command will die.
If a user attempts to use remote-object-info with protocol v1, cat-file --batch-command will die.

Currently, only the size (%(objectsize)) is supported end to end in this implementation. The type (%(objecttype)) is known by the client's allow-list and request path but is not supported on the server side nor the response parsing. A follow up series will add full end-to-end support for %(objecttype).

The default format for remote-object-info is set to "%(objectname) %(objectsize)". Once %(objecttype) is supported, the default format will be unified accordingly.

If the batch command format includes unsupported fields such as %(objecttype), %(objectsize:disk), or %(deltabase), the command will return empty strings for each unsupported field.

This series completes Eric's work mainly with the refactor of the validation of the placeholders with an allow-list that filters what the client asks with what the server is capable of providing, following Jeff King's idea [4].

GitHub CI: https://github.com/pabloosabaterr/git/actions/runs/30086202054

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/#t
[3]: https://git.kernel.org/pub/scm/git/git.git/commit/?id=a2ba162cda2acc171c3e36acbbc854792b093cb7
[4]: https://lore.kernel.org/git/[email protected]/

Changes in v21:
- Dropped unnecessary change in git-cat-file.adoc
- Fixed EXPAND_DATA_INIT macro formating
- typo and blank line before WARNING:

---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info

Eric Ju (3):
cat-file: declare loop counter inside for()
t1006: extract helper functions into new 'lib-cat-file.sh'
cat-file: add remote-object-info to batch-command

Pablo Sabater (7):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: drop the static advertise_sid variable
fetch-pack: use unsigned int for hash_algo variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
protocol-caps: check object existence regardless of the attributes requested
cat-file: make remote-object-info allow-list adapt to the server

 Documentation/git-cat-file.adoc        |  23 +-
 Documentation/gitprotocol-v2.adoc      |  21 +-
 Makefile                               |   1 +
 builtin/cat-file.c                     | 224 +++++++++-
 connect.c                              |  34 ++
 connect.h                              |   8 +
 fetch-object-info.c                    | 156 +++++++
 fetch-object-info.h                    |  25 ++
 fetch-pack.c                           |  58 +--
 meson.build                            |   1 +
 object-file.c                          |  10 +
 odb.h                                  |   9 +
 protocol-caps.c                        |  45 +-
 serve.c                                |   5 +-
 t/lib-cat-file.sh                      |  16 +
 t/meson.build                          |   1 +
 t/t1006-cat-file.sh                    |  15 +-
 t/t1017-cat-file-remote-object-info.sh | 747 +++++++++++++++++++++++++++++++++
 t/t5701-git-serve.sh                   |  63 +++
 transport-helper.c                     |  12 +-
 transport-internal.h                   |   8 +
 transport.c                            |  44 ++
 transport.h                            |   9 +
 23 files changed, 1444 insertions(+), 91 deletions(-)

Range-diff versus v20:

 1:  ddc6386af3 =  1:  6c868e0e29 transport-helper: fix memory leak of helper on disconnect
 2:  24e0c07dde =  2:  40579c0095 cat-file: declare loop counter inside for()
 3:  08882a9ddd =  3:  b70d1f91c4 t1006: extract helper functions into new 'lib-cat-file.sh'
 4:  0925c208c1 =  4:  5c5050d541 fetch-pack: drop the static advertise_sid variable
 5:  f130092dd9 =  5:  29c321ad3e fetch-pack: use unsigned int for hash_algo variable
 6:  37326255e9 =  6:  bdcd407a22 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
 7:  069b9d4def =  7:  970d59e2cf connect: make write_fetch_command_and_capabilities() more generic
 8:  4225d3087c =  8:  86c64d10d7 fetch-pack: move fetch initialization
 9:  af3b2bb293 =  9:  7007808819 protocol-caps: check object existence regardless of the attributes requested
10:  99df8374df = 10:  2f5acaa540 serve: advertise object-info feature
11:  380bca2a43 = 11:  abb9306254 transport: add client support for object-info
12:  eb48e94f6f ! 12:  36fe34a596 cat-file: add remote-object-info to batch-command

@@ Documentation/git-cat-file.adoc: one per line, and print information based on th
contents command followed by an object prints contents in the same way
---batch would.
+--batch would. The remote-object-info command followed by a remote and
-+objects IDs prints object info from the remote without downloading the objects. ++object IDs prints object info from the remote without downloading the objects.

You can specify the information shown for each object by using a custom <format>. The <format> is copied literally to stdout for each
-@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
- deltabase::
- If the object is stored as a delta on-disk, this expands to the
- full hex representation of the delta base object name.
-- Otherwise, expands to the null OID (all zeroes). See CAVEATS
-+ Otherwise, expands to the null OID (all zeroes). See CAVEATS section
- below.
-
- rest::
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
after that first run of whitespace (i.e., the "rest" of the line) are output in place of the %(rest) atom.
@@ Documentation/git-cat-file.adoc: newline. The available atoms are:
If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize), except for remote-object-info` commands which
-+use %(objectname) %(objectsize) because "%(objecttype)" is not supported yet. ++use %(objectname) %(objectsize) because %(objecttype) is not supported yet. ++
+WARNING: When "%(objecttype)" is supported, the default format WILL be unified, +so DO NOT RELY on the current default format to stay the same!!!

@@ builtin/cat-file.c: static const struct parse_cmd {
static void batch_objects_command(struct batch_options *opt,

## object-file.c ##
-@@ object-file.c: struct odb_transaction *odb_transaction_files_begin(struct odb_source *source) +@@ object-file.c: int odb_transaction_files_begin(struct odb_source *source,

- return &transaction->base;
+ return 0;
}
+
+void free_object_info_contents(struct object_info *object_info)
13: 75b1c61c67 ! 13: 71e19e8d27 cat-file: make remote-object-info allow-list adapt to the server

    @@ builtin/cat-file.c: struct expand_data {
     +	struct string_list remote_allowed_atoms;
      };

    -+#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, \
    -+			    .type = OBJ_BAD, \
    -+			    .remote_allowed_atoms = STRING_LIST_INIT_NODUP }
    ++#define EXPAND_DATA_INIT  {  \
    ++	.mode = S_IFINVALID, \
    ++	.type = OBJ_BAD,     \
    ++	.remote_allowed_atoms = STRING_LIST_INIT_NODUP, \
    ++}
     +
      static int is_atom(const char *atom, const char *s, int slen)
      {

---
base-commit: 9a0c4701dcd5725c4184599322b52933ff5005ca
change-id: 20260608-ps-eric-work-rebase-b73ae84ba671

#383 lore raw

PATCH v21 1/13 transport-helper: fix memory leak of helper on disconnect

disconnect_helper() only frees data inside of the if(data->helper) block [1]. When the transport is disconnected without the helper being fully started, data->name allocated in transport_helper_init()
is never freed.

Move FREE_AND_NULL(data->name) outside the conditional block so it's always freed on disconnect.

[1]: https://lore.kernel.org/git/05fbadbae2184479c87c37675dde7bd79b3e32ab.1716465556.git.ps@pks.im/

---
 transport-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
transport-helper.c +1 -1
index 80f90eb7ba..f195070788 100644
@@ -266,9 +266,9 @@ static int disconnect_helper(struct transport *transport)
 		close(data->helper->out);
 		fclose(data->out);
 		res = finish_command(data->helper);
-		FREE_AND_NULL(data->name);
 		FREE_AND_NULL(data->helper);
 	}
+	FREE_AND_NULL(data->name);
 	return res;
 }
 
Signature · 1 line
2.54.0
#384 lore raw

PATCH v21 2/13 cat-file: declare loop counter inside for()

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Declare loop counters in the for statement when they are only used within the loop body, limiting their scope and improving readability.

While updating the loop counters, use size_t instead of int for counters that iterate over object counts.

Update the 'nr' parameter of dispatch_calls() to size_t as all callers already pass a value of that type.

---
 builtin/cat-file.c | 13 ++++---------
 fetch-pack.c       |  3 +--
 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
index 1458dd76d6..fc18a92736 100644
@@ -720,14 +720,12 @@ static void dispatch_calls(struct batch_options *opt,
 		struct strbuf *output,
 		struct expand_data *data,
 		struct queued_cmd *cmd,
-		int nr)
+		size_t nr)
 {
-	int i;
-
 	if (!opt->buffer_output)
 		die(_("flush is only for --buffer mode"));
 
-	for (i = 0; i < nr; i++)
+	for (size_t i = 0; i < nr; i++)
 		cmd[i].fn(opt, cmd[i].line, output, data);
 
 	fflush(stdout);
@@ -735,9 +733,7 @@ static void dispatch_calls(struct batch_options *opt,
 
 static void free_cmds(struct queued_cmd *cmd, size_t *nr)
 {
-	size_t i;
-
-	for (i = 0; i < *nr; i++)
+	for (size_t i = 0; i < *nr; i++)
 		FREE_AND_NULL(cmd[i].line);
 
 	*nr = 0;
@@ -764,7 +760,6 @@ static void batch_objects_command(struct batch_options *opt,
 	size_t alloc = 0, nr = 0;
 
 	while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
-		int i;
 		const struct parse_cmd *cmd = NULL;
 		const char *p = NULL, *cmd_end;
 		struct queued_cmd call = {0};
@@ -774,7 +769,7 @@ static void batch_objects_command(struct batch_options *opt,
 		if (isspace(*input.buf))
 			die(_("whitespace before command: '%s'"), input.buf);
 
-		for (i = 0; i < ARRAY_SIZE(commands); i++) {
+		for (size_t i = 0; i < ARRAY_SIZE(commands); i++) {
 			if (!skip_prefix(input.buf, commands[i].name, &cmd_end))
 				continue;
 
fetch-pack.c +1 -2
index 29c41132ee..9eb8fc5399 100644
@@ -1388,9 +1388,8 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	if (advertise_sid && server_supports_v2("session-id"))
 		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
 	if (server_options && server_options->nr) {
-		int i;
 		ensure_server_supports_v2("server-option");
-		for (i = 0; i < server_options->nr; i++)
+		for (size_t i = 0; i < server_options->nr; i++)
 			packet_buf_write(req_buf, "server-option=%s",
 					 server_options->items[i].string);
 	}
Signature · 1 line
2.54.0
#385 lore raw

PATCH v21 3/13 t1006: extract helper functions into new 'lib-cat-file.sh'

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Extract utility functions from the cat-file's test script 't1006-cat-file.sh' into a new 'lib-cat-file.sh' dedicated library file.

A subsequent commit will need these functions. This improves the code reuse and readability, enabling future cat-file tests to share these helpers without duplicating code.

While at it update the style of this line to follow coding guidelines:

. "$TEST_DIRECTORY/lib-loose.sh"

to

. "$TEST_DIRECTORY"/lib-loose.sh

---
 t/lib-cat-file.sh   | 16 ++++++++++++++++
 t/t1006-cat-file.sh | 15 ++-------------
 2 files changed, 18 insertions(+), 13 deletions(-)
t/lib-cat-file.sh new file +16 -0
new file mode 100644
index 0000000000..7c2e877016
@@ -0,0 +1,16 @@
+# Library of git-cat-file related test functions.
+
+# Print a string without a trailing newline.
+echo_without_newline () {
+	printf '%s' "$*"
+}
+
+# Print a string without newlines and replace them with a NUL character (\0).
+echo_without_newline_nul () {
+	echo_without_newline "$@" | tr '\n' '\0'
+}
+
+# Calculate the length of a string.
+strlen () {
+	echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
t/t1006-cat-file.sh +2 -13
index 762c77c351..0c1dc0fb40 100755
@@ -3,7 +3,8 @@
 test_description='git cat-file'
 
 . ./test-lib.sh
-. "$TEST_DIRECTORY/lib-loose.sh"
+. "$TEST_DIRECTORY"/lib-loose.sh
+. "$TEST_DIRECTORY"/lib-cat-file.sh
 
 test_cmdmode_usage () {
 	test_expect_code 129 "$@" 2>err &&
@@ -99,18 +100,6 @@ do
 	'
 done
 
-echo_without_newline () {
-    printf '%s' "$*"
-}
-
-echo_without_newline_nul () {
-	echo_without_newline "$@" | tr '\n' '\0'
-}
-
-strlen () {
-    echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
-}
-
 run_tests () {
     type=$1
     object_name="$2"
Signature · 1 line
2.54.0
#386 lore raw

PATCH v21 4/13 fetch-pack: drop the static advertise_sid variable

In reply to Pablo Sabater

write_fetch_command_and_capabilities() is moved to 'connect.c' in a subsequent commit. To prepare for that, drop the static variable usage of advertise_sid.

Currently advertise_sid is set in fetch_pack_config() by reading "transfer.advertisesid". It is used in three places:

1. In do_fetch_pack(), to clear it when the server lacks support:

        if (!server_supports("session-id"))
               advertise_sid = 0;

2. In find_common(), to advertise the session id over protocol v0/v1:

        if (advertise_sid)
                strbuf_addf(&c, " session-id=%s", trace2_session_id());

3. In write_fetch_command_and_capabilities(), to advertise it over protocol v2:

        if (advertise_sid && server_supports_v2("session-id"))
                packet_buf_write(req_buf, "session-id=%s", trace2_session_id());

About 1, the check only guards the v0/v1 path, and the v2 path already checks server support inline in its condition. Follow the same pattern and fold the check into the condition in find_common().

About 2 and 3, replace the static variable with a local read via repo_config_get_bool() in each function.

Because repo_config_get_bool() leaves advertise_sid as is if it is not set, initialize it to 0, matching its default.

---
 fetch-pack.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fetch-pack.c +7 -6
index 9eb8fc5399..65ebfec09f 100644
@@ -49,7 +49,6 @@ static int fetch_fsck_objects = -1;
 static int transfer_fsck_objects = -1;
 static int agent_supported;
 static int server_supports_filtering;
-static int advertise_sid;
 static struct shallow_lock shallow_lock;
 static const char *alternate_shallow_file;
 static struct strbuf fsck_msg_types = STRBUF_INIT;
@@ -363,6 +362,9 @@ static int find_common(struct fetch_negotiator *negotiator,
 	size_t state_len = 0;
 	struct packet_reader reader;
 	struct oidset negotiation_include_oids = OIDSET_INIT;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	if (args->stateless_rpc && multi_ack == 1)
 		die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
@@ -414,7 +416,7 @@ static int find_common(struct fetch_negotiator *negotiator,
 			if (deepen_not_ok)      strbuf_addstr(&c, " deepen-not");
 			if (agent_supported)    strbuf_addf(&c, " agent=%s",
 							    git_user_agent_sanitized());
-			if (advertise_sid)
+			if (advertise_sid && server_supports("session-id"))
 				strbuf_addf(&c, " session-id=%s", trace2_session_id());
 			if (args->filter_options.choice)
 				strbuf_addstr(&c, " filter");
@@ -1160,9 +1162,6 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
 				      (int)agent_len, agent_feature);
 	}
 
-	if (!server_supports("session-id"))
-		advertise_sid = 0;
-
 	if (server_supports("shallow"))
 		print_verbose(args, _("Server supports %s"), "shallow");
 	else if (args->depth > 0 || is_repository_shallow(r))
@@ -1380,6 +1379,9 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 						 const struct string_list *server_options)
 {
 	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
 	ensure_server_supports_v2("fetch");
 	packet_buf_write(req_buf, "command=fetch");
@@ -1998,7 +2000,6 @@ static void fetch_pack_config(void)
 	repo_config_get_bool(the_repository, "repack.usedeltabaseoffset", &prefer_ofs_delta);
 	repo_config_get_bool(the_repository, "fetch.fsckobjects", &fetch_fsck_objects);
 	repo_config_get_bool(the_repository, "transfer.fsckobjects", &transfer_fsck_objects);
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 	if (!uri_protocols.nr) {
 		char *str;
 
Signature · 1 line
2.54.0
#387 lore raw

PATCH v21 5/13 fetch-pack: use unsigned int for hash_algo variable

In reply to Pablo Sabater

hash_algo_by_name() returns unsigned int, but it is stored in hash_algo variable as int. This goes unnoticed because of:

DISABLE_SIGN_COMPARE_WARNINGS

On 'fetch-pack.c'

On a subsequent commit this function will be moved to 'connect.c' that would notice this.

Change hash_algo variable type to match its return type, also make it const because they are never modified.

---
 fetch-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
index 65ebfec09f..f1e64160fc 100644
@@ -1397,7 +1397,7 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
 	}
 
 	if (server_feature_v2("object-format", &hash_name)) {
-		int hash_algo = hash_algo_by_name(hash_name);
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
 		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
 			die(_("mismatched algorithms: client %s; server %s"),
 			    the_hash_algo->name, hash_name);
Signature · 1 line
2.54.0
#388 lore raw

PATCH v21 6/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c

In reply to Pablo Sabater

In a subsequent commit write_fetch_command_and_capabilities() will be refactored to a more general-purpose function, making it more accessible to additional commands in the future.

Move write_fetch_command_and_capabilities() to 'connect.c', where there are similar purpose functions.

Because string_list is only used as a pointer, use a forward declaration [1].

[1]: https://lore.kernel.org/git/[email protected]/

---
 connect.c    | 34 ++++++++++++++++++++++++++++++++++
 connect.h    |  4 ++++
 fetch-pack.c | 34 ----------------------------------
 3 files changed, 38 insertions(+), 34 deletions(-)
connect.c +34 -0
index 1d74c1eda2..0d197eca84 100644
@@ -709,6 +709,40 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options)
+{
+	const char *hash_name;
+	int advertise_sid = 0;
+
+	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
+
+	ensure_server_supports_v2("fetch");
+	packet_buf_write(req_buf, "command=fetch");
+	if (server_supports_v2("agent"))
+		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
+	if (advertise_sid && server_supports_v2("session-id"))
+		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+	if (server_options && server_options->nr) {
+		ensure_server_supports_v2("server-option");
+		for (size_t i = 0; i < server_options->nr; i++)
+			packet_buf_write(req_buf, "server-option=%s",
+					 server_options->items[i].string);
+	}
+
+	if (server_feature_v2("object-format", &hash_name)) {
+		const unsigned int hash_algo = hash_algo_by_name(hash_name);
+		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
+			die(_("mismatched algorithms: client %s; server %s"),
+			    the_hash_algo->name, hash_name);
+		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
+	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
+		die(_("the server does not support algorithm '%s'"),
+		    the_hash_algo->name);
+	}
+	packet_buf_delim(req_buf);
+}
+
 static const char *url_scheme_name(enum url_scheme scheme)
 {
 	switch (scheme) {
connect.h +4 -0
index aa482a37fb..c4f6ea4b0a 100644
@@ -34,4 +34,8 @@ void check_stateless_delimiter(int stateless_rpc,
 			       struct packet_reader *reader,
 			       const char *error);
 
+struct string_list;
+void write_fetch_command_and_capabilities(struct strbuf *req_buf,
+					  const struct string_list *server_options);
+
 #endif
fetch-pack.c +0 -34
index f1e64160fc..f7789e8456 100644
@@ -1375,40 +1375,6 @@ static int add_haves(struct fetch_negotiator *negotiator,
 	return haves_added;
 }
 
-static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-						 const struct string_list *server_options)
-{
-	const char *hash_name;
-	int advertise_sid = 0;
-
-	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
-
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
-	if (server_supports_v2("agent"))
-		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
-	if (advertise_sid && server_supports_v2("session-id"))
-		packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
-	if (server_options && server_options->nr) {
-		ensure_server_supports_v2("server-option");
-		for (size_t i = 0; i < server_options->nr; i++)
-			packet_buf_write(req_buf, "server-option=%s",
-					 server_options->items[i].string);
-	}
-
-	if (server_feature_v2("object-format", &hash_name)) {
-		const unsigned int hash_algo = hash_algo_by_name(hash_name);
-		if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
-			die(_("mismatched algorithms: client %s; server %s"),
-			    the_hash_algo->name, hash_name);
-		packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
-	} else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1_LEGACY) {
-		die(_("the server does not support algorithm '%s'"),
-		    the_hash_algo->name);
-	}
-	packet_buf_delim(req_buf);
-}
-
 static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 			      struct fetch_pack_args *args,
 			      const struct ref *wants, struct oidset *common,
Signature · 1 line
2.54.0
#389 lore raw

PATCH v21 7/13 connect: make write_fetch_command_and_capabilities() more generic

In reply to Pablo Sabater

Refactor write_fetch_command_and_capabilities(), enabling it to serve both fetch and additional commands.

In this context, "command" refers to the "operations" supported by Git's wire protocol Documentation/gitprotocol-v2.adoc, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162cda (object-info: support for retrieving object info, 2021-04-20).

Refactor the function signature to accept a command instead of the hardcoded "fetch".

---
 connect.c    | 8 ++++----
 connect.h    | 8 ++++++--
 fetch-pack.c | 4 ++--
 3 files changed, 12 insertions(+), 8 deletions(-)
connect.c +4 -4
index 0d197eca84..feb0420b0f 100644
@@ -709,16 +709,16 @@ int server_supports(const char *feature)
 	return !!server_feature_value(feature, NULL);
 }
 
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options)
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options)
 {
 	const char *hash_name;
 	int advertise_sid = 0;
 
 	repo_config_get_bool(the_repository, "transfer.advertisesid", &advertise_sid);
 
-	ensure_server_supports_v2("fetch");
-	packet_buf_write(req_buf, "command=fetch");
+	ensure_server_supports_v2(command);
+	packet_buf_write(req_buf, "command=%s", command);
 	if (server_supports_v2("agent"))
 		packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
 	if (advertise_sid && server_supports_v2("session-id"))
connect.h +6 -2
index c4f6ea4b0a..957e5fe2b9 100644
@@ -35,7 +35,11 @@ void check_stateless_delimiter(int stateless_rpc,
 			       const char *error);
 
 struct string_list;
-void write_fetch_command_and_capabilities(struct strbuf *req_buf,
-					  const struct string_list *server_options);
+/*
+ * Write a protocol v2 command request, along with the capability
+ * advertisements, into req_buf.
+ */
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+				    const struct string_list *server_options);
 
 #endif
fetch-pack.c +2 -2
index f7789e8456..3695059cd5 100644
@@ -1386,7 +1386,7 @@ static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
 	int done_sent = 0;
 	struct strbuf req_buf = STRBUF_INIT;
 
-	write_fetch_command_and_capabilities(&req_buf, args->server_options);
+	write_command_and_capabilities(&req_buf, "fetch", args->server_options);
 
 	if (args->use_thin_pack)
 		packet_buf_write(&req_buf, "thin-pack");
@@ -2253,7 +2253,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_restrict_tips,
 					   the_repository, "%d",
 					   negotiation_round);
 		strbuf_reset(&req_buf);
-		write_fetch_command_and_capabilities(&req_buf, server_options);
+		write_command_and_capabilities(&req_buf, "fetch", server_options);
 
 		packet_buf_write(&req_buf, "wait-for-done");
 
Signature · 1 line
2.54.0
#390 lore raw

PATCH v21 8/13 fetch-pack: move fetch initialization

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

There are some variables initialized at the start of the do_fetch_pack_v2() state machine. Currently, they are initialized in FETCH_CHECK_LOCAL, which is the initial state set at the beginning of the function.

However, a subsequent patch will allow for another initial state, while still requiring these initialized variables.
Move the initialization to be before the state machine, so that they are set regardless of the initial state.

Note that there is no change in behavior, because we're moving code from the beginning of the first state to just before the execution of the state machine.

---
 fetch-pack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
fetch-pack.c +6 -6
index 3695059cd5..922a9b2581 100644
@@ -1735,18 +1735,18 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
 		reader.me = "fetch-pack";
 	}
 
+	/* v2 supports these by default */
+	allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
+	use_sideband = 2;
+	if (args->depth > 0 || args->deepen_since || args->deepen_not)
+		args->deepen = 1;
+
 	while (state != FETCH_DONE) {
 		switch (state) {
 		case FETCH_CHECK_LOCAL:
 			sort_ref_list(&ref, ref_compare_name);
 			QSORT(sought, nr_sought, cmp_ref_by_name);
 
-			/* v2 supports these by default */
-			allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
-			use_sideband = 2;
-			if (args->depth > 0 || args->deepen_since || args->deepen_not)
-				args->deepen = 1;
-
 			/* Filter 'ref' by 'sought' and those that aren't local */
 			mark_complete_and_common_ref(negotiator, args, &ref);
 			filter_refs(args, &ref, sought, nr_sought);
Signature · 1 line
2.54.0
#391 lore raw

PATCH v21 9/13 protocol-caps: check object existence regardless of the attributes requested

In reply to Pablo Sabater

Currently, send_info() only checks for existence when the attribute 'size' is also requested. Requesting a bare OID, without attributes only echoes back the OID.

Extract the existence check to be done regardless of the number of attributes requested.

While at it, introduce a wrapper called get_object_info() similar to odb_read_object_info() that returns OBJ_BAD on fail and adds OBJECT_INFO_SKIP_FETCH_OBJECT and OBJECT_INFO_QUICK flags. OBJECT_INFO_SKIP_FETCH_OBJECT is so a server with a partial clone doesn't trigger fetching objects when it gets an object-info request with an OID that is not available locally. A server should only report what it has locally.

Tighten the condition used to determine whether an object is recognized. get_object_info() returns OBJ_BAD for unknown objects, but OBJ_NONE (0) can also mean "not found". Change the check from '< 0' to '<= OBJ_NONE' to cover both as unrecognized.

With this patch, a bare OID has two possible responses:

1. Recognized OID: the server answers with "<OID>"

2. Unrecognized OID: the server answers with "<OID> SP"

Update the object-info section in 'gitprotocol-v2.adoc':
- Require full obj-oid explicitly.
- Fix parentheses.
- Define obj-size explicitly.
- Make obj-size optional in obj-info and document the behavior for unrecognized object IDs.
- Describe the attr header as zero or more pkt-lines, one per attribute, matching what the server implements. A request with no attributes gets no header.

---
 Documentation/gitprotocol-v2.adoc | 21 ++++++++-----
 protocol-caps.c                   | 45 ++++++++++++++++++++++++----
 t/t5701-git-serve.sh              | 63 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 14 deletions(-)
Documentation/gitprotocol-v2.adoc +13 -8
index 2beb70595f..7bf62014c3 100644
@@ -568,21 +568,26 @@ An `object-info` request takes the following arguments:
 
 	oid <oid>
 	Indicates to the server an object which the client wants to obtain
-	information for.
+	information for. They must be full OIDs.
 
-The response of `object-info` is a list of the requested object ids
-and associated requested information, each separated by a single space.
+The response of `object-info` consists of one pkt-line per requested attribute,
+echoing the attributes the server will report, followed by one pkt-line per
+requested object id with its information, each field separated by a single
+space.
 
 	output = info flush-pkt
 
-	info = PKT-LINE(attrs) LF)
-		*PKT-LINE(obj-info LF)
-
-	attrs = attr | attrs SP attrs
+	info = *PKT-LINE(attr LF)
+	       *PKT-LINE(obj-info LF)
 
 	attr = "size"
 
-	obj-info = obj-id SP obj-size
+	obj-size = 1*DIGIT
+
+	obj-info = obj-id [SP [obj-size]]
+
+If the server does not recognize the OID, the response will be `<oid> SP`
+regardless of the number of attributes requested.
 
 bundle-uri
 ~~~~~~~~~~
protocol-caps.c +39 -6

60 lines not on this page · raw message

t/t5701-git-serve.sh +63 -0

75 lines not on this page · raw message

Signature · 1 line
2.54.0
#392 lore raw

PATCH v21 10/13 serve: advertise object-info feature

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

In order for a client to know what object-info components a server can provide, advertise supported object-info features. This allows a client to decide whether to query the server for object-info or fetch as a fallback.

---
 serve.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
serve.c +4 -1
index 49a6e39b1d..2b07d922b3 100644
@@ -89,7 +89,7 @@ static void session_id_receive(struct repository *r UNUSED,
 	trace2_data_string("transfer", NULL, "client-sid", client_sid);
 }
 
-static int object_info_advertise(struct repository *r, struct strbuf *value UNUSED)
+static int object_info_advertise(struct repository *r, struct strbuf *value)
 {
 	if (advertise_object_info == -1 &&
 	    repo_config_get_bool(r, "transfer.advertiseobjectinfo",
@@ -97,6 +97,9 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS
 		/* disabled by default */
 		advertise_object_info = 0;
 	}
+	/* Currently only size is supported */
+	if (value && advertise_object_info)
+		strbuf_addstr(value, "size");
 	return advertise_object_info;
 }
 
Signature · 1 line
2.54.0
#393 lore raw

PATCH v21 11/13 transport: add client support for object-info

In reply to Pablo Sabater

From: Calvin Wan <[email protected]>

Sometimes, it is beneficial to retrieve information about an object without downloading it entirely. The server-side logic for this functionality was implemented in commit "a2ba162cda (object-info:
support for retrieving object info, 2021-04-20)." And the wire format is documented at
https://git-scm.com/docs/protocol-v2#_object_info.

Introduce client-side support for the object-info capability.

Add its own function for object-info separate from existing fetch infrastructure.

Currently, the client supports requesting a list of OIDs with the size attribute from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client returns an error and exits.

Note that:

1. The entire request is written into req_buf before being sent to the remote. This approach follows the pattern used in the send_fetch_request() logic within 'fetch-pack.c'. Streaming the request is not addressed in this patch.

2. A new field 'unrecognized' has been added to object_info. This new field is set at fetch_object_info() when the object is unrecognized by the server.

---
 Makefile             |   1 +
 fetch-object-info.c  | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++
 fetch-object-info.h  |  22 ++++++++
 meson.build          |   1 +
 odb.h                |   6 +++
 transport-helper.c   |  10 ++++
 transport-internal.h |   8 +++
 transport.c          |  45 +++++++++++++++++
 transport.h          |   9 ++++
 9 files changed, 240 insertions(+)
Makefile +1 -0
index 98e995e4be..fac3e8879c 100644
@@ -1169,6 +1169,7 @@ LIB_OBJS += ewah/ewah_io.o
 LIB_OBJS += ewah/ewah_rlw.o
 LIB_OBJS += exec-cmd.o
 LIB_OBJS += fetch-negotiator.o
+LIB_OBJS += fetch-object-info.o
 LIB_OBJS += fetch-pack.o
 LIB_OBJS += fmt-merge-msg.o
 LIB_OBJS += fsck.o
fetch-object-info.c new file +138 -0

138 lines not on this page · raw message

fetch-object-info.h new file +22 -0
new file mode 100644
index 0000000000..31aad98408
@@ -0,0 +1,22 @@
+#ifndef FETCH_OBJECT_INFO_H
+#define FETCH_OBJECT_INFO_H
+
+#include "pkt-line.h"
+#include "protocol.h"
+
+struct object_info_args {
+	struct string_list *object_info_options;
+	const struct string_list *server_options;
+	struct oid_array *oids;
+};
+
+struct object_info;
+/*
+ * Sends git-cat-file object-info command into the request buf and read the
+ * results from packets.
+ */
+int fetch_object_info(enum protocol_version version, struct object_info_args *args,
+		      struct packet_reader *reader, struct object_info *object_info_data,
+		      int stateless_rpc, int fd_out);
+
+#endif /* FETCH_OBJECT_INFO_H */
meson.build +1 -0
index f7c40ea079..7073d5844d 100644
@@ -374,6 +374,7 @@ libgit_sources = [
   'ewah/ewah_rlw.c',
   'exec-cmd.c',
   'fetch-negotiator.c',
+  'fetch-object-info.c',
   'fetch-pack.c',
   'fmt-merge-msg.c',
   'fsck.c',
odb.h +6 -0
index 7995bed97b..b7bc0ee844 100644
@@ -340,6 +340,12 @@ struct object_info {
 	 * or multiple times in the same source.
 	 */
 	struct odb_source_info *source_infop;
+
+	/*
+	 * object-info protocol specific. Set by the protocol when the remote
+	 * does not recognize the requested object.
+	 */
+	unsigned int unrecognized:1;
 };
 
 /*
transport-helper.c +10 -0
index f195070788..623463dcea 100644
@@ -784,6 +784,15 @@ static int fetch_refs(struct transport *transport,
 	return -1;
 }
 
+static int fetch_object_info_helper(struct transport *transport)
+{
+	get_helper(transport);
+	if (process_connect(transport, 0))
+		return transport->vtable->fetch_object_info(transport);
+
+	die(_("object-info requires protocol v2"));
+}
+
 struct push_update_ref_state {
 	struct ref *hint;
 	struct ref_push_report *report;
@@ -1330,6 +1339,7 @@ static struct transport_vtable vtable = {
 	.get_refs_list	= get_refs_list,
 	.get_bundle_uri = get_bundle_uri,
 	.fetch_refs	= fetch_refs,
+	.fetch_object_info = fetch_object_info_helper,
 	.push_refs	= push_refs,
 	.connect	= connect_helper,
 	.disconnect	= release_helper
transport-internal.h +8 -0
index 051f3ab0dc..60db0bedcd 100644
@@ -45,6 +45,14 @@ struct transport_vtable {
 	 **/
 	int (*fetch_refs)(struct transport *transport, int refs_nr, struct ref **refs);
 
+	/*
+	 * Fetch object info (only size currently) from remote without
+	 * downloading the objects.
+	 *
+	 * Uses object-info capability of v2 protocol.
+	 */
+	int (*fetch_object_info)(struct transport *transport);
+
 	/**
 	 * Push the objects and refs. Send the necessary objects, and
 	 * then, for any refs where peer_ref is set and
transport.c +45 -0

69 lines not on this page · raw message

transport.h +9 -0
index 7e5867cffa..a7869d18e0 100644
@@ -55,6 +55,10 @@ struct git_transport_options {
 	 * common commits to this oidset instead of fetching any packfiles.
 	 */
 	struct oidset *acked_commits;
+
+	struct oid_array *object_info_oids;
+	struct object_info *object_info_data;
+	struct string_list *object_info_options;
 };
 
 enum transport_family {
@@ -309,6 +313,11 @@ int transport_get_remote_bundle_uri(struct transport *transport);
 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport);
 int transport_fetch_refs(struct transport *transport, struct ref *refs);
 
+/*
+ * Fetch the object info from remote
+ */
+int transport_fetch_object_info(struct transport *transport);
+
 /*
  * If this flag is set, unlocking will avoid to call non-async-signal-safe
  * functions. This will necessarily leave behind some data structures which
Signature · 1 line
2.54.0
#394 lore raw

PATCH v21 12/13 cat-file: add remote-object-info to batch-command

In reply to Pablo Sabater

From: Eric Ju <[email protected]>

Since the info command in cat-file --batch-command prints object info for a given object, it is natural to add another command in cat-file --batch-command to print object info for a given object from a remote.

Add remote-object-info command to cat-file --batch-command.

While info takes object ids one at a time, this creates overhead when making requests to a server. So remote-object-info instead can take multiple object ids at once.

The cat-file --batch-command command is generally implemented in the following manner:

- Receive and parse input from user
- Call respective function attached to command
- Get object info, print object info

In --buffer mode, this changes to:

- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue
- Call respective function attached to command
- Get object info, print object info

Notice how the getting and printing of object info is accomplished one at a time. As described above, this creates a problem for making requests to a server. Therefore, remote-object-info is implemented in the following manner:

- Receive and parse input from user
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Parse input, get object info, print object info

And finally for --buffer mode remote-object-info:
- Receive and parse input from user
- Store respective function attached to command in a queue
- After flush, loop through commands in queue:
If command is remote-object-info:
- Get object info from remote
- Loop through and print each object info
Else:
- Call respective function attached to command
- Get object info, print object info

To summarize, remote-object-info gets object info from the remote and then loops through the object info passed in, printing the info.

In order for remote-object-info to avoid remote communication overhead in the non-buffer mode, the objects are passed in as such:

remote-object-info <remote> <oid> <oid> ... <oid>

rather than

remote-object-info <remote> <oid>
remote-object-info <remote> <oid>
...
remote-object-info <remote> <oid>

Placeholders in the format are validated against an allow-list of the atoms the remote path supports: "objectname" and "objectsize". Unsupported atoms expand to an empty string, honoring how for-each-ref handles known but inapplicable atoms.
Without this, atoms like %(objecttype) would mark data->info.typep and because the server only sends size, type_name() would later crash. As extra safety, even outside of the remote path, initialize expand_data's type to OBJ_BAD and handle type_name() returning NULL.

---
 Documentation/git-cat-file.adoc        |  23 +-
 builtin/cat-file.c                     | 181 ++++++++-
 object-file.c                          |  10 +
 odb.h                                  |   3 +
 t/meson.build                          |   1 +
 t/t1017-cat-file-remote-object-info.sh | 719 +++++++++++++++++++++++++++++++++
 6 files changed, 930 insertions(+), 7 deletions(-)
Documentation/git-cat-file.adoc +21 -2
index 86b9181599..ac3b528c6f 100644
@@ -169,6 +169,13 @@ info <object>::
 	Print object info for object reference `<object>`. This corresponds to the
 	output of `--batch-check`.
 
+remote-object-info <remote> <object>...::
+	Print object info for object references `<object>` at specified
+	`<remote>` without downloading objects from the remote.
+	Raise an error when the `object-info` capability is not supported by the remote.
+	Raise an error when no object references are provided.
+	This command may be combined with `--buffer`.
+
 flush::
 	Used with `--buffer` to execute all preceding commands that were issued
 	since the beginning or since the last flush was issued. When `--buffer`
@@ -301,7 +308,8 @@ one per line, and print information based on the command given. With
 `--batch-command`, the `info` command followed by an object will print
 information about the object the same way `--batch-check` would, and the
 `contents` command followed by an object prints contents in the same way
-`--batch` would.
+`--batch` would. The `remote-object-info` command followed by a remote and
+object IDs prints object info from the remote without downloading the objects.
 
 You can specify the information shown for each object by using a custom
 `<format>`. The `<format>` is copied literally to stdout for each
@@ -340,8 +348,15 @@ newline. The available atoms are:
 	after that first run of whitespace (i.e., the "rest" of the
 	line) are output in place of the `%(rest)` atom.
 
+The command `remote-object-info` only supports the `%(objectname)` and
+`%(objectsize)` placeholders. See `CAVEATS` below for more information.
+
 If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize)`, except for `remote-object-info` commands which
+use `%(objectname) %(objectsize)` because `%(objecttype)` is not supported yet.
+
+WARNING: When "%(objecttype)" is supported, the default format WILL be unified,
+so DO NOT RELY on the current default format to stay the same!!!
 
 If `--batch` is specified, or if `--batch-command` is used with the `contents`
 command, the object information is followed by the object contents (consisting
@@ -438,6 +453,10 @@ scripting purposes.
 CAVEATS
 -------
 
+Note that only `%(objectname)` and `%(objectsize)` are currently
+supported by the `remote-object-info` command. Using any other placeholder in
+the format string will return an empty string in its position.
+
 Note that the sizes of objects on disk are reported accurately, but care
 should be taken in drawing conclusions about which refs or objects are
 responsible for disk usage. The size of a packed non-delta object may be
builtin/cat-file.c +176 -5

225 lines not on this page · raw message

object-file.c +10 -0
index 7ff2b730ac..c5809db598 100644
@@ -1740,3 +1740,13 @@ int odb_transaction_files_begin(struct odb_source *source,
 
 	return 0;
 }
+
+void free_object_info_contents(struct object_info *object_info)
+{
+	if (!object_info)
+		return;
+	free(object_info->typep);
+	free(object_info->sizep);
+	free(object_info->disk_sizep);
+	free(object_info->delta_base_oid);
+}
odb.h +3 -0
index b7bc0ee844..3f7c483656 100644
@@ -635,4 +635,7 @@ void parse_alternates(const char *string,
 		      const char *relative_base,
 		      struct strvec *out);
 
+/* Free pointers inside of object_info, but not object_info itself */
+void free_object_info_contents(struct object_info *object_info);
+
 #endif /* ODB_H */
t/meson.build +1 -0
index 8ae6ab6c5f..10241e3dcc 100644
@@ -171,6 +171,7 @@ integration_tests = [
   't1014-read-tree-confusing.sh',
   't1015-read-index-unmerged.sh',
   't1016-compatObjectFormat.sh',
+  't1017-cat-file-remote-object-info.sh',
   't1020-subdirectory.sh',
   't1022-read-tree-partial-clone.sh',
   't1050-large.sh',
t/t1017-cat-file-remote-object-info.sh new file +719 -0

719 lines not on this page · raw message

Signature · 1 line
2.54.0
#395 lore raw

PATCH v21 13/13 cat-file: make remote-object-info allow-list adapt to the server

In reply to Pablo Sabater

The static allow-list in expand_atom() is hardcoded to allow only "objectname" and "objectsize" for remote queries. This works because, up to this point, servers will either support object-info with name and size or they do not support them at all.

As object-info gains new capabilities, we cannot expect different servers with different Git versions to have the same object-info capabilities. Therefore, the client needs to adapt its allow-list to what the server advertises.

The client now:

1. Requests the protocol option that the placeholder refers to (i.e. "size" for "%(objectsize)").

2. Drops any requested option that the server does not advertise in fetch_object_info().

3. Maps the remaining advertised options back to their placeholders and populates remote_allowed_atoms.

4. Uses remote_allowed_atoms in expand_atom(), preserving the previous behavior for supported placeholders.

For example, if the client requests "%(objectsize) %(objecttype)" and the server only supports 'size', then the client only requests 'size'. The server returns the size (i.e "42") "%(objectsize)" is expanded normally while "%(objecttype)" expands to an empty string:

	"42 "

Note that the empty string expansion is only for known but unsupported placeholders. "%(objectcolor)" which doesn't exist would die().

This honors what for-each-ref does for known but inapplicable atoms (placeholders).

Move object_info_options out of get_remote_info() so the caller which has data can select what options will be requested instead of requesting always size.

Move batch_object_write() out so output is always produced. If there are no supported attributes, the output is a blank line.

Include "type" in the object_info_options even though the client does not yet know how to parse the server's "type" capability.

As a result, "type" is always filtered out, allowing the tests to verify that known but unsupported placeholders expand to an empty string.

Since the filter removes options by swapping with the last element, the list is no longer kept sorted. Drop the pre-sort in fetch_object_info_via_pack() and use the unsorted string_list lookup for the response header. This has no effect in performance as the list can only be two entries long ('size' and 'type').

---
 builtin/cat-file.c                     | 96 ++++++++++++++++++++++------------
 fetch-object-info.c                    | 20 ++++++-
 fetch-object-info.h                    |  3 ++
 t/t1017-cat-file-remote-object-info.sh | 28 ++++++++++
 transport.c                            |  1 -
 5 files changed, 113 insertions(+), 35 deletions(-)
builtin/cat-file.c +63 -33
index b2a1ad6a0f..884b6d5ad3 100644
@@ -337,15 +337,20 @@ struct expand_data {
 	 * Flags about when an object info is being fetched from remote.
 	 */
 	unsigned is_remote:1;
-};
-
-#define EXPAND_DATA_INIT  { .mode = S_IFINVALID, .type = OBJ_BAD }
 
-static const char *remote_object_info_atoms[] = {
-	"objectname",
-	"objectsize",
+	/*
+	 * List of atoms (i.e. "objectsize") that the server supports. Built
+	 * from the server's object-info advertised capabilities.
+	 */
+	struct string_list remote_allowed_atoms;
 };
 
+#define EXPAND_DATA_INIT  {  \
+	.mode = S_IFINVALID, \
+	.type = OBJ_BAD,     \
+	.remote_allowed_atoms = STRING_LIST_INIT_NODUP, \
+}
+
 static int is_atom(const char *atom, const char *s, int slen)
 {
 	int alen = strlen(atom);
@@ -356,17 +361,12 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
 		       struct expand_data *data)
 {
 	if (data->is_remote) {
-		size_t i, allowed_nr = ARRAY_SIZE(remote_object_info_atoms);
-		for (i = 0; i < allowed_nr; i++)
-			if (is_atom(remote_object_info_atoms[i], atom, len))
+		size_t i;
+		for (i = 0; i < data->remote_allowed_atoms.nr; i++)
+			if (is_atom(data->remote_allowed_atoms.items[i].string,
+				    atom, len))
 				break;
-
-		/*
-		 * On remote, skip unsupported atoms returning an empty sb,
-		 * honoring how for-each-ref handles known but inapplicable
-		 * atoms (e.g. %(tagger)).
-		 */
-		if (i == allowed_nr)
+		if (i == data->remote_allowed_atoms.nr)
 			return 1;
 	}
 
@@ -682,12 +682,12 @@ static void batch_one_object(const char *obj_name,
 static int get_remote_info(int argc,
 			   const char **argv,
 			   struct object_info **remote_object_info,
-			   struct oid_array *object_info_oids)
+			   struct oid_array *object_info_oids,
+			   struct string_list *object_info_options)
 {
 	int retval = 0;
 	struct remote *remote = NULL;
 	struct object_id oid;
-	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	struct transport *gtransport;
 
 	remote = remote_get(argv[0]);
@@ -727,13 +727,10 @@ static int get_remote_info(int argc,
 	CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
 	gtransport->smart_options->object_info_oids = object_info_oids;
 
-	string_list_append(&object_info_options, "size");
-
-	gtransport->smart_options->object_info_options = &object_info_options;
+	gtransport->smart_options->object_info_options = object_info_options;
 	gtransport->smart_options->object_info_data = *remote_object_info;
 	retval = transport_fetch_object_info(gtransport);
 cleanup:
-	string_list_clear(&object_info_options, 0);
 	transport_disconnect(gtransport);
 	return retval;
 }
@@ -819,6 +816,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
 		load_mailmap();
 }
 
+struct protocol_placeholder_entry {
+	const char *option;
+	const char *atom;
+};
+
+static const struct protocol_placeholder_entry remote_atom_map[] = {
+	{"size", "objectsize"},
+	{"type", "objecttype"},
+	/*
+	 * Add new protocol options here. Even if the server doesn't support
+	 * them the allow_list will drop them if the server doesn't advertise
+	 * them.
+	 */
+};
+
 static void parse_cmd_remote_object_info(struct batch_options *opt,
 					 const char *line, struct strbuf *output,
 					 struct expand_data *data)
@@ -828,6 +840,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 	char *line_to_split;
 	struct object_info *remote_object_info = NULL;
 	struct oid_array object_info_oids = OID_ARRAY_INIT;
+	struct string_list object_info_options = STRING_LIST_INIT_NODUP;
 	const char *saved_format = opt->format;
 
 	if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
@@ -847,10 +860,22 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 		die(_("remote-object-info supports at most %d objects"),
 		    MAX_ALLOWED_OBJ_LIMIT);
 
+	if (data->info.sizep)
+		string_list_append(&object_info_options, "size");
+	if (data->info.typep)
+		string_list_append(&object_info_options, "type");
+
 	if (get_remote_info(count, argv, &remote_object_info,
-			    &object_info_oids))
+			    &object_info_oids, &object_info_options))
 		die(_("failed to get object info from the remote: %s"), argv[0]);
 
+	string_list_clear(&data->remote_allowed_atoms, 0);
+	string_list_append(&data->remote_allowed_atoms, "objectname");
+	for (size_t i = 0; i < ARRAY_SIZE(remote_atom_map); i++)
+		if (unsorted_string_list_has_string(&object_info_options, remote_atom_map[i].option))
+			string_list_append(&data->remote_allowed_atoms,
+					   remote_atom_map[i].atom);
+
 	data->skip_object_info = 1;
 	for (size_t i = 0; i < object_info_oids.nr; i++) {
 		data->oid = object_info_oids.oid[i];
@@ -861,25 +886,29 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
 			continue;
 		}
 
+		/*
+		 * When reaching here, it means remote-object-info can retrieve
+		 * information from server without downloading them.
+		 */
 		if (remote_object_info[i].sizep) {
-			/*
-			 * When reaching here, it means remote-object-info can retrieve
-			 * information from server without downloading them.
-			 */
 			data->size = *remote_object_info[i].sizep;
-			opt->batch_mode = BATCH_MODE_INFO;
-			data->is_remote = 1;
-			batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
-			data->is_remote = 0;
-		} else {
-			report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing");
 		}
+
+		if (remote_object_info[i].typep) {
+			data->type = *remote_object_info[i].typep;
+		}
+
+		opt->batch_mode = BATCH_MODE_INFO;
+		data->is_remote = 1;
+		batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
+		data->is_remote = 0;
 	}
 	data->skip_object_info = 0;
 	opt->format = saved_format;
 
 	for (size_t i = 0; i < object_info_oids.nr; i++)
 		free_object_info_contents(&remote_object_info[i]);
+	string_list_clear(&object_info_options, 0);
 	free(line_to_split);
 	free(argv);
 	free(remote_object_info);
@@ -1138,6 +1167,7 @@ static int batch_objects(struct batch_options *opt)
  cleanup:
 	strbuf_release(&input);
 	strbuf_release(&output);
+	string_list_clear(&data.remote_allowed_atoms, 0);
 	cfg->warn_on_object_refname_ambiguity = save_warning;
 	return retval;
 }
fetch-object-info.c +19 -1
index 30475a1e87..ba7e179c44 100644
@@ -55,6 +55,24 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 	case protocol_v2:
 		if (!server_supports_v2("object-info"))
 			die(_("object-info capability is not enabled on the server"));
+		/*
+		 * When removing an element from the list it gets swapped by the
+		 * last element, iterate backwards to prevent elements skipping
+		 * evaluation.
+		 *
+		 * object_info_options->nr can be safely casted without overflow
+		 * because the number of options is a small known number (the
+		 * supported placeholders which currently are size and type).
+		 */
+		for (int i = (int)args->object_info_options->nr - 1; i >= 0; i--)
+			if (!server_supports_feature("object-info",
+						     args->object_info_options->items[i].string, 0))
+				unsorted_string_list_delete_item(args->object_info_options, i, 0);
+
+		/*
+		 * Even if no options are left, we still send the oid so we get
+		 * at least an existence check.
+		 */
 		send_object_info_request(fd_out, args);
 		break;
 	case protocol_v1:
@@ -71,7 +89,7 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
 			return -1;
 		}
 
-		if (!string_list_has_string(args->object_info_options, reader->line))
+		if (!unsorted_string_list_has_string(args->object_info_options, reader->line))
 			return -1;
 
 		if (!strcmp(reader->line, "size")) {
fetch-object-info.h +3 -0
index 31aad98408..269cebb3f7 100644
@@ -14,6 +14,9 @@ struct object_info;
 /*
  * Sends git-cat-file object-info command into the request buf and read the
  * results from packets.
+ *
+ * Modifies args->object_info_options, on return it contains only the supported
+ * options by the server.
  */
 int fetch_object_info(enum protocol_version version, struct object_info_args *args,
 		      struct packet_reader *reader, struct object_info *object_info_data,
t/t1017-cat-file-remote-object-info.sh +28 -0
index edc20394d8..116862f9d0 100755
@@ -271,6 +271,34 @@ test_expect_success 'unsupported placeholder on remote returns empty string' '
 	)
 '
 
+test_expect_success 'requesting only objectname echoes back' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo $hello_oid >expect &&
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
+		EOF
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'objectname goes through existence check' '
+	(
+		set_transport_variables "$daemon_parent" &&
+		cd "$daemon_parent/daemon_client_empty" &&
+
+		echo "$unstored_oid missing" >expect &&
+
+		git cat-file --batch-command="%(objectname)" >actual <<-EOF &&
+		remote-object-info "$GIT_DAEMON_URL/parent" $unstored_oid
+		EOF
+
+		test_cmp expect actual
+	)
+'
+
 # Test --batch-command remote-object-info with 'git://' and
 # transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
 test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
transport.c +0 -1
index 9342680531..f0a6a45547 100644
@@ -443,7 +443,6 @@ static int fetch_object_info_via_pack(struct transport *transport)
 	args.server_options = transport->server_options;
 	args.oids = transport->smart_options->object_info_oids;
 	args.object_info_options = transport->smart_options->object_info_options;
-	string_list_sort(args.object_info_options);
 
 	connect_setup(transport, 0);
 	packet_reader_init(&reader, data->fd[0], NULL, 0,
Signature · 1 line
2.54.0
#396 lore raw
In reply to Pablo Sabater
Quoted 6 lines · Pablo Sabater

Changes in v21:
- Dropped unnecessary change in git-cat-file.adoc
- Fixed EXPAND_DATA_INIT macro formating
- typo and blank line before WARNING:
...
Range-diff versus v20:

All looks good. Let's mark it for 'next' now.