cat-file: add remote-object-info to batch-command
Subsystems (3)
RFC PATCH v13 9/12 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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.
This commit introduces client functions to interact with the server.
Currently, the client supports requesting a list of object IDs with the 'size' feature from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client will return an error and exit.
Notice that 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.
--- Makefile | 1 + fetch-object-info.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ meson.build | 1 + transport-helper.c | 11 +++++-- transport.c | 28 ++++++++++++++++- transport.h | 11 +++++++ 9 files changed, 166 insertions(+), 3 deletions(-)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +90 -0
90 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.c +3 -0
@@ -1742,6 +1742,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,if (args->depth >0 || args->deepen_since || args->deepen_not) args->deepen =1 ; +if (args->object_info) + state =FETCH_SEND_REQUEST ; +while (state !=FETCH_DONE ) {switch (state) {case FETCH_CHECK_LOCAL :
fetch-pack.h +2 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
@@ -43,6 +44,7 @@ struct fetch_pack_args { unsigned reject_shallow_remote:1; unsigned deepen:1; unsigned refetch:1; + unsigned object_info:1;/* * Indicate that the remote of this request is a promisor remote. The
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +9 -2
@@ -727,8 +727,8 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only and cat-file +* remote-object-info require protocol v2. */ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
@@ -744,6 +744,13 @@ static int fetch_refs(struct transport *transport,free_refs (dummy); } +/* fail the command explicitly to avoid further commands input. */ +if (transport->smart_options->object_info) +die (_ ("remote-object-info requires protocol v2" )); + +if (!data->get_refs_list_called) +get_refs_list_using_list (transport,0 ); + count =0 ;for (i =0 ; i < nr_heads; i++)if (!(to_fetch[i]->status &REF_STATUS_UPTODATE ))
transport.c +27 -1
@@ -9,6 +9,7 @@#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "fetch-object-info.h" #include "remote.h" #include "connect.h" #include "send-pack.h"
@@ -467,8 +468,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.negotiation_restrict_tips = data->options.negotiation_restrict_tips; args.negotiation_include_tips = data->options.negotiation_include_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) { +struct packet_reader reader; +struct object_info_args obj_info_args = {0 }; + + obj_info_args.server_options = transport->server_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + obj_info_args.object_info_options = transport->smart_options->object_info_options; +string_list_sort (obj_info_args.object_info_options); + +connect_setup (transport,0 ); +packet_reader_init (&reader, data->fd[0 ],NULL ,0 , +PACKET_READ_CHOMP_NEWLINE | +PACKET_READ_GENTLE_ON_EOF | +PACKET_READ_DIE_ON_ERR_PACKET ); + + data->version =discover_version (&reader); + transport->hash_algo = reader.hash_algo; + + ret =fetch_object_info (data->version, &obj_info_args, &reader, + data->options.object_info_data, transport->stateless_rpc, + data->fd[1 ]); + goto cleanup; -if (!data->finished_handshake) { + }else if (!data->finished_handshake) {int i;int must_list_refs =0 ;for (i =0 ; i < nr_heads; i++) {
transport.h +11 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -31,6 +32,12 @@ struct git_transport_options {*/ unsigned connectivity_checked:1; +/* +* Transport will attempt to retrieve only object-info. +* If object-info is not supported, the operation will error and exit. +*/ + unsigned object_info : 1; +int depth;const char *deepen_since;const struct string_list *deepen_not;
@@ -55,6 +62,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 {
Signature · 1 line
2.54.0
RFC PATCH v13 10/12 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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 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>
--- Documentation/git-cat-file.adoc | 24 +- builtin/cat-file.c | 144 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ transport.c | 4 +- 7 files changed, 859 insertions(+), 7 deletions(-)
Documentation/git-cat-file.adoc +20 -4
@@ -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`
@@ -312,7 +319,8 @@ newline. The available atoms are: The full hex representation of the object name. `objecttype`:: - The type of the object (the same as `cat-file -t` reports). + The type of the object (the same as `cat-file -t` reports). See + `CAVEATS` below. Not supported by `remote-object-info`. `objectmode`:: If the specified object has mode information (such as a tree or
@@ -325,13 +333,14 @@ newline. The available atoms are: `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. + note about on-disk sizes in the `CAVEATS` section below. Not + supported by `remote-object-info`. `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. + below. Not supported by `remote-object-info`. `rest`:: If this atom is used in the output string, input lines are split
@@ -341,7 +350,10 @@ newline. The available atoms are: line) are output in place of the `%(rest)` atom. If no format is specified, the default format is `%(objectname) -%(objecttype) %(objectsize)`. +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which use +`%(objectname) %(objectsize)` for now 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 since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will raise +an error and exit when they appear in the format string. + 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 +143 -1
169 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -170,6 +170,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 +680 -0
680 lines not on this page · raw message
transport.c +2 -2
@@ -470,8 +470,8 @@ static int fetch_refs_via_pack(struct transport *transport, args.reject_shallow_remote = transport->smart_options->reject_shallow; args.object_info = transport->smart_options->object_info; -if (transport->smart_options->object_info - && transport->smart_options->object_info_oids->nr >0 ) { +if (transport->smart_options->object_info && + transport->smart_options->object_info_oids->nr >0 ) {struct packet_reader reader;struct object_info_args obj_info_args = {0 };
Signature · 1 line
2.54.0
[snip]
builtin/cat-file.c static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 821
Quoted 9 lines
builtin/cat-file.c +9
-0+static void parse_cmd_remote_object_info (struct batch_options *opt, +const char *line,struct strbuf *output, +struct expand_data *data) +{ +int count; +const char **argv; +char *line_to_split; +static struct object_info *remote_object_info; +static struct oid_array object_info_oids =OID_ARRAY_INIT ;
I don't get the point of remote_object_info and object_info_oids being static here? These variables are allocated, utilized, and completely freed/disconnected within a single command cycle.
Making them static gives me the false impression that state needs to persist between calls.
Quoted 40 lines
builtin/cat-file.c +40
-0+if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) +die (_ ("remote-object-info command too long" )); + + line_to_split =xstrdup (line); + count =split_cmdline (line_to_split, &argv); +if (count <0 ) +die (_ ("split remote-object-info command" )); +if (count -1 >MAX_ALLOWED_OBJ_LIMIT ) +die (_ ("remote-object-info supports at most %d objects" ), +MAX_ALLOWED_OBJ_LIMIT ); + +if (get_remote_info (opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup; + + data->skip_object_info =1 ; +for (size_t i =0 ; i < object_info_oids.nr; i++) { + data->oid = object_info_oids.oid[i]; +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 ; +batch_object_write (argv[i +1 ], output, opt, data,NULL ,0 ); + }else { +report_object_status (opt,oid_to_hex (&data->oid), &data->oid,"missing" ); + } + } + 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]); +free (line_to_split); +free (argv); +free (remote_object_info); +} +
[snip]
Quoted 14 lines · Chandra Pratap
[snip]
builtin/cat-file.c static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 821
Quoted 9 lines
builtin/cat-file.c +9
-0+static void parse_cmd_remote_object_info (struct batch_options *opt, +const char *line,struct strbuf *output, +struct expand_data *data) +{ +int count; +const char **argv; +char *line_to_split; +static struct object_info *remote_object_info; +static struct oid_array object_info_oids =OID_ARRAY_INIT ;I don't get the point of
remote_object_infoandobject_info_oidsbeing static here? These variables are allocated, utilized, and completely freed/disconnected within a single command cycle.
Great observation.
Quoted 2 lines
Making them static gives me the false impression that state needs to persist between calls.
Yes, and makes it thread-unsafe, even though if is questionable if this particular function has to be thread safe ;-)
RFC PATCH v13 11/12 cat-file: validate remote atoms with allow_list
In reply to Pablo Sabaterstrstr() is not enough to validate the format placeholders in remote-object-info causing two errors:
- Atoms recognized by expand_atom() but the remote doesn't returns 1, but data->type contains garbage causing segfault.
- expand_atom() returns 0 for unknown atoms, calling strbuf_expand_bad_format() which ends in die() 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 sb 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() behaviour to expect an empty string and add an explicit test for empty string return on unknown placeholder.
Update caveat behaviour documentation.
--- Documentation/git-cat-file.adoc | 5 +++-- builtin/cat-file.c | 41 +++++++++++++++++++++++++++------- t/t1017-cat-file-remote-object-info.sh | 27 ++++++++++++++++++---- 3 files changed, 59 insertions(+), 14 deletions(-)
Documentation/git-cat-file.adoc +3 -2
@@ -451,8 +451,9 @@ CAVEATS ------- Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are -currently not supported by the `remote-object-info` command, we will raise -an error and exit when they appear in the format string. +currently not supported by the `remote-object-info` command, they will +return an empty string for remote queries, matching how `for-each-ref` +behaves for known but inapplicable placeholders. 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
@@ -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
RFC PATCH v13 12/12 cat-file: make remote-object-info allow-list dynamic
In reply to Pablo SabaterThe 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 does the server advertise.
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 | 16 +++++++++ 2 files changed, 80 insertions(+), 33 deletions(-)
builtin/cat-file.c +64 -33
@@ -341,13 +341,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) {
@@ -359,17 +357,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 ; }
@@ -685,12 +677,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 ;static struct transport *gtransport;/*
@@ -739,15 +731,12 @@ static int get_remote_info(struct batch_options *opt, gtransport->smart_options->object_info =1 ; 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_refs (gtransport,NULL ); } cleanup: -string_list_clear (&object_info_options,0 );transport_disconnect (gtransport);return retval; }
@@ -833,6 +822,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)
@@ -842,6 +846,7 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,char *line_to_split;static struct object_info *remote_object_info;static 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" ));
@@ -854,32 +859,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);
@@ -1194,6 +1224,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 +16 -0
@@ -39,6 +39,22 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arcase 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. +*/ +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
Re: [PATCH GSoC RFC v13 12/12] 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, theallow_listneeds to be dynamic depending on what does the server advertise.
Nit: "...depending on what does the server advertise." -> "...depending on what the server advertises."
Re: [PATCH GSoC RFC v13 12/12] cat-file: make remote-object-info allow-list dynamic
Forgot to mention this in the earlier email, but the comment explaining why the backward iteration of the list is needed should also explain why it is fine to cast args->object_info_options->nr to int from size_t (it will always be a small number, so no risk of overflow).
Quoted 2 lines · Pablo Sabater
This path series is a continuation of Eric Ju's ([email protected]) and
s/path/patch
Quoted 36 lines
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-infocommand tocat-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-commandwill die.If a user attempts to use
remote-object-infowith protocol v1,cat-file --batch-commandwill 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].
I have a question for the design:
Quoted 8 lines
1. If the format includes unsupported fields such as %(objecttype) or %(deltabase) it currently returns an empty string for each unsupported field, this follows what for-each-ref does with known but inapplicable atoms. However future placeholders that will be implemented: %(rest), %(objectmode) can return empty strings. How should we differentiate "unsupported" vs "no data".
Eric proposed to use a placeholder like "???" [5].
Should a placeholder be used?
Maybe it's best to fail cleanly if the user requests an unsupported atom? I don't really like the placeholder idea though. If a placeholder like "???" is introduced, any script/test parsing the output must add explicit logic to check for literal question marks, and that sounds flaky. Not to mention some atom's response may legitimately contain "???".
Quoted 9 lines
2. _tangent/not related with this
series_
'a2ba162cda' is designed to only work with full OIDs, which is inconsistent with localinfothat does support short OIDs and in case of being ambiguous returns a list of what possibly the user meant.Because V2 protocol is thought to be stateless supporting short OIDs could become more inconsistent with other remote commands that do not support short OIDs. Maybe a --pick-first option? That does accept short oids and picks the first match.
We might return the wrong object's info if we do this. With the server giving us no information to verify whether the returned value _really_ corresponds to our intended object, I'd say this isn't the right choice.
Quoted 2 lines
Alternatively, would sending a list of possible OIDs to the client so it can re-request with the correct one be ok?
As far as I know, disambiguation like this is treated purely as a local UI convenience in Git, never a network-level operation.
fetch already requires users to input exact, full OIDs for their want lines (obtained via a prior ls-remote or ref advertisement), and dies if one isn't provided. Thus, I think erroring out is fine here.
v14 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis 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/28159024232
[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]/
[5]: https://lore.kernel.org/git/CAN2LT1D3d=yMYVhBjpj5PvyjfTVjwqcFPNViuCJ=f49YbCZuJg@mail.gmail.com/
Changes since v13:
- Changed the introduced strtoul_ul to strtoul_szt as a recent patch changed the type of object_info.sizep to size_t. <37d030d8675e94caee2eecb8398691d385d444bd.1781524349.git.gitgitgadget@gmail.com>
- Fixed commit typos and style.
- "fetch-pack: move function to connect.c" is now 2 different commits:
first a cleanup and a second to only move the function.
- Fixed old code brought from the rebase to match upstream: use SHA1_LEGACY instead of SHA1
- Remove static declarations from cmd_remote_remote_info() and get_remote_info().
- Add a comment justifying why there will be no overflow on explicit cast.
---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info
Eric Ju (4):
git-compat-util: add strtoul_szt() with error handling
cat-file: declare loop counter inside for()
t1006: split test utility functions into new "lib-cat-file.sh"
cat-file: add remote-object-info to batch-command
Pablo Sabater (6):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: prepare function to be moved
fetch-pack: move function to connect.c
connect: refactor packet writing
cat-file: validate remote atoms with allow_list
cat-file: make remote-object-info allow-list dynamic
Documentation/git-cat-file.adoc | 25 +- Makefile | 1 + builtin/cat-file.c | 222 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 110 ++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 51 +-- fetch-pack.h | 2 + git-compat-util.h | 20 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 13 +- transport.c | 28 +- transport.h | 11 + 21 files changed, 1220 insertions(+), 75 deletions(-)
---
base-commit: ab776a62a78576513ee121424adb19597fbb7613
change-id: 20260608-ps-eric-work-rebase-b73ae84ba671
Best regards,
Signature · 1 line
Pablo Sabater <[email protected]>
PATCH v14 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
@@ -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
PATCH v14 2/13 git-compat-util: add strtoul_szt() with error handling
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
We already have strtoul_ui() and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns an unsigned long.
This variant is needed in a subsequent commit to enable returning an size_t with proper error handling.
--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
git-compat-util.h +20 -0
@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoul, with +* additional error handling to ensure robustness. +*/ +static inline int strtoul_szt (char const *s,int base,size_t *result) +{ +unsigned long ul; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; + ul =strtoul (s, &p, base); +if (errno || *p || p == s) +return -1 ; + *result = ul; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Signature · 1 line
2.54.0
Re: [PATCH GSoC v14 02/13] git-compat-util: add strtoul_szt() with error handling
Quoted 5 lines · Pablo Sabater
From: Eric Ju <[email protected]>
We already have
strtoul_ui()and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns an unsigned long.
But this one no longer returns an unsigned long anymore ;-)
Quoted 2 lines
This variant is needed in a subsequent commit to enable returning an
size_twith proper error handling.
I think it would allow a lot of code paths that want to deal with size_t not to worry about "is ulong large enough?" to have a function like this, but for that to happen, the implementation of the function must carefully think through if these steps do sensible things on platforms with too small ulong (which often is OK when we are coming from decimal string to ulong and then to size_t) and too large ulong (which is not OK, when coming from decimal string to ulong which might be fine, but will bust the size of the final type), etc.
Also, would it make sense to add yet another "static inline" like this? After the dust settles, we may want to rethink these strtoX wrappers we have, benchmark, and possibly make them into a proper library function, not "static inline" that may bloat the runtime.
Quoted 31 lines
git-compat-util.h +20
-0@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoul, with +* additional error handling to ensure robustness. +*/ +static inline int strtoul_szt (char const *s,int base,size_t *result) +{ +unsigned long ul; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; + ul =strtoul (s, &p, base); +if (errno || *p || p == s) +return -1 ; + *result = ul; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Re: [PATCH GSoC v14 02/13] git-compat-util: add strtoul_szt() with error handling
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Pablo Sabater
From: Eric Ju <[email protected]>
We already have
strtoul_ui()and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns an unsigned long.But this one no longer returns an unsigned long anymore ;-)
True, I missed that.
Quoted 4 lines
Quoted 3 lines
This variant is needed in a subsequent commit to enable returning an
size_twith proper error handling.
Quoted 9 lines
I think it would allow a lot of code paths that want to deal with
size_tnot to worry about "is ulong large enough?" to have a function like this, but for that to happen, the implementation of the function must carefully think through if these steps do sensible things on platforms with too small ulong (which often is OK when we are coming from decimal string to ulong and then tosize_t) and too large ulong (which is not OK, when coming from decimal string to ulong which might be fine, but will bust the size of the final type), etc.
Ok, so the strtoul_szt() is not a bad idea but it is not ok how I did it. What about using uintmax_t and strtoumax() and after that (before casting) check if it fits into a size_t?
Something like:
static inline int strtoumax_szt(char const *s, int base, size_t *result)
{
uintmax_t val;
char *p;
errno = 0;
/* negative values would be accepted by strtoul */
if (strchr(s, '-'))
return -1;
val = strtoumax(s, &p, base);
if ((errno || *p || p == s) || val > SIZE_MAX)
return -1;
*result = val;
return 0;
}Alternatively I could go back to the unsigned long version and make the relevant checks on the caller which is only one place at fetch_object_info()
Quoted 5 lines
Also, would it make sense to add yet another "static inline" like this? After the dust settles, we may want to rethink these strtoX wrappers we have, benchmark, and possibly make them into a proper library function, not "static inline" that may bloat the runtime.
Yeah, I thought the same, I kept it on the header also to avoid bloating this series too much because it's kinda big already. But a follow-up after would be a good idea tho.
Quoted 24 lines
Quoted 24 lines
git-compat-util.h +15
-0@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoul, with +* additional error handling to ensure robustness. +*/ +static inline int strtoul_szt (char const *s,int base,size_t *result) +{ +unsigned long ul; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; + ul =strtoul (s, &p, base); +if (errno || *p || p == s)
Quoted 8 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 1000
Quoted 8 lines
git-compat-util.h +5
-0+return -1 ; + *result = ul; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Thanks for the review,
Pablo.
Re: [PATCH GSoC v14 02/13] git-compat-util: add strtoul_szt() with error handling
In reply to Pablo SabaterQuoted 6 lines · Pablo Sabater
From: Eric Ju <[email protected]>
We already have
strtoul_ui()and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns an unsigned long.
Shouldn't this say returns a 'size_t'?
Quoted 23 lines
This variant is needed in a subsequent commit to enable returning an
size_twith proper error handling.Signed-off-by Pablo Sabater <[email protected]>--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)git-compat-util.h +7
-0@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoul, with +* additional error handling to ensure robustness. +*/ +static inline int strtoul_szt (char const *s,int base,size_t *result) +{ +unsigned long ul;
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 994
Quoted 10 lines
git-compat-util.h +10
-0+char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; + ul =strtoul (s, &p, base); +if (errno || *p || p == s) +return -1 ; + *result = ul;
This converts unsigned long to size_t, but that is fine.
Looks good.
Quoted 9 lines
git-compat-util.h +3
-1+return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul; --2.54.0
PATCH v14 3/13 cat-file: declare loop counter inside for()
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop.
Change the declaration of i to be inside the for loop for readability. While at it, we also change its type from "int" to "size_t" where the latter makes more sense.
--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
@@ -718,14 +718,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);
@@ -733,9 +731,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 ;
@@ -762,7 +758,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 };
@@ -772,7 +767,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
@@ -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
PATCH v14 4/13 t1006: split test utility functions into new "lib-cat-file.sh"
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
This refactor extracts 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 this functions, the goal is to improve code reuse and readability,enabling future tests to leverage these utilities without duplicating code.
--- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-)
t/lib-cat-file.sh new file +16 -0
@@ -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 NULL 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 +1 -12
@@ -4,6 +4,7 @@ test_description='git cat-file' . ./test-lib.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
PATCH v14 5/13 fetch-pack: prepare function to be moved
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities() will be refactored and moved in subsequent commits where it will become a more general-purpose function, making it more accessible to additional commands in the future.
To move write_fetch_command_and_capabilities() to connect.c, we previously need to adjust how advertise_sid is managed. Currently in fetch_pack.c, advertise_sid is a static variable, modified using repo_config_get_bool().
Initialize advertise_sid at the begining by directly using repo_config_get_bool(). This change is safe because:
In the original fetch-pack.c code, there are only two places that write advertise_sid:
1. In function do_fetch_pack():
if (!server_supports("session_id"))
advertise_sid = 0;
2. In function `fetch_pack_config()`:
repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored, as write_fetch_command_and_capabilities() is only used in v2.
About 2, repo_config_get_bool() is from config.h and it's an out-of-box dependency of connect.c, so we can reuse it directly.
--- fetch-pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fetch-pack.c +4 -1
@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );
@@ -1395,7 +1398,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
Re: [PATCH GSoC v14 05/13] fetch-pack: prepare function to be moved
Quoted 22 lines · Pablo Sabater
write_fetch_command_and_capabilities()will be refactored and moved in subsequent commits where it will become a more general-purpose function, making it more accessible to additional commands in the future.To move
write_fetch_command_and_capabilities()toconnect.c, we previously need to adjust howadvertise_sidis managed. Currently infetch_pack.c,advertise_sidis a static variable, modified usingrepo_config_get_bool().Initialize
advertise_sidat the begining by directly usingrepo_config_get_bool(). This change is safe because:In the original
fetch-pack.ccode, there are only two places that writeadvertise_sid:1. In function
do_fetch_pack():if (!server_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);
Quoted 6 lines
About 1, since
do_fetch_pack()is only relevant for protocol v1, this assignment can be ignored, aswrite_fetch_command_and_capabilities()is only used in v2.About 2,
repo_config_get_bool()is fromconfig.hand it's an out-of-box dependency ofconnect.c, so we can reuse it directly.
Nit: This only explains the advertise_sid change in this patch. We should also add a few lines explaining the hash_algo change. Maybe something like:
While at it, change hash_algo's type to hash_algo_by_name()'s actual return type (unsigned int) and make it const.
Quoted 35 lines
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- fetch-pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fetch-pack.c +4
-2@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );@@ -1395,7 +1398,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); --2.54.0
Re: [PATCH GSoC v14 05/13] fetch-pack: prepare function to be moved
In reply to Pablo SabaterPablo Sabater <[email protected]> writes:
The subject doesn't really give much insight into what the patch does. Perhaps something like:
fetch-pack: use repo config in write_fetch_command_and_capabilities()
fetch-pack: drop static variable use inwrite_fetch_command_and_capabilities()
Quoted 6 lines
write_fetch_command_and_capabilities()will be refactored and moved in subsequent commits where it will become a more general-purpose function, making it more accessible to additional commands in the future.To move
write_fetch_command_and_capabilities()toconnect.c, we previously need to adjust howadvertise_sidis managed. Currently in
I don't think 'previously' makes sense here.
Quoted 3 lines
fetch_pack.c,advertise_sidis a static variable, modified usingrepo_config_get_bool().
Perhaps:
To move write_fetch_command_and_capabilities() to connect.c, drop the usage of file static variable advertise_sid within the function. Currently, advertise_sid is modified...
Quoted 7 lines
Initialize
advertise_sidat the begining by directly usingrepo_config_get_bool(). This change is safe because:In the original
fetch-pack.ccode, there are only two places that writeadvertise_sid:
This needs to be modified no? This is from the prev patch, where we moved and refactored in the same patch, this no longer is the case.
Quoted 37 lines
1. In function
do_fetch_pack():if (!server_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since
do_fetch_pack()is only relevant for protocol v1, this assignment can be ignored, aswrite_fetch_command_and_capabilities()is only used in v2.About 2,
repo_config_get_bool()is fromconfig.hand it's an out-of-box dependency ofconnect.c, so we can reuse it directly.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- fetch-pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fetch-pack.c +3
-0@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1402
Quoted 6 lines
fetch-pack.c +1
-1@@ -1395,7 +1398,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);
Agreed with Chandra, this needs to be assessed.
Quoted 6 lines
fetch-pack.c +0
-0if (hash_algo_by_ptr (the_hash_algo) != hash_algo) die(_("mismatched algorithms: client %s; server %s"),
the_hash_algo->name,hash_name);--
2.54.0
Re: [PATCH GSoC v14 05/13] fetch-pack: prepare function to be moved
Quoted 79 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
The subject doesn't really give much insight into what the patch does. Perhaps something like:
fetch-pack: use repo config in
write_fetch_command_and_capabilities()
fetch-pack: drop static variable use inwrite_fetch_command_and_capabilities()Quoted 6 lines
write_fetch_command_and_capabilities()will be refactored and moved in subsequent commits where it will become a more general-purpose function, making it more accessible to additional commands in the future.To move
write_fetch_command_and_capabilities()toconnect.c, we previously need to adjust howadvertise_sidis managed. Currently inI don't think 'previously' makes sense here.
Quoted 3 lines
fetch_pack.c,advertise_sidis a static variable, modified usingrepo_config_get_bool().Perhaps:
To move
write_fetch_command_and_capabilities()toconnect.c, drop the usage of file static variableadvertise_sidwithin the function. Currently,advertise_sidis modified...Quoted 7 lines
Initialize
advertise_sidat the begining by directly usingrepo_config_get_bool(). This change is safe because:In the original
fetch-pack.ccode, there are only two places that writeadvertise_sid:This needs to be modified no? This is from the prev patch, where we moved and refactored in the same patch, this no longer is the case.
Quoted 39 lines
1. In function
do_fetch_pack():if (!server_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since
do_fetch_pack()is only relevant for protocol v1, this assignment can be ignored, aswrite_fetch_command_and_capabilities()is only used in v2.About 2,
repo_config_get_bool()is fromconfig.hand it's an out-of-box dependency ofconnect.c, so we can reuse it directly.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- fetch-pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)fetch-pack.c +3
-0@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );@@ -1395,7 +1398,7 @@ static void write_fetch_command_and_capabilities(struct strbuf *req_buf, }
Quoted 13 lines
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
Quoted 4 lines
fetch-pack.c +1
-1if (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);Agreed with Chandra, this needs to be assessed.
Quoted 6 lines
fetch-pack.c +0
-0if (hash_algo_by_ptr (the_hash_algo) != hash_algo) die(_("mismatched algorithms: client %s; server %s"),the_hash_algo->name, hash_name);--
2.54.0
I'll reword the commit message with all you and Chandra reviewed.
Thanks for the feedback,
Pablo.
PATCH v14 6/13 fetch-pack: move function to connect.c
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities will be refactored in a subsequent commit where it will become 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
@@ -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; + +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
@@ -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
@@ -1376,40 +1376,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; - -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
Re: [PATCH GSoC v14 06/13] fetch-pack: move function to connect.c
Quoted 2 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill be refactored in a subsequent
Nit: The paragraph below and the preceding patches refer to this function as write_fetch_command_and_capabilities(). It will be nice to maintain consistency throughout this series.
Quoted 17 lines
commit where it will become a more general-purpose function, making it more accessible to additional commands in the future.
Move
write_fetch_command_and_capabilities()toconnect.c, where there are similar purpose functions.Because
string_listis only used as a pointer, use a forward declaration [1].[1]: https://lore.kernel.org/git/[email protected]/
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>---
[snip]
Re: [PATCH GSoC v14 06/13] fetch-pack: move function to connect.c
Quoted 8 lines · Chandra Pratap
Quoted 2 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill be refactored in a subsequentNit: The paragraph below and the preceding patches refer to this function as
write_fetch_command_and_capabilities(). It will be nice to maintain consistency throughout this series.
I'll do that.
Quoted 12 lines
Quoted 12 lines
commit where it will become a more general-purpose function, making it more accessible to additional commands in the future.
Move
write_fetch_command_and_capabilities()toconnect.c, where there are similar purpose functions.Because
string_listis only used as a pointer, use a forward declaration [1].
Quoted 7 lines
Quoted 6 lines
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>---
[snip]
Thanks for the feedback,
Pablo.
Re: [PATCH GSoC v14 06/13] fetch-pack: move function to connect.c
In reply to Pablo SabaterPablo Sabater <[email protected]> writes:
Nit: could we mention the function in the subject?
Quoted 2 lines
write_fetch_command_and_capabilitieswill be refactored in a subsequent
Super Nit: Some parts of your patches use backticks for quoting code or filenames and others skip the convention. It would be nice to be consistent.
Quoted 128 lines
commit where it will become a more general-purpose function, making it more accessible to additional commands in the future.
Move
write_fetch_command_and_capabilities()toconnect.c, where there are similar purpose functions.Because
string_listis only used as a pointer, use a forward declaration [1].[1]: https://lore.kernel.org/git/[email protected]/
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 4 ++++ fetch-pack.c | 34 ---------------------------------- 3 files changed, 38 insertions(+), 34 deletions(-)connect.c +34
-0@@ -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; + +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@@ -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
-35@@ -1376,40 +1376,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; - -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, --2.54.0
Re: [PATCH GSoC v14 06/13] fetch-pack: move function to connect.c
Quoted 4 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
Nit: could we mention the function in the subject?
Yes, I'll mention it.
Quoted 7 lines
Quoted 3 lines
write_fetch_command_and_capabilitieswill be refactored in a subsequentSuper Nit: Some parts of your patches use backticks for quoting code or filenames and others skip the convention. It would be nice to be consistent.
Sorry about that, I'll stick to backticks always.
Quoted 12 lines
Quoted 12 lines
commit where it will become a more general-purpose function, making it more accessible to additional commands in the future.
Move
write_fetch_command_and_capabilities()toconnect.c, where there are similar purpose functions.Because
string_listis only used as a pointer, use a forward declaration [1].
Quoted 11 lines
Quoted 11 lines
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 4 ++++ fetch-pack.c | 34 ---------------------------------- 3 files changed, 38 insertions(+), 34 deletions(-)
[snip]
Thanks for the feedback,
Pablo.
PATCH v14 7/13 connect: refactor packet writing
In reply to Pablo SabaterRefactor 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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(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
@@ -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;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
@@ -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); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options);#endif
fetch-pack.c +2 -2
@@ -1387,7 +1387,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" );
@@ -2255,7 +2255,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
Re: [PATCH GSoC v14 07/13] connect: refactor packet writing
Pablo Sabater <[email protected]> writes:
The subject is bit too generic no? Maybe we can talk about the function? Perhaps:
connect: make write_fetch_command_and_capabilities() more generic
Quoted 11 lines
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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(object-info: support for retrieving object info, 2021-04-20).Refactor the function signature to accept a command instead of the hardcoded "fetch".
[snip]
Re: [PATCH GSoC v14 07/13] connect: refactor packet writing
Quoted 7 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
The subject is bit too generic no? Maybe we can talk about the function? Perhaps:
connect: make
write_fetch_command_and_capabilities()more generic
Okay, I'll use that as the subject.
Quoted 10 lines
Quoted 10 lines
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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(object-info: support for retrieving object info, 2021-04-20).
Quoted 4 lines
Quoted 2 lines
Refactor the function signature to accept a command instead of the hardcoded "fetch".
[snip]
Thanks for the feedback,
Pablo.
PATCH v14 8/13 fetch-pack: move fetch initialization
In reply to Pablo SabaterFrom: 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
@@ -1736,18 +1736,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
PATCH v14 9/13 serve: advertise object-info feature
In reply to Pablo SabaterFrom: 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 will allow 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
@@ -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
Re: [PATCH GSoC v14 09/13] serve: advertise object-info feature
Quoted 30 lines · 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 will allow a client to decide whether to query the server for object-info or fetch as a fallback.
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- serve.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)serve.c +1
-1@@ -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",
serve.c static int object_info_advertise(struct repository *r, struct strbuf *value UNUS · line 102
Quoted 6 lines
serve.c +3
-0@@ -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" );
So is the plan that further options will be added here to value? If so, whats the format we will follow?
Quoted 6 lines
serve.c +0
-1return advertise_object_info; } --2.54.0
Re: [PATCH GSoC v14 09/13] serve: advertise object-info feature
Quoted 27 lines · Karthik Nayak
Quoted 24 lines · 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 will allow a client to decide whether to query the server for object-info or fetch as a fallback.
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- serve.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)serve.c +0
-0@@ -89,7 +89,7 @@ static void session_id_receive(struct repository *r UNUSED,trace2_data_string ("transfer" ,NULL ,"client-sid" , client_sid); }
Quoted 15 lines
serve.c static int object_info_advertise(struct repository *r, struct strbuf *value UNUS · line 102
Quoted 12 lines
serve.c +4
-1-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" );So is the plan that further options will be added here to value? If so, whats the format we will follow?
Hi!
The current documented format is at gitprotocol-v2.adoc, however I think it could be improved. I have a more complete version in the not-yet-sent %(objecttype) support series, but since the question comes up here, I will update the format documentation in this series for size only:
oid <oid>
Indicates to the server an object which the client wants to obtain
- information for.
+ information for. They must be full object IDs.
- info = PKT-LINE(attrs) LF)
+ info = PKT-LINE(attrs LF)
*PKT-LINE(obj-info LF)
attrs = attr | attrs SP attrs
+ obj-size = 1*DIGIT
+
attr = "size"
- obj-info = obj-id SP obj-size
+ obj-info = obj-id SP [obj-size]
+
+If the server does not recognize the object id, the response will be +obj-id SP regardless of the number of attributes requested.
About the names size and future ones type they are arbitrarily chosen, so for example: delta:base could be delta. They are appended to the buffer so in case of adding type, it would look like:
strbuf_addstr(value, "size type");
What do you think?
Thanks for the review,
Pablo.
Quoted 7 lines
Quoted 7 lines
serve.c +0
-1return advertise_object_info; } --2.54.0
Re: [PATCH GSoC v14 09/13] serve: advertise object-info feature
Quoted 45 lines · Pablo Sabater
Quoted 27 lines · Karthik Nayak
Quoted 24 lines · 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 will allow a client to decide whether to query the server for object-info or fetch as a fallback.
Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- serve.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)serve.c +0
-0@@ -89,7 +89,7 @@ static void session_id_receive(struct repository *r UNUSED,trace2_data_string ("transfer" ,NULL ,"client-sid" , client_sid); }Quoted 15 lines
serve.c static int object_info_advertise(struct repository *r, struct strbuf *value UNUS · line 102
Quoted 12 lines
serve.c +4
-1-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" );So is the plan that further options will be added here to value? If so, whats the format we will follow?
Quoted 7 lines
Hi!
The current documented format is atgitprotocol-v2.adoc, however I think it could be improved. I have a more complete version in the not-yet-sent %(objecttype) support series, but since the question comes up here, I will update the format documentation in this series for size only:
Ah nice, I didn't see that.
Quoted 21 lines
oid <oid>
Indicates to the server an object which the client wants to obtain
- information for.
+ information for. They must be full object IDs.- info = PKT-LINE(attrs) LF)
+ info = PKT-LINE(attrs LF)
*PKT-LINE(obj-info LF)attrs = attr | attrs SP attrs
+ obj-size = 1*DIGIT
(quoted diff) +5
-1+ attr = "size" - obj-info = obj-id SP obj-size + obj-info = obj-id SP [obj-size] + +If the server does not recognize the object id, the response will be +`obj-id SP` regardless of the number of attributes requested.
Quoted 9 lines
About the names
sizeand future onestypethey are arbitrarily chosen, so for example:delta:basecould bedelta. They are appended to the buffer so in case of addingtype, it would look like:
strbuf_addstr(value, "size type");What do you think?
I didn't know this part was already documented, so its all good :)
Quoted 10 lines
Thanks for the review,
Pablo.Quoted 7 lines
Quoted 7 lines
serve.c +0
-1return advertise_object_info; } --2.54.0
PATCH v14 10/13 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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.
This commit introduces client functions to interact with the server.
Currently, the client supports requesting a list of object IDs with the 'size' feature from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client will return an error and exit.
Notice that 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.
--- Makefile | 1 + fetch-object-info.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ meson.build | 1 + transport-helper.c | 11 +++++-- transport.c | 28 ++++++++++++++++- transport.h | 11 +++++++ 9 files changed, 166 insertions(+), 3 deletions(-)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +90 -0
90 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.c +3 -0
@@ -1742,6 +1742,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,if (args->depth >0 || args->deepen_since || args->deepen_not) args->deepen =1 ; +if (args->object_info) + state =FETCH_SEND_REQUEST ; +while (state !=FETCH_DONE ) {switch (state) {case FETCH_CHECK_LOCAL :
fetch-pack.h +2 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
@@ -43,6 +44,7 @@ struct fetch_pack_args { unsigned reject_shallow_remote:1; unsigned deepen:1; unsigned refetch:1; + unsigned object_info:1;/* * Indicate that the remote of this request is a promisor remote. The
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +9 -2
@@ -727,8 +727,8 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only and cat-file +* remote-object-info require protocol v2. */ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
@@ -744,6 +744,13 @@ static int fetch_refs(struct transport *transport,free_refs (dummy); } +/* fail the command explicitly to avoid further commands input. */ +if (transport->smart_options->object_info) +die (_ ("remote-object-info requires protocol v2" )); + +if (!data->get_refs_list_called) +get_refs_list_using_list (transport,0 ); + count =0 ;for (i =0 ; i < nr_heads; i++)if (!(to_fetch[i]->status &REF_STATUS_UPTODATE ))
transport.c +27 -1
@@ -9,6 +9,7 @@#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "fetch-object-info.h" #include "remote.h" #include "connect.h" #include "send-pack.h"
@@ -467,8 +468,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.negotiation_restrict_tips = data->options.negotiation_restrict_tips; args.negotiation_include_tips = data->options.negotiation_include_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) { +struct packet_reader reader; +struct object_info_args obj_info_args = {0 }; + + obj_info_args.server_options = transport->server_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + obj_info_args.object_info_options = transport->smart_options->object_info_options; +string_list_sort (obj_info_args.object_info_options); + +connect_setup (transport,0 ); +packet_reader_init (&reader, data->fd[0 ],NULL ,0 , +PACKET_READ_CHOMP_NEWLINE | +PACKET_READ_GENTLE_ON_EOF | +PACKET_READ_DIE_ON_ERR_PACKET ); + + data->version =discover_version (&reader); + transport->hash_algo = reader.hash_algo; + + ret =fetch_object_info (data->version, &obj_info_args, &reader, + data->options.object_info_data, transport->stateless_rpc, + data->fd[1 ]); + goto cleanup; -if (!data->finished_handshake) { + }else if (!data->finished_handshake) {int i;int must_list_refs =0 ;for (i =0 ; i < nr_heads; i++) {
transport.h +11 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -31,6 +32,12 @@ struct git_transport_options {*/ unsigned connectivity_checked:1; +/* +* Transport will attempt to retrieve only object-info. +* If object-info is not supported, the operation will error and exit. +*/ + unsigned object_info : 1; +int depth;const char *deepen_since;const struct string_list *deepen_not;
@@ -55,6 +62,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 {
Signature · 1 line
2.54.0
Re: [PATCH GSoC v14 10/13] transport: add client support for object-info
Quoted 57 lines · 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.This commit introduces client functions to interact with the server.
Currently, the client supports requesting a list of object IDs with the 'size' feature from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client will return an error and exit.
Notice that the entire request is written into
req_bufbefore being sent to the remote. This approach follows the pattern used in thesend_fetch_request()logic withinfetch-pack.c. Streaming the request is not addressed in this patch.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- Makefile | 1 + fetch-object-info.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ meson.build | 1 + transport-helper.c | 11 +++++-- transport.c | 28 ++++++++++++++++- transport.h | 11 +++++++ 9 files changed, 166 insertions(+), 3 deletions(-)Makefile +1
-0@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.ofetch-object-info.c new file +1
-0@@ -0,0 +1,90 @@ +#include "git-compat-util.h"
fetch-object-info.c line 11
Quoted 10 lines
fetch-object-info.c new file +10
-0+#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "odb.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/* Sends git-cat-file object-info command and its arguments into the request buffer. */
This file doesn't know about git-cat-file(1), nor should it care about it, right? Since git-cat-file(1) is simply a user of this function. Would it make more sense to structure the document around what this function is supposed to do?
Also the comment for fetch_object_info seems to be similar, perhaps worthwhile changing both without referring to git-cat-file(1). Theoretically there could be other users in the future too.
fetch-object-info.c line 20
Quoted 9 lines
fetch-object-info.c new file +9
-0+static void send_object_info_request (const int fd_out,struct object_info_args *args) +{ +struct strbuf req_buf =STRBUF_INIT ; + +write_command_and_capabilities (&req_buf,"object-info" , args->server_options); + +if (unsorted_string_list_has_string (args->object_info_options,"size" )) +packet_buf_write (&req_buf,"size" ); +
Okay so if the user requests 'size', we forward that to the server. But what about the 'else' condition? Should we BUG() out?
fetch-object-info.c line 28
Quoted 8 lines
fetch-object-info.c new file +8
-0+if (args->oids) +for (size_t i =0 ; i < args->oids->nr; i++) +packet_buf_write (&req_buf,"oid %s" ,oid_to_hex (&args->oids->oid[i])); + +packet_buf_flush (&req_buf); +if (write_in_full (fd_out, req_buf.buf, req_buf.len) <0 ) +die_errno (_ ("unable to write request to remote" )); +
We write out all the oids, flush and write to the fd. Okay.
Quoted 4 lines
fetch-object-info.c new file +4
-0+strbuf_release (&req_buf); +} + +int fetch_object_info(const enum protocol_version version, struct object_info_args *args,
fetch-object-info.c line 42
Quoted 10 lines
fetch-object-info.c new file +10
-0+ struct packet_reader *reader, struct object_info *object_info_data, + const int stateless_rpc, const int fd_out) +{ + int size_index = -1; + +switch (version) { +case protocol_v2: +if (!server_supports_v2 ("object-info" )) +die (_ ("object-info capability is not enabled on the server" )); +send_object_info_request (fd_out, args);
So if the server does support 'object-info', we call send_object_info_request(). Makes sense.
fetch-object-info.c line 50
Quoted 8 lines
fetch-object-info.c new file +8
-0+break ; +case protocol_v1: +case protocol_v0: +die (_ ("unsupported protocol version. expected v2" )); +case protocol_unknown_version: +BUG ("unknown protocol version" ); + } +
Now that we've sent the request, we should start parsing the response.
Quoted 7 lines
fetch-object-info.c new file +7
-0+for (size_t i =0 ; i < args->object_info_options->nr; i++) { +if (packet_reader_read (reader) !=PACKET_READ_NORMAL ) { +check_stateless_delimiter (stateless_rpc, reader, +"stateless delimiter expected" ); +return -1 ; + } +
fetch-object-info.c line 67
Quoted 10 lines
fetch-object-info.c new file +10
-0+if (!string_list_has_string (args->object_info_options, reader->line)) +return -1 ; + +if (!strcmp (reader->line,"size" )) { + size_index = i; +for (size_t j =0 ; j < args->oids->nr; j++) + object_info_data[j].sizep =xcalloc (1 ,sizeof (*object_info_data[j].sizep)); + } + } +
So this function seems to iterate over the list of options to only find and store the indexes. If the server does support size we also allocate the pointers to store the size.
Shouldn't we similarly BUG() if there is anything apart from 'size' here?
fetch-object-info.c line 79
Quoted 12 lines
fetch-object-info.c new file +12
-0+for (size_t i =0 ;packet_reader_read (reader) ==PACKET_READ_NORMAL && i < args->oids->nr; i++) { +struct string_list object_info_values =STRING_LIST_INIT_DUP ; + +string_list_split (&object_info_values, reader->line," " ,-1 ); +if (0 <= size_index) { +if (!strcmp (object_info_values.items[1 + size_index].string,"" )) { +FREE_AND_NULL (object_info_data[i].sizep); +string_list_clear (&object_info_values,0 ); +continue ; + } +if (strtoul_szt (object_info_values.items[1 + size_index].string, +10 , object_info_data[i].sizep))
This is now no longer correctly aligned.
fetch-object-info.c line 88
Quoted 9 lines
fetch-object-info.c new file +9
-0+die ("object-info: ref %s has invalid size %s" , + object_info_values.items[0 ].string, + object_info_values.items[1 + size_index].string); + } + +string_list_clear (&object_info_values,0 ); + } +check_stateless_delimiter (stateless_rpc, reader,"stateless delimiter expected" ); +
We parse each line and obtain the size and parse it into object_info_data[i].sizep. If the value is missing, we simply continue the iteration.
I think we had this discussion off the list about how this means that oids which do not have a size will not error out but rather display a missing info value.
The argument for not error'ing out was better user experience where the command would complete without exiting. I still think we should error out, because:
1. Without error'ing out, we'd have to display to the user a missing value token. There is contention around what this token should be, as such a token shouldn't be a valid value for the info type being displayed. Future options must be considered here.
2. What will the error code of such a situation be? Do we consider it a success or a failure? Is there a situation where an object can have a missing size?
Quoted 81 lines
fetch-object-info.c new file +2
-0+return 0 ; +}fetch-object-info.h new file +22
-0@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */ fetch-pack.c +3
-0@@ -1742,6 +1742,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,if (args->depth >0 || args->deepen_since || args->deepen_not) args->deepen =1 ; +if (args->object_info) + state =FETCH_SEND_REQUEST ; +while (state !=FETCH_DONE ) {switch (state) {case FETCH_CHECK_LOCAL :fetch-pack.h +2
-0@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have" @@ -43,6 +44,7 @@ struct fetch_pack_args { unsigned reject_shallow_remote:1; unsigned deepen:1; unsigned refetch:1; + unsigned object_info:1;/* * Indicate that the remote of this request is a promisor remote. The meson.build +1
-0@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',transport-helper.c +0
-0
transport-helper.c static int fetch_refs(struct transport *transport, · line 731
Quoted 7 lines
transport-helper.c +2
-2@@ -727,8 +727,8 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only and cat-file +* remote-object-info require protocol v2.
This is not really true as of this commit. This only comes into effect in the next commit. So shouldn't this be added there? That said, I would modify this to only talk about object-info requiring v2 and drop the reference to cat-file.
Quoted 4 lines
transport-helper.c +0
-0*/ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
transport-helper.c static int fetch_refs(struct transport *transport, · line 753
Quoted 10 lines
transport-helper.c +7
-0@@ -744,6 +744,13 @@ static int fetch_refs(struct transport *transport,free_refs (dummy); } +/* fail the command explicitly to avoid further commands input. */ +if (transport->smart_options->object_info) +die (_ ("remote-object-info requires protocol v2" )); + +if (!data->get_refs_list_called) +get_refs_list_using_list (transport,0 ); +
Don't we already do this right above? Why is this needed again?
Quoted 16 lines
transport-helper.c +0
-0count =0 ;for (i =0 ; i < nr_heads; i++)if (!(to_fetch[i]->status &REF_STATUS_UPTODATE ))transport.c +1
-0@@ -9,6 +9,7 @@#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "fetch-object-info.h" #include "remote.h" #include "connect.h" #include "send-pack.h"
transport.c static int fetch_refs_via_pack(struct transport *transport, · line 472
Quoted 5 lines
transport.c +2
-0@@ -467,8 +468,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.negotiation_restrict_tips = data->options.negotiation_restrict_tips; args.negotiation_include_tips = data->options.negotiation_include_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; +
Hmm, so we piggy-back on top of the fetch_refs_via_pack() function...
Quoted 14 lines
transport.c +14
-0+if (transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) { +struct packet_reader reader; +struct object_info_args obj_info_args = {0 }; + + obj_info_args.server_options = transport->server_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + obj_info_args.object_info_options = transport->smart_options->object_info_options; +string_list_sort (obj_info_args.object_info_options); + +connect_setup (transport,0 ); + packet_reader_init(&reader, data->fd[0], NULL, 0, + PACKET_READ_CHOMP_NEWLINE | + PACKET_READ_GENTLE_ON_EOF |
transport.c static int fetch_refs_via_pack(struct transport *transport, · line 496
Quoted 10 lines
transport.c +9
-0+ PACKET_READ_DIE_ON_ERR_PACKET); + + data->version =discover_version (&reader); + transport->hash_algo = reader.hash_algo; + + ret =fetch_object_info (data->version, &obj_info_args, &reader, + data->options.object_info_data, transport->stateless_rpc, + data->fd[1 ]); + goto cleanup;
... and we jump to exit when we only want object info information. This skips the call to fetch_pack(). I'm a bit uneasy with this. Ideally we should be adding a new function to the vtable to only fetch object info. While this works, this doesn't fit the contract of what this function is supposed to do. See the comment around fetch_refs in struct transport_vtable. Shouldn't we update that documentation at the very least?
Quoted 18 lines
transport.c +1
-1-if (!data->finished_handshake) { + }else if (!data->finished_handshake) {int i;int must_list_refs =0 ;for (i =0 ; i < nr_heads; i++) {transport.h +1
-0@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
transport.h struct git_transport_options { · line 40
Quoted 9 lines
transport.h +6
-0@@ -31,6 +32,12 @@ struct git_transport_options {*/ unsigned connectivity_checked:1; +/* +* Transport will attempt to retrieve only object-info. +* If object-info is not supported, the operation will error and exit. +*/ + unsigned object_info : 1; +
According to our style, this should be unsigned object_info:1.
Quoted 16 lines
transport.h +4
-1int depth;const char *deepen_since;const struct string_list *deepen_not;@@ -55,6 +62,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 { --
Quoted 1 line
2.54.0
Nit: I do think the commit message doesn't sufficiently capture the entirety of this patch. We do not talk about:
1. How we piggy-back on top of fetch_refs_via_pack() and don't introduce our own pointer in the vtable.
2. The changes made to 'transport-helper.c' and why that is required.
3. How we don't error out when there is no size value provided for an OID and what the implication of this is.
Re: [PATCH GSoC v14 10/13] transport: add client support for object-info
Quoted 76 lines · Karthik Nayak
Quoted 57 lines · 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.This commit introduces client functions to interact with the server.
Currently, the client supports requesting a list of object IDs with the 'size' feature from a v2 server. If the server does not advertise this feature (i.e., transfer.advertiseobjectinfo is set to false), the client will return an error and exit.
Notice that the entire request is written into
req_bufbefore being sent to the remote. This approach follows the pattern used in thesend_fetch_request()logic withinfetch-pack.c. Streaming the request is not addressed in this patch.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- Makefile | 1 + fetch-object-info.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ meson.build | 1 + transport-helper.c | 11 +++++-- transport.c | 28 ++++++++++++++++- transport.h | 11 +++++++ 9 files changed, 166 insertions(+), 3 deletions(-)Makefile +1
-0@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.ofetch-object-info.c new file +1
-0@@ -0,0 +1,90 @@ +#include "git-compat-util.h" fetch-object-info.c line 11
Quoted 10 lines
fetch-object-info.c new file +10
-0+#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "odb.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/* Sends git-cat-file object-info command and its arguments into the request buffer. */ This file doesn't know about git-cat-file(1), nor should it care about it, right? Since git-cat-file(1) is simply a user of this function. Would it make more sense to structure the document around what this function is supposed to do?
Quoted 3 lines
Also the comment for
fetch_object_infoseems to be similar, perhaps worthwhile changing both without referring to git-cat-file(1). Theoretically there could be other users in the future too.
git cat-file is currently the only consumer of object-info but you are right, this file shouldn't know about it nor mention it in a comment, I'll drop it.
Quoted 13 lines
fetch-object-info.c line 20
Quoted 10 lines
fetch-object-info.c new file +9
-0+static void send_object_info_request (const int fd_out,struct object_info_args *args) +{ +struct strbuf req_buf =STRBUF_INIT ; + +write_command_and_capabilities (&req_buf,"object-info" , args->server_options); + +if (unsorted_string_list_has_string (args->object_info_options,"size" )) +packet_buf_write (&req_buf,"size" ); +Okay so if the user requests 'size', we forward that to the server. But what about the 'else' condition? Should we
BUG()out?
In patch [13/13] the options are built dynamically and filtered against what the server advertises, so by the time we reach here only valid options remain. For this commit there is no client support yet so there is no way of this to fail and in the next commit [11/13]:
get_remote_info()
[snip]
string_list_append(&object_info_options, "size");
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_refs(gtransport, NULL);
}[snip]
get_remote_info() appends "size" always to object_info_options and this is changed in commit [12/13] with the allow-list where only asked object-info is appended. I could move that check to this commit as if size always gets appended the if below will always be true.
Also because size is always appended there can't be an else condition to BUG() out.
Quoted 63 lines
fetch-object-info.c line 28
Quoted 9 lines
fetch-object-info.c new file +8
-0+if (args->oids) +for (size_t i =0 ; i < args->oids->nr; i++) +packet_buf_write (&req_buf,"oid %s" ,oid_to_hex (&args->oids->oid[i])); + +packet_buf_flush (&req_buf); +if (write_in_full (fd_out, req_buf.buf, req_buf.len) <0 ) +die_errno (_ ("unable to write request to remote" )); +We write out all the oids, flush and write to the fd. Okay.
Quoted 4 lines
fetch-object-info.c new file +4
-0+strbuf_release (&req_buf); +} + +int fetch_object_info(const enum protocol_version version, struct object_info_args *args,fetch-object-info.c line 42
Quoted 10 lines
fetch-object-info.c new file +10
-0+ struct packet_reader *reader, struct object_info *object_info_data, + const int stateless_rpc, const int fd_out) +{ + int size_index = -1; + +switch (version) { +case protocol_v2: +if (!server_supports_v2 ("object-info" )) +die (_ ("object-info capability is not enabled on the server" )); +send_object_info_request (fd_out, args);So if the server does support 'object-info', we call
send_object_info_request(). Makes sense.fetch-object-info.c line 50
Quoted 8 lines
fetch-object-info.c new file +8
-0+break ; +case protocol_v1: +case protocol_v0: +die (_ ("unsupported protocol version. expected v2" )); +case protocol_unknown_version: +BUG ("unknown protocol version" ); + } +Now that we've sent the request, we should start parsing the response.
Quoted 7 lines
fetch-object-info.c new file +7
-0+for (size_t i =0 ; i < args->object_info_options->nr; i++) { +if (packet_reader_read (reader) !=PACKET_READ_NORMAL ) { +check_stateless_delimiter (stateless_rpc, reader, +"stateless delimiter expected" ); +return -1 ; + } +fetch-object-info.c line 67
Quoted 10 lines
fetch-object-info.c new file +10
-0+if (!string_list_has_string (args->object_info_options, reader->line)) +return -1 ; + +if (!strcmp (reader->line,"size" )) { + size_index = i; +for (size_t j =0 ; j < args->oids->nr; j++) + object_info_data[j].sizep =xcalloc (1 ,sizeof (*object_info_data[j].sizep)); + } + } +So this function seems to iterate over the list of options to only find and store the indexes. If the server does support size we also allocate the pointers to store the size.
Quoted 1 line
Shouldn't we similarly
BUG()if there is anything apart from 'size' here?
Because only size is appended and the server size support comes with the series, there shouldn't be a scenario where anything else appears.
1. Full response with size.
2. OID unrecognized by the server.
We can quickly check this because the server response when the OID is unrecognized is OID SP if there's nothing after the SP the object is unrecognized.
Quoted 14 lines
fetch-object-info.c line 79
Quoted 13 lines
fetch-object-info.c new file +12
-0+for (size_t i =0 ;packet_reader_read (reader) ==PACKET_READ_NORMAL && i < args->oids->nr; i++) { +struct string_list object_info_values =STRING_LIST_INIT_DUP ; + +string_list_split (&object_info_values, reader->line," " ,-1 ); +if (0 <= size_index) { +if (!strcmp (object_info_values.items[1 + size_index].string,"" )) { +FREE_AND_NULL (object_info_data[i].sizep); +string_list_clear (&object_info_values,0 ); +continue ; + } +if (strtoul_szt (object_info_values.items[1 + size_index].string, +10 , object_info_data[i].sizep))
Quoted 1 line
This is now no longer correctly aligned.
I will fix it.
Quoted 14 lines
fetch-object-info.c line 88
Quoted 10 lines
fetch-object-info.c new file +9
-0+die ("object-info: ref %s has invalid size %s" , + object_info_values.items[0 ].string, + object_info_values.items[1 + size_index].string); + } + +string_list_clear (&object_info_values,0 ); + } +check_stateless_delimiter (stateless_rpc, reader,"stateless delimiter expected" ); +We parse each line and obtain the size and parse it into
object_info_data[i].sizep. If the value is missing, we simply continue the iteration.
Related to that above, if size is missing we FREE_AND_NULL so later on parse_cmd_remote_object_info() can detect that and print "missing" for that oid. This "missing" behavior follows what the local option info does for unrecognized OIDs.
parse_cmd_remote_object_info()
[snip]
for (size_t i = 0; i < object_info_oids.nr; i++) {
data->oid = object_info_oids.oid[i];
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;
batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
} else {
report_object_status(opt,
oid_to_hex(&data->oid), &data->oid, "missing");
}
}[snip]
Quoted 4 lines
I think we had this discussion off the list about how this means that oids which do not have a size will not error out but rather display a missing info value.
The following commit [11/13] that uses the functions introduced in this commit, will print "oid missing" in case of the object being unrecognized by the server, the no error out (empty string) would be in the scenario where the object is recognized by the server but the capability is not. Because this same series introduces the capability advertisement at [9/13] commit we can be sure that size will always be supported.
Subsequent commits with the allow-list aim to tackle this with the empty-strings when a server doesn't support a capability.
Quoted 9 lines
The argument for not error'ing out was better user experience where the command would complete without exiting. I still think we should error out, because:
1. Without error'ing out, we'd have to display to the user a missing value token. There is contention around what this token should be, as such a token shouldn't be a valid value for the info type being displayed. Future options must be considered here.
I believe that better user experience was my argument last time we discussed and I still believe that. But a stronger argument is that the local option info doesn't die either but rather it prints "OID missing" and the next commit where the cmd-related functions are implemented tries to be consistent with it for unrecognized OIDs.
Quoted 4 lines
2. What will the error code of such a situation be? Do we consider it a success or a failure? Is there a situation where an object can have a missing size?
There are two different scenarios to consider here:
1. OID unrecognized by the server: the server responds with oid SP (no value after the space).
In this case, the next commit [11/13] prints "<OID> missing" usingreport_object_status(), which is consistent with how the local info command handles missing objects in --batch-command.
2. OID recognized but a capability is not supported by the server:
subsequent commits [12/13] and [13/13] introduce an allow-list that filters options against what the server advertises.
Unsupported placeholders return an empty string, following howfor-each-refhandles known but inapplicable atoms.
For the exit code, both cases return 0. In case 1, this matches the local info behavior. In case 2, we are not failing to fetch, we simply see that the server does not support the requested capabilities and skip the request to the server. For the missing token (your point 1), we reuse the existing "missing" token from report_object_status(), which is what the local code path already uses, so no new token is introduced.
Quoted 83 lines
Quoted 83 lines
fetch-object-info.c new file +2
-0+return 0 ; +}fetch-object-info.h new file +22
-0@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */ fetch-pack.c +3
-0@@ -1742,6 +1742,9 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,if (args->depth >0 || args->deepen_since || args->deepen_not) args->deepen =1 ; +if (args->object_info) + state =FETCH_SEND_REQUEST ; +while (state !=FETCH_DONE ) {switch (state) {case FETCH_CHECK_LOCAL :fetch-pack.h +2
-0@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have" @@ -43,6 +44,7 @@ struct fetch_pack_args { unsigned reject_shallow_remote:1; unsigned deepen:1; unsigned refetch:1; + unsigned object_info:1;/* * Indicate that the remote of this request is a promisor remote. The meson.build +1
-0@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',transport-helper.c +0
-0@@ -727,8 +727,8 @@ static int fetch_refs(struct transport *transport,
Quoted 11 lines
transport-helper.c static int fetch_refs(struct transport *transport, · line 731
Quoted 6 lines
transport-helper.c +2
-2/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only and cat-file +* remote-object-info require protocol v2. This is not really true as of this commit. This only comes into effect in the next commit. So shouldn't this be added there? That said, I would modify this to only talk about object-info requiring v2 and drop the reference to cat-file.
Agreed, I will do that, thanks.
Quoted 8 lines
Quoted 8 lines
transport-helper.c +0
-0*/ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));@@ -744,6 +744,13 @@ static int fetch_refs(struct transport *transport,free_refs (dummy); }
Quoted 9 lines
transport-helper.c static int fetch_refs(struct transport *transport, · line 753
Quoted 7 lines
transport-helper.c +7
-0+/* fail the command explicitly to avoid further commands input. */ +if (transport->smart_options->object_info) +die (_ ("remote-object-info requires protocol v2" )); + +if (!data->get_refs_list_called) +get_refs_list_using_list (transport,0 ); +Don't we already do this right above? Why is this needed again?
True, I'll drop the second if.
Quoted 50 lines
Quoted 17 lines
transport-helper.c +0
-0count =0 ;for (i =0 ; i < nr_heads; i++)if (!(to_fetch[i]->status &REF_STATUS_UPTODATE ))transport.c +1
-0@@ -9,6 +9,7 @@#include "hook.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "fetch-object-info.h" #include "remote.h" #include "connect.h" #include "send-pack.h" transport.c static int fetch_refs_via_pack(struct transport *transport, · line 472
Quoted 5 lines
transport.c +2
-0@@ -467,8 +468,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.negotiation_restrict_tips = data->options.negotiation_restrict_tips; args.negotiation_include_tips = data->options.negotiation_include_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; +Hmm, so we piggy-back on top of the
fetch_refs_via_pack()function...Quoted 14 lines
transport.c +14
-0+if (transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) { +struct packet_reader reader; +struct object_info_args obj_info_args = {0 }; + + obj_info_args.server_options = transport->server_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + obj_info_args.object_info_options = transport->smart_options->object_info_options; +string_list_sort (obj_info_args.object_info_options); + +connect_setup (transport,0 ); + packet_reader_init(&reader, data->fd[0], NULL, 0, + PACKET_READ_CHOMP_NEWLINE | + PACKET_READ_GENTLE_ON_EOF |transport.c static int fetch_refs_via_pack(struct transport *transport, · line 496
Quoted 10 lines
transport.c +9
-0+ PACKET_READ_DIE_ON_ERR_PACKET); + + data->version =discover_version (&reader); + transport->hash_algo = reader.hash_algo; + + ret =fetch_object_info (data->version, &obj_info_args, &reader, + data->options.object_info_data, transport->stateless_rpc, + data->fd[1 ]); + goto cleanup;
Quoted 7 lines
... and we jump to exit when we only want object info information. This skips the call to
fetch_pack(). I'm a bit uneasy with this. Ideally we should be adding a new function to the vtable to only fetch object info. While this works, this doesn't fit the contract of what this function is supposed to do. See the comment aroundfetch_refsinstruct transport_vtable. Shouldn't we update that documentation at the very least?
Now that you point that out, piggy-backing doesn't look very good, while it works, updating the documentation leaves this in a bad situation, so I will move it to a new function and add it to the vtable for the next version. Thanks for noticing it.
Quoted 22 lines
Quoted 22 lines
transport.c +1
-1-if (!data->finished_handshake) { + }else if (!data->finished_handshake) {int i;int must_list_refs =0 ;for (i =0 ; i < nr_heads; i++) {transport.h +1
-0@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;@@ -31,6 +32,12 @@ struct git_transport_options {*/ unsigned connectivity_checked:1;
Quoted 8 lines
transport.h struct git_transport_options { · line 40
Quoted 6 lines
transport.h +6
-0+/* +* Transport will attempt to retrieve only object-info. +* If object-info is not supported, the operation will error and exit. +*/ + unsigned object_info : 1; +According to our style, this should be
unsigned object_info:1.
I'll fix it to properly follow the style, I kept it like this because other bit fields on these files seem to be outdated in that style.
Quoted 22 lines
Quoted 17 lines
transport.h +4
-1int depth;const char *deepen_since;const struct string_list *deepen_not;@@ -55,6 +62,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 { --Quoted 1 line
2.54.0
Nit: I do think the commit message doesn't sufficiently capture the entirety of this patch. We do not talk about:
Quoted 5 lines
1. How we piggy-back on top of
fetch_refs_via_pack()and don't introduce our own pointer in the vtable.
2. The changes made to 'transport-helper.c' and why that is required.
3. How we don't error out when there is no size value provided for an OID and what the implication of this is.
I will extend the commit message to include all 3 points (I'll change the piggy-backing to have its own function).
Thanks for the review,
Pablo.
PATCH v14 11/13 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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 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>
--- Documentation/git-cat-file.adoc | 24 +- builtin/cat-file.c | 145 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ transport.c | 4 +- 7 files changed, 860 insertions(+), 7 deletions(-)
Documentation/git-cat-file.adoc +20 -4
@@ -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`
@@ -312,7 +319,8 @@ newline. The available atoms are: The full hex representation of the object name. `objecttype`:: - The type of the object (the same as `cat-file -t` reports). + The type of the object (the same as `cat-file -t` reports). See + `CAVEATS` below. Not supported by `remote-object-info`. `objectmode`:: If the specified object has mode information (such as a tree or
@@ -325,13 +333,14 @@ newline. The available atoms are: `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. + note about on-disk sizes in the `CAVEATS` section below. Not + supported by `remote-object-info`. `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. + below. Not supported by `remote-object-info`. `rest`:: If this atom is used in the output string, input lines are split
@@ -341,7 +350,10 @@ newline. The available atoms are: line) are output in place of the `%(rest)` atom. If no format is specified, the default format is `%(objectname) -%(objecttype) %(objectsize)`. +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which use +`%(objectname) %(objectsize)` for now 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 since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will raise +an error and exit when they appear in the format string. + 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 +144 -1
170 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -170,6 +170,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 +680 -0
680 lines not on this page · raw message
transport.c +2 -2
@@ -470,8 +470,8 @@ static int fetch_refs_via_pack(struct transport *transport, args.reject_shallow_remote = transport->smart_options->reject_shallow; args.object_info = transport->smart_options->object_info; -if (transport->smart_options->object_info - && transport->smart_options->object_info_oids->nr >0 ) { +if (transport->smart_options->object_info && + transport->smart_options->object_info_oids->nr >0 ) {struct packet_reader reader;struct object_info_args obj_info_args = {0 };
Signature · 1 line
2.54.0
Pablo Sabater <[email protected]> writes:
[snip]
Quoted 19 lines
Documentation/git-cat-file.adoc +7
-0@@ -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`
Documentation/git-cat-file.adoc newline. The available atoms are: · line 324
Quoted 7 lines
Documentation/git-cat-file.adoc +2
-1@@ -312,7 +319,8 @@ newline. The available atoms are: The full hex representation of the object name. `objecttype`:: - The type of the object (the same as `cat-file -t` reports). + The type of the object (the same as `cat-file -t` reports). See + `CAVEATS` below. Not supported by `remote-object-info`.
Do we have to keep adding 'Not supported by remote-object-info' to each type? Can't we do the inverse and only add 'Supported by remote-object-info' to objectsize. This avoid having to add this line to every new type.
Documentation/git-cat-file.adoc newline. The available atoms are: · line 354
Quoted 4 lines
Documentation/git-cat-file.adoc +2
-1If no format is specified, the default format is `%(objectname) -%(objecttype) %(objectsize)`. +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which use +`%(objectname) %(objectsize)` for now because "%(objecttype)" is not supported yet.
Nit: I would drop the 'for now' here, since we don't know when the changes for 'objecttype' will land.
[snip]
Quoted 10 lines
enum
batch_mode{Documentation/git-cat-file.adoc +4
-0BATCH_MODE_CONTENTS,@@ -633,6 +649,81 @@ static void batch_one_object(const char *obj_name, object_context_release(&ctx); } +static int get_remote_info(struct batch_options *opt, + int argc, + const char **argv, + struct object_info **remote_object_info,
Documentation/git-cat-file.adoc static void batch_one_object(const char *obj_name, · line 665
Quoted 10 lines
Documentation/git-cat-file.adoc +10
-0+ struct oid_array *object_info_oids) +{ + int retval = 0; + struct remote *remote = NULL; + struct object_id oid; + struct string_list object_info_options = STRING_LIST_INIT_NODUP; + struct transport *gtransport; + + /* + * Change the format to "%(objectname) %(objectsize)" when
Nit: perhaps prepend a "TODO"
Quoted 38 lines
Documentation/git-cat-file.adoc +38
-0+ * remote-object-info command is used. Once we start supporting objecttype + * the default format should change to DEFAULT_FORMAT. + */ + if (!opt->format) + opt->format = "%(objectname) %(objectsize)"; + + remote = remote_get(argv[0]); + if (!remote) + die(_("must supply valid remote when using remote-object-info")); + + oid_array_clear(object_info_oids); + for (size_t i = 1; i < argc; i++) { + if (get_oid_hex(argv[i], &oid)) { + size_t len = strlen(argv[i]); + + if (len < the_hash_algo->hexsz && len >= 4) { + size_t j; + for (j = 0; j < len; j++) + if (!isxdigit(argv[i][j])) + break; + if (j == len) + die(_("remote-object-info does not support " + "short oids, %d characters required"), + (int)the_hash_algo->hexsz); + } + die(_("not a valid object name '%s'"), argv[i]); + } + oid_array_append(object_info_oids, &oid); + } + + if (!object_info_oids->nr) + die(_("remote-object-info requires objects")); + + gtransport = transport_get(remote, NULL); + + if (!gtransport->smart_options) { + retval = -1; + goto cleanup;
Documentation/git-cat-file.adoc static void batch_one_object(const char *obj_name, · line 713
Quoted 10 lines
Documentation/git-cat-file.adoc +10
-0+ } + + CALLOC_ARRAY(*remote_object_info, object_info_oids->nr); + gtransport->smart_options->object_info = 1; + gtransport->smart_options->object_info_oids = object_info_oids; + + /* 'objectsize' is the only option currently supported */ + if (!strstr(opt->format, "%(objectsize)")) + die(_("%s is currently not supported with remote-object-info"), opt->format); +
Aren't we setting the opt->format ourselves in this function? Why do we need to check it?
Quoted 27 lines
Documentation/git-cat-file.adoc +20
-0+ string_list_append(&object_info_options, "size"); + + 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_refs(gtransport, NULL); + } +cleanup: + string_list_clear(&object_info_options, 0); + transport_disconnect(gtransport); + return retval; +} + struct object_cb_data { struct batch_options *opt; struct expand_data *expand;@@ -714,6 +805,57 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED, load_mailmap(); } +static void parse_cmd_remote_object_info(struct batch_options *opt, + const char *line, struct strbuf *output, + struct expand_data *data) +{ + int count; + const char **argv; + char *line_to_split;
Documentation/git-cat-file.adoc static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 824
Quoted 10 lines
Documentation/git-cat-file.adoc +10
-0+ struct object_info *remote_object_info = NULL; + struct oid_array object_info_oids = OID_ARRAY_INIT; + + if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE) + die(_("remote-object-info command too long")); + + line_to_split = xstrdup(line); + count = split_cmdline(line_to_split, &argv); + if (count < 0) + die(_("split remote-object-info command"));
We should be using split_cmdline_strerror() here
Quoted 34 lines
Documentation/git-cat-file.adoc +34
-0+ if (count - 1 > MAX_ALLOWED_OBJ_LIMIT) + die(_("remote-object-info supports at most %d objects"), + MAX_ALLOWED_OBJ_LIMIT); + + if (get_remote_info(opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup; + + data->skip_object_info = 1; + for (size_t i = 0; i < object_info_oids.nr; i++) { + data->oid = object_info_oids.oid[i]; + 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; + batch_object_write(argv[i + 1], output, opt, data, NULL, 0); + } else { + report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing"); + } + } + 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]); + free(line_to_split); + free(argv); + free(remote_object_info); + oid_array_clear(&object_info_oids); +} +
[snip]
Quoted 12 lines
t/meson.build +1
-0@@ -170,6 +170,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',
[snip]
Quoted 26 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
[snip]
Quoted 21 lines
Documentation/git-cat-file.adoc +7
-0@@ -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`@@ -312,7 +319,8 @@ newline. The available atoms are: The full hex representation of the object name.
Quoted 10 lines
Documentation/git-cat-file.adoc newline. The available atoms are: · line 324
Quoted 5 lines
Documentation/git-cat-file.adoc +2
-1`objecttype`:: - The type of the object (the same as `cat-file -t` reports). + The type of the object (the same as `cat-file -t` reports). See + `CAVEATS` below. Not supported by `remote-object-info`.Do we have to keep adding 'Not supported by
remote-object-info' to each type? Can't we do the inverse and only add 'Supported byremote-object-info' toobjectsize. This avoid having to add this line to every new type.
Yes, I will do that.
Quoted 8 lines
Documentation/git-cat-file.adoc newline. The available atoms are: · line 354
Quoted 5 lines
Documentation/git-cat-file.adoc +2
-1If no format is specified, the default format is `%(objectname) -%(objecttype) %(objectsize)`. +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which use +`%(objectname) %(objectsize)` for now because "%(objecttype)" is not supported yet.Nit: I would drop the 'for now' here, since we don't know when the changes for 'objecttype' will land.
Okay, I'll drop it.
Quoted 9 lines
[snip]
Quoted 6 lines
enum
batch_mode{
BATCH_MODE_CONTENTS,Documentation/git-cat-file.adoc +0
-0@@ -633,6 +649,81 @@ static void batch_one_object(const char *obj_name, object_context_release(&ctx); }
Quoted 16 lines
Quoted 4 lines
Documentation/git-cat-file.adoc +4
-0+static int get_remote_info(struct batch_options *opt, + int argc, + const char **argv, + struct object_info **remote_object_info,Documentation/git-cat-file.adoc static void batch_one_object(const char *obj_name, · line 665
Quoted 10 lines
Documentation/git-cat-file.adoc +10
-0+ struct oid_array *object_info_oids) +{ + int retval = 0; + struct remote *remote = NULL; + struct object_id oid; + struct string_list object_info_options = STRING_LIST_INIT_NODUP; + struct transport *gtransport; + + /* + * Change the format to "%(objectname) %(objectsize)" whenNit: perhaps prepend a "TODO"
I'll add it.
Quoted 44 lines
Quoted 44 lines
Documentation/git-cat-file.adoc +43
-0+ * remote-object-info command is used. Once we start supporting objecttype + * the default format should change to DEFAULT_FORMAT. + */ + if (!opt->format) + opt->format = "%(objectname) %(objectsize)"; + + remote = remote_get(argv[0]); + if (!remote) + die(_("must supply valid remote when using remote-object-info")); + + oid_array_clear(object_info_oids); + for (size_t i = 1; i < argc; i++) { + if (get_oid_hex(argv[i], &oid)) { + size_t len = strlen(argv[i]); + + if (len < the_hash_algo->hexsz && len >= 4) { + size_t j; + for (j = 0; j < len; j++) + if (!isxdigit(argv[i][j])) + break; + if (j == len) + die(_("remote-object-info does not support " + "short oids, %d characters required"), + (int)the_hash_algo->hexsz); + } + die(_("not a valid object name '%s'"), argv[i]); + } + oid_array_append(object_info_oids, &oid); + } + + if (!object_info_oids->nr) + die(_("remote-object-info requires objects")); + + gtransport = transport_get(remote, NULL); + + if (!gtransport->smart_options) { + retval = -1; + goto cleanup; + } + + CALLOC_ARRAY(*remote_object_info, object_info_oids->nr); + gtransport->smart_options->object_info = 1; + gtransport->smart_options->object_info_oids = object_info_oids;
Quoted 8 lines
Documentation/git-cat-file.adoc static void batch_one_object(const char *obj_name, · line 713
Quoted 5 lines
Documentation/git-cat-file.adoc +5
-0+ + /* 'objectsize' is the only option currently supported */ + if (!strstr(opt->format, "%(objectsize)")) + die(_("%s is currently not supported with remote-object-info"), opt->format); +Aren't we setting the
opt->formatourselves in this function? Why do we need to check it?
We only set opt->format when the user does not provide a custom format.
The strstr check catches cases like %(objecttype) alone, but is not sufficient for mixed formats like %(objecttype) %(objectsize). This is fixed in [12/13] with the allow-list.
Quoted 32 lines
Quoted 32 lines
Documentation/git-cat-file.adoc +24
-0+ string_list_append(&object_info_options, "size"); + + 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_refs(gtransport, NULL); + } +cleanup: + string_list_clear(&object_info_options, 0); + transport_disconnect(gtransport); + return retval; +} + struct object_cb_data { struct batch_options *opt; struct expand_data *expand;@@ -714,6 +805,57 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED, load_mailmap(); } +static void parse_cmd_remote_object_info(struct batch_options *opt, + const char *line, struct strbuf *output, + struct expand_data *data) +{ + int count; + const char **argv; + char *line_to_split; + struct object_info *remote_object_info = NULL; + struct oid_array object_info_oids = OID_ARRAY_INIT; + + if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
Quoted 8 lines
Documentation/git-cat-file.adoc static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 824
Quoted 6 lines
Documentation/git-cat-file.adoc +6
-0+ die(_("remote-object-info command too long")); + + line_to_split = xstrdup(line); + count = split_cmdline(line_to_split, &argv); + if (count < 0) + die(_("split remote-object-info command"));We should be using
split_cmdline_strerror()here
Ok, I'll use it.
Quoted 38 lines
Quoted 35 lines
Documentation/git-cat-file.adoc +34
-0+ if (count - 1 > MAX_ALLOWED_OBJ_LIMIT) + die(_("remote-object-info supports at most %d objects"), + MAX_ALLOWED_OBJ_LIMIT); + + if (get_remote_info(opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup; + + data->skip_object_info = 1; + for (size_t i = 0; i < object_info_oids.nr; i++) { + data->oid = object_info_oids.oid[i]; + 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; + batch_object_write(argv[i + 1], output, opt, data, NULL, 0); + } else { + report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing"); + } + } + 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]); + free(line_to_split); + free(argv); + free(remote_object_info); + oid_array_clear(&object_info_oids); +} +[snip]
Quoted 14 lines
Quoted 12 lines
t/meson.build +1
-0@@ -170,6 +170,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',[snip]
Thanks for the feedback,
Pablo.
PATCH v14 12/13 cat-file: validate remote atoms with allow_list
In reply to Pablo Sabaterstrstr() is not enough to validate the format placeholders in remote-object-info causing two errors:
- Atoms recognized by expand_atom() but the remote doesn't returns 1, but data->type contains garbage causing segfault.
- expand_atom() returns 0 for unknown atoms, calling strbuf_expand_bad_format() which ends in die() 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 sb 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() behaviour to expect an empty string and add an explicit test for empty string return on unknown placeholder.
Update caveat behaviour documentation.
--- Documentation/git-cat-file.adoc | 5 +++-- builtin/cat-file.c | 41 +++++++++++++++++++++++++++------- t/t1017-cat-file-remote-object-info.sh | 27 ++++++++++++++++++---- 3 files changed, 59 insertions(+), 14 deletions(-)
Documentation/git-cat-file.adoc +3 -2
@@ -451,8 +451,9 @@ CAVEATS ------- Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are -currently not supported by the `remote-object-info` command, we will raise -an error and exit when they appear in the format string. +currently not supported by the `remote-object-info` command, they will +return an empty string for remote queries, matching how `for-each-ref` +behaves for known but inapplicable placeholders. 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
@@ -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
PATCH v14 13/13 cat-file: make remote-object-info allow-list dynamic
In reply to Pablo SabaterThe 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
@@ -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 ; }
@@ -680,12 +672,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;/*
@@ -734,15 +726,12 @@ static int get_remote_info(struct batch_options *opt, gtransport->smart_options->object_info =1 ; 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_refs (gtransport,NULL ); } cleanup: -string_list_clear (&object_info_options,0 );transport_disconnect (gtransport);return retval; }
@@ -828,6 +817,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)
@@ -837,6 +841,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" ));
@@ -849,32 +854,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);
@@ -1190,6 +1220,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
@@ -39,6 +39,26 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arcase 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
v15 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis 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/28435046129
[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 since v14:
- Changed strtou_szt to be strtoumax_szt so there are no problems between size_t and unsigned long.
- Reworded commits to be more consistent and clear.
- gitprotocol-v2.adoc documentation is now being modified on commit [9/13].
- Added BUG() calls on [10/13].
- style and nits fixes.
---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info
Eric Ju (4):
git-compat-util: add strtoumax_szt() with error handling
cat-file: declare loop counter inside for()
t1006: split test utility functions into new 'lib-cat-file.sh'
cat-file: add remote-object-info to batch-command
Pablo Sabater (6):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: drop static advertise_sid variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
cat-file: validate remote atoms with an allow-list
cat-file: make remote-object-info allow-list dynamic
Documentation/git-cat-file.adoc | 29 +- Documentation/gitprotocol-v2.adoc | 11 +- Makefile | 1 + builtin/cat-file.c | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 115 ++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 48 +-- fetch-pack.h | 1 + git-compat-util.h | 20 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 15 +- transport-internal.h | 8 + transport.c | 46 +++ transport.h | 10 + 23 files changed, 1257 insertions(+), 80 deletions(-)
---
base-commit: e9019fcafe0040228b8631c30f97ae1adb61bcdc
change-id: 20260608-ps-eric-work-rebase-b73ae84ba671
Best regards,
Signature · 1 line
Pablo Sabater <[email protected]>
PATCH v15 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
@@ -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
PATCH v15 2/13 git-compat-util: add `strtoumax_szt()` with error handling
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
We already have strtoul_ui() and similar functions that provide proper error handling using strtoul from the standard library. However, there isn't currently a variant that returns a size_t.
Using strtoul is unreliable because size_t is platform-dependent, unsigned long could be too big to fit into a size_t or too small to hold a size_t.
Use strtoumax which returns a uintmax_t guaranteed to be at least as large as size_t, add a range check against SIZE_MAX to prevent size_t overflow.
This variant is needed in a subsequent commit to enable returning a size_t with proper error handling.
--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
git-compat-util.h +20 -0
@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoumax, with +* additional error handling to ensure robustness. +*/ +static inline int strtoumax_szt (char const *s,int base,size_t *result) +{ +uintmax_t uim; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ; + uim =strtoumax (s, &p, base); +if ((errno || *p || p == s) || uim >SIZE_MAX ) +return -1 ; + *result = uim; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Signature · 1 line
2.54.0
Re: [PATCH GSoC v15 02/13] git-compat-util: add `strtoumax_szt()` with error handling
Quoted 9 lines · Pablo Sabater
From: Eric Ju <[email protected]>
We already have
strtoul_ui()and similar functions that provide proper error handling usingstrtoulfrom the standard library. However, there isn't currently a variant that returns asize_t.Using
strtoulis unreliable becausesize_t is platform-dependent,unsigned longcould be too big to fit into asize_tor too small to hold asize_t.
It is somehow annoying to see that the commit log desciption, which is *clearly* meant to be plaintext, is so heavily riddled with backquoted references to code/program symbols. Yes, literal is a correct way to format them in both AsciiDoc and Markdown, so we very much welcome them in our documentation, but not in proposed log messages.
In any case, you dropped 't' in 'size_t' outside the pair of backquotes.
Quoted 1 line
Use
strtoumaxwhich returns auintmax_tguaranteed to be at least as
strtoumax(), as the convention you used above for strtoul_ui() is to suffix function names with ().
Quoted 2 lines
large as
size_t, add a range check againstSIZE_MAXto preventsize_toverflow.
OK.
Quoted 21 lines
This variant is needed in a subsequent commit to enable returning a
size_twith proper error handling.Mentored-by Karthik Nayak <[email protected]>Mentored-by Chandra Pratap <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)git-compat-util.h +3
-0@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoumax, with +* additional error handling to ensure robustness.
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 990
Quoted 10 lines
git-compat-util.h +10
-0+*/ +static inline int strtoumax_szt (char const *s,int base,size_t *result) +{ +uintmax_t uim; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ;
Hmph, wouldn't
if (*s == '-' || !*s) return -1
cut it? Since your call to strtoumax() checks that the string was parsed to the end by insisting *p is NUL?
If you are trying to more explicitly insist that s[] has only digits, which may not be a bad idea, as that is what we generally expect, then
if (!s[0] || s[strspn(s, "0123456789")]) return -1;
perhaps.
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 993
Quoted 3 lines
git-compat-util.h +3
-0+ uim =strtoumax (s, &p, base); +if ((errno || *p || p == s) || uim >SIZE_MAX ) +return -1 ;
And with !s[0] upfront, we can discard (p==s) case from here. Other strto*() wrappers we have may need the "cannot be empty" check, because they do not need any upfront validation of s[] like we do here (we do so to reject negative numbers), but since we do need to check s[] before calling the system strto*() function anyway, it is OK to be different here from the others.
If uintmax_t and size_t are of the same width, then (SIZE_MAX < uim) becomes mathmatically impossible, but hopefully no compiler or static checker is stupid enough to warn against it.
Quoted 7 lines
git-compat-util.h +4
-0+ *result = uim; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Re: [PATCH GSoC v15 02/13] git-compat-util: add `strtoumax_szt()` with error handling
Quoted 20 lines · Junio C Hamano
Quoted 9 lines · Pablo Sabater
From: Eric Ju <[email protected]>
We already have
strtoul_ui()and similar functions that provide proper error handling usingstrtoulfrom the standard library. However, there isn't currently a variant that returns asize_t.Using
strtoulis unreliable becausesize_t is platform-dependent,unsigned longcould be too big to fit into asize_tor too small to hold asize_t.It is somehow annoying to see that the commit log desciption, which is *clearly* meant to be plaintext, is so heavily riddled with backquoted references to code/program symbols. Yes,
literalis a correct way to format them in both AsciiDoc and Markdown, so we very much welcome them in our documentation, but not in proposed log messages.
Quoted 2 lines
In any case, you dropped 't' in '
size_t' outside the pair of backquotes.
Ok, I'll drop the backquotes for the commit messages of this series.
Quoted 5 lines
Quoted 2 lines
Use
strtoumaxwhich returns auintmax_tguaranteed to be at least as
strtoumax(), as the convention you used above forstrtoul_ui()is to suffix function names with ().
ACK.
Quoted 46 lines
Quoted 3 lines
large as
size_t, add a range check againstSIZE_MAXto preventsize_toverflow.OK.
Quoted 21 lines
This variant is needed in a subsequent commit to enable returning a
size_twith proper error handling.Mentored-by Karthik Nayak <[email protected]>Mentored-by Chandra Pratap <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)git-compat-util.h +3
-0@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to a size_t using the standard library's strtoumax, with +* additional error handling to ensure robustness. git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 990
Quoted 10 lines
git-compat-util.h +10
-0+*/ +static inline int strtoumax_szt (char const *s,int base,size_t *result) +{ +uintmax_t uim; +char *p; + + errno =0 ; +/* negative values would be accepted by strtoul */ +if (strchr (s,'-' )) +return -1 ;Hmph, wouldn't
if (*s == '-' || !*s) return -1cut it? Since your call to
strtoumax()checks that the string was parsed to the end by insisting *p is NUL?
Quoted 8 lines
If you are trying to more explicitly insist that s[] has only digits, which may not be a bad idea, as that is what we generally expect, then
if (!s[0] || s[strspn(s, "0123456789")]) return -1;perhaps.
I like the idea of only digits but, even though in this series I only use this function in base 10, I want the function to work in other bases, that's why I left the base in the function signature instead of hardcoding it. strspn(s, "0123456789") rejects bases >10 ("ff" for base 16) while strtoumax does support higher ones.
I think that it would be better to explicitly reject what we don't want similarly to "-":
if (!*s || isspace((unsigned char)*s) || *s == '-' || *s == '+')
return -1;
About that, strtoumax works fine with "+" and ignores starting whitespaces, but for consistency (we reject "-" and whitespaces between or at the end) rejecting whitespaces and +/- will be better and make the caller format it correctly.
I'll do that for the next version.
Quoted 11 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 993
Quoted 4 lines
git-compat-util.h +3
-0+ uim =strtoumax (s, &p, base); +if ((errno || *p || p == s) || uim >SIZE_MAX ) +return -1 ;And with !s[0] upfront, we can discard (p==s) case from here. Other strto*() wrappers we have may need the "cannot be empty" check, because they do not need any upfront validation of s[] like we do here (we do so to reject negative numbers), but since we do need to check s[] before calling the system strto*() function anyway, it is OK to be different here from the others.
Agreed
Quoted 5 lines
If
uintmax_tandsize_tare of the same width, then (SIZE_MAX< uim) becomes mathmatically impossible, but hopefully no compiler or static checker is stupid enough to warn against it.
Quoted 7 lines
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 1000
Quoted 7 lines
git-compat-util.h +4
-0+ *result = uim; +return 0 ; +} +static inline int strtol_i (char const *s,int base,int *result) {long ul;
Thanks for the feedback,
Pablo
Re: [PATCH GSoC v15 02/13] git-compat-util: add `strtoumax_szt()` with error handling
Quoted 9 lines · Pablo Sabater
Quoted 8 lines
If you are trying to more explicitly insist that s[] has only digits, which may not be a bad idea, as that is what we generally expect, then
if (!s[0] || s[strspn(s, "0123456789")]) return -1;perhaps.
Quoted 7 lines
I like the idea of only digits but, even though in this series I only use this function in base 10, I want the function to work in other bases, that's why I left the base in the function signature instead of hardcoding it.
strspn(s, "0123456789")rejects bases >10 ("ff" for base 16) while strtoumax does support higher ones.
I think that it would be better to explicitly reject what we don't want similarly to "-":
Let's step back a bit and think.
Where do we plan to use this function? Remember that being a superset is not always necessarily good for a helper function that serves as a format checker.
In the output of "git diff master...ps/cat-file-remote-object-info", there is only one caller, which is fetch_object_info(). It reads into object_info_data[].sizep. Do we expect to express the object size in anything but an unsigned decimal integer? Remember that it is better to be unambiguous when designing a protocol. We do not want a third-party reimplementation of whatever is talking to fetch_object_info() to send object size in hex ;-).
It may also be usable to parse the size of the object payload in object-file.c::parse_loose_header() but notice that it is already even stricter not to use strto<anything> system function and instead handcrafts the trivial number parsing. This would avoid system dependent funnyness, which is a good thing.
Quoted 9 lines
if (!*s ||
isspace((unsigned char)*s)|| *s == '-' || *s == '+')return -1;About that, strtoumax works fine with "+" and ignores starting whitespaces, but for consistency (we reject "-" and whitespaces between or at the end) rejecting whitespaces and +/- will be better and make the caller format it correctly.
I'll do that for the next version.
Re: [PATCH GSoC v15 02/13] git-compat-util: add `strtoumax_szt()` with error handling
Quoted 26 lines · Junio C Hamano
Quoted 9 lines · Pablo Sabater
Quoted 8 lines
If you are trying to more explicitly insist that s[] has only digits, which may not be a bad idea, as that is what we generally expect, then
if (!s[0] || s[strspn(s, "0123456789")]) return -1;perhaps.
Quoted 7 lines
I like the idea of only digits but, even though in this series I only use this function in base 10, I want the function to work in other bases, that's why I left the base in the function signature instead of hardcoding it.
strspn(s, "0123456789")rejects bases >10 ("ff" for base 16) while strtoumax does support higher ones.
I think that it would be better to explicitly reject what we don't want similarly to "-":Let's step back a bit and think.
Where do we plan to use this function? Remember that being a superset is not always necessarily good for a helper function that serves as a format checker.
Quoted 7 lines
In the output of "git diff master...ps/cat-file-remote-object-info", there is only one caller, which is
fetch_object_info(). It reads into object_info_data[].sizep. Do we expect to express the object size in anything but an unsigned decimal integer? Remember that it is better to be unambiguous when designing a protocol. We do not want a third-party reimplementation of whatever is talking tofetch_object_info()to send object size in hex ;-).
No haha, we don't want size being sent in hex :), I agree that it is better to be unambiguous. We could hardcode the base 10 but I feel that calling the function strtoumax_szt() when it does not support >10 base (or I hardcode the base to be 10) lies to a future developer that tries to use this function thinking that it behaves as strtoumax_*().
Maybe because it is called only once in this series it is better to have a static function close to its caller that explicitly does what we want and it is unambiguous.
If that sounds reasonable I'll move this function to the commit where it's called, call the function parse_object_size() and keep the strict digits only with strspn proposed.
Quoted 16 lines
It may also be usable to parse the size of the object payload in object-file.c::parse_loose_header() but notice that it is already even stricter not to use strto<anything> system function and instead handcrafts the trivial number parsing. This would avoid system dependent funnyness, which is a good thing.
Quoted 9 lines
if (!*s ||
isspace((unsigned char)*s)|| *s == '-' || *s == '+')return -1;About that, strtoumax works fine with "+" and ignores starting whitespaces, but for consistency (we reject "-" and whitespaces between or at the end) rejecting whitespaces and +/- will be better and make the caller format it correctly.
I'll do that for the next version.
Thanks,
Pablo
PATCH v15 3/13 cat-file: declare loop counter inside for()
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop.
Change the declaration of i to be inside the for loop for readability. While at it, we also change its type from int to size_t where the latter makes more sense.
--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
@@ -718,14 +718,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);
@@ -733,9 +731,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 ;
@@ -762,7 +758,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 };
@@ -772,7 +767,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
@@ -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
PATCH v15 4/13 t1006: split test utility functions into new 'lib-cat-file.sh'
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
This refactor extracts 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 this functions, the goal is to improve code reuse and readability,enabling future tests to leverage these utilities without duplicating code.
--- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-)
t/lib-cat-file.sh new file +16 -0
@@ -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 NULL 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 +1 -12
@@ -4,6 +4,7 @@ test_description='git cat-file' . ./test-lib.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
PATCH v15 5/13 fetch-pack: drop static `advertise_sid` variable
In reply to Pablo Sabaterwrite_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 used in two places:
1. In function do_fetch_pack():
if (!server_supports("session_id"))
advertise_sid = 0;
2. In function `fetch_pack_config()`:
repo_config_get_bool("transfer.advertisesid", &advertise_sid);Since do_fetch_pack() is only relevant for protocol v1, it can be ignored because write_fetch_command_and_capabilities() is only used in protocol v2.
About 2, call repo_config_get_bool() directly inside of the function.
While at it, change hash_algo's type to match hash_algo_by_name()'s actual return type (unsigned int) and make it const.
--- fetch-pack.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fetch-pack.c +4 -1
@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );
@@ -1395,7 +1398,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
Re: [PATCH GSoC v15 05/13] fetch-pack: drop static `advertise_sid` variable
Quoted 10 lines · Pablo Sabater
write_fetch_command_and_capabilities()is moved toconnect.cin a subsequent commit. To prepare for that, drop the static variable usage ofadvertise_sid. Currentlyadvertise_sidis used in two places:1. In function
do_fetch_pack():if (!server_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);
Quoted 5 lines
Since
do_fetch_pack()is only relevant for protocol v1, it can be ignored becausewrite_fetch_command_and_capabilities()is only used in protocol v2.About 2, call
repo_config_get_bool()directly inside of the function.
Puzzled. The patch does introduce a local on-stack variable with the same name, but does not remove the file-scope global one.
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1385
Quoted 11 lines
fetch-pack.c +3
-0@@ -1380,6 +1380,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; + +repo_config_get_bool (the_repository,"transfer.advertisesid" , &advertise_sid);
If there is no such configuration variable defined anywhere in the system, advertise_sid will be left uninitialied. Wouldn't it cause problems later.
Initialize it to 0 (if the default is not to advertise, which I think is the case but please double check), and you would be OK, probably?
Quoted 12 lines
fetch-pack.c +1
-1ensure_server_supports_v2 ("fetch" );packet_buf_write (req_buf,"command=fetch" );@@ -1395,7 +1398,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);
Quoted 2 lines
While at it, change
hash_algo's type to matchhash_algo_by_name()'s actual return type (unsigned int) and make itconst.
This one makes sense, but is better left out to a separate step. Especially when the primary focus of the change is iffy (see above).
PATCH v15 6/13 fetch-pack: move `write_fetch_command_and_capabilities()` to connect.c
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities() is refactored in a subsequent commit where it becomes 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
@@ -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; + +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
@@ -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
@@ -1376,40 +1376,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; - -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
PATCH v15 7/13 connect: make `write_fetch_command_and_capabilities()` more generic
In reply to Pablo SabaterRefactor 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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(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
@@ -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;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
@@ -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); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options);#endif
fetch-pack.c +2 -2
@@ -1387,7 +1387,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" );
@@ -2255,7 +2255,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
PATCH v15 8/13 fetch-pack: move fetch initialization
In reply to Pablo SabaterFrom: 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
@@ -1736,18 +1736,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
PATCH v15 9/13 serve: advertise object-info feature
In reply to Pablo SabaterFrom: 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.
While at it, 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.
--- Documentation/gitprotocol-v2.adoc | 11 ++++++++--- serve.c | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/gitprotocol-v2.adoc +8 -3
@@ -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 object IDs. The response of `object-info` is a list of the requested object ids and associated requested information, each separated by a single space. output = info flush-pkt - info = PKT-LINE(attrs) LF) + info = PKT-LINE(attrs LF) *PKT-LINE(obj-info LF) attrs = attr | attrs SP attrs + obj-size = 1*DIGIT + attr = "size" - obj-info = obj-id SP obj-size + obj-info = obj-id SP [obj-size] + + If the server does not recognize the object id, the response will be + `obj-id SP` regardless of the number of attributes requested. bundle-uri ~~~~~~~~~~
serve.c +4 -1
@@ -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
PATCH v15 10/13 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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 object IDs with the size feature 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 exit.
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. When the server does not recognize an OID, following the v2 protocol, the server returns "<OID> SP", when this happens, fetch_object_info() sets the corresponding size pointer to NULL so that callers can detect and handle it.
--- Makefile | 1 + fetch-object-info.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++++ fetch-pack.h | 1 + meson.build | 1 + transport-helper.c | 13 +++++-- transport-internal.h | 8 +++++ transport.c | 46 +++++++++++++++++++++++++ transport.h | 10 ++++++ 9 files changed, 195 insertions(+), 2 deletions(-)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +95 -0
95 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.h +1 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +11 -2
@@ -727,8 +727,7 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only. */ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
@@ -784,6 +783,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 +1338,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
@@ -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 +46 -0
73 lines not on this page · raw message
transport.h +10 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -55,6 +56,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 +314,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
PATCH v15 11/13 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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 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>
--- Documentation/git-cat-file.adoc | 29 +- builtin/cat-file.c | 144 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ 6 files changed, 859 insertions(+), 8 deletions(-)
Documentation/git-cat-file.adoc +22 -7
@@ -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,15 +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 at the first whitespace boundary. All characters before that
@@ -340,8 +345,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 +449,10 @@ scripting purposes. 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. + 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 +143 -1
169 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -170,6 +170,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 +680 -0
680 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v15 12/13 cat-file: validate remote atoms with an allow-list
In reply to Pablo Sabaterstrstr() 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
@@ -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
@@ -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
PATCH v15 13/13 cat-file: make remote-object-info allow-list dynamic
In reply to Pablo SabaterThe 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
@@ -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 ; }
@@ -680,12 +672,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;/*
@@ -733,15 +725,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; }
@@ -827,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)
@@ -836,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 ;if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE )die (_ ("remote-object-info command too long" ));
@@ -848,32 +853,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);
@@ -1189,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 +20 -0
@@ -41,6 +41,26 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arcase 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
v16 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis 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/29091116939
[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 since v15:
- Completely dropped the static advertise_sid variable at fetch-pack.c
- Split the hash_algo type change into its own commit.
- Removed strtoumax_szt() from git-compat-util.h (and its commit) into a static parse_object_size() helper.
- Removed backquotes from commit message bodies and fixed typos.
---
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: split test utility 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: fix hash_algo variable type
fetch-pack: drop static advertise_sid variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
cat-file: validate remote atoms with an allow-list
cat-file: make remote-object-info allow-list dynamic
Documentation/git-cat-file.adoc | 29 +- Documentation/gitprotocol-v2.adoc | 11 +- Makefile | 1 + builtin/cat-file.c | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 129 ++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 58 +-- fetch-pack.h | 1 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 15 +- transport-internal.h | 8 + transport.c | 46 +++ transport.h | 10 + 22 files changed, 1255 insertions(+), 86 deletions(-)
base-commit: f60db8d575adb79761d363e026fb49bddf330c73
PATCH v16 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
@@ -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
PATCH v16 2/13 cat-file: declare loop counter inside for()
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop.
Change the declaration of i to be inside the for loop for readability. While at it, we also change its type from int to size_t where the latter makes more sense.
--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
@@ -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
@@ -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
PATCH v16 3/13 t1006: split test utility functions into new 'lib-cat-file.sh'
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
This refactor extracts 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 this functions, the goal is to improve code reuse and readability,enabling future tests to leverage these utilities without duplicating code.
--- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-)
t/lib-cat-file.sh new file +16 -0
@@ -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 NULL 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 +1 -12
@@ -4,6 +4,7 @@ test_description='git cat-file' . ./test-lib.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
PATCH v16 4/13 fetch-pack: fix hash_algo variable type
In reply to Pablo Sabaterhash_algo_by_name() returns "unsigned int", but the variable that it is assigned to is "int".
Change hash_algo variable type to match hash_algo_by_name() type, also make it const because it is never modified.
--- fetch-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
@@ -1395,7 +1395,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
PATCH v16 5/13 fetch-pack: drop static advertise_sid variable
In reply to Pablo Sabaterwrite_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 used in two places:
1. In function do_fetch_pack():
if (!server_supports("session-id"))
advertise_sid = 0;2. In function fetch_pack_config():
repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, it is only relevant for v0/v1 protocol, move it into find_common().
About 2, call repo_config_get_bool() inside of write_fetch_command_and_capabilities() and find_common() replacing the static variable.
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
@@ -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
PATCH v16 6/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities() is refactored in a subsequent commit where it becomes 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
@@ -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
@@ -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
@@ -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
PATCH v16 7/13 connect: make write_fetch_command_and_capabilities() more generic
In reply to Pablo SabaterRefactor 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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(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
@@ -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
@@ -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); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options);#endif
fetch-pack.c +2 -2
@@ -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
PATCH v16 8/13 fetch-pack: move fetch initialization
In reply to Pablo SabaterFrom: 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
@@ -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
PATCH v16 9/13 serve: advertise object-info feature
In reply to Pablo SabaterFrom: 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.
While at it, 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.
--- Documentation/gitprotocol-v2.adoc | 11 ++++++++--- serve.c | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/gitprotocol-v2.adoc +8 -3
@@ -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 object IDs. The response of `object-info` is a list of the requested object ids and associated requested information, each separated by a single space. output = info flush-pkt - info = PKT-LINE(attrs) LF) + info = PKT-LINE(attrs LF) *PKT-LINE(obj-info LF) attrs = attr | attrs SP attrs + obj-size = 1*DIGIT + attr = "size" - obj-info = obj-id SP obj-size + obj-info = obj-id SP [obj-size] + + If the server does not recognize the object id, the response will be + `obj-id SP` regardless of the number of attributes requested. bundle-uri ~~~~~~~~~~
serve.c +4 -1
@@ -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
PATCH v16 10/13 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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 object IDs with the size feature 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 exit.
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. When the server does not recognize an OID, following the v2 protocol, the server returns "<OID> SP", when this happens, fetch_object_info() sets the corresponding size pointer to NULL so that callers can detect and handle it.
--- Makefile | 1 + fetch-object-info.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++ fetch-pack.h | 1 + meson.build | 1 + transport-helper.c | 13 +++++- transport-internal.h | 8 ++++ transport.c | 46 ++++++++++++++++++++++ transport.h | 10 +++++ 9 files changed, 209 insertions(+), 2 deletions(-)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +109 -0
109 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.h +1 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +11 -2
@@ -727,8 +727,7 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only. */ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
@@ -784,6 +783,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 +1338,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
@@ -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 +46 -0
73 lines not on this page · raw message
transport.h +10 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -55,6 +56,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 +314,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
Re: [PATCH GSoC v16 10/13] transport: add client support for object-info
On Fri Jul 10, 2026 at 6:41 PM CEST, Pablo Sabater wrote:
[snip]
transport.c line 2
Quoted 7 lines
transport.c +1
-0@@ -1,3 +1,4 @@ +#include "compat/posix.h" #define USE_THE_REPOSITORY_VARIABLE
Hi while working on another series I realized that I had introduced this include which is wrong. I didn't notice because strangely it didn't gave any problems on the CI.
I'll drop it next reroll.
Regards,
Pablo.
PATCH v16 11/13 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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>
--- Documentation/git-cat-file.adoc | 29 +- builtin/cat-file.c | 144 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ 6 files changed, 859 insertions(+), 8 deletions(-)
Documentation/git-cat-file.adoc +22 -7
@@ -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,15 +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 at the first whitespace boundary. All characters before that
@@ -340,8 +345,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 +449,10 @@ scripting purposes. 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. + 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 +143 -1
169 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -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 +680 -0
680 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v16 12/13 cat-file: validate remote atoms with an allow-list
In reply to Pablo Sabaterstrstr() 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
@@ -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
@@ -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
PATCH v16 13/13 cat-file: make remote-object-info allow-list dynamic
In reply to Pablo SabaterThe 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
@@ -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
@@ -55,6 +55,26 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arcase 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
Quoted 99 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/29091116939
[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 since v15:
- Completely dropped the staticadvertise_sidvariable atfetch-pack.c
- Split thehash_algotype change into its own commit.
- Removedstrtoumax_szt()fromgit-compat-util.h(and its commit) into a staticparse_object_size()helper.
- Removed backquotes from commit message bodies and fixed typos.Signed-off-by Pablo Sabater <[email protected]>---
Calvin Wan (3):
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-infoEric Ju (3):
cat-file: declare loop counter insidefor()
t1006: split test utility functions into new 'lib-cat-file.sh'
cat-file: add remote-object-info to batch-commandPablo Sabater (7):
transport-helper: fix memory leak of helper on disconnect
fetch-pack: fixhash_algovariable type
fetch-pack: drop staticadvertise_sidvariable
fetch-pack: movewrite_fetch_command_and_capabilities()toconnect.c
connect: makewrite_fetch_command_and_capabilities()more generic
cat-file: validate remote atoms with an allow-list
cat-file: make remote-object-info allow-list dynamicDocumentation/git-cat-file.adoc | 29 +- Documentation/gitprotocol-v2.adoc | 11 +- Makefile | 1 + builtin/cat-file.c | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 129 ++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 58 +-- fetch-pack.h | 1 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 15 +- transport-internal.h | 8 + transport.c | 46 +++ transport.h | 10 + 22 files changed, 1255 insertions(+), 86 deletions(-)
Quoted 1 line
base-commit:
f60db8d575adb79761d363e026fb49bddf330c73
This version looks fine to me.
Thanks,
Chandra.
v17 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis 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/28435046129
[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 since v16:
- Droped a wrongly introduced include at transport.c
---
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: split test utility 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: fix hash_algo variable type
fetch-pack: drop static advertise_sid variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
cat-file: validate remote atoms with an allow-list
cat-file: make remote-object-info allow-list dynamic
Documentation/git-cat-file.adoc | 29 +- Documentation/gitprotocol-v2.adoc | 11 +- Makefile | 1 + builtin/cat-file.c | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 129 ++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 58 +-- fetch-pack.h | 1 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 15 +- transport-internal.h | 8 + transport.c | 45 +++ transport.h | 10 + 22 files changed, 1254 insertions(+), 86 deletions(-)
---
base-commit: f60db8d575adb79761d363e026fb49bddf330c73
PATCH v17 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
@@ -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
PATCH v17 2/13 cat-file: declare loop counter inside for()
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop.
Change the declaration of i to be inside the for loop for readability. While at it, we also change its type from int to size_t where the latter makes more sense.
--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
@@ -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
@@ -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
PATCH v17 3/13 t1006: split test utility functions into new 'lib-cat-file.sh'
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
This refactor extracts 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 this functions, the goal is to improve code reuse and readability,enabling future tests to leverage these utilities without duplicating code.
--- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-)
t/lib-cat-file.sh new file +16 -0
@@ -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 NULL 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 +1 -12
@@ -4,6 +4,7 @@ test_description='git cat-file' . ./test-lib.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
PATCH v17 4/13 fetch-pack: fix hash_algo variable type
In reply to Pablo Sabaterhash_algo_by_name() returns "unsigned int", but the variable that it is assigned to is "int".
Change hash_algo variable type to match hash_algo_by_name() type, also make it const because it is never modified.
--- fetch-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
@@ -1395,7 +1395,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
PATCH v17 5/13 fetch-pack: drop static advertise_sid variable
In reply to Pablo Sabaterwrite_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 used in two places:
1. In function do_fetch_pack():
if (!server_supports("session-id"))
advertise_sid = 0;2. In function fetch_pack_config():
repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, it is only relevant for v0/v1 protocol, move it into find_common().
About 2, call repo_config_get_bool() inside of write_fetch_command_and_capabilities() and find_common() replacing the static variable.
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
@@ -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
PATCH v17 6/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities() is refactored in a subsequent commit where it becomes 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
@@ -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
@@ -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
@@ -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
PATCH v17 7/13 connect: make write_fetch_command_and_capabilities() more generic
In reply to Pablo SabaterRefactor 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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(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
@@ -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
@@ -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); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options);#endif
fetch-pack.c +2 -2
@@ -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
PATCH v17 8/13 fetch-pack: move fetch initialization
In reply to Pablo SabaterFrom: 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
@@ -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
PATCH v17 9/13 serve: advertise object-info feature
In reply to Pablo SabaterFrom: 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.
While at it, 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.
--- Documentation/gitprotocol-v2.adoc | 11 ++++++++--- serve.c | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/gitprotocol-v2.adoc +8 -3
@@ -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 object IDs. The response of `object-info` is a list of the requested object ids and associated requested information, each separated by a single space. output = info flush-pkt - info = PKT-LINE(attrs) LF) + info = PKT-LINE(attrs LF) *PKT-LINE(obj-info LF) attrs = attr | attrs SP attrs + obj-size = 1*DIGIT + attr = "size" - obj-info = obj-id SP obj-size + obj-info = obj-id SP [obj-size] + + If the server does not recognize the object id, the response will be + `obj-id SP` regardless of the number of attributes requested. bundle-uri ~~~~~~~~~~
serve.c +4 -1
@@ -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
PATCH v17 10/13 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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 object IDs with the size feature 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 exit.
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. When the server does not recognize an OID, following the v2 protocol, the server returns "<OID> SP", when this happens, fetch_object_info() sets the corresponding size pointer to NULL so that callers can detect and handle it.
--- Makefile | 1 + fetch-object-info.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 +++++++++++ fetch-pack.h | 1 + meson.build | 1 + transport-helper.c | 13 +++++- transport-internal.h | 8 ++++ transport.c | 45 +++++++++++++++++++++ transport.h | 10 +++++ 9 files changed, 208 insertions(+), 2 deletions(-)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +109 -0
109 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.h +1 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +11 -2
@@ -727,8 +727,7 @@ static int fetch_refs(struct transport *transport,/* * If we reach here, then the server, the client, and/or the transport -* helper does not support protocol v2. --negotiate-only requires -* protocol v2. +* helper does not support protocol v2. --negotiate-only. */ if (data->transport_options.acked_commits) {warning (_ ("--negotiate-only requires protocol v2" ));
@@ -784,6 +783,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 +1338,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
@@ -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 +10 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -55,6 +56,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 +314,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
Re: [PATCH GSoC v17 10/13] transport: add client support for object-info
fetch-object-info.c line 86
Quoted 1 line · Pablo Sabater
fetch-object-info.c new file +1
-0+for (size_t i =0 ;packet_reader_read (reader) ==PACKET_READ_NORMAL && i < args->oids->nr; i++) {
An overly long line. Format it like this, perhaps?
for (size_t i = 0;
packet_reader_read(reader) == PACKET_READ_NORMAL && i < args->oids->nr;
i++) {or even:
for (size_t i = 0;
packet_reader_read(reader) == PACKET_READ_NORMAL &&
i < args->oids->nr;
i++) {Quoted 8 lines
fetch-object-info.c new file +8
-0+struct string_list object_info_values =STRING_LIST_INIT_DUP ; + +string_list_split (&object_info_values, reader->line," " ,-1 ); +if (size_index >=0 ) { +if (!strcmp (object_info_values.items[1 + size_index].string,"" )) { +FREE_AND_NULL (object_info_data[i].sizep); +string_list_clear (&object_info_values,0 ); +continue ;
fetch-object-info.c line 104
Quoted 10 lines
fetch-object-info.c new file +10
-0+ } + +if (parse_object_size (object_info_values.items[1 + size_index].string, + object_info_data[i].sizep)) +die ("object-info: ref %s has invalid size %s" , + object_info_values.items[0 ].string, + object_info_values.items[1 + size_index].string); + } + +string_list_clear (&object_info_values,0 );
Is this not trusting the other side too much?
If the other end returns fewer values than expected (e.g., if a buggy or malicious server returns only "<oid>" without a trailing space for an unrecognized object, or if we request multiple attributes in the future and the server returns fewer values than expected), string_list_split may return a list with fewer elements than size_index + 1. Accessing object_info_values.items[size_index
+ 1] will then result in an out-of-bounds read/crash.
By the way, from a stylistic standpoint, "size_index + 1" reads a bit more naturally than the "1 + size_index" used in the current patch.
Re: [PATCH GSoC v17 10/13] transport: add client support for object-info
Quoted 10 lines · Junio C Hamano
fetch-object-info.c line 86
Quoted 1 line · Pablo Sabater
fetch-object-info.c new file +1
-0+for (size_t i =0 ;packet_reader_read (reader) ==PACKET_READ_NORMAL && i < args->oids->nr; i++) {An overly long line. Format it like this, perhaps?
for (size_t i = 0; packet_reader_read(reader) == PACKET_READ_NORMAL && i < args->oids->nr; i++) {
Quoted 7 lines
or even:
for (size_t i = 0; packet_reader_read(reader) == PACKET_READ_NORMAL && i < args->oids->nr; i++) {
Will wrap that line, thanks.
Quoted 22 lines
Quoted 9 lines
fetch-object-info.c new file +8
-0+struct string_list object_info_values =STRING_LIST_INIT_DUP ; + +string_list_split (&object_info_values, reader->line," " ,-1 ); +if (size_index >=0 ) { +if (!strcmp (object_info_values.items[1 + size_index].string,"" )) { +FREE_AND_NULL (object_info_data[i].sizep); +string_list_clear (&object_info_values,0 ); +continue ;fetch-object-info.c line 104
Quoted 10 lines
fetch-object-info.c new file +10
-0+ } + +if (parse_object_size (object_info_values.items[1 + size_index].string, + object_info_data[i].sizep)) +die ("object-info: ref %s has invalid size %s" , + object_info_values.items[0 ].string, + object_info_values.items[1 + size_index].string); + } + +string_list_clear (&object_info_values,0 );Is this not trusting the other side too much?
Quoted 7 lines
If the other end returns fewer values than expected (e.g., if a buggy or malicious server returns only "<oid>" without a trailing space for an unrecognized object, or if we request multiple attributes in the future and the server returns fewer values than expected),
string_list_splitmay return a list with fewer elements thansize_index+ 1. Accessing object_info_values.items[size_index
+ 1] will then result in an out-of-bounds read/crash.
I will add a check for a malformed response from the server so "<oid>" is considered corrupted, similar to the size values a few lines below.
A subsequent commit in this series (13) adds a filter that drops attributes requested by the client but that the server doesn't support so we should expect full return of the attributes asked or "<oid> SP".
I'll add a guard just in case in a future what we expect changes.
Quoted 4 lines
By the way, from a stylistic standpoint, "
size_index+ 1" reads a bit more naturally than the "1 +size_index" used in the current patch.
Will change it.
Thanks for the feedback,
Pablo.
PATCH v17 11/13 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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>
--- Documentation/git-cat-file.adoc | 29 +- builtin/cat-file.c | 144 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ 6 files changed, 859 insertions(+), 8 deletions(-)
Documentation/git-cat-file.adoc +22 -7
@@ -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,15 +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 at the first whitespace boundary. All characters before that
@@ -340,8 +345,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 +449,10 @@ scripting purposes. 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. + 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 +143 -1
169 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -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 +680 -0
680 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v17 12/13 cat-file: validate remote atoms with an allow-list
In reply to Pablo Sabaterstrstr() 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
@@ -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
@@ -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
PATCH v17 13/13 cat-file: make remote-object-info allow-list dynamic
In reply to Pablo SabaterThe 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
@@ -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
@@ -55,6 +55,26 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arcase 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
Quoted 3 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.
Yuck. I thought we had this marked as "Will merge to 'next'?" for some time and this morning I pushed out a merge to 'next' of v16. I'll revert the merge and replace.
Quoted 9 lines · Junio C Hamano
Quoted 3 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.
Yuck. I thought we had this marked as "Will merge to 'next'?" for some time and this morning I pushed out a merge to 'next' of v16. I'll revert the merge and replace.
Hi!
Sorry, I'm confused about the last line about 'next' and the replace.
You gave me feedback for v17 10th commit:
https://lore.kernel.org/git/[email protected]/
Should I send a v18 or a new patch on top of 'next'?
The fix is simple and I already have it on my local, I just want to do whatever is better.
Regards,
Pablo.
Quoted 8 lines · Pablo Sabater
You gave me feedback for v17 10th commit:
https://lore.kernel.org/git/[email protected]/
Should I send a v18 or a new patch on top of 'next'?
The fix is simple and I already have it on my local, I just want to do whatever is better.
I had v16 merged (prematurely) to 'next' and then saw v17, so I reverted the merge, which means 'next' no longer has your topic.
And v17, as a brand new iteration, is not in, and will stay out of, 'next' until we are happy with it. If you have an updated v18, please send it as a whole replacement.
Thanks. How close are we to the finish line, by the way?
Quoted 11 lines · Junio C Hamano
Quoted 8 lines · Pablo Sabater
You gave me feedback for v17 10th commit:
https://lore.kernel.org/git/[email protected]/
Should I send a v18 or a new patch on top of 'next'?
The fix is simple and I already have it on my local, I just want to do whatever is better.
Quoted 6 lines
I had v16 merged (prematurely) to 'next' and then saw v17, so I reverted the merge, which means 'next' no longer has your topic.
And v17, as a brand new iteration, is not in, and will stay out of, 'next' until we are happy with it. If you have an updated v18, please send it as a whole replacement.
I'll send v18 very soon.
Quoted 2 lines
Thanks. How close are we to the finish line, by the way?
There's one month left. Final evaluation ends on 17th August (more weeks can be asked, if it seems too rushed, but I think it will fit just right). I have the other series that supports objecttype but I wanted this to be on 'next' before sending it.
I expect it to go faster because it's on top of all the infrastructure built in this series and it's ~150 lines.
Quoted 4 lines · Pablo Sabater
Quoted 1 line
Thanks. How close are we to the finish line, by the way?
There's one month left. Final evaluation ends on 17th August (more weeks can be asked, if it seems too rushed, ...
That is the deadline to wrap up your work, which is not quite what I was asking. I meant to ask how close you assess this topic is to completion at iteration #18. Are all remaining issues just minor nits? Are there still large gaps between the desired and actual behavior of the new feature? That sort of thing.
Thanks.
Quoted 4 lines · Junio C Hamano
Quoted 2 lines · Pablo Sabater
Quoted 1 line
Thanks. How close are we to the finish line, by the way?
Quoted 8 lines
Quoted 2 lines
There's one month left. Final evaluation ends on 17th August (more weeks can be asked, if it seems too rushed, ...
That is the deadline to wrap up your work, which is not quite what I was asking. I meant to ask how close you assess this topic is to completion at iteration #18. Are all remaining issues just minor nits? Are there still large gaps between the desired and actual behavior of the new feature? That sort of thing.
Oh, sorry.
It already does what it's supposed to do, the last rerolls have been mostly cleanups.
If there's anything more to do it should only be minor nits.
Quoted 2 lines
Thanks.
Regards,
Pablo
v18 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis 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]/
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.
---
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: split test utility 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: fix hash_algo variable type
fetch-pack: drop static advertise_sid variable
fetch-pack: move write_fetch_command_and_capabilities() to connect.c
connect: make write_fetch_command_and_capabilities() more generic
cat-file: validate remote atoms with an allow-list
cat-file: make remote-object-info allow-list dynamic
Documentation/git-cat-file.adoc | 29 +- Documentation/gitprotocol-v2.adoc | 11 +- Makefile | 1 + builtin/cat-file.c | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 141 +++++++ fetch-object-info.h | 22 ++ fetch-pack.c | 58 +-- fetch-pack.h | 1 + meson.build | 1 + object-file.c | 10 + odb.h | 3 + serve.c | 5 +- t/lib-cat-file.sh | 16 + t/meson.build | 1 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 699 +++++++++++++++++++++++++++++++++ transport-helper.c | 12 +- transport-internal.h | 8 + transport.c | 45 +++ transport.h | 10 + 22 files changed, 1265 insertions(+), 84 deletions(-)
---
base-commit: f60db8d575adb79761d363e026fb49bddf330c73
PATCH v18 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
@@ -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
PATCH v18 2/13 cat-file: declare loop counter inside for()
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
Some code used in this series declares variable i and only uses it in a for loop, not in any other logic outside the loop.
Change the declaration of i to be inside the for loop for readability. While at it, we also change its type from int to size_t where the latter makes more sense.
--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)
builtin/cat-file.c +4 -9
@@ -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
@@ -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
PATCH v18 3/13 t1006: split test utility functions into new 'lib-cat-file.sh'
In reply to Pablo SabaterFrom: Eric Ju <[email protected]>
This refactor extracts 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 this functions, the goal is to improve code reuse and readability,enabling future tests to leverage these utilities without duplicating code.
--- t/lib-cat-file.sh | 16 ++++++++++++++++ t/t1006-cat-file.sh | 13 +------------ 2 files changed, 17 insertions(+), 12 deletions(-)
t/lib-cat-file.sh new file +16 -0
@@ -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 NULL 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 +1 -12
@@ -4,6 +4,7 @@ test_description='git cat-file' . ./test-lib.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
PATCH v18 4/13 fetch-pack: fix hash_algo variable type
In reply to Pablo Sabaterhash_algo_by_name() returns "unsigned int", but the variable that it is assigned to is "int".
Change hash_algo variable type to match hash_algo_by_name() type, also make it const because it is never modified.
--- fetch-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fetch-pack.c +1 -1
@@ -1395,7 +1395,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
PATCH v18 5/13 fetch-pack: drop static advertise_sid variable
In reply to Pablo Sabaterwrite_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 used in two places:
1. In function do_fetch_pack():
if (!server_supports("session-id"))
advertise_sid = 0;2. In function fetch_pack_config():
repo_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, it is only relevant for v0/v1 protocol, move it into find_common().
About 2, call repo_config_get_bool() inside of write_fetch_command_and_capabilities() and find_common() replacing the static variable.
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
@@ -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
Re: [PATCH GSoC v18 05/13] fetch-pack: drop static advertise_sid variable
Quoted 11 lines · 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 ofadvertise_sid. Currentlyadvertise_sidis used in two places:1. In function
do_fetch_pack():if (!server_supports("session-id")) advertise_sid = 0;2. In function
fetch_pack_config():repo_config_get_bool("transfer.advertisesid", &advertise_sid);
Nit: But #2 isn't a usecase, it's where it is set no? Looking at the usecase, it seems like we have two:
#1 like you stated
#2 within write_fetch_command_and_capabilities(). But the flow is
that, the variable is set in fetch_pack_config(), right?
Quoted 51 lines
About 1, it is only relevant for v0/v1 protocol, move it into
find_common().About 2, call
repo_config_get_bool()inside ofwrite_fetch_command_and_capabilities()andfind_common()replacing the static variable.Because
repo_config_get_bool()leavesadvertise_sidas is if it is not set, initialize it to 0 matching its default.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- fetch-pack.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)fetch-pack.c +4
-2@@ -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" );
fetch-pack.c static struct ref *do_fetch_pack(struct fetch_pack_args *args, · line 1165
Quoted 6 lines
fetch-pack.c +0
-3@@ -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 ; -
So earlier we'd set the advertise_sid to 0 if the server didn't support 'session-id'. But we could directly check where it is needed, which is find_common(). Is find_common() only called in v0/v1 as stated in the commit message?
Quoted 24 lines
fetch-pack.c +3
-2if (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; --2.54.0
Re: [PATCH GSoC v18 05/13] fetch-pack: drop static advertise_sid variable
Quoted 14 lines · Karthik Nayak
Quoted 11 lines · 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 ofadvertise_sid. Currentlyadvertise_sidis used in two places:1. In function
do_fetch_pack():if (!server_supports("session-id")) advertise_sid = 0;2. In function
fetch_pack_config():repo_config_get_bool("transfer.advertisesid", &advertise_sid);
Quoted 6 lines
Nit: But #2 isn't a usecase, it's where it is set no? Looking at the usecase, it seems like we have two:
#1 like you stated
#2 withinwrite_fetch_command_and_capabilities(). But the flow is
that, the variable is set infetch_pack_config(), right?
Completly true, fetch_pack_config() is where it's set, but not where it's used.
I'll fix #2 to be where it is used.
Thanks.
Quoted 55 lines
Quoted 55 lines
About 1, it is only relevant for v0/v1 protocol, move it into
find_common().About 2, call
repo_config_get_bool()inside ofwrite_fetch_command_and_capabilities()andfind_common()replacing the static variable.Because
repo_config_get_bool()leavesadvertise_sidas is if it is not set, initialize it to 0 matching its default.Helped-by Jonathan Tan <[email protected]>Helped-by Christian Couder <[email protected]>Signed-off-by Calvin Wan <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- fetch-pack.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)fetch-pack.c +4
-2@@ -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); }
Quoted 8 lines
fetch-pack.c static struct ref *do_fetch_pack(struct fetch_pack_args *args, · line 1165
Quoted 3 lines
fetch-pack.c +0
-3-if (!server_supports ("session-id" )) - advertise_sid =0 ; -So earlier we'd set the
advertise_sidto 0 if the server didn't support 'session-id'. But we could directly check where it is needed, which isfind_common(). Isfind_common()only called in v0/v1 as stated in the commit message?
Yes, find_common() is only called from do_fetch_pack() which is the v0/v1 path, if we take a look at fetch_pack():
if (version == protocol_v2) {
[snip]
ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
&shallows_scratch, &si,
pack_lockfiles);
} else {
[snip]
ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
&si, pack_lockfiles);
}Quoted 23 lines
Quoted 23 lines
fetch-pack.c +3
-1if (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;
Quoted 2 lines
Quoted 2 lines
--
2.54.0
Thanks for the feedback, will make it clear next reroll, Pablo
PATCH v18 6/13 fetch-pack: move write_fetch_command_and_capabilities() to connect.c
In reply to Pablo Sabaterwrite_fetch_command_and_capabilities() is refactored in a subsequent commit where it becomes 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
@@ -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
@@ -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
@@ -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
PATCH v18 7/13 connect: make write_fetch_command_and_capabilities() more generic
In reply to Pablo SabaterRefactor 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 https://git-scm.com/docs/protocol-v2, such as a Git subcommand (e.g., git-fetch(1)) or a server-side operation like "object-info" as implemented in commit a2ba162
(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
@@ -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
@@ -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); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options);#endif
fetch-pack.c +2 -2
@@ -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
PATCH v18 8/13 fetch-pack: move fetch initialization
In reply to Pablo SabaterFrom: 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
@@ -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
PATCH v18 9/13 serve: advertise object-info feature
In reply to Pablo SabaterFrom: 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.
While at it, 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.
--- Documentation/gitprotocol-v2.adoc | 11 ++++++++--- serve.c | 5 ++++- 2 files changed, 12 insertions(+), 4 deletions(-)
Documentation/gitprotocol-v2.adoc +8 -3
@@ -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 object IDs. The response of `object-info` is a list of the requested object ids and associated requested information, each separated by a single space. output = info flush-pkt - info = PKT-LINE(attrs) LF) + info = PKT-LINE(attrs LF) *PKT-LINE(obj-info LF) attrs = attr | attrs SP attrs + obj-size = 1*DIGIT + attr = "size" - obj-info = obj-id SP obj-size + obj-info = obj-id SP [obj-size] + + If the server does not recognize the object id, the response will be + `obj-id SP` regardless of the number of attributes requested. bundle-uri ~~~~~~~~~~
serve.c +4 -1
@@ -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
PATCH v18 10/13 transport: add client support for object-info
In reply to Pablo SabaterFrom: 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 object IDs with the size feature 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 exit.
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. When the server does not recognize an OID, following the v2 protocol, the server returns "<OID> SP", when this happens, fetch_object_info() sets the corresponding size pointer to NULL so that callers can detect and handle it.
--- Makefile | 1 + fetch-object-info.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++ fetch-pack.h | 1 + meson.build | 1 + transport-helper.c | 10 +++++ transport-internal.h | 8 ++++ transport.c | 45 +++++++++++++++++++ transport.h | 10 +++++ 9 files changed, 219 insertions(+)
Makefile +1 -0
@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
fetch-object-info.c new file +121 -0
121 lines not on this page · raw message
fetch-object-info.h new file +22 -0
@@ -0,0 +1,22 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "odb.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +/* +* 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 */
fetch-pack.h +1 -0
@@ -16,6 +16,7 @@ struct fetch_pack_args {const struct string_list *deepen_not;struct list_objects_filter_options filter_options;const struct string_list *server_options; +struct object_info *object_info_data;/* * If not NULL, during packfile negotiation, fetch-pack will send "have"
meson.build +1 -0
@@ -347,6 +347,7 @@ libgit_sources = [ 'exec-cmd.c', 'fetch-negotiator.c', 'fetch-pack.c', + 'fetch-object-info.c', 'fmt-merge-msg.c', 'fsck.c', 'fsmonitor.c',
transport-helper.c +10 -0
@@ -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
@@ -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 +10 -0
@@ -6,6 +6,7 @@#include "list-objects-filter-options.h" #include "string-list.h" #include "connect.h" +#include "odb.h" struct git_transport_options {unsigned thin :1 ;
@@ -55,6 +56,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 +314,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
--
2.54.0
Re: [PATCH GSoC v18 10/13] transport: add client support for object-info
fetch-object-info.c line 46
Quoted 13 lines · Pablo Sabater
fetch-object-info.c new file +13
-0+static size_t parse_object_size (const char *s,size_t *res) +{ +uintmax_t uim; + +if (!s[0 ] || s[strspn (s,"0123456789" )]) +return -1 ; + errno =0 ; + uim =strtoumax (s,NULL ,10 ); +if (errno || uim >SIZE_MAX ) +return -1 ; + *res = uim; +return 0 ; +}
Since size_t is unsigned, returning -1 is a bit problematic, isn't it? Perhaps this should return a plain 'int' instead.
The sole caller only cares about a boolean "did we succeed or fail?" result, and more importantly, the actual size parsed is already returned via the out-parameter.
Thanks.
Re: [PATCH GSoC v18 10/13] transport: add client support for object-info
Quoted 19 lines · Junio C Hamano
fetch-object-info.c line 46
Quoted 13 lines · Pablo Sabater
fetch-object-info.c new file +13
-0+static size_t parse_object_size (const char *s,size_t *res) +{ +uintmax_t uim; + +if (!s[0 ] || s[strspn (s,"0123456789" )]) +return -1 ; + errno =0 ; + uim =strtoumax (s,NULL ,10 ); +if (errno || uim >SIZE_MAX ) +return -1 ; + *res = uim; +return 0 ; +}Since
size_tis unsigned, returning -1 is a bit problematic, isn't it? Perhaps this should return a plain 'int' instead.
Quoted 5 lines
The sole caller only cares about a boolean "did we succeed or fail?" result, and more importantly, the actual size parsed is already returned via the out-parameter.
Thanks.
Completly true, when I changed this from beign strtoumax_szt() I must have been thinking too much about size_t.
I will change it to return int. Thanks.
Regards,
Pablo
Re: [PATCH GSoC v18 10/13] transport: add client support for object-info
In reply to Pablo SabaterMakefile LIB_OBJS += ewah/ewah_rlw.o · line 1165
Quoted 8 lines · Pablo Sabater
Makefile +1
-0@@ -1159,6 +1159,7 @@ LIB_OBJS += ewah/ewah_rlw.o LIB_OBJS += exec-cmd.o LIB_OBJS += fetch-negotiator.o LIB_OBJS += fetch-pack.o +LIB_OBJS += fetch-object-info.o LIB_OBJS += fmt-merge-msg.o LIB_OBJS += fsck.o LIB_OBJS += fsmonitor.o
Noticed while preparing evil merge for Patrick's patch to move everything under a new lib/ directory, but you inserted the new entry in a wrong place, i.e. 'o' < 'p'. Keep the list sorted.
PATCH v18 11/13 cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterFrom: 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>
--- Documentation/git-cat-file.adoc | 29 +- builtin/cat-file.c | 144 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 680 +++++++++++++++++++++++++++++++++ 6 files changed, 859 insertions(+), 8 deletions(-)
Documentation/git-cat-file.adoc +22 -7
@@ -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,15 +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 at the first whitespace boundary. All characters before that
@@ -340,8 +345,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 +449,10 @@ scripting purposes. 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. + 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 +143 -1
169 lines not on this page · raw message
object-file.c +10 -0
@@ -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
@@ -573,4 +573,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
@@ -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 +680 -0
680 lines not on this page · raw message
Signature · 1 line
2.54.0
Quoted 14 lines · Pablo Sabater
builtin/cat-file.c +14
-0+static void parse_cmd_remote_object_info (struct batch_options *opt, +const char *line,struct strbuf *output, +struct expand_data *data) +{ +int count; +const char **argv; +char *line_to_split; +struct object_info *remote_object_info =NULL ; +struct oid_array object_info_oids =OID_ARRAY_INIT ; + +if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) +die (_ ("remote-object-info command too long" )); + + line_to_split =xstrdup (line);
builtin/cat-file.c static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 833
Quoted 10 lines
builtin/cat-file.c +10
-0+ count =split_cmdline (line_to_split, &argv); +if (count <0 ) +die (_ ("remote-object-info: %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 ); + +if (get_remote_info (opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup;
Since this function does not return a value, the caller cannot even tell if there was an error if we just silently return like this. Is it really OK to silently ignore such a failure? Should we not die() loudly to report it instead?
Thanks.
Quoted 22 lines
Quoted 22 lines
builtin/cat-file.c +21
-0+static void parse_cmd_remote_object_info (struct batch_options *opt, +const char *line,struct strbuf *output, +struct expand_data *data) +{ +int count; +const char **argv; +char *line_to_split; +struct object_info *remote_object_info =NULL ; +struct oid_array object_info_oids =OID_ARRAY_INIT ; + +if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) +die (_ ("remote-object-info command too long" )); + + line_to_split =xstrdup (line); + count =split_cmdline (line_to_split, &argv); +if (count <0 ) +die (_ ("remote-object-info: %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 ); +
Quoted 8 lines
builtin/cat-file.c static void parse_cmd_mailmap(struct batch_options *opt UNUSED, · line 833
Quoted 3 lines
builtin/cat-file.c +3
-0+if (get_remote_info (opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup;Since this function does not return a value, the caller cannot even tell if there was an error if we just silently return like this. Is it really OK to silently ignore such a failure? Should we not
die()loudly to report it instead?
True, this comes from the v11 before I got into, I think Eric tried to do something like what 'info' does (it doesn't die) prints "<oid> missing" but this comes from failing fetching.
'remote-object-info' prints "<oid> missing" when the fetching works but the oid is unrecognized.
I will add a die instead of the goto. Thanks.
Quoted 2 lines
Thanks.
Regards,
Pablo
Quoted 29 lines · Pablo Sabater
Documentation/git-cat-file.adoc +9
-1@@ -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
Documentation/git-cat-file.adoc newline. The available atoms are: · line 336
Quoted 7 lines
Documentation/git-cat-file.adoc +1
-2@@ -324,15 +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.
Why did we remove the second sentence here? Cause the caveat still exists and holds good.
Documentation/git-cat-file.adoc newline. The available atoms are: · line 340
Quoted 7 lines
Documentation/git-cat-file.adoc +1
-3`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).
We also remove the newline here...
Quoted 32 lines
Documentation/git-cat-file.adoc +11
-1`rest`:: If this atom is used in the output string, input lines are split at the first whitespace boundary. All characters before that@@ -340,8 +345,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 +449,10 @@ scripting purposes. 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. + 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 maybe
[snip]
Quoted 7 lines
Documentation/git-cat-file.adoc +7
-0+static void parse_cmd_remote_object_info(struct batch_options *opt, + const char *line, struct strbuf *output, + struct expand_data *data) +{ + int count; + const char **argv; + char *line_to_split;
Documentation/git-cat-file.adoc
Quoted 10 lines
Documentation/git-cat-file.adoc +10
-0+ struct object_info *remote_object_info = NULL; + struct oid_array object_info_oids = OID_ARRAY_INIT; + + if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE) + die(_("remote-object-info command too long")); + + line_to_split = xstrdup(line); + count = split_cmdline(line_to_split, &argv); + if (count < 0) + die(_("remote-object-info: %s"), split_cmdline_strerror(count));
Nit: Should we provide some context here? Otherwise no point being marked for translation?
Quoted 38 lines
Documentation/git-cat-file.adoc +34
-0+ if (count - 1 > MAX_ALLOWED_OBJ_LIMIT) + die(_("remote-object-info supports at most %d objects"), + MAX_ALLOWED_OBJ_LIMIT); + + if (get_remote_info(opt, count, argv, &remote_object_info, + &object_info_oids)) + goto cleanup; + + data->skip_object_info = 1; + for (size_t i = 0; i < object_info_oids.nr; i++) { + data->oid = object_info_oids.oid[i]; + 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; + batch_object_write(argv[i + 1], output, opt, data, NULL, 0); + } else { + report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing"); + } + } + 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]); + free(line_to_split); + free(argv); + free(remote_object_info); + oid_array_clear(&object_info_oids); +} + static void dispatch_calls(struct batch_options *opt, struct strbuf *output, struct expand_data *data,
Documentation/git-cat-file.adoc static const struct parse_cmd { · line 894
Quoted 7 lines
Documentation/git-cat-file.adoc +2
-1@@ -748,8 +889,9 @@ static const struct parse_cmd { } commands[] = { { "contents", parse_cmd_contents, 1 }, { "info", parse_cmd_info, 1 }, - { "flush", NULL, 0 }, { "mailmap", parse_cmd_mailmap, 1 }, + { "remote-object-info", parse_cmd_remote_object_info, 1 }, + { "flush", NULL, 0 },
Why do we move this? we remove the alphabetical ordering by doing s
Quoted 21 lines
Documentation/git-cat-file.adoc +0
-0}; static void batch_objects_command(struct batch_options *opt,object-file.c +10
-0@@ -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); +}
[snip]