transport: add client support for object-info

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. Helped-by: Jonathan Tan <jonathantanmy@google.com> Helped-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Eric Ju <eric.peijian@gmail.com> Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Calvin Wan committed Jul 24, 2026 at 12:54 UTC 12a19eec1d7c0121e0c82c95a2300fdbad1f18f2
9 files changed +240
Makefile
+1
@@ -1158,6 +1158,7 @@ LIB_OBJS += ewah/ewah_io.o
1158 LIB_OBJS += ewah/ewah_rlw.o
1159 LIB_OBJS += exec-cmd.o
1160 LIB_OBJS += fetch-negotiator.o
1161 +LIB_OBJS += fetch-object-info.o
1162 LIB_OBJS += fetch-pack.o
1163 LIB_OBJS += fmt-merge-msg.o
1164 LIB_OBJS += fsck.o
fetch-object-info.c new
+138
@@ -0,0 +1,138 @@
1 +#include "git-compat-util.h"
2 +#include "gettext.h"
3 +#include "hex.h"
4 +#include "pkt-line.h"
5 +#include "connect.h"
6 +#include "oid-array.h"
7 +#include "odb.h"
8 +#include "fetch-object-info.h"
9 +#include "string-list.h"
10 +
11 +/* Sends object-info command and its arguments into the request buffer. */
12 +static void send_object_info_request(const int fd_out, struct object_info_args *args)
13 +{
14 + struct strbuf req_buf = STRBUF_INIT;
15 +
16 + write_command_and_capabilities(&req_buf, "object-info", args->server_options);
17 +
18 + if (unsorted_string_list_has_string(args->object_info_options, "size"))
19 + packet_buf_write(&req_buf, "size");
20 + else if (args->object_info_options->nr)
21 + BUG("only size should be in object_info_options");
22 +
23 + if (args->oids)
24 + for (size_t i = 0; i < args->oids->nr; i++)
25 + packet_buf_write(&req_buf, "oid %s", oid_to_hex(&args->oids->oid[i]));
26 +
27 + packet_buf_flush(&req_buf);
28 + if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
29 + die_errno(_("unable to write request to remote"));
30 +
31 + strbuf_release(&req_buf);
32 +}
33 +
34 +static int parse_object_size(const char *s, size_t *res)
35 +{
36 + uintmax_t uim;
37 +
38 + if (!s[0] || s[strspn(s, "0123456789")])
39 + return -1;
40 + errno = 0;
41 + uim = strtoumax(s, NULL, 10);
42 + if (errno || uim > SIZE_MAX)
43 + return -1;
44 + *res = uim;
45 + return 0;
46 +}
47 +
48 +int fetch_object_info(const enum protocol_version version, struct object_info_args *args,
49 + struct packet_reader *reader, struct object_info *object_info_data,
50 + const int stateless_rpc, const int fd_out)
51 +{
52 + int size_index = -1;
53 +
54 + switch (version) {
55 + case protocol_v2:
56 + if (!server_supports_v2("object-info"))
57 + die(_("object-info capability is not enabled on the server"));
58 + send_object_info_request(fd_out, args);
59 + break;
60 + case protocol_v1:
61 + case protocol_v0:
62 + die(_("object-info requires protocol v2"));
63 + case protocol_unknown_version:
64 + BUG("unknown protocol version");
65 + }
66 +
67 + for (size_t i = 0; i < args->object_info_options->nr; i++) {
68 + if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
69 + check_stateless_delimiter(stateless_rpc, reader,
70 + "stateless delimiter expected");
71 + return -1;
72 + }
73 +
74 + if (!string_list_has_string(args->object_info_options, reader->line))
75 + return -1;
76 +
77 + if (!strcmp(reader->line, "size")) {
78 + /*
79 + * i is the number of supported options which currently
80 + * is only size. No risk of overflow.
81 + */
82 + size_index = (int)i;
83 + for (size_t j = 0; j < args->oids->nr; j++)
84 + object_info_data[j].sizep =
85 + xcalloc(1, sizeof(*object_info_data[j].sizep));
86 + } else {
87 + BUG("only size is supported");
88 + }
89 + }
90 +
91 + for (size_t i = 0;
92 + packet_reader_read(reader) == PACKET_READ_NORMAL &&
93 + i < args->oids->nr;
94 + i++) {
95 + struct string_list object_info_values = STRING_LIST_INIT_DUP;
96 +
97 + string_list_split(&object_info_values, reader->line, " ", -1);
98 +
99 + if (strcmp(object_info_values.items[0].string,
100 + oid_to_hex(&args->oids->oid[i])))
101 + die(_("object-info: expected OID: %s, got %s"),
102 + oid_to_hex(&args->oids->oid[i]),
103 + object_info_values.items[0].string);
104 +
105 + /*
106 + * If the response is two elements but the second one is an
107 + * empty string, that means that the OID is unrecognized by the
108 + * server.
109 + */
110 + if (object_info_values.nr >= 2 &&
111 + !strcmp(object_info_values.items[1].string, "")) {
112 + object_info_data[i].unrecognized = 1;
113 + string_list_clear(&object_info_values, 0);
114 + continue;
115 + }
116 +
117 + /*
118 + * Because we filter the options to be only the supported by
119 + * the server we expect the server to answer with the same
120 + * number of attributes requested.
121 + */
122 + if (args->object_info_options->nr + 1 != object_info_values.nr)
123 + die("object-info: unexpected number of attributes: %s",
124 + reader->line);
125 +
126 + if (size_index >= 0 &&
127 + parse_object_size(object_info_values.items[size_index + 1].string,
128 + object_info_data[i].sizep))
129 + die("object-info: ref %s has invalid size %s",
130 + object_info_values.items[0].string,
131 + object_info_values.items[size_index + 1].string);
132 +
133 + string_list_clear(&object_info_values, 0);
134 + }
135 + check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected");
136 +
137 + return 0;
138 +}
fetch-object-info.h new
+22
@@ -0,0 +1,22 @@
1 +#ifndef FETCH_OBJECT_INFO_H
2 +#define FETCH_OBJECT_INFO_H
3 +
4 +#include "pkt-line.h"
5 +#include "protocol.h"
6 +
7 +struct object_info_args {
8 + struct string_list *object_info_options;
9 + const struct string_list *server_options;
10 + struct oid_array *oids;
11 +};
12 +
13 +struct object_info;
14 +/*
15 + * Sends git-cat-file object-info command into the request buf and read the
16 + * results from packets.
17 + */
18 +int fetch_object_info(enum protocol_version version, struct object_info_args *args,
19 + struct packet_reader *reader, struct object_info *object_info_data,
20 + int stateless_rpc, int fd_out);
21 +
22 +#endif /* FETCH_OBJECT_INFO_H */
meson.build
+1
@@ -359,6 +359,7 @@ libgit_sources = [
359 'ewah/ewah_rlw.c',
360 'exec-cmd.c',
361 'fetch-negotiator.c',
362 + 'fetch-object-info.c',
363 'fetch-pack.c',
364 'fmt-merge-msg.c',
365 'fsck.c',
odb.h
+6
@@ -339,6 +339,12 @@ struct object_info {
339 * or multiple times in the same source.
340 */
341 struct odb_source_info *source_infop;
342 +
343 + /*
344 + * object-info protocol specific. Set by the protocol when the remote
345 + * does not recognize the requested object.
346 + */
347 + unsigned int unrecognized:1;
348 };
349
350 /*
transport-helper.c
+10
@@ -784,6 +784,15 @@ static int fetch_refs(struct transport *transport,
784 return -1;
785 }
786
787 +static int fetch_object_info_helper(struct transport *transport)
788 +{
789 + get_helper(transport);
790 + if (process_connect(transport, 0))
791 + return transport->vtable->fetch_object_info(transport);
792 +
793 + die(_("object-info requires protocol v2"));
794 +}
795 +
796 struct push_update_ref_state {
797 struct ref *hint;
798 struct ref_push_report *report;
@@ -1330,6 +1339,7 @@ static struct transport_vtable vtable = {
1339 .get_refs_list = get_refs_list,
1340 .get_bundle_uri = get_bundle_uri,
1341 .fetch_refs = fetch_refs,
1342 + .fetch_object_info = fetch_object_info_helper,
1343 .push_refs = push_refs,
1344 .connect = connect_helper,
1345 .disconnect = release_helper
transport-internal.h
+8
@@ -45,6 +45,14 @@ struct transport_vtable {
45 **/
46 int (*fetch_refs)(struct transport *transport, int refs_nr, struct ref **refs);
47
48 + /*
49 + * Fetch object info (only size currently) from remote without
50 + * downloading the objects.
51 + *
52 + * Uses object-info capability of v2 protocol.
53 + */
54 + int (*fetch_object_info)(struct transport *transport);
55 +
56 /**
57 * Push the objects and refs. Send the necessary objects, and
58 * then, for any refs where peer_ref is set and
transport.c
+45
@@ -9,6 +9,7 @@
9 #include "hook.h"
10 #include "pkt-line.h"
11 #include "fetch-pack.h"
12 +#include "fetch-object-info.h"
13 #include "remote.h"
14 #include "connect.h"
15 #include "send-pack.h"
@@ -432,6 +433,48 @@ static int get_bundle_uri(struct transport *transport)
433 transport->bundles, stateless_rpc);
434 }
435
436 +static int fetch_object_info_via_pack(struct transport *transport)
437 +{
438 + int ret = 0;
439 + struct git_transport_data *data = transport->data;
440 + struct packet_reader reader;
441 + struct object_info_args args = { 0 };
442 +
443 + args.server_options = transport->server_options;
444 + args.oids = transport->smart_options->object_info_oids;
445 + args.object_info_options = transport->smart_options->object_info_options;
446 + string_list_sort(args.object_info_options);
447 +
448 + connect_setup(transport, 0);
449 + packet_reader_init(&reader, data->fd[0], NULL, 0,
450 + PACKET_READ_CHOMP_NEWLINE |
451 + PACKET_READ_GENTLE_ON_EOF |
452 + PACKET_READ_DIE_ON_ERR_PACKET);
453 +
454 + data->version = discover_version(&reader);
455 + transport->hash_algo = reader.hash_algo;
456 +
457 + ret = fetch_object_info(data->version, &args, &reader,
458 + data->options.object_info_data,
459 + transport->stateless_rpc, data->fd[1]);
460 +
461 + close(data->fd[0]);
462 + if (data->fd[1] >= 0)
463 + close(data->fd[1]);
464 + if (finish_connect(data->conn))
465 + ret = -1;
466 + data->conn = NULL;
467 +
468 + return ret;
469 +}
470 +
471 +int transport_fetch_object_info(struct transport *transport)
472 +{
473 + if (!transport->vtable->fetch_object_info)
474 + die(_("remote does not support object-info"));
475 + return transport->vtable->fetch_object_info(transport);
476 +}
477 +
478 static int fetch_refs_via_pack(struct transport *transport,
479 int nr_heads, struct ref **to_fetch)
480 {
@@ -1004,6 +1047,7 @@ static struct transport_vtable taken_over_vtable = {
1047 .get_refs_list = get_refs_via_connect,
1048 .get_bundle_uri = get_bundle_uri,
1049 .fetch_refs = fetch_refs_via_pack,
1050 + .fetch_object_info = fetch_object_info_via_pack,
1051 .push_refs = git_transport_push,
1052 .disconnect = disconnect_git
1053 };
@@ -1169,6 +1213,7 @@ static struct transport_vtable builtin_smart_vtable = {
1213 .get_refs_list = get_refs_via_connect,
1214 .get_bundle_uri = get_bundle_uri,
1215 .fetch_refs = fetch_refs_via_pack,
1216 + .fetch_object_info = fetch_object_info_via_pack,
1217 .push_refs = git_transport_push,
1218 .connect = connect_git,
1219 .disconnect = disconnect_git
transport.h
+9
@@ -55,6 +55,10 @@ struct git_transport_options {
55 * common commits to this oidset instead of fetching any packfiles.
56 */
57 struct oidset *acked_commits;
58 +
59 + struct oid_array *object_info_oids;
60 + struct object_info *object_info_data;
61 + struct string_list *object_info_options;
62 };
63
64 enum transport_family {
@@ -309,6 +313,11 @@ int transport_get_remote_bundle_uri(struct transport *transport);
313 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport);
314 int transport_fetch_refs(struct transport *transport, struct ref *refs);
315
316 +/*
317 + * Fetch the object info from remote
318 + */
319 +int transport_fetch_object_info(struct transport *transport);
320 +
321 /*
322 * If this flag is set, unlocking will avoid to call non-async-signal-safe
323 * functions. This will necessarily leave behind some data structures which