cat-file: add remote-object-info to batch-command
Subsystems (3)
Quoted 14 lines · Junio C Hamano
Quoted 4 lines · Peijian Ju
- Based on Mr. Patrick Steinhardt’s comment at [1], since remote-object-info operates only on protocol v2, adding a quarantine mechanism may lead to differing client-side behavior depending on the protocol, which could complicate the feature’s consistency.
Not doing quarantine would give even _more_ different client-side behaviour, though. When talking with a server with v2, you'll not see a cruft object left locally, but with older servers, you'll see crufts left behind. After a failed remote-object-info call, you can do an object-info to figure out what you needed to learn about the object, but only after the failed remote-object-info was against an older server.
Quoted 2 lines
So, I do not see it as a reason against putting temporary objects into quarantine.
I agree, and that's also what I wanted to say in the linked comment.
Quoted 4 lines
Not that I consider it important to give the same client-side behaviour when talking with older and newer servers, though. It is natural for a new feature to be available only with versions of Git that supports the feature, after all.
I think having subtly different behaviour like this is a recipe for confusion. As an end user (or rather end developer in this context) it is quite likely that you start to rely on the object either being fetched or not fetched as they probably won't end up testing against servers with both protocol versions. So you'd have to be aware of the fallback behaviour, and given that it is rather subtle and thus easy to miss the end result would likely be confusion when it works different in some repos than in others.
Quoted 2 lines
And if we throw that away as a goal, it starts to make more sense not to add "fetch and locally inspect" anywhere.
While having a quarantine directory would help with the case where you have differing end user behaviour depending on the protocol, it of course wouldn't help with the implied performance hit when using the fallback logic.
So maybe not having the fallback is the best solution after all, and when users have a good use case for why they need it we could implement it in a future iteration.
Patrick
Quoted 8 lines · Patrick Steinhardt
While having a quarantine directory would help with the case where you have differing end user behaviour depending on the protocol, it of course wouldn't help with the implied performance hit when using the fallback logic.
So maybe not having the fallback is the best solution after all, and when users have a good use case for why they need it we could implement it in a future iteration.
And I would strongly suspect that we won't have to implement any fallback---hopefully folks upgrade their server side to be capable of whatever capability is needed fast enough ;-).
Thanks.
Quoted 12 lines · Junio C Hamano
Quoted 8 lines · Patrick Steinhardt
While having a quarantine directory would help with the case where you have differing end user behaviour depending on the protocol, it of course wouldn't help with the implied performance hit when using the fallback logic.
So maybe not having the fallback is the best solution after all, and when users have a good use case for why they need it we could implement it in a future iteration.
Quoted 5 lines
And I would strongly suspect that we won't have to implement any fallback---hopefully folks upgrade their server side to be capable of whatever capability is needed fast enough ;-).
Thanks.
Thank you, sir, and also Patrick and Christian, for helping clarify this for me. That makes sense. I will remove the "fetch and inspect" approach in the next series.
Quoted 17 lines · Peijian Ju
Quoted 16 lines · Junio C Hamano
Quoted 8 lines · Patrick Steinhardt
While having a quarantine directory would help with the case where you have differing end user behaviour depending on the protocol, it of course wouldn't help with the implied performance hit when using the fallback logic.
So maybe not having the fallback is the best solution after all, and when users have a good use case for why they need it we could implement it in a future iteration.
And I would strongly suspect that we won't have to implement any fallback---hopefully folks upgrade their server side to be capable of whatever capability is needed fast enough ;-).
Quoted 5 lines
Quoted 1 line
Thanks.
Thank you, sir, and also Patrick and Christian, for helping clarify this for me. That makes sense. I will remove the "fetch and inspect" approach in the next series.
Thanks, everybody. And thank you for working on this topic.
v7 cover cat-file: add remote-object-info to batch-command
In reply to Eric JuThis patch series is a continuation of Calvin Wan’s ([email protected]) patch series [PATCH v5 0/6] cat-file: add --batch-command remote-object-info command at [1].
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)"[2]. This patch series implement the client option for it.
This patch series add 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 is supported in this implementation. If the batch command format contains objecttype objectsize:disk or deltabase, it will die.
The default format is set to %(objectname) %(objectsize) when remote-object-info is used. When "%(objecttype)" is supported, the default format will be unified.
[1] https://lore.kernel.org/git/[email protected]/#t
[2] https://git.kernel.org/pub/scm/git/git.git/commit/?id=a2ba162cda2acc171c3e36acbbc854792b093cb7
Changes since V6
================
- Removal of “Fetch and Inspect” Fallback. In the previous patch series, if the object-info capability was not supported (i.e., transfer.advertiseobjectinfo was set to false), the remote-object-info command would fall back to fetching the object locally and inspecting it. This “fetch and inspect” behavior has been removed. Instead, the client will now error and exit if the object-info capability is not supported by the server. For more details, refer to the discussion at this thread
https://lore.kernel.org/git/CAN2LT1Dm17-mmoMQr457fb5ta-TxG6Fj3Ma-gPh4YRJV9rRDrw@mail.gmail.com/.
- Test Updates. Adjusted tests to cover cases where the object-info capability is not supported by the server.
- Documentation Updates. Removed references to the “fetch and inspect” fallback mechanism.
- Typos and Formatting Fixes.
Calvin Wan (4):
fetch-pack: refactor packet writing
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info
Eric Ju (2):
cat-file: add declaration of variable i inside its for loop
cat-file: add remote-object-info to batch-command
Documentation/git-cat-file.txt | 24 +- Makefile | 1 + builtin/cat-file.c | 110 ++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 92 ++++ fetch-object-info.h | 18 + fetch-pack.c | 51 +- fetch-pack.h | 2 + object-file.c | 11 + object-store-ll.h | 3 + serve.c | 4 +- t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 652 +++++++++++++++++++++++++ transport-helper.c | 11 +- transport.c | 28 +- transport.h | 11 + 18 files changed, 1021 insertions(+), 68 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Range-diff against v6:
1: 0998382d5e = 1: 92feca4218 cat-file: add declaration of variable i inside its for loop 2: 26b861f416 ! 2: 58a24a4b92 fetch-pack: refactor packet writing
@@ connect.h: void check_stateless_delimiter(int stateless_rpc,
struct packet_reader *reader, const char *error);
++/**
++ * write_command_and_capabilities 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);
+
3: 044d0ec46c = 3: 6bd0f945c2 fetch-pack: move fetch initialization
4: 09b4a2c081 = 4: 000c82b681 serve: advertise object-info feature
5: 4782211b31 ! 5: 97c03a9c2c transport: add client support for object-info
@@ Metadata
## Commit message ##
transport: add client support for object-info
- Sometimes it is useful to get information about an object without having
- to download it completely. The server logic has already been implemented
- in “a2ba162cda (object-info: support for retrieving object info,
- 2021-04-20)”.
+ 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)."
- Add client functions to communicate with the server.
+ This commit introduces client functions to interact with the server.
- The client currently supports requesting a list of object ids with
- feature 'size' from a v2 server. If a server does not
- advertise the feature, then the client falls back
- to making the request through 'fetch'.
+ 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.
Helped-by: Jonathan Tan <[email protected]>
Helped-by: Christian Couder <[email protected]>
@@ fetch-object-info.c (new)
+ switch (version) {
+ case protocol_v2:
+ if (!server_supports_v2("object-info"))
-+ return -1;
-+ if (unsorted_string_list_has_string(args->object_info_options, "size")
-+ && !server_supports_feature("object-info", "size", 0))
-+ return -1;
++ die(_("object-info capability is not enabled on the server"));
+ send_object_info_request(fd_out, args);
+ break;
+ case protocol_v1:
@@ transport.c
#include "remote.h"
#include "connect.h"
#include "send-pack.h"
-@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
- struct ref *refs = NULL;
- struct fetch_pack_args args;
- struct ref *refs_tmp = NULL, **to_fetch_dup = NULL;
-+ struct ref *object_info_refs = NULL;
-
- memset(&args, 0, sizeof(args));
- args.uploadpack = data->options.uploadpack;
@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
args.server_options = transport->server_options;
args.negotiation_tips = data->options.negotiation_tips;
@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
+ if (transport->smart_options
+ && transport->smart_options->object_info
+ && transport->smart_options->object_info_oids->nr > 0) {
-+ struct ref *ref_itr = object_info_refs = alloc_ref("");
+ struct packet_reader reader;
+ struct object_info_args obj_info_args = { 0 };
+
@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
+ data->version = discover_version(&reader);
+ transport->hash_algo = reader.hash_algo;
+
-+ if (!fetch_object_info(data->version, &obj_info_args, &reader,
-+ data->options.object_info_data, transport->stateless_rpc,
-+ data->fd[1])) {
-+ /*
-+ * If the code reaches here, fetch_object_info is successful and
-+ * remote object info are retrieved from packets (i.e. without
-+ * downloading the objects).
-+ */
-+ goto cleanup;
-+ }
++ 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) {
-+ /*
-+ * If the code reaches here, it means we can't retrieve object info from
-+ * packets, and we will fallback to downland the pack files.
-+ * We set quiet and no_progress to be true, so that the internal call to
-+ * fetch-pack is less verbose.
-+ */
-+ args.object_info_data = data->options.object_info_data;
-+ args.quiet = 1;
-+ args.no_progress = 1;
-+
-+ /*
-+ * Allocate memory for object info data according to oids.
-+ * The actual results will be retrieved later from the downloaded
-+ * pack files.
-+ */
-+ for (size_t i = 0; i < transport->smart_options->object_info_oids->nr; i++) {
-+ ref_itr->old_oid = transport->smart_options->object_info_oids->oid[i];
-+ ref_itr->exact_oid = 1;
-+ if (i == transport->smart_options->object_info_oids->nr - 1)
-+ /* last element, no need to allocate to next */
-+ ref_itr->next = NULL;
-+ else
-+ ref_itr->next = alloc_ref("");
-+
-+ ref_itr = ref_itr->next;
-+ }
-+
-+ transport->remote_refs = object_info_refs;
-+
+ } else if (!data->finished_handshake) {
int i;
int must_list_refs = 0;
for (i = 0; i < nr_heads; i++) {
-@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
- &transport->pack_lockfiles, data->version);
-
- data->finished_handshake = 0;
-+
-+ /* Retrieve object info data from the downloaded pack files */
-+ if (args.object_info) {
-+ struct ref *ref_cpy_reader = object_info_refs;
-+ for (int i = 0; ref_cpy_reader; i++) {
-+ oid_object_info_extended(the_repository, &ref_cpy_reader->old_oid,
-+ &args.object_info_data[i], OBJECT_INFO_LOOKUP_REPLACE);
-+ ref_cpy_reader = ref_cpy_reader->next;
-+ }
-+ }
-+
- data->options.self_contained_and_connected =
- args.self_contained_and_connected;
- data->options.connectivity_checked = args.connectivity_checked;
-@@ transport.c: static int fetch_refs_via_pack(struct transport *transport,
- ret = -1;
-
- cleanup:
-+ free_refs(object_info_refs);
- close(data->fd[0]);
- if (data->fd[1] >= 0)
- close(data->fd[1]);
## transport.h ##
@@
@@ transport.h: struct git_transport_options {
unsigned connectivity_checked:1;
+ /*
-+ * Transport will attempt to pull only object-info. Fallbacks
-+ * to pulling entire object if object-info is not supported.
++ * Transport will attempt to retrieve only object-info.
++ * If object-info is not supported, the operation will error and exit.
+ */
+ unsigned object_info : 1;
+
6: 91689c0d0d ! 6: 7d1936591d cat-file: add remote-object-info to batch-command
@@ Commit message
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.
+ 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.
cat-file --batch-command is generally implemented in the following
manner:
@@ Documentation/git-cat-file.txt: info <object>::
output of `--batch-check`.
+remote-object-info <remote> <object>...::
-+ Print object info for object references `<object>` at specified <remote>
-+ without downloading objects from remote. If the object-info capability
-+ is not supported by the server, the objects will be downloaded instead.
++ Print object info for object references `<object>` at specified
++ `<remote>` without downloading objects from the remote.
++ Error when the `object-info` capability is not supported by the server.
+ Error when no object references are provided.
+ This command may be combined with `--buffer`.
+
@@ builtin/cat-file.c: static void parse_cmd_info(struct batch_options *opt,
+ data->oid = object_info_oids.oid[i];
+
+ if (remote_object_info[i].sizep) {
-+ data->size = *remote_object_info[i].sizep;
-+ } else {
+ /*
-+ * When reaching here, it means remote-object-info can't retrieve
-+ * information from server without downloading them, and the objects
-+ * have been fetched to client already.
-+ * Print the information using the logic for local objects.
++ * When reaching here, it means remote-object-info can retrieve
++ * information from server without downloading them.
+ */
-+ data->skip_object_info = 0;
++ data->size = *remote_object_info[i].sizep;
++ opt->batch_mode = BATCH_MODE_INFO;
++ batch_object_write(argv[i+1], output, opt, data, NULL, 0);
+ }
-+
-+ opt->batch_mode = BATCH_MODE_INFO;
-+ batch_object_write(argv[i+1], output, opt, data, NULL, 0);
-+
+ }
+ opt->use_remote_info = 0;
+ data->skip_object_info = 0;
@@ t/t1017-cat-file-remote-object-info.sh (new)
+
+# 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 'remote-object-info fallback git://: fetch objects to client' '
++test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
+ (
+ git -C "$daemon_parent" config transfer.advertiseobjectinfo false &&
+ set_transport_variables "$daemon_parent" &&
-+ cd "$daemon_parent/daemon_client_empty" &&
-+
-+ # Prove object is not on the client
-+ echo "$hello_oid missing" >expect &&
-+ echo "$tree_oid missing" >>expect &&
-+ echo "$commit_oid missing" >>expect &&
-+ echo "$tag_oid missing" >>expect &&
+
-+ # These results prove remote-object-info can retrieve object info
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ # These results are for the info command
-+ # They prove objects are downloaded
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
++ test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
+ remote-object-info $GIT_DAEMON_URL/parent $hello_oid $tree_oid $commit_oid $tag_oid
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
+ EOF
++ test_grep "object-info capability is not enabled on the server" err &&
+
+ # revert server state back
-+ git -C "$daemon_parent" config transfer.advertiseobjectinfo true &&
++ git -C "$daemon_parent" config transfer.advertiseobjectinfo true
+
-+ test_cmp expect actual
+ )
+'
+
@@ t/t1017-cat-file-remote-object-info.sh (new)
+
+# Test --batch-command remote-object-info with 'file://' and
+# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
-+
-+test_expect_success 'remote-object-info fallback file://: fetch objects to client' '
++test_expect_success 'batch-command remote-object-info file:// fails when transfer.advertiseobjectinfo=false' '
+ (
+ set_transport_variables "server" &&
+ server_path="$(pwd)/server" &&
+ git -C "${server_path}" config transfer.advertiseobjectinfo false &&
-+ cd file_client_empty &&
+
-+ # Prove object is not on the client
-+ echo "$hello_oid missing" >expect &&
-+ echo "$tree_oid missing" >>expect &&
-+ echo "$commit_oid missing" >>expect &&
-+ echo "$tag_oid missing" >>expect &&
-+
-+ # These results prove remote-object-info can retrieve object info
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ # These results are for the info command
-+ # They prove objects are downloaded
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
++ test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
+ remote-object-info "file://${server_path}" $hello_oid $tree_oid $commit_oid $tag_oid
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
+ EOF
++ test_grep "object-info capability is not enabled on the server" err &&
+
+ # revert server state back
-+ git -C "${server_path}" config transfer.advertiseobjectinfo true &&
-+ test_cmp expect actual
++ git -C "${server_path}" config transfer.advertiseobjectinfo true
+ )
+'
+
@@ t/t1017-cat-file-remote-object-info.sh (new)
+ )
+'
+
-+test_expect_success 'remote-object-info fails on server with legacy protocol fallback' '
++test_expect_success 'remote-object-info fails on server with legacy protocol' '
+ (
+ set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
@@ t/t1017-cat-file-remote-object-info.sh (new)
+ )
+'
+
-+test_expect_success 'remote-object-info fails on malformed OID fallback' '
++test_expect_success 'remote-object-info fails on malformed OID with default filter' '
+ (
+ set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
@@ t/t1017-cat-file-remote-object-info.sh (new)
+ )
+'
+
++
+# Test --batch-command remote-object-info with 'http://' transport and
+# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
-+
-+test_expect_success 'remote-object-info fallback http://: fetch objects to client' '
++test_expect_success 'batch-command remote-object-info http:// fails when transfer.advertiseobjectinfo=false ' '
+ (
+ set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo false &&
-+ cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
-+
-+ # Prove object is not on the client
-+ echo "$hello_oid missing" >expect &&
-+ echo "$tree_oid missing" >>expect &&
-+ echo "$commit_oid missing" >>expect &&
-+ echo "$tag_oid missing" >>expect &&
+
-+ # These results prove remote-object-info can retrieve object info
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ # These results are for the info command
-+ # They prove objects are downloaded
-+ echo "$hello_oid $hello_size" >>expect &&
-+ echo "$tree_oid $tree_size" >>expect &&
-+ echo "$commit_oid $commit_size" >>expect &&
-+ echo "$tag_oid $tag_size" >>expect &&
-+
-+ git cat-file --batch-command >actual <<-EOF &&
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
++ test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
+ remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid $commit_oid $tag_oid
-+ info $hello_oid
-+ info $tree_oid
-+ info $commit_oid
-+ info $tag_oid
+ EOF
++ test_grep "object-info capability is not enabled on the server" err &&
+
+ # revert server state back
-+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true &&
-+
-+ test_cmp expect actual
++ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true
+ )
+'
+Signature · 5 lines
2.47.0 Information Footer: base-commit: 8f8d6eee531b3fa1a8ef14f169b0cb5035f7a772 Merge Request: https://gitlab.com/gitlab-org/git/-/merge_requests/168
PATCH v7 1/6 cat-file: add declaration of variable i inside its for loop
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.
--- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -673,12 +673,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -686,9 +684,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 ;
@@ -714,7 +710,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 };
@@ -724,7 +719,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
@@ -1328,9 +1328,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 (int i =0 ; i < server_options->nr; i++)packet_buf_write (req_buf,"server-option=%s" , server_options->items[i].string); }
Signature · 1 line
2.47.0
Re: [PATCH v7 1/6] cat-file: add declaration of variable i inside its for loop
Quoted 5 lines · Eric Ju
fetch-pack.c +0
-0
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1335
Quoted 10 lines
fetch-pack.c +1
-2@@ -1328,9 +1328,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 (int i =0 ; i < server_options->nr; i++)packet_buf_write (req_buf,"server-option=%s" , server_options->items[i].string); }
It's somewhat curious that you change the type to size_t while at it in other spots, but not here. Doubly so because server_options is a struct string_list, and string_list::nr is of type size_t.
Patrick
Re: [PATCH v7 1/6] cat-file: add declaration of variable i inside its for loop
Quoted 22 lines · Patrick Steinhardt
Quoted 5 lines · Eric Ju
fetch-pack.c +0
-0fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1335
Quoted 10 lines
fetch-pack.c +1
-2@@ -1328,9 +1328,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 (int i =0 ; i < server_options->nr; i++)packet_buf_write (req_buf,"server-option=%s" , server_options->items[i].string); }It's somewhat curious that you change the type to
size_twhile at it in other spots, but not here. Doubly so becauseserver_optionsis astruct string_list, andstring_list::nris of typesize_t.
Quoted 1 line
Patrick
Thank you.
Yes, I missed that. Will correct it in v8.
PATCH v7 2/6 fetch-pack: refactor packet writing
In reply to Eric JuFrom: Calvin Wan <[email protected]>
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_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 a2ba162c (object-info: support for retrieving object info, 2021-04-20).
Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future.
To move write_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously,
in fetch_pack.c, advertise_sid was a static variable, modified using git_config_get_bool().
In connect.c, we now initialize advertise_sid at the beginning by directly using git_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():
git_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2.
About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly.
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-)
connect.c +34 -0
@@ -688,6 +688,40 @@ int server_supports(const char *feature)return !!server_feature_value (feature,NULL ); } +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; + +git_config_get_bool ("transfer.advertisesid" , &advertise_sid); + +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" )) +packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); +if (server_options && server_options->nr) { +ensure_server_supports_v2 ("server-option" ); +for (int 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 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 ) { +die (_ ("the server does not support algorithm '%s'" ), + the_hash_algo->name); + } +packet_buf_delim (req_buf); +} +enum protocol {PROTO_LOCAL =1 ,PROTO_FILE ,
connect.h +8 -0
@@ -1,6 +1,7 @@#ifndef CONNECT_H #define CONNECT_H +#include "string-list.h" #include "protocol.h" #define CONNECT_VERBOSE (1u << 0)
@@ -30,4 +31,11 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/** +* write_command_and_capabilities 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 -33
@@ -1316,37 +1316,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; - -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 (int i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) { -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,
@@ -1357,7 +1326,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" );
@@ -2175,7 +2144,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_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.47.0
Re: [PATCH v7 2/6] fetch-pack: refactor packet writing
connect.h line 7
Quoted 12 lines · Eric Ju
connect.h +1
-0@@ -1,6 +1,7 @@#ifndef CONNECT_H #define CONNECT_H +#include "string-list.h" #include "protocol.h" #define CONNECT_VERBOSE (1u << 0)
Instead of including this header, you can add a forward declaration of struct string_list. This is mostly done to keep compilation times at bay by not including too many headers.
connect.h void check_stateless_delimiter(int stateless_rpc, · line 35
Quoted 6 lines
connect.h +2
-0@@ -30,4 +31,11 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/** +* write_command_and_capabilities writes a command along with the requested
Nit: we don't typically use Go-style comments where the comment starts with the name of what's being documented.
Patrick
Re: [PATCH v7 2/6] fetch-pack: refactor packet writing
Thank you, Patrick.
Your comments make perfect sense to me, and I will revise them accordingly to v8.
Quoted 30 lines · Patrick Steinhardt
connect.h line 7
Quoted 12 lines · Eric Ju
connect.h +1
-0@@ -1,6 +1,7 @@#ifndef CONNECT_H #define CONNECT_H +#include "string-list.h" #include "protocol.h" #define CONNECT_VERBOSE (1u << 0)Instead of including this header, you can add a forward declaration of
struct string_list. This is mostly done to keep compilation times at bay by not including too many headers.connect.h void check_stateless_delimiter(int stateless_rpc, · line 35
Quoted 6 lines
connect.h +2
-0@@ -30,4 +31,11 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/** +* write_command_and_capabilities writes a command along with the requested Nit: we don't typically use Go-style comments where the comment starts with the name of what's being documented.
Patrick
PATCH v7 3/6 fetch-pack: move fetch initialization
In reply to Eric JuFrom: 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
@@ -1647,18 +1647,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.47.0
PATCH v7 4/6 serve: advertise object-info feature
In reply to Eric JuFrom: 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
serve.c +3 -1
@@ -70,7 +70,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",
@@ -78,6 +78,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS/* disabled by default */ advertise_object_info =0 ; } +if (value && advertise_object_info) +strbuf_addstr (value,"size" );return advertise_object_info; }
Signature · 1 line
2.47.0
PATCH v7 5/6 transport: add client support for object-info
In reply to Eric JuFrom: 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)."
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.
--- Makefile | 1 + fetch-object-info.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 18 +++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 + transport-helper.c | 11 +++++- transport.c | 28 +++++++++++++- transport.h | 11 ++++++ 8 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h
Makefile +1 -0
@@ -1024,6 +1024,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 +92 -0
92 lines not on this page · raw message
fetch-object-info.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "object-store-ll.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +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
@@ -1653,6 +1653,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"
@@ -42,6 +43,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
transport-helper.c +9 -2
@@ -711,8 +711,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" ));
@@ -728,6 +728,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"
@@ -444,8 +445,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options + && 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.object_info_options = transport->smart_options->object_info_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + +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
@@ -5,6 +5,7 @@#include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options {unsigned thin :1 ;
@@ -30,6 +31,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;
@@ -53,6 +60,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.47.0
Re: [PATCH v7 5/6] transport: add client support for object-info
Quoted 18 lines · Eric Ju
fetch-object-info.c new file +12
-0@@ -0,0 +1,92 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "object-store-ll.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/** +* send_object_info_request sends git-cat-file object-info command and its
fetch-object-info.c line 22
Quoted 10 lines
fetch-object-info.c new file +10
-0+* arguments into the request buffer. +*/ +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" );
Do we have a document somewhere that spells out the wire format that client- and server-side talk with each other? If so it would be nice to point it out in the commit message so that I know where to look, and otherwise we should document it. Without such a doc it's hard to figure out whether this is correct.
fetch-object-info.c line 27
Quoted 4 lines
fetch-object-info.c new file +4
-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])); + }
Nit: needless curly braces.
fetch-object-info.c line 31
Quoted 3 lines
fetch-object-info.c new file +3
-0+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" ));
So we write the whole request into req_buf first before sending it to the remote. Isn't that quite inefficient memory wise? In other words, couldn't we instead stream the request line by line or at least in batches to the file descriptor?
fetch-object-info.c line 36
Quoted 4 lines
fetch-object-info.c new file +4
-0+strbuf_release (&req_buf); +} + +/**
Nit: s|/**|/*|
Quoted 18 lines
fetch-object-info.c new file +18
-0+* fetch_object_info sends git-cat-file object-info command into the request buf +* and read the results from packets. +*/ +int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +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); +break ; +case protocol_v1: +case protocol_v0: +die (_ ("wrong protocol version. expected v2" ));
fetch-object-info.c line 64
Quoted 10 lines
fetch-object-info.c new file +10
-0+case protocol_unknown_version: +BUG ("unknown protocol version" ); + } + +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 ; + } +if (unsorted_string_list_has_string (args->object_info_options, reader->line)) {
Hum. Does this result in quadratic runtime behaviour?
fetch-object-info.c line 68
Quoted 4 lines
fetch-object-info.c new file +4
-0+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 (long ));
This might be a bit more future proof in case the sizep type were ever to change:
object_info_data[j].sizep = xcalloc(1, sizeof(*object_info_data[j].sizep));
It also allows you to skip double-checking whether you picked the correct type. In fact, the type is actually an unsigned long, which is confusing but ultimately does not make much of a difference because it should have the same size.
Quoted 6 lines
fetch-object-info.c new file +6
-0+ } +continue ; + } +return -1 ; + } +
fetch-object-info.c line 84
Quoted 10 lines
fetch-object-info.c new file +10
-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,"" )) +die ("object-info: not our ref %s" , + object_info_values.items[0 ].string); + + *object_info_data[i].sizep =strtoul (object_info_values.items[1 + size_index].string,NULL ,10 );
We're completely missing error handling for strtoul(3p) here. That function is also discouraged nowadays because error handling is hard to do correct. We have strtoul_ui() and friends, but don't have a variant yet that know to return an unsigned long. We might backfill that omission and then use it instead.
transport-helper.c static int fetch_refs(struct transport *transport, · line 733
Quoted 11 lines
transport-helper.c +3
-0@@ -728,6 +728,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" ));
The code path that checks for for protocol v2 with "--negotiate-only" knows to warn and return an error. Should we do the same here?
Quoted 13 lines
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 452
Quoted 8 lines
transport.c +5
-0@@ -444,8 +445,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options + && transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) {
Formatting is wrong here:
if (transport->smart_options &&
transport->smart_options->object_info &&
transport->smart_options->object_info_oids->nr > 0) {Patrick
Re: [PATCH v7 5/6] transport: add client support for object-info
Hi Patrick,
Thank you for your feedback. I have a few questions. I agree with the comments I didn’t specifically respond to and will address them in v8.
Eric.
Quoted 28 lines · Patrick Steinhardt
Quoted 26 lines · Eric Ju
fetch-object-info.c new file +20
-0@@ -0,0 +1,92 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "object-store-ll.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/** +* send_object_info_request sends git-cat-file object-info command and its +* arguments into the request buffer. +*/ +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); +
Quoted 9 lines
fetch-object-info.c line 22
Quoted 2 lines
fetch-object-info.c new file +2
-0+if (unsorted_string_list_has_string (args->object_info_options,"size" )) +packet_buf_write (&req_buf,"size" );Do we have a document somewhere that spells out the wire format that client- and server-side talk with each other? If so it would be nice to point it out in the commit message so that I know where to look, and otherwise we should document it. Without such a doc it's hard to figure out whether this is correct.
Thank you. Is this what you are looking for?
https://git-scm.com/docs/protocol-v2#_object_info
If so, I will put it in the commit message in v8.
Quoted 7 lines
fetch-object-info.c line 27
Quoted 4 lines
fetch-object-info.c new file +4
-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])); + }Nit: needless curly braces.
Quoted 9 lines
fetch-object-info.c line 31
Quoted 3 lines
fetch-object-info.c new file +3
-0+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" ));So we write the whole request into
req_buffirst before sending it to the remote. Isn't that quite inefficient memory wise? In other words, couldn't we instead stream the request line by line or at least in batches to the file descriptor?
Thank you.
I followed the send_fetch_request() logic in fetch-pack.c. I’m not entirely clear on how to “stream the request line by line or in batches.” Could you point me to an example or reference that demonstrates this approach?
Quoted 84 lines
fetch-object-info.c line 36
Quoted 4 lines
fetch-object-info.c new file +4
-0+strbuf_release (&req_buf); +} + +/** Nit: s|/**|/*|
Quoted 18 lines
fetch-object-info.c new file +18
-0+* fetch_object_info sends git-cat-file object-info command into the request buf +* and read the results from packets. +*/ +int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +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); +break ; +case protocol_v1: +case protocol_v0: +die (_ ("wrong protocol version. expected v2" ));fetch-object-info.c line 64
Quoted 10 lines
fetch-object-info.c new file +10
-0+case protocol_unknown_version: +BUG ("unknown protocol version" ); + } + +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 ; + } +if (unsorted_string_list_has_string (args->object_info_options, reader->line)) {Hum. Does this result in quadratic runtime behaviour?
fetch-object-info.c line 68
Quoted 4 lines
fetch-object-info.c new file +4
-0+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 (long ));This might be a bit more future proof in case the
sizeptype were ever to change:object_info_data[j].sizep = xcalloc(1, sizeof(*object_info_data[j].sizep));It also allows you to skip double-checking whether you picked the correct type. In fact, the type is actually an
unsigned long, which is confusing but ultimately does not make much of a difference because it should have the same size.Quoted 6 lines
fetch-object-info.c new file +6
-0+ } +continue ; + } +return -1 ; + } +fetch-object-info.c line 84
Quoted 10 lines
fetch-object-info.c new file +10
-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,"" )) +die ("object-info: not our ref %s" , + object_info_values.items[0 ].string); + + *object_info_data[i].sizep =strtoul (object_info_values.items[1 + size_index].string,NULL ,10 );We're completely missing error handling for strtoul(3p) here. That function is also discouraged nowadays because error handling is hard to do correct. We have
strtoul_ui()and friends, but don't have a variant yet that know to return anunsigned long. We might backfill that omission and then use it instead.Quoted 8 lines
transport-helper.c +0
-0@@ -728,6 +728,13 @@ static int fetch_refs(struct transport *transport,free_refs (dummy); }
Quoted 7 lines
transport-helper.c static int fetch_refs(struct transport *transport, · line 733
Quoted 3 lines
transport-helper.c +3
-0+/* fail the command explicitly to avoid further commands input. */ +if (transport->smart_options->object_info) +die (_ ("remote-object-info requires protocol v2" ));The code path that checks for for protocol v2 with "--negotiate-only" knows to warn and return an error. Should we do the same here?
Thank you.
If we follow "warn and return an error" as "--negotiate-only", we will end up with "warn and wait". This was the question I was asking in the previous patches:
In the current implementation, if a user putsremote-object-info in protocol v1,
cat-file --batch-command will die. Which way do we prefer?
"error and exit (i.e. die)"
or "warn and wait for new command".
And we decided to take the path of "error and exit (i.e. die)", the reason was explained at
https://lore.kernel.org/git/CAN2LT1Cmsw3RB1kbRBvoeLs8WaQeZWqrG96EQfMkMe_jdKaO4g@mail.gmail.com/:
Our primary use case is to use git cat-file remote-object-info in
a promisor remote setup to retrieve metadata
about an object stored in the promisor remote, without fetching it
back to the local repository.
This approach helps conserve disk space. I don’t believe other
commands can achieve this functionality,
particularly without requiring the object to be downloaded.
Quoted 29 lines
Quoted 13 lines
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 452
Quoted 8 lines
transport.c +5
-0@@ -444,8 +445,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options + && transport->smart_options->object_info + && transport->smart_options->object_info_oids->nr >0 ) {Formatting is wrong here:
if (transport->smart_options && transport->smart_options->object_info && transport->smart_options->object_info_oids->nr > 0) {Patrick
PATCH v7 6/6 cat-file: add remote-object-info to batch-command
In reply to Eric JuSince 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.
cat-file --batch-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 loop 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.txt | 24 +- builtin/cat-file.c | 99 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 652 +++++++++++++++++++++++++ 7 files changed, 802 insertions(+), 16 deletions(-) create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Documentation/git-cat-file.txt +20 -4
@@ -149,6 +149,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. + Error when the `object-info` capability is not supported by the server. + 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`
@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`
@@ -298,13 +306,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
@@ -314,7 +323,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 the 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
@@ -396,6 +408,10 @@ scripting purposes. CAVEATS ------- +Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will error +and exit when they are 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 +99 -0
132 lines not on this page · raw message
object-file.c +11 -0
@@ -3132,3 +3132,14 @@ int read_loose_object(const char *path,munmap (map, mapsize);return ret; } + +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); +free (object_info->type_name); +}
object-store-ll.h +3 -0
@@ -548,4 +548,7 @@ int for_each_object_in_pack(struct packed_git *p,int for_each_packed_object (each_packed_object_fn ,void *,enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents (struct object_info *object_info); +#endif /* OBJECT_STORE_LL_H */
t/lib-cat-file.sh new file +16 -0
@@ -0,0 +1,16 @@ +# Library of git-cat-file related tests. + +# Print a string without a trailing newline +echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0). +echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces. +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_PASSES_SANITIZE_LEAK=true . ./test-lib.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 oid=$2
t/t1017-cat-file-remote-object-info.sh new file +652 -0
652 lines not on this page · raw message
Signature · 1 line
2.47.0
Documentation/git-cat-file.txt newline. The available atoms are: · line 329
Quoted 13 lines · Eric Ju
Documentation/git-cat-file.txt +4
-1@@ -314,7 +323,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 the default format to stay the same!!!
Is this stale or do we still not support %(objecttype)? I thought we wanted to support that, as well, so that we don't have to change the default format.
Quoted 13 lines
builtin/cat-file.c +5
-0@@ -576,6 +582,59 @@ 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) +{ +int retval =0 ; +struct remote *remote =NULL ; +struct object_id oid;
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 599
Quoted 10 lines
builtin/cat-file.c +10
-0+struct string_list object_info_options =STRING_LIST_INIT_NODUP ; +static struct transport *gtransport; + +/* +* Change the format to "%(objectname) %(objectsize)" when +* 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)" ;
Seems like it isn't stale. Hum.
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 610
Quoted 10 lines
builtin/cat-file.c +10
-0+ 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)) +die (_ ("Not a valid object name %s" ), argv[i]); +oid_array_append (&object_info_oids, &oid); + }
Should we return an error when the user didn't pass any object IDs?
Quoted 10 lines
builtin/cat-file.c +6
-0@@ -667,6 +726,45 @@ static void parse_cmd_info(struct batch_options *opt,batch_one_object (line, output, opt, data); } +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;
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 744
Quoted 10 lines
builtin/cat-file.c +10
-0+ +char *line_to_split =xstrdup_or_null (line); + count =split_cmdline (line_to_split, &argv); +if (get_remote_info (opt, count, argv)) + goto cleanup; + + opt->use_remote_info =1 ; + data->skip_object_info =1 ; +for (size_t i =0 ; i < object_info_oids.nr; i++) { +
Nit: empty newline at the start of a block.
t/lib-cat-file.sh
Quoted 5 lines
t/lib-cat-file.sh new file +0
-0
I think it would make sense to split the introduction of "lib-cat-file.sh" into a separate commit.
Patrick
Quoted 9 lines · Patrick Steinhardt
Quoted 7 lines · Eric Ju
Documentation/git-cat-file.txt +0
-0@@ -314,7 +323,10 @@ newline. The available atoms are: line) are output in place of the `%(rest)` atom.
Quoted 11 lines
Documentation/git-cat-file.txt newline. The available atoms are: · line 329
Quoted 6 lines
Documentation/git-cat-file.txt +4
-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. +WARNING: When "%(objecttype)" is supported, the default format WILL be unified, so +DO NOT RELY on the current the default format to stay the same!!!Is this stale or do we still not support
%(objecttype)? I thought we wanted to support that, as well, so that we don't have to change the default format.
Please see my next reply.
Quoted 17 lines
Quoted 17 lines
builtin/cat-file.c +9
-0@@ -576,6 +582,59 @@ 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) +{ +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; + +/*
Quoted 9 lines
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 599
Quoted 6 lines
builtin/cat-file.c +6
-0+* Change the format to "%(objectname) %(objectsize)" when +* 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)" ;Seems like it isn't stale. Hum.
No, this isn’t stale. As I mentioned in my response to Junio in
https://lore.kernel.org/git/CAN2LT1Cmsw3RB1kbRBvoeLs8WaQeZWqrG96EQfMkMe_jdKaO4g@mail.gmail.com/,
adding type support is planned for the next patch series. Based on the documentation at https://git-scm.com/docs/protocol-v2#_object_info, it seems type isn’t yet supported on the server side either. My plan is to implement the logic for both server and client in the next series.
Unless the reviewers feel strongly that this must be included now, I’d prefer to stick to the original plan.
Quoted 11 lines
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 610
Quoted 10 lines
builtin/cat-file.c +10
-0+ 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)) +die (_ ("Not a valid object name %s" ), argv[i]); +oid_array_append (&object_info_oids, &oid); + }
Quoted 2 lines
Should we return an error when the user didn't pass any object IDs?
Thank you. Revising in v8 and also adding a new test case to cover it.
Quoted 14 lines
Quoted 14 lines
builtin/cat-file.c +10
-0@@ -667,6 +726,45 @@ static void parse_cmd_info(struct batch_options *opt,batch_one_object (line, output, opt, data); } +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 =xstrdup_or_null (line); + count =split_cmdline (line_to_split, &argv); +if (get_remote_info (opt, count, argv))
Quoted 9 lines
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 744
Quoted 6 lines
builtin/cat-file.c +6
-0+ goto cleanup; + + opt->use_remote_info =1 ; + data->skip_object_info =1 ; +for (size_t i =0 ; i < object_info_oids.nr; i++) { +Nit: empty newline at the start of a block.
Thank you. Fixing in v8.
Quoted 9 lines
t/lib-cat-file.sh
Quoted 5 lines
t/lib-cat-file.sh new file +0
-0I think it would make sense to split the introduction of "
lib-cat-file.sh" into a separate commit.
Thank you. Will split it into a separate commit in v8.
Quoted 1 line
Patrick
Quoted 36 lines · Peijian Ju
Quoted 18 lines · Patrick Steinhardt
Quoted 17 lines · Eric Ju
builtin/cat-file.c +9
-0@@ -576,6 +582,59 @@ 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) +{ +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; + +/* Quoted 9 lines
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 599
Quoted 6 lines
builtin/cat-file.c +6
-0+* Change the format to "%(objectname) %(objectsize)" when +* 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)" ;Seems like it isn't stale. Hum.
No, this isn’t stale. As I mentioned in my response to Junio in
https://lore.kernel.org/git/CAN2LT1Cmsw3RB1kbRBvoeLs8WaQeZWqrG96EQfMkMe_jdKaO4g@mail.gmail.com/,
adding type support is planned for the next patch series. Based on the documentation at https://git-scm.com/docs/protocol-v2#_object_info, it seems type isn’t yet supported on the server side either. My plan is to implement the logic for both server and client in the next series.
Quoted 2 lines
Unless the reviewers feel strongly that this must be included now, I’d prefer to stick to the original plan.
The problem is that you cannot introduce a different format first and then change it in a subsequent patch series because that would be a backwards-incompatible change. So if the follow-up patch series would implement that you cannot revert back to the default format, and consequently the behaviour would now be inconsistent with the non-remote case without a good reason.
Patrick
Quoted 12 lines · Patrick Steinhardt
Quoted 9 lines · Peijian Ju
No, this isn’t stale. As I mentioned in my response to Junio in
https://lore.kernel.org/git/CAN2LT1Cmsw3RB1kbRBvoeLs8WaQeZWqrG96EQfMkMe_jdKaO4g@mail.gmail.com/,
adding type support is planned for the next patch series. Based on the documentation at https://git-scm.com/docs/protocol-v2#_object_info, it seems type isn’t yet supported on the server side either. My plan is to implement the logic for both server and client in the next series.Unless the reviewers feel strongly that this must be included now, I’d prefer to stick to the original plan.
Quoted 6 lines
The problem is that you cannot introduce a different format first and then change it in a subsequent patch series because that would be a backwards-incompatible change. So if the follow-up patch series would implement that you cannot revert back to the default format, and consequently the behaviour would now be inconsistent with the non-remote case without a good reason.
The doc added with this patch clearly says that the default format is very likely to change in the future and that users should not rely on it. Also there are very simple ways for users (who are likely to be very advanced users) to use a custom format instead of the default format.
If we always reject any backward-incompatible change, even on features we have clearly marked as experimental or temporary, then it means there is no point in marking features as experimental or temporary in the docs, and it will make developing new features more difficult as we will likely have to spend a lot of time to get it right the first time instead developing them organically. As we will likely fail in some cases to get it right the first time, it will mean more things we will have to redo in other ways and more things we will have to deprecate and eventually remove, which will be bad for backward compatibility anyway.
v8 cover add remote-object-info to batch-command
In reply to Eric JuThis patch series is a continuation of Calvin Wan’s ([email protected]) patch series [PATCH v5 0/6] cat-file: add --batch-command remote-object-info command at [1].
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)"[2]. This patch series implement the client option for it.
This patch series add 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 in this implementation. The type (%(objecttype)) is not included in this patch series, as it is not yet supported on the server side either. The plan is to implement the necessary logic for both the server and client in a subsequent series.
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 terminate with an error.
Changes since V7
================
- Introduced strtoul_ul() in git-compat-util.h to ensure proper error handling using strtoul from the standard library.
- Separated the test library into its own commit for better clarity and organization.
- Use string_list_has_string() instead of unsorted_string_list_has_string() to avoid quadratic runtime behaviour
- Added a documentation link to the wire format in the commit message to provide additional context.
- New test case "remote-object-info fails on not providing OID"
- Fixed typos and formatting issues for improved readability.
Calvin Wan (4):
fetch-pack: refactor packet writing
fetch-pack: move fetch initialization
serve: advertise object-info feature
transport: add client support for object-info
Eric Ju (2):
cat-file: add declaration of variable i inside its for loop
cat-file: add remote-object-info to batch-command
Documentation/git-cat-file.txt | 24 +- Makefile | 1 + builtin/cat-file.c | 110 ++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 92 ++++ fetch-object-info.h | 18 + fetch-pack.c | 51 +- fetch-pack.h | 2 + object-file.c | 11 + object-store-ll.h | 3 + serve.c | 4 +- t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 652 +++++++++++++++++++++++++ transport-helper.c | 11 +- transport.c | 28 +- transport.h | 11 + 18 files changed, 1021 insertions(+), 68 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Range-diff against v7:
-: ---------- > 1: c09e21a9d6 cat-file: add declaration of variable i inside its for loop -: ---------- > 2: ed04a4a7c4 fetch-pack: refactor packet writing -: ---------- > 3: bc52c4f80c fetch-pack: move fetch initialization -: ---------- > 4: 4c1b989c41 serve: advertise object-info feature -: ---------- > 5: dbc95a9ae5 transport: add client support for object-info -: ---------- > 6: f244ec8a2f cat-file: add remote-object-info to batch-command
Signature · 5 lines
2.47.0 Information Footer: base-commit: 8f8d6eee531b3fa1a8ef14f169b0cb5035f7a772 Merge Request: https://gitlab.com/gitlab-org/git/-/merge_requests/168
PATCH v8 1/6 cat-file: add declaration of variable i inside its for loop
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.
--- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -676,12 +676,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -689,9 +687,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 ;
@@ -717,7 +713,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 };
@@ -727,7 +722,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
@@ -1329,9 +1329,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 (int i =0 ; i < server_options->nr; i++)packet_buf_write (req_buf,"server-option=%s" , server_options->items[i].string); }
Signature · 1 line
2.47.0
PATCH v8 2/6 fetch-pack: refactor packet writing
In reply to Eric JuFrom: Calvin Wan <[email protected]>
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_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 a2ba162c (object-info: support for retrieving object info, 2021-04-20).
Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future.
To move write_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously,
in fetch_pack.c, advertise_sid was a static variable, modified using git_config_get_bool().
In connect.c, we now initialize advertise_sid at the beginning by directly using git_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():
git_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2.
About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly.
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-)
connect.c +34 -0
@@ -689,6 +689,40 @@ int server_supports(const char *feature)return !!server_feature_value (feature,NULL ); } +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; + +git_config_get_bool ("transfer.advertisesid" , &advertise_sid); + +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" )) +packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); +if (server_options && server_options->nr) { +ensure_server_supports_v2 ("server-option" ); +for (int 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 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 ) { +die (_ ("the server does not support algorithm '%s'" ), + the_hash_algo->name); + } +packet_buf_delim (req_buf); +} +enum protocol {PROTO_LOCAL =1 ,PROTO_FILE ,
connect.h +8 -0
@@ -1,6 +1,7 @@#ifndef CONNECT_H #define CONNECT_H +#include "string-list.h" #include "protocol.h" #define CONNECT_VERBOSE (1u << 0)
@@ -30,4 +31,11 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/** +* write_command_and_capabilities 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 -33
@@ -1317,37 +1317,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; - -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 (int i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) { -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,
@@ -1358,7 +1327,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" );
@@ -2186,7 +2155,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_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.47.0
PATCH v8 3/6 fetch-pack: move fetch initialization
In reply to Eric JuFrom: 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
@@ -1648,18 +1648,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.47.0
PATCH v8 4/6 serve: advertise object-info feature
In reply to Eric JuFrom: 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
serve.c +3 -1
@@ -70,7 +70,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",
@@ -78,6 +78,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS/* disabled by default */ advertise_object_info =0 ; } +if (value && advertise_object_info) +strbuf_addstr (value,"size" );return advertise_object_info; }
Signature · 1 line
2.47.0
PATCH v8 5/6 transport: add client support for object-info
In reply to Eric JuFrom: 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)."
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.
--- Makefile | 1 + fetch-object-info.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 18 +++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 + transport-helper.c | 11 +++++- transport.c | 28 +++++++++++++- transport.h | 11 ++++++ 8 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h
Makefile +1 -0
@@ -1020,6 +1020,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 +92 -0
92 lines not on this page · raw message
fetch-object-info.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef FETCH_OBJECT_INFO_H +#define FETCH_OBJECT_INFO_H + +#include "pkt-line.h" +#include "protocol.h" +#include "object-store-ll.h" + +struct object_info_args { +struct string_list *object_info_options; +const struct string_list *server_options; +struct oid_array *oids; +}; + +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
@@ -1654,6 +1654,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"
@@ -42,6 +43,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
transport-helper.c +9 -2
@@ -710,8 +710,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" ));
@@ -727,6 +727,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"
@@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options + && 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.object_info_options = transport->smart_options->object_info_options; + obj_info_args.oids = transport->smart_options->object_info_oids; + +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
@@ -5,6 +5,7 @@#include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options {unsigned thin :1 ;
@@ -30,6 +31,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;
@@ -53,6 +60,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.47.0
Re: [PATCH v8 5/6] transport: add client support for object-info
Thanks for picking up this series btw!
Quoted 94 lines · Eric Ju
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)."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.
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]>--- Makefile | 1 + fetch-object-info.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 18 +++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 + transport-helper.c | 11 +++++- transport.c | 28 +++++++++++++- transport.h | 11 ++++++ 8 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.hMakefile +1
-0@@ -1020,6 +1020,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 +44
-0@@ -0,0 +1,92 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "object-store-ll.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/** +* send_object_info_request sends git-cat-file object-info command and its +* arguments into the request buffer. +*/ +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" ); + +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" )); + +strbuf_release (&req_buf); +} + +/** +* fetch_object_info sends git-cat-file object-info command into the request buf +* and read the results from packets. +*/ +int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +struct packet_reader *reader,struct object_info *object_info_data, +const int stateless_rpc,const int fd_out) +{ +int size_index =-1 ;
fetch-object-info.c line 54
Quoted 10 lines
fetch-object-info.c new file +10
-0+ +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); +break ; +case protocol_v1: +case protocol_v0: +die (_ ("wrong protocol version. expected v2" ));
s/wrong/unsupported
Quoted 9 lines
fetch-object-info.c new file +9
-0+case protocol_unknown_version: +BUG ("unknown protocol version" ); + } + +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 73
Quoted 10 lines
fetch-object-info.c new file +10
-0+if (unsorted_string_list_has_string (args->object_info_options, reader->line)) { +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 (long )); + } +continue ; + } +return -1 ; + }
I think we can flatten this logic a bit more here to make it more intuitive.
if (!unsorted_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(long));
}Re: [PATCH v8 5/6] transport: add client support for object-info
Quoted 11 lines · Calvin Wan
Thanks for picking up this series btw!
...
I think we can flatten this logic a bit more here to make it more intuitive.if (!unsorted_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(long)); }
Indeed the updated code structure gets easier to follow.
Thanks, both of you.
Re: [PATCH v8 5/6] transport: add client support for object-info
In reply to Calvin WanQuoted 102 lines · Calvin Wan
Thanks for picking up this series btw!
Quoted 98 lines · Eric Ju
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)."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.
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]>--- Makefile | 1 + fetch-object-info.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 18 +++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 + transport-helper.c | 11 +++++- transport.c | 28 +++++++++++++- transport.h | 11 ++++++ 8 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.hMakefile +1
-0@@ -1020,6 +1020,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 +48
-0@@ -0,0 +1,92 @@ +#include "git-compat-util.h" +#include "gettext.h" +#include "hex.h" +#include "pkt-line.h" +#include "connect.h" +#include "oid-array.h" +#include "object-store-ll.h" +#include "fetch-object-info.h" +#include "string-list.h" + +/** +* send_object_info_request sends git-cat-file object-info command and its +* arguments into the request buffer. +*/ +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" ); + +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" )); + +strbuf_release (&req_buf); +} + +/** +* fetch_object_info sends git-cat-file object-info command into the request buf +* and read the results from packets. +*/ +int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +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" ))
Quoted 9 lines
fetch-object-info.c line 54
Quoted 6 lines
fetch-object-info.c new file +6
-0+die (_ ("object-info capability is not enabled on the server" )); +send_object_info_request (fd_out, args); +break ; +case protocol_v1: +case protocol_v0: +die (_ ("wrong protocol version. expected v2" ));s/wrong/unsupported
Thank you. Fixing it in v9.
Quoted 22 lines
Quoted 9 lines
fetch-object-info.c new file +9
-0+case protocol_unknown_version: +BUG ("unknown protocol version" ); + } + +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 73
Quoted 10 lines
fetch-object-info.c new file +10
-0+if (unsorted_string_list_has_string (args->object_info_options, reader->line)) { +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 (long )); + } +continue ; + } +return -1 ; + }I think we can flatten this logic a bit more here to make it more intuitive.
Quoted 7 lines
if (!unsorted_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(long)); }
Thank you. Revising it in v9.
PATCH v8 6/6 cat-file: add remote-object-info to batch-command
In reply to Eric JuSince 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.
cat-file --batch-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 loop 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.txt | 24 +- builtin/cat-file.c | 99 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 652 +++++++++++++++++++++++++ 7 files changed, 802 insertions(+), 16 deletions(-) create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Documentation/git-cat-file.txt +20 -4
@@ -149,6 +149,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. + Error when the `object-info` capability is not supported by the server. + 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`
@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`
@@ -298,13 +306,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
@@ -314,7 +323,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 the 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
@@ -396,6 +408,10 @@ scripting purposes. CAVEATS ------- +Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will error +and exit when they are 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 +99 -0
132 lines not on this page · raw message
object-file.c +11 -0
@@ -3128,3 +3128,14 @@ int read_loose_object(const char *path,munmap (map, mapsize);return ret; } + +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); +free (object_info->type_name); +}
object-store-ll.h +3 -0
@@ -553,4 +553,7 @@ int for_each_object_in_pack(struct packed_git *p,int for_each_packed_object (struct repository *repo,each_packed_object_fn cb,void *data,enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents (struct object_info *object_info); +#endif /* OBJECT_STORE_LL_H */
t/lib-cat-file.sh new file +16 -0
@@ -0,0 +1,16 @@ +# Library of git-cat-file related tests. + +# Print a string without a trailing newline +echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0). +echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces. +strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +}
t/t1006-cat-file.sh +1 -12
@@ -3,6 +3,7 @@ test_description='git cat-file' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh test_cmdmode_usage () { test_expect_code 129 "$@" 2>err &&
@@ -98,18 +99,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 oid=$2
t/t1017-cat-file-remote-object-info.sh new file +652 -0
652 lines not on this page · raw message
Signature · 1 line
2.47.0
Quoted 127 lines · Eric Ju
Since the
infocommand 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-infoto cat-file --batch-command.While
infotakes object ids one at a time, this creates overhead when making requests to a server.Soremote-object-infoinstead can take multiple object ids at once.cat-file --batch-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 infoIn --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 infoNotice 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-infois implemented in the following manner:- Receive and parse input from user
If command isremote-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 infoAnd 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 isremote-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 infoTo summarize,
remote-object-infogets object info from the remote and then loop 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>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]>--- Documentation/git-cat-file.txt | 24 +- builtin/cat-file.c | 99 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 652 +++++++++++++++++++++++++ 7 files changed, 802 insertions(+), 16 deletions(-) create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.shDocumentation/git-cat-file.txt +12
-3@@ -149,6 +149,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. + Error when the `object-info` capability is not supported by the server. + 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`@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`@@ -298,13 +306,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
Documentation/git-cat-file.txt newline. The available atoms are: · line 329
Quoted 8 lines
Documentation/git-cat-file.txt +4
-1@@ -314,7 +323,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 the default format to stay the same!!!
I remember this was one of my initial concerns when I first worked on this series -- without a use case for other fields, it's definitely hard to say how a default format for such would look and obviously when implemented, would cause the default format of %(objectsize) to change as well. I'm glad to see this outcome is well documented so we can have this feature working with a backdoor to change it if necessary for the future. Thanks again for your work on this series.
Re: [PATCH v8 0/6] add remote-object-info to batch-command
In reply to Eric JuQuoted 7 lines · Eric Ju
Range-diff against v7:
-: ---------- > 1: c09e21a9d6 cat-file: add declaration of variable i inside its for loop -: ---------- > 2: ed04a4a7c4 fetch-pack: refactor packet writing -: ---------- > 3: bc52c4f80c fetch-pack: move fetch initialization -: ---------- > 4: 4c1b989c41 serve: advertise object-info feature -: ---------- > 5: dbc95a9ae5 transport: add client support for object-info -: ---------- > 6: f244ec8a2f cat-file: add remote-object-info to batch-command
This is curious. Did you compare the right things?
--
2.47.0
Information Footer:
base-commit: 8f8d6eee531b3fa1a8ef14f169b0cb5035f7a772
Merge Request: https://gitlab.com/gitlab-org/git/-/merge_requests/168If the base-commit information is relevant, please do not write it below the "signature" like (i.e. a line that consists only of dash-dash-space near the end of the message), as some e-mail programs consider them irrelevant and omit from quoting.
I tried to apply them on top of 8f8d6eee (The seventh batch, 2024-11-01) but the last step [6/6] fails to apply (the first five applied cleanly, and matched what I already had).
Could you help to figure out what is going wrong on your end?
Thanks.
Re: [PATCH v8 0/6] add remote-object-info to batch-command
Sorry for the noise. I forgot to CC others, so I am resending it.
Quoted 11 lines · Junio C Hamano
Quoted 7 lines · Eric Ju
Range-diff against v7:
-: ---------- > 1: c09e21a9d6 cat-file: add declaration of variable i inside its for loop -: ---------- > 2: ed04a4a7c4 fetch-pack: refactor packet writing -: ---------- > 3: bc52c4f80c fetch-pack: move fetch initialization -: ---------- > 4: 4c1b989c41 serve: advertise object-info feature -: ---------- > 5: dbc95a9ae5 transport: add client support for object-info -: ---------- > 6: f244ec8a2f cat-file: add remote-object-info to batch-command
Quoted 2 lines
This is curious. Did you compare the right things?
Thank you.
I think I may compare it wrong.
Quoted 7 lines
--
2.47.0
Information Footer:
base-commit: 8f8d6eee531b3fa1a8ef14f169b0cb5035f7a772
Merge Request: https://gitlab.com/gitlab-org/git/-/merge_requests/168Quoted 5 lines
If the base-commit information is relevant, please do not write it below the "signature" like (i.e. a line that consists only of dash-dash-space near the end of the message), as some e-mail programs consider them irrelevant and omit from quoting.
Roger that.
Quoted 6 lines
I tried to apply them on top of
8f8d6eee(The seventh batch, 2024-11-01) but the last step [6/6] fails to apply (the first five applied cleanly, and matched what I already had).Could you help to figure out what is going wrong on your end?
Should I resend v8 or send a v9 instead?
Quoted 1 line
Thanks.
v9 cover cat-file: add remote-object-info to batch-command
In reply to Eric JuBecause I mistakenly sent a wrong range-diff in v8, please consider this v9 as both an update addressing new comments from Calvin Wan at
https://lore.kernel.org/git/CAFySSZAqh6J14+r9JLM3LmRmV02ZvPRf5dB3rWVnUZS_5XaHcQ@mail.gmail.com/
and a resend of the corrected range-diff for v8.
This patch series is a continuation of Calvin Wan’s ([email protected]) patch series [PATCH v5 0/6] cat-file: add --batch-command remote-object-info command at [1].
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)"[2]. This patch series implement the client option for it.
This patch series add 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 in this implementation. The type (%(objecttype)) is not included in this patch series, as it is not yet supported on the server side either. The plan is to implement the necessary logic for both the server and client in a subsequent series.
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 terminate with an error.
Changes since V7 (v8 had an incorrect range-diff)
================
- Introduced strtoul_ul() in git-compat-util.h to ensure proper error handling using strtoul from the standard library.
- Separated the test library into its own commit for better clarity and organization.
- Use string_list_has_string() instead of unsorted_string_list_has_string() to avoid quadratic runtime behaviour
- Added a documentation link to the wire format in the commit message to provide additional context.
- New test case "remote-object-info fails on not providing OID"
- Fixed typos and formatting issues for improved readability.
- Flattened the memory allocation logic of sizep in object_info_data for better intuitiveness and readability.
Calvin Wan (4):
fetch-pack: refactor packet writing
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_ul() with error handling
cat-file: add declaration of variable i inside its for loop
cat-file: split test utility functions into a separate library file
cat-file: add remote-object-info to batch-command
Documentation/git-cat-file.txt | 24 +- Makefile | 1 + builtin/cat-file.c | 110 +++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 85 ++++ fetch-object-info.h | 22 + fetch-pack.c | 51 +- fetch-pack.h | 2 + git-compat-util.h | 18 + object-file.c | 11 + object-store-ll.h | 3 + serve.c | 4 +- t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ transport-helper.c | 11 +- transport.c | 28 +- transport.h | 11 + 19 files changed, 1048 insertions(+), 68 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Range-diff against v7:
-: ---------- > 1: 63997081d1 git-compat-util: add strtoul_ul() with error handling
1: 5181e849eb ! 2: f188962f05 cat-file: add declaration of variable i inside its for loop
@@ fetch-pack.c: static void write_fetch_command_and_capabilities(struct strbuf *re
- int i;
ensure_server_supports_v2("server-option");
- for (i = 0; i < server_options->nr; i++)
-+ for (int 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);
}
-: ---------- > 3: 71250a03d2 cat-file: split test utility functions into a separate library file
2: 0c6acf58c2 ! 4: 0ab26e6cd5 fetch-pack: refactor packet writing
@@ connect.c: int server_supports(const char *feature)
+ packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
+ if (server_options && server_options->nr) {
+ ensure_server_supports_v2("server-option");
-+ for (int 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);
+ }
@@ connect.c: int server_supports(const char *feature)
PROTO_FILE,
## connect.h ##
-@@
- #ifndef CONNECT_H
- #define CONNECT_H
-
-+#include "string-list.h"
- #include "protocol.h"
-
- #define CONNECT_VERBOSE (1u << 0)
@@ connect.h: void check_stateless_delimiter(int stateless_rpc,
struct packet_reader *reader,
const char *error);
-+/**
-+ * write_command_and_capabilities writes a command along with the requested
++/*
++ * Writes a command along with the requested
+ * server capabilities/features into a request buffer.
+ */
++struct string_list;
+void write_command_and_capabilities(struct strbuf *req_buf, const char *command,
+ const struct string_list *server_options);
+
@@ fetch-pack.c: static int add_haves(struct fetch_negotiator *negotiator,
- packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
- if (server_options && server_options->nr) {
- ensure_server_supports_v2("server-option");
-- for (int 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);
- }
3: 28ef74980c = 5: 8b381b4bdc fetch-pack: move fetch initialization
4: cb5bf65b88 = 6: a0a15e1e4f serve: advertise object-info feature
5: 79eab87dd2 ! 7: e1aad1ec30 transport: add client support for object-info
@@ Commit message
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)."
+ 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
+ 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.
+
Helped-by: Jonathan Tan <[email protected]>
Helped-by: Christian Couder <[email protected]>
Signed-off-by: Calvin Wan <[email protected]>
@@ fetch-object-info.c (new)
+#include "fetch-object-info.h"
+#include "string-list.h"
+
-+/**
-+ * send_object_info_request sends git-cat-file object-info command and its
-+ * arguments into the request buffer.
-+ */
++/* Sends git-cat-file object-info command and its arguments into the request buffer. */
+static void send_object_info_request(const int fd_out, struct object_info_args *args)
+{
+ struct strbuf req_buf = STRBUF_INIT;
@@ fetch-object-info.c (new)
+ if (unsorted_string_list_has_string(args->object_info_options, "size"))
+ packet_buf_write(&req_buf, "size");
+
-+ if (args->oids) {
++ 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)
@@ fetch-object-info.c (new)
+ strbuf_release(&req_buf);
+}
+
-+/**
-+ * fetch_object_info sends git-cat-file object-info command into the request buf
-+ * and read the results from packets.
-+ */
+int fetch_object_info(const enum protocol_version version, struct object_info_args *args,
+ struct packet_reader *reader, struct object_info *object_info_data,
+ const int stateless_rpc, const int fd_out)
@@ fetch-object-info.c (new)
+ break;
+ case protocol_v1:
+ case protocol_v0:
-+ die(_("wrong protocol version. expected v2"));
++ die(_("unsupported protocol version. expected v2"));
+ case protocol_unknown_version:
+ BUG("unknown protocol version");
+ }
@@ fetch-object-info.c (new)
+ check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected");
+ return -1;
+ }
-+ if (unsorted_string_list_has_string(args->object_info_options, reader->line)) {
-+ 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(long));
-+ }
-+ continue;
++ 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));
+ }
-+ return -1;
+ }
+
+ for (size_t i = 0; packet_reader_read(reader) == PACKET_READ_NORMAL && i < args->oids->nr; i++){
@@ fetch-object-info.c (new)
+ die("object-info: not our ref %s",
+ object_info_values.items[0].string);
+
-+ *object_info_data[i].sizep = strtoul(object_info_values.items[1 + size_index].string, NULL, 10);
++ if (strtoul_ul(object_info_values.items[1 + size_index].string, 10, 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);
@@ fetch-object-info.h (new)
+ 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);
@@ transport.c: 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
-+ && transport->smart_options->object_info
-+ && transport->smart_options->object_info_oids->nr > 0) {
++ if (transport->smart_options && 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.object_info_options = transport->smart_options->object_info_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,
6: b60863aa5b ! 8: 0795ad53fe cat-file: add remote-object-info to batch-command
@@ builtin/cat-file.c: static void batch_one_object(const char *obj_name,
+ die(_("Not a valid object name %s"), argv[i]);
+ oid_array_append(&object_info_oids, &oid);
+ }
-+
++ if (object_info_oids.nr == 0) {
++ die(_("remote-object-info requires objects"));
++ }
+ gtransport = transport_get(remote, NULL);
+ if (gtransport->smart_options) {
+ CALLOC_ARRAY(remote_object_info, object_info_oids.nr);
@@ builtin/cat-file.c: static void parse_cmd_info(struct batch_options *opt,
+ opt->use_remote_info = 1;
+ 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
@@ object-store-ll.h: int for_each_object_in_pack(struct packed_git *p,
+
#endif /* OBJECT_STORE_LL_H */
- ## t/lib-cat-file.sh (new) ##
-@@
-+# Library of git-cat-file related tests.
-+
-+# Print a string without a trailing newline
-+echo_without_newline () {
-+ printf '%s' "$*"
-+}
-+
-+# Print a string without newlines and replaces them with a NULL character (\0).
-+echo_without_newline_nul () {
-+ echo_without_newline "$@" | tr '\n' '\0'
-+}
-+
-+# Calculate the length of a string removing any leading spaces.
-+strlen () {
-+ echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
-+}
-
- ## t/t1006-cat-file.sh ##
-@@
- test_description='git cat-file'
-
- . ./test-lib.sh
-+. "$TEST_DIRECTORY"/lib-cat-file.sh
-
- test_cmdmode_usage () {
- test_expect_code 129 "$@" 2>err &&
-@@ t/t1006-cat-file.sh: 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
- oid=$2
-
## t/t1017-cat-file-remote-object-info.sh (new) ##
@@
+#!/bin/sh
@@ t/t1017-cat-file-remote-object-info.sh (new)
+ )
+'
+
-+test_expect_success 'remote-object-info fails on server with legacy protocol' '
++test_expect_success 'remote-object-info fails on server with legacy protocol with default filter' '
+ (
+ set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
@@ t/t1017-cat-file-remote-object-info.sh (new)
+ )
+'
+
++test_expect_success 'remote-object-info fails on not providing OID' '
++ (
++ set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
++ cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
++
++ test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
++ remote-object-info "$HTTPD_URL/smart/http_parent"
++ EOF
++ test_grep "remote-object-info requires objects" err
++ )
++'
++
+
+# Test --batch-command remote-object-info with 'http://' transport and
+# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capabilitySignature · 1 line
2.47.0
PATCH v9 1/8 git-compat-util: add strtoul_ul() with error handling
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 commit introduces strtoul_ul() to address this gap, enabling the return of an unsigned long with proper error handling.
--- git-compat-util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
git-compat-util.h +18 -0
@@ -1351,6 +1351,24 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +// Converts a string to an unsigned long using the standard library's strtoul, +// with additional error handling to ensure robustness. +static inline int strtoul_ul (char const *s,int base,unsigned long *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.47.0
Re: [PATCH v9 1/8] git-compat-util: add strtoul_ul() with error handling
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 1355
Quoted 2 lines · Eric Ju
git-compat-util.h +2
-0+// Converts a string to an unsigned long using the standard library's strtoul, +// with additional error handling to ensure robustness.
We use comments like this:
/*
* Converts a string to an unsigned long using the standard library's strtoul,
* with additional error handling to ensure robustness.
*/
Also we use the imperative mood in comments before a function, so:
s/Converts/Convert/
Re: [PATCH v9 1/8] git-compat-util: add strtoul_ul() with error handling
Thank you. Fixed in v10.
Quoted 16 lines · Christian Couder
git-compat-util.h static inline int strtoul_ui(char const *s, int base, unsigned int *result) · line 1355
Quoted 2 lines · Eric Ju
git-compat-util.h +2
-0+// Converts a string to an unsigned long using the standard library's strtoul, +// with additional error handling to ensure robustness. We use comments like this:
/*
* Converts a string to an unsigned long using the standard library's strtoul,
* with additional error handling to ensure robustness.
*/Also we use the imperative mood in comments before a function, so:
s/Converts/Convert/
PATCH v9 2/8 cat-file: add declaration of variable i inside its for loop
In reply to Eric JuSome 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.
--- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -676,12 +676,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -689,9 +687,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 ;
@@ -717,7 +713,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 };
@@ -727,7 +722,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
@@ -1329,9 +1329,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.47.0
Re: [PATCH v9 2/8] cat-file: add declaration of variable i inside its for loop
Quoted 5 lines · Eric Ju
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.
It might be nice to say that, while at it, we also change the type from "int" to "size_t" where the latter makes more sense.
Re: [PATCH v9 2/8] cat-file: add declaration of variable i inside its for loop
Thank you. Add that in the commit message.
Quoted 10 lines · Christian Couder
Quoted 5 lines · Eric Ju
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.
It might be nice to say that, while at it, we also change the type from "int" to "
size_t" where the latter makes more sense.
PATCH v9 3/8 cat-file: split test utility functions into a separate library file
In reply to Eric JuThis refactor extracts utility functions from the cat-file's test t1006-cat-file.sh into a dedicated library file. 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(-) create mode 100644 t/lib-cat-file.sh
t/lib-cat-file.sh new file +16 -0
@@ -0,0 +1,16 @@ +# Library of git-cat-file related tests. + +# Print a string without a trailing newline +echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0). +echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces. +strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +}
t/t1006-cat-file.sh +1 -12
@@ -3,6 +3,7 @@ test_description='git cat-file' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh test_cmdmode_usage () { test_expect_code 129 "$@" 2>err &&
@@ -98,18 +99,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 oid=$2
Signature · 1 line
2.47.0
Re: [PATCH v9 3/8] cat-file: split test utility functions into a separate library file
About the commit subject, maybe something like the following would be a bit shorter:
t1006: split test utility functions into new "lib-cat-file.sh"
Quoted 2 lines · Eric Ju
This refactor extracts utility functions from the cat-file's test
s/test/test script/
Quoted 1 line
t1006-cat-file.shinto a dedicated library file. The goal is to improve
s/a dedicated library file/a new "lib-cat-file.sh" dedicated library file/
Quoted 2 lines
code reuse and readability, enabling future tests to leverage these utilities without duplicating code
s/code/code./
t/lib-cat-file.sh line 1
Quoted 7 lines
t/lib-cat-file.sh new file +1
-0@@ -0,0 +1,16 @@ +# Library of git-cat-file related tests.
s/tests/test functions/
t/lib-cat-file.sh line 3
Quoted 2 lines
t/lib-cat-file.sh new file +2
-0+ +# Print a string without a trailing newline
s/newline/newline./
t/lib-cat-file.sh line 8
Quoted 5 lines
t/lib-cat-file.sh new file +5
-0+echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0).
s/replaces/replace/
t/lib-cat-file.sh line 13
Quoted 5 lines
t/lib-cat-file.sh new file +5
-0+echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces.
This might be a bit misleading as leading spaces are removed from the output from wc -c, not from the string.
Quoted 3 lines
t/lib-cat-file.sh new file +3
-0+strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +}
Re: [PATCH v9 3/8] cat-file: split test utility functions into a separate library file
Thank you Christian. They are all fixed in v10
Quoted 44 lines · Christian Couder
About the commit subject, maybe something like the following would be a bit shorter:
t1006: split test utility functions into new "
lib-cat-file.sh"Quoted 2 lines · Eric Ju
This refactor extracts utility functions from the cat-file's test
s/test/test script/
Quoted 1 line
t1006-cat-file.shinto a dedicated library file. The goal is to improves/a dedicated library file/a new "
lib-cat-file.sh" dedicated library file/Quoted 2 lines
code reuse and readability, enabling future tests to leverage these utilities without duplicating code
s/code/code./
t/lib-cat-file.sh line 1
Quoted 7 lines
t/lib-cat-file.sh new file +1
-0@@ -0,0 +1,16 @@ +# Library of git-cat-file related tests.s/tests/test functions/
t/lib-cat-file.sh line 3
Quoted 2 lines
t/lib-cat-file.sh new file +2
-0+ +# Print a string without a trailing newlines/newline/newline./
t/lib-cat-file.sh line 8
Quoted 5 lines
t/lib-cat-file.sh new file +5
-0+echo_without_newline () { + printf '%s' "$*" +} + +# Print a string without newlines and replaces them with a NULL character (\0).s/replaces/replace/
Quoted 9 lines
t/lib-cat-file.sh line 13
Quoted 5 lines
t/lib-cat-file.sh new file +5
-0+echo_without_newline_nul () { + echo_without_newline "$@" | tr '\n' '\0' +} + +# Calculate the length of a string removing any leading spaces.This might be a bit misleading as leading spaces are removed from the output from
wc -c, not from the string.
Yes, I will just change it to "Calculate the length of a string. "
Quoted 3 lines
Quoted 3 lines
t/lib-cat-file.sh new file +3
-0+strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +}
PATCH v9 4/8 fetch-pack: refactor packet writing
In reply to Eric JuFrom: Calvin Wan <[email protected]>
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_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 a2ba162c (object-info: support for retrieving object info, 2021-04-20).
Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future.
To move write_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously,
in fetch_pack.c, advertise_sid was a static variable, modified using git_config_get_bool().
In connect.c, we now initialize advertise_sid at the beginning by directly using git_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():
git_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2.
About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly.
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-)
connect.c +34 -0
@@ -689,6 +689,40 @@ int server_supports(const char *feature)return !!server_feature_value (feature,NULL ); } +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; + +git_config_get_bool ("transfer.advertisesid" , &advertise_sid); + +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" )) +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 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 ) { +die (_ ("the server does not support algorithm '%s'" ), + the_hash_algo->name); + } +packet_buf_delim (req_buf); +} +enum protocol {PROTO_LOCAL =1 ,PROTO_FILE ,
connect.h +8 -0
@@ -30,4 +30,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested +* server capabilities/features into a request buffer. +*/ +struct string_list ; +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options); +#endif
fetch-pack.c +2 -33
@@ -1317,37 +1317,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; - -ensure_server_supports_v2 ("fetch" ); -packet_buf_write (req_buf,"command=fetch" ); -if (server_supports_v2 ("agent" )) -packet_buf_write (req_buf,"agent=%s" ,git_user_agent_sanitized ()); -if (advertise_sid &&server_supports_v2 ("session-id" )) -packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); -if (server_options && server_options->nr) { -ensure_server_supports_v2 ("server-option" ); -for (size_t i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) { -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,
@@ -1358,7 +1327,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" );
@@ -2186,7 +2155,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_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.47.0
PATCH v9 5/8 fetch-pack: move fetch initialization
In reply to Eric JuFrom: 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
@@ -1648,18 +1648,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.47.0
PATCH v9 6/8 serve: advertise object-info feature
In reply to Eric JuFrom: 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
serve.c +3 -1
@@ -70,7 +70,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",
@@ -78,6 +78,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS/* disabled by default */ advertise_object_info =0 ; } +if (value && advertise_object_info) +strbuf_addstr (value,"size" );return advertise_object_info; }
Signature · 1 line
2.47.0
PATCH v9 7/8 transport: add client support for object-info
In reply to Eric JuFrom: 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 | 85 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ transport-helper.c | 11 ++++-- transport.c | 28 ++++++++++++++- transport.h | 11 ++++++ 8 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h
Makefile +1 -0
@@ -1020,6 +1020,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 +85 -0
85 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 "object-store-ll.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
@@ -1654,6 +1654,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"
@@ -42,6 +43,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
transport-helper.c +9 -2
@@ -710,8 +710,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" ));
@@ -727,6 +727,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"
@@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options && 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
@@ -5,6 +5,7 @@#include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options {unsigned thin :1 ;
@@ -30,6 +31,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;
@@ -53,6 +60,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.47.0
PATCH v9 8/8 cat-file: add remote-object-info to batch-command
In reply to Eric JuSince 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.
cat-file --batch-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 loop 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.txt | 24 +- builtin/cat-file.c | 99 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ 5 files changed, 797 insertions(+), 4 deletions(-) create mode 100755 t/t1017-cat-file-remote-object-info.sh
Documentation/git-cat-file.txt +20 -4
@@ -149,6 +149,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. + Error when the `object-info` capability is not supported by the server. + 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`
@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`
@@ -298,13 +306,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
@@ -314,7 +323,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 the 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
@@ -396,6 +408,10 @@ scripting purposes. CAVEATS ------- +Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will error +and exit when they are 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 +99 -0
132 lines not on this page · raw message
object-file.c +11 -0
@@ -3128,3 +3128,14 @@ int read_loose_object(const char *path,munmap (map, mapsize);return ret; } + +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); +free (object_info->type_name); +}
object-store-ll.h +3 -0
@@ -553,4 +553,7 @@ int for_each_object_in_pack(struct packed_git *p,int for_each_packed_object (struct repository *repo,each_packed_object_fn cb,void *data,enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents (struct object_info *object_info); +#endif /* OBJECT_STORE_LL_H */
t/t1017-cat-file-remote-object-info.sh new file +664 -0
664 lines not on this page · raw message
Signature · 1 line
2.47.0
Quoted 2 lines · Eric Ju
Since the
infocommand in cat-file --batch-command prints object info
Nit: Everywhere in this commit message, it would be a bit clearer and easier to read with:
s/cat-file --batch-command/cat-file --batch-command/
Quoted 4 lines
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-infoto cat-file --batch-command.
s/remote-object-info/a new remote-object-info command/
[...]
Quoted 2 lines
To summarize,
remote-object-infogets object info from the remote and then loop through the object info passed in, printing the info.
s/loop/loops/
Documentation/git-cat-file.txt info <object>:: · line 155
Quoted 4 lines
Documentation/git-cat-file.txt +4
-0+remote-object-info <remote> <object>...:: + Print object info for object references `<object>` at specified + `<remote>` without downloading objects from the remote. + Error when the `object-info` capability is not supported by the server.
I think it's more grammatically correct to use "Error out when..." or "Raise an error when..." than just "Error when..."
Also maybe: s/server/remote/
Quoted 1 line
+ Error when no object references are provided.
Here also "Error out when..." or "Raise an error when..."
Quoted 1 line
+ This command may be combined with
--buffer.
[...]
Documentation/git-cat-file.txt newline. The available atoms are: · line 329
Quoted 6 lines
If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize), except forremote-object-infocommands which use +%(objectname) %(objectsize)` for now because "%(objecttype)" is not supported yet.Documentation/git-cat-file.txt +2
-0+WARNING: When "%(objecttype)" is supported, the default format WILL be unified, so +DO NOT RELY on the current the default format to stay the same!!!
s/current the default/current default/
Documentation/git-cat-file.txt scripting purposes. · line 412
Quoted 5 lines
CAVEATS
-------Documentation/git-cat-file.txt +2
-0+Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will error
s/error/raise an error/
or maybe:
s/error and exit/error out/
Quoted 1 line
+and exit when they are in the format string.
s/are/appear/
builtin/cat-file.c struct batch_options { · line 51
Quoted 5 lines
builtin/cat-file.c +1
-0@@ -45,9 +48,12 @@ struct batch_options {char input_delim;char output_delim;const char *format; +int use_remote_info;
"unsigned int" might be a bit better for bool fields like this.
Actually it seems to me that this field is set to 0 and 1 in some places but we never read it, so I wonder if it's actually useful.
Quoted 8 lines
builtin/cat-file.c +2
-0};@@ -579,6 +585,61 @@ 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) +{
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 599
Quoted 10 lines
builtin/cat-file.c +10
-0+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; + +/* +* Change the format to "%(objectname) %(objectsize)" when +* remote-object-info command is used. Once we start supporting objecttype +* the default format should change to DEFAULT_FORMAT
s/DEFAULT_FORMAT/DEFAULT_FORMAT./
Quoted 7 lines
builtin/cat-file.c +7
-0+*/ +if (!opt->format) + opt->format ="%(objectname) %(objectsize)" ; + + remote =remote_get (argv[0 ]); +if (!remote) +die (_ ("must supply valid remote when using remote-object-info" ));
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 616
Quoted 10 lines
builtin/cat-file.c +10
-0+ +oid_array_clear (&object_info_oids); +for (size_t i =1 ; i < argc; i++) { +if (get_oid_hex (argv[i], &oid)) +die (_ ("Not a valid object name %s" ), argv[i]); +oid_array_append (&object_info_oids, &oid); + } +if (object_info_oids.nr ==0 ) { +die (_ ("remote-object-info requires objects" )); + }
We prefer to drop '{' and '}' and use "!X" instead of "X == 0" when possible, so:
if (!object_info_oids.nr)
die(_("remote-object-info requires objects"));Thanks.
Quoted 10 lines · Christian Couder
Quoted 2 lines · Eric Ju
Since the
infocommand in cat-file --batch-command prints object infoNit: Everywhere in this commit message, it would be a bit clearer and easier to read with:
s/cat-file --batch-command/
cat-file --batch-command/
Thank you. Fixed in v10.
Quoted 7 lines
Quoted 4 lines
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-infoto cat-file --batch-command.s/
remote-object-info/a newremote-object-infocommand/
Thank you. Fixed in v10.
Quoted 7 lines
[...]
Quoted 2 lines
To summarize,
remote-object-infogets object info from the remote and then loop through the object info passed in, printing the info.s/loop/loops/
Thank you. Fixed in v10.
Quoted 10 lines
Documentation/git-cat-file.txt info <object>:: · line 155
Quoted 4 lines
Documentation/git-cat-file.txt +4
-0+remote-object-info <remote> <object>...:: + Print object info for object references `<object>` at specified + `<remote>` without downloading objects from the remote. + Error when the `object-info` capability is not supported by the server.I think it's more grammatically correct to use "Error out when..." or "Raise an error when..." than just "Error when..."
Also maybe: s/server/remote/
Thank you. Fixed in v10. I will use "Raise an error when..."
Quoted 4 lines
Quoted 1 line
+ Error when no object references are provided.
Here also "Error out when..." or "Raise an error when..."
Thank you. Fixed in v10.
Quoted 4 lines
Quoted 1 line
+ This command may be combined with
--buffer.[...]
Quoted 9 lines
Documentation/git-cat-file.txt newline. The available atoms are: · line 329
Quoted 6 lines
If no format is specified, the default format is `%(objectname)
-%(objecttype) %(objectsize)`.
+%(objecttype) %(objectsize), except forremote-object-infocommands which use +%(objectname) %(objectsize)` for now because "%(objecttype)" is not supported yet.Documentation/git-cat-file.txt +2
-0+WARNING: When "%(objecttype)" is supported, the default format WILL be unified, so +DO NOT RELY on the current the default format to stay the same!!!s/current the default/current default/
Thank you. Fixed in v10.
Quoted 12 lines
Documentation/git-cat-file.txt scripting purposes. · line 412
Quoted 5 lines
CAVEATS
-------Documentation/git-cat-file.txt +2
-0+Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are +currently not supported by the `remote-object-info` command, we will errors/error/raise an error/
or maybe:
s/error and exit/error out/
Thank you. Replaced with "raise an error".
Quoted 4 lines
Quoted 1 line
+and exit when they are in the format string.
s/are/appear/
Thank you. Fixed in v10.
Quoted 11 lines
builtin/cat-file.c struct batch_options { · line 51
Quoted 5 lines
builtin/cat-file.c +1
-0@@ -45,9 +48,12 @@ struct batch_options {char input_delim;char output_delim;const char *format; +int use_remote_info;"unsigned int" might be a bit better for bool fields like this.
Actually it seems to me that this field is set to 0 and 1 in some places but we never read it, so I wonder if it's actually useful.
Thank you. It is used at all and removed in v10.
Quoted 6 lines
Quoted 6 lines
builtin/cat-file.c +0
-0};@@ -579,6 +585,61 @@ static void batch_one_object(const char *obj_name,object_context_release (&ctx); }
Quoted 15 lines
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 599
Quoted 12 lines
builtin/cat-file.c +12
-0+static int get_remote_info (struct batch_options *opt,int argc,const char **argv) +{ +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; + +/* +* Change the format to "%(objectname) %(objectsize)" when +* remote-object-info command is used. Once we start supporting objecttype +* the default format should change to DEFAULT_FORMAT s/DEFAULT_FORMAT/DEFAULT_FORMAT./
Thank you. Fixed in v10.
Quoted 21 lines
Quoted 7 lines
builtin/cat-file.c +7
-0+*/ +if (!opt->format) + opt->format ="%(objectname) %(objectsize)" ; + + remote =remote_get (argv[0 ]); +if (!remote) +die (_ ("must supply valid remote when using remote-object-info" ));builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 616
Quoted 10 lines
builtin/cat-file.c +10
-0+ +oid_array_clear (&object_info_oids); +for (size_t i =1 ; i < argc; i++) { +if (get_oid_hex (argv[i], &oid)) +die (_ ("Not a valid object name %s" ), argv[i]); +oid_array_append (&object_info_oids, &oid); + } +if (object_info_oids.nr ==0 ) { +die (_ ("remote-object-info requires objects" )); + }We prefer to drop '{' and '}' and use "!X" instead of "X == 0" when possible, so:
Quoted 4 lines
if (!object_info_oids.nr) die(_("remote-object-info requires objects"));Thanks.
Thank you. Fixed in v10.
v10 cover cat-file: add remote-object-info to batch-command
In reply to Eric JuThis patch series is a continuation of Calvin Wan’s ([email protected]) patch series [PATCH v5 0/6] cat-file: add --batch-command remote-object-info command at [1].
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)"[2]. This patch series implement the client option for it.
This patch series add 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 in this implementation. The type (%(objecttype)) is not included in this patch series, as it is not yet supported on the server side either. The plan is to implement the necessary logic for both the server and client in a subsequent series.
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 terminate with an error.
Changes since V9
================
- Refactored documentation for improved clarity.
- Refactored commit messages to provide more detailed and relevant information.
- Revised comments to align with best practices and reduce potential confusion or misinterpretation.
- Fixed grammatical errors and typos throughout the code and documentation.
- Removed unused variables to improve code cleanliness and maintainability.
Calvin Wan (4):
fetch-pack: refactor packet writing
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_ul() with error handling
cat-file: add declaration of variable i inside its for loop
t1006: split test utility functions into new "lib-cat-file.sh"
cat-file: add remote-object-info to batch-command
Documentation/git-cat-file.txt | 24 +- Makefile | 1 + builtin/cat-file.c | 107 +++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 85 ++++ fetch-object-info.h | 22 + fetch-pack.c | 51 +- fetch-pack.h | 2 + git-compat-util.h | 20 + object-file.c | 11 + object-store-ll.h | 3 + serve.c | 4 +- t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ transport-helper.c | 11 +- transport.c | 28 +- transport.h | 11 + 19 files changed, 1047 insertions(+), 68 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Range-diff against v9:
1: 0a77ace719 ! 1: a567de3dc6 git-compat-util: add strtoul_ul() with error handling
@@ git-compat-util.h: static inline int strtoul_ui(char const *s, int base, unsigne
return 0;
}
-+// Converts a string to an unsigned long using the standard library's strtoul, -+// with additional error handling to ensure robustness.
++/*
++ * Convert a string to an unsigned long using the standard library's strtoul, ++ * with additional error handling to ensure robustness.
++ */
+static inline int strtoul_ul(char const *s, int base, unsigned long *result) +{
+ unsigned long ul;
2: 51a0a48d7b ! 2: 8d5140b111 cat-file: add declaration of variable i inside its for loop
@@ Commit message
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.
3: fa1d6678a0 ! 3: 42d0539e9b cat-file: split test utility functions into a separate library file
@@ Metadata
Author: Eric Ju <[email protected]>
## Commit message ##
- cat-file: split test utility functions into a separate library file
+ t1006: split test utility functions into new "lib-cat-file.sh"
This refactor extracts utility functions from the cat-file's test
- t1006-cat-file.sh into a dedicated library file. The goal is to improve
- code reuse and readability, enabling future tests to leverage these
- utilities without duplicating code
+ script "t1006-cat-file.sh" into a new "lib-cat-file.sh" dedicated
+ library file. The goal is to improve code reuse and readability,
+ enabling future tests to leverage these utilities without duplicating
+ code.
## t/lib-cat-file.sh (new) ##
@@
-+# Library of git-cat-file related tests.
++# Library of git-cat-file related test functions.
+
-+# Print a string without a trailing newline
++# Print a string without a trailing newline.
+echo_without_newline () {
+ printf '%s' "$*"
+}
+
-+# Print a string without newlines and replaces them with a NULL character (\0). ++# 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 removing any leading spaces. ++# Calculate the length of a string.
+strlen () {
+ echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
4: 61dd598576 = 4: 8fdb2c6f81 fetch-pack: refactor packet writing 5: 106e776dda = 5: 7d5f53de6e fetch-pack: move fetch initialization 6: fe9366b59d = 6: 17583ebcd9 serve: advertise object-info feature 7: 09646f6517 = 7: 22d5eb26d1 transport: add client support for object-info 8: 99c450fc7e ! 8: 0698aa3606 cat-file: add remote-object-info to batch-command
@@ Metadata
## Commit message ##
cat-file: add remote-object-info to batch-command
- 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.
+ 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.
+ 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
+ overhead when making requests to a server. So remote-object-info
instead can take multiple object ids at once.
- cat-file --batch-command is generally implemented in the following
- manner:
+ 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
@@ Commit message
- Get object info, print object info
To summarize, remote-object-info gets object info from the remote and
- then loop through the object info passed in, printing the info.
+ 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:
+ 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>
@@ Documentation/git-cat-file.txt: info <object>::
+remote-object-info <remote> <object>...::
+ Print object info for object references <object> at specified
+ <remote> without downloading objects from the remote.
-+ Error when the object-info capability is not supported by the server.
-+ Error when no object references are provided.
++ 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::
@@ Documentation/git-cat-file.txt: newline. The available atoms are:
+%(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 the default format to stay the same!!! ++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
@@ Documentation/git-cat-file.txt: scripting purposes.
-------
+Note that since %(objecttype), %(objectsize:disk) and %(deltabase) are
-+currently not supported by the remote-object-info command, we will error -+and exit when they are in the format string.
++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
@@ builtin/cat-file.c
enum batch_mode {
BATCH_MODE_CONTENTS,
@@ builtin/cat-file.c: struct batch_options {
- char input_delim;
- char output_delim;
- const char *format;
-+ int use_remote_info;
};
static const char *force_path;
@@ builtin/cat-file.c: static void batch_one_object(const char *obj_name,
+ /*
+ * Change the format to "%(objectname) %(objectsize)" when
+ * remote-object-info command is used. Once we start supporting objecttype
-+ * the default format should change to DEFAULT_FORMAT
++ * the default format should change to DEFAULT_FORMAT.
+ */
+ if (!opt->format)
+ opt->format = "%(objectname) %(objectsize)";
@@ builtin/cat-file.c: static void batch_one_object(const char *obj_name,
+ die(_("Not a valid object name %s"), argv[i]);
+ oid_array_append(&object_info_oids, &oid);
+ }
-+ if (object_info_oids.nr == 0) {
++ if (!object_info_oids.nr)
+ die(_("remote-object-info requires objects"));
-+ }
++
+ gtransport = transport_get(remote, NULL);
+ if (gtransport->smart_options) {
+ CALLOC_ARRAY(remote_object_info, object_info_oids.nr);
@@ builtin/cat-file.c: static void parse_cmd_info(struct batch_options *opt,
+ if (get_remote_info(opt, count, argv))
+ goto cleanup;
+
-+ opt->use_remote_info = 1;
+ data->skip_object_info = 1;
+ for (size_t i = 0; i < object_info_oids.nr; i++) {
+ data->oid = object_info_oids.oid[i];
@@ builtin/cat-file.c: static void parse_cmd_info(struct batch_options *opt,
+ batch_object_write(argv[i+1], output, opt, data, NULL, 0);
+ }
+ }
-+ opt->use_remote_info = 0;
+ data->skip_object_info = 0;
+
+cleanup:
Signature · 1 line
2.47.1
PATCH v10 1/8 git-compat-util: add strtoul_ul() with error handling
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 commit introduces strtoul_ul() to address this gap, enabling the return of an unsigned long with proper error handling.
--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
git-compat-util.h +20 -0
@@ -1351,6 +1351,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to an unsigned long using the standard library's strtoul, +* with additional error handling to ensure robustness. +*/ +static inline int strtoul_ul (char const *s,int base,unsigned long *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.47.1
PATCH v10 2/8 cat-file: add declaration of variable i inside its for loop
In reply to Eric JuSome 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 | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -676,12 +676,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -689,9 +687,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 ;
@@ -717,7 +713,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 };
@@ -727,7 +722,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
@@ -1329,9 +1329,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.47.1
PATCH v10 3/8 t1006: split test utility functions into new "lib-cat-file.sh"
In reply to Eric JuThis 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. 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(-) create mode 100644 t/lib-cat-file.sh
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
@@ -3,6 +3,7 @@ test_description='git cat-file' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh test_cmdmode_usage () { test_expect_code 129 "$@" 2>err &&
@@ -98,18 +99,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 oid=$2
Signature · 1 line
2.47.1
PATCH v10 4/8 fetch-pack: refactor packet writing
In reply to Eric JuFrom: Calvin Wan <[email protected]>
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_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 a2ba162c (object-info: support for retrieving object info, 2021-04-20).
Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future.
To move write_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously,
in fetch_pack.c, advertise_sid was a static variable, modified using git_config_get_bool().
In connect.c, we now initialize advertise_sid at the beginning by directly using git_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():
git_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2.
About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly.
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-)
connect.c +34 -0
@@ -689,6 +689,40 @@ int server_supports(const char *feature)return !!server_feature_value (feature,NULL ); } +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; + +git_config_get_bool ("transfer.advertisesid" , &advertise_sid); + +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" )) +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 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 ) { +die (_ ("the server does not support algorithm '%s'" ), + the_hash_algo->name); + } +packet_buf_delim (req_buf); +} +enum protocol {PROTO_LOCAL =1 ,PROTO_FILE ,
connect.h +8 -0
@@ -30,4 +30,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested +* server capabilities/features into a request buffer. +*/ +struct string_list ; +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options); +#endif
fetch-pack.c +2 -33
@@ -1317,37 +1317,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; - -ensure_server_supports_v2 ("fetch" ); -packet_buf_write (req_buf,"command=fetch" ); -if (server_supports_v2 ("agent" )) -packet_buf_write (req_buf,"agent=%s" ,git_user_agent_sanitized ()); -if (advertise_sid &&server_supports_v2 ("session-id" )) -packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); -if (server_options && server_options->nr) { -ensure_server_supports_v2 ("server-option" ); -for (size_t i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) { -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,
@@ -1358,7 +1327,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" );
@@ -2186,7 +2155,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_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.47.1
PATCH v10 5/8 fetch-pack: move fetch initialization
In reply to Eric JuFrom: 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
@@ -1648,18 +1648,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.47.1
PATCH v10 6/8 serve: advertise object-info feature
In reply to Eric JuFrom: 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
serve.c +3 -1
@@ -70,7 +70,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",
@@ -78,6 +78,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS/* disabled by default */ advertise_object_info =0 ; } +if (value && advertise_object_info) +strbuf_addstr (value,"size" );return advertise_object_info; }
Signature · 1 line
2.47.1
PATCH v10 7/8 transport: add client support for object-info
In reply to Eric JuFrom: 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 | 85 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ transport-helper.c | 11 ++++-- transport.c | 28 ++++++++++++++- transport.h | 11 ++++++ 8 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h
Makefile +1 -0
@@ -1020,6 +1020,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 +85 -0
85 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 "object-store-ll.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
@@ -1654,6 +1654,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"
@@ -42,6 +43,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
transport-helper.c +9 -2
@@ -710,8 +710,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" ));
@@ -727,6 +727,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"
@@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + +if (transport->smart_options && 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
@@ -5,6 +5,7 @@#include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options {unsigned thin :1 ;
@@ -30,6 +31,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;
@@ -53,6 +60,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.47.1
Re: [PATCH v10 7/8] transport: add client support for object-info
transport.c static int fetch_refs_via_pack(struct transport *transport, · line 470
Quoted 7 lines · Eric Ju
transport.c +3
-0@@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + + if (transport->smart_options && transport->smart_options->object_info
Coverity complains about the check for a NULL transport->smart_options here. If it's NULL we'd already have segfaulted a few lines above when we look at the reject_shallow flag.
Not sure if that's an existing bug in the earlier code or not. ;) Your extra check can't hurt anything, in the sense that it's just being overly defensive, but it does make puzzling out the expected value of smart_options harder.
-Peff
Re: [PATCH v10 7/8] transport: add client support for object-info
Quoted 15 lines · Jeff King
transport.c static int fetch_refs_via_pack(struct transport *transport, · line 470
Quoted 7 lines · Eric Ju
transport.c +3
-0@@ -464,8 +465,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_tips; args.reject_shallow_remote = transport->smart_options->reject_shallow; + args.object_info = transport->smart_options->object_info; + + if (transport->smart_options && transport->smart_options->object_infoCoverity complains about the check for a NULL
transport->smart_optionshere. If it's NULL we'd already have segfaulted a few lines above when we look at thereject_shallowflag.
Quoted 6 lines
Not sure if that's an existing bug in the earlier code or not. ;) Your extra check can't hurt anything, in the sense that it's just being overly defensive, but it does make puzzling out the expected value of
smart_optionsharder.-Peff
Thank you Jeff. Sorry for the late response.
I will remove the extra check. transport->smart_options will not be NULL when it reachesargs.reject_shallow_remote = transport->smart_options->reject_shallow;
The call sequence is like this
get_remote_info() in cat-file.c ==> transport_fetch_refs() ==> transport->vtable->fetch_refs ==> fetch_refs_via_pack()
in get_remote_info(), we already have a check for NULL:
if (gtransport->smart_options) {
...
} else {
retval = -1;
}PATCH v10 8/8 cat-file: add remote-object-info to batch-command
In reply to Eric JuSince 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.txt | 24 +- builtin/cat-file.c | 96 ++++ object-file.c | 11 + object-store-ll.h | 3 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ 5 files changed, 794 insertions(+), 4 deletions(-) create mode 100755 t/t1017-cat-file-remote-object-info.sh
Documentation/git-cat-file.txt +20 -4
@@ -149,6 +149,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`
@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`
@@ -298,13 +306,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
@@ -314,7 +323,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
@@ -396,6 +408,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 +96 -0
126 lines not on this page · raw message
object-file.c +11 -0
@@ -3128,3 +3128,14 @@ int read_loose_object(const char *path,munmap (map, mapsize);return ret; } + +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); +free (object_info->type_name); +}
object-store-ll.h +3 -0
@@ -553,4 +553,7 @@ int for_each_object_in_pack(struct packed_git *p,int for_each_packed_object (struct repository *repo,each_packed_object_fn cb,void *data,enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents (struct object_info *object_info); +#endif /* OBJECT_STORE_LL_H */
t/t1017-cat-file-remote-object-info.sh new file +664 -0
664 lines not on this page · raw message
Signature · 1 line
2.47.1
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 743
Quoted 11 lines · Eric Ju
builtin/cat-file.c +11
-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 =xstrdup_or_null (line); + count =split_cmdline (line_to_split, &argv); +if (get_remote_info (opt, count, argv)) + goto cleanup;
Coverity complains that split_cmdline() can return a negative value when the input is malformed, which we then feed to get_remote_info(). If I understand correctly (from my very brief glance at the series), that string would be under the control of the untrusted client?
I _think_ an attacker can't do anything too bad here, since get_remote_info() also takes a signed int, and so iterating from 0 will just find no entries. But probably we should explicitly check for error and bail.
While just looking at this code from a security perspective, two other things occur to me:
1. Calling xstrdup_or_null() implies that "line" may be NULL, which would make "line_to_split" also NULL. But I think split_cmdline() would segfault in that case. Should it just be xstrdup()?
2. Are there any bounds on the size of "line"? E.g., is it coming in as a single pkt, or can it be arbitrarily large if an attacker wants (it looks like maybe the latter, since it comes from a strbuf in batch_objects_command(), but I didn't look at how network data gets passed in to that). At any rate, I think we ran into problems before with split_cmdline() and integer overflow, since it returns an int (CVE-2022-39260). I thought we fixed it by rejecting long lines in git-shell, but it looks like we also hardened split_cmdline() in 0ca6ead81e (alias.c: reject too-long cmdline strings in split_cmdline(), 2022-09-28).
So we are maybe OK, but I wonder if we should punt on absurd lines. Related, can an attacker just flood input into that strbuf, making it grow forever and waste memory? That's just a simple resource attack, but we have tried to avoid those elsewhere in upload-pack, etc.
-Peff
Quoted 20 lines · Jeff King
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 743
Quoted 11 lines · Eric Ju
builtin/cat-file.c +11
-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 =xstrdup_or_null (line); + count =split_cmdline (line_to_split, &argv); +if (get_remote_info (opt, count, argv)) + goto cleanup;Coverity complains that
split_cmdline()can return a negative value when the input is malformed, which we then feed toget_remote_info(). If I understand correctly (from my very brief glance at the series), that string would be under the control of the untrusted client?
Quoted 5 lines
I _think_ an attacker can't do anything too bad here, since
get_remote_info()also takes a signed int, and so iterating from 0 will just find no entries. But probably we should explicitly check for error and bail.
An explicit check is added to make sure if a negative value is returned, we will error and bail.
Quoted 7 lines
While just looking at this code from a security perspective, two other things occur to me:
1. Calling
xstrdup_or_null()implies that "line" may be NULL, which would make "line_to_split" also NULL. But I thinksplit_cmdline()would segfault in that case. Should it just bexstrdup()?
Thank you. Revised to use xstrdup() in v11.
Quoted 11 lines
2. Are there any bounds on the size of "line"? E.g., is it coming in as a single pkt, or can it be arbitrarily large if an attacker wants (it looks like maybe the latter, since it comes from a strbuf in
batch_objects_command(), but I didn't look at how network data gets passed in to that). At any rate, I think we ran into problems before withsplit_cmdline()and integer overflow, since it returns an int (CVE-2022-39260). I thought we fixed it by rejecting long lines in git-shell, but it looks like we also hardenedsplit_cmdline()in0ca6ead81e(alias.c: reject too-long cmdline strings insplit_cmdline(), 2022-09-28).
Quoted 6 lines
So we are maybe OK, but I wonder if we should punt on absurd lines. Related, can an attacker just flood input into that strbuf, making it grow forever and waste memory? That's just a simple resource attack, but we have tried to avoid those elsewhere in upload-pack, etc.
Thank you. Adding a check in v11 for the length of lines. Please let me know if something like this makes sense:
if (strlen(line) >= INT_MAX) {
die(_("remote-object-info command input overflow"));
}Quoted 1 line
-Peff
Quoted 20 lines · Peijian Ju
Thank you. Revised to use
xstrdup()in v11.Quoted 11 lines
2. Are there any bounds on the size of "line"? E.g., is it coming in as a single pkt, or can it be arbitrarily large if an attacker wants (it looks like maybe the latter, since it comes from a strbuf in
batch_objects_command(), but I didn't look at how network data gets passed in to that). At any rate, I think we ran into problems before withsplit_cmdline()and integer overflow, since it returns an int (CVE-2022-39260). I thought we fixed it by rejecting long lines in git-shell, but it looks like we also hardenedsplit_cmdline()in0ca6ead81e(alias.c: reject too-long cmdline strings insplit_cmdline(), 2022-09-28).Quoted 6 lines
So we are maybe OK, but I wonder if we should punt on absurd lines. Related, can an attacker just flood input into that strbuf, making it grow forever and waste memory? That's just a simple resource attack, but we have tried to avoid those elsewhere in upload-pack, etc.
Quoted 6 lines
Thank you. Adding a check in v11 for the length of
lines. Please let me know if something like this makes sense:if (strlen(line) >= INT_MAX) { die(_("remote-object-info command input overflow")); }
I took a look at what you ended up with in v11, and...I think I totally misunderstood what was going on in your series, or when this code would be run.
I had thought the cat-file here was running on the server side, and that we needed to protect ourselves against malicious clients. But your new parse_cmd_remote_object_info() is purely a client-side function that will then access the server behind the scenes. And its input will be coming from the stdin of cat-file locally.
So I'm not sure that we need to protect it unless we think there's some way that an attacker can automatically trigger arbitrary remote-object-info requests.
That said, I'm not sure why you need split_cmdline() at all. The format seems to be:
remote-object-info <url> <oid>...
The only thing that _might_ need quoting is the url, but is shell quoting a reasonable thing there? I'd think that it would be URL-encoded, and thus contain no spaces. The <oid> has to be a real full oid, I think, because the object-info on the server side insists on that.
So why not just split on space? Something like this:
builtin/cat-file.c +17 -21
@@ -597,7 +597,7 @@ 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) +static int get_remote_info (struct batch_options *opt,const char *url,const char *oid_list) {int retval =0 ;struct remote *remote =NULL ;
@@ -613,16 +613,19 @@ static int get_remote_info(struct batch_options *opt, int argc, const char **argif (!opt->format) opt->format ="%(objectname) %(objectsize)" ; - remote =remote_get (argv[0 ]); + remote =remote_get (url);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)) -die (_ ("Not a valid object name %s" ), argv[i]); +while (*oid_list) { +if (parse_oid_hex (oid_list, &oid, &oid_list)) +die (_ ("Not a valid object name %s" ), oid_list);oid_array_append (&object_info_oids, &oid); +while (*oid_list ==' ' ) + oid_list++; } +if (!object_info_oids.nr)die (_ ("remote-object-info requires objects" ));
@@ -747,21 +750,15 @@ 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; - -if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) -die (_ ("remote-object-info command input overflow " -"(no more than %d objects are allowed)" ), -MAX_ALLOWED_OBJ_LIMIT ); + char *url; +const char *space; - line_to_split =xstrdup (line); - count =split_cmdline (line_to_split, &argv); -if (count <0 ) -die (_ ("split remote-object-info command" )); + space =strchr (line,' ' ); +if (!space) +return ;/* report error somehow? */ + url =xmemdupz (line, space - line); -if (get_remote_info (opt, count, argv)) +if (get_remote_info (opt, url, space +1 )) goto cleanup; data->skip_object_info =1 ;
@@ -774,16 +771,15 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,*/ data->size = *remote_object_info[i].sizep; opt->batch_mode =BATCH_MODE_INFO ; -batch_object_write (argv[i+1 ], output, opt, data,NULL ,0 ); +batch_object_write (oid_to_hex (&data->oid), output, opt, data,NULL ,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]); -free (line_to_split); -free (argv); +free (url);free (remote_object_info); }
You'd need to adjust t1017 to remote the quotes from the inputs, and I think you'd have to correctly url-encoded the file:// one to avoid spaces (but that is technically true already! If the filesystem path has a "%" in it, it would be misinterpreted).
-Peff
Quoted 40 lines · Jeff King
Quoted 20 lines · Peijian Ju
Thank you. Revised to use
xstrdup()in v11.Quoted 11 lines
2. Are there any bounds on the size of "line"? E.g., is it coming in as a single pkt, or can it be arbitrarily large if an attacker wants (it looks like maybe the latter, since it comes from a strbuf in
batch_objects_command(), but I didn't look at how network data gets passed in to that). At any rate, I think we ran into problems before withsplit_cmdline()and integer overflow, since it returns an int (CVE-2022-39260). I thought we fixed it by rejecting long lines in git-shell, but it looks like we also hardenedsplit_cmdline()in0ca6ead81e(alias.c: reject too-long cmdline strings insplit_cmdline(), 2022-09-28).Quoted 6 lines
So we are maybe OK, but I wonder if we should punt on absurd lines. Related, can an attacker just flood input into that strbuf, making it grow forever and waste memory? That's just a simple resource attack, but we have tried to avoid those elsewhere in upload-pack, etc.
Quoted 6 lines
Thank you. Adding a check in v11 for the length of
lines. Please let me know if something like this makes sense:if (strlen(line) >= INT_MAX) { die(_("remote-object-info command input overflow")); }I took a look at what you ended up with in v11, and...I think I totally misunderstood what was going on in your series, or when this code would be run.
I had thought the cat-file here was running on the server side, and that we needed to protect ourselves against malicious clients. But your new
parse_cmd_remote_object_info()is purely a client-side function that will then access the server behind the scenes. And its input will be coming from the stdin of cat-file locally.
Quoted 4 lines
So I'm not sure that we need to protect it unless we think there's some way that an attacker can automatically trigger arbitrary remote-object-info requests.
Thank you. Yes, remote-object-info is purely a client-side command.If an attacker is able to automatically
trigger arbitrary remote-object-info requests, it likely means they already have control over that system.
From my understanding, Git generally trusts its clients. So unless there are strong objections,
I will revert those input length checks.
Quoted 11 lines
That said, I'm not sure why you need
split_cmdline()at all. The format seems to be:remote-object-info <url> <oid>...
The only thing that _might_ need quoting is the url, but is shell quoting a reasonable thing there? I'd think that it would be URL-encoded, and thus contain no spaces. The <oid> has to be a real full oid, I think, because the object-info on the server side insists on that.
Thank you! We hadn’t given much thought to the URL format earlier, but I agree that it’s reasonable to require the URL in remote-object-info to be properly URL-encoded.
With that assumption, splitting on spaces makes sense. I’ll update this in the next patch and
also revise the documentation to clarify that URL parameters must be URL-encoded.
Quoted 88 lines
So why not just split on space? Something like this:
builtin/cat-file.c +17
-21@@ -597,7 +597,7 @@ 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) +static int get_remote_info (struct batch_options *opt,const char *url,const char *oid_list) {int retval =0 ;struct remote *remote =NULL ;@@ -613,16 +613,19 @@ static int get_remote_info(struct batch_options *opt, int argc, const char **argif (!opt->format) opt->format ="%(objectname) %(objectsize)" ; - remote =remote_get (argv[0 ]); + remote =remote_get (url);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)) -die (_ ("Not a valid object name %s" ), argv[i]); +while (*oid_list) { +if (parse_oid_hex (oid_list, &oid, &oid_list)) +die (_ ("Not a valid object name %s" ), oid_list);oid_array_append (&object_info_oids, &oid); +while (*oid_list ==' ' ) + oid_list++; } +if (!object_info_oids.nr)die (_ ("remote-object-info requires objects" ));@@ -747,21 +750,15 @@ 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; - -if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) -die (_ ("remote-object-info command input overflow " -"(no more than %d objects are allowed)" ), -MAX_ALLOWED_OBJ_LIMIT ); + char *url; +const char *space; - line_to_split =xstrdup (line); - count =split_cmdline (line_to_split, &argv); -if (count <0 ) -die (_ ("split remote-object-info command" )); + space =strchr (line,' ' ); +if (!space) +return ;/* report error somehow? */ + url =xmemdupz (line, space - line); -if (get_remote_info (opt, count, argv)) +if (get_remote_info (opt, url, space +1 )) goto cleanup; data->skip_object_info =1 ;@@ -774,16 +771,15 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,*/ data->size = *remote_object_info[i].sizep; opt->batch_mode =BATCH_MODE_INFO ; -batch_object_write (argv[i+1 ], output, opt, data,NULL ,0 ); +batch_object_write (oid_to_hex (&data->oid), output, opt, data,NULL ,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]); -free (line_to_split); -free (argv); +free (url);free (remote_object_info); }
Quoted 5 lines
You'd need to adjust t1017 to remote the quotes from the inputs, and I think you'd have to correctly url-encoded the file:// one to avoid spaces (but that is technically true already! If the filesystem path has a "%" in it, it would be misinterpreted).
Thank you. Tests will be adjusted.
Quoted 1 line
-Peff
v11 cover cat-file: add remote-object-info to batch-command
In reply to Eric JuThis patch series is a continuation of Calvin Wan’s ([email protected]) patch series [PATCH v5 0/6] cat-file: add --batch-command remote-object-info command at [1].
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)"[2]. This patch series implement the client option for it.
This patch series add 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 in this implementation. The type (%(objecttype)) is not included in this patch series, as it is not yet supported on the server side either. The plan is to implement the necessary logic for both the server and client in a subsequent series.
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 terminate with an error.
Changes since V10
================
- Add a check on command input to prevent overflow.
- Add other checks to prevent potential abuse.
Calvin Wan (4):
fetch-pack: refactor packet writing
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_ul() with error handling
cat-file: add declaration of variable i inside its for loop
t1006: split test utility functions into new "lib-cat-file.sh"
cat-file: add remote-object-info to batch-command
Documentation/git-cat-file.adoc | 24 +- Makefile | 1 + builtin/cat-file.c | 125 ++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 85 ++++ fetch-object-info.h | 22 + fetch-pack.c | 51 +- fetch-pack.h | 2 + git-compat-util.h | 20 + object-file.c | 11 + object-store-ll.h | 3 + serve.c | 4 +- t/lib-cat-file.sh | 16 + t/t1006-cat-file.sh | 13 +- t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ transport-helper.c | 11 +- transport.c | 28 +- transport.h | 11 + 19 files changed, 1065 insertions(+), 68 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h create mode 100644 t/lib-cat-file.sh create mode 100755 t/t1017-cat-file-remote-object-info.sh
Range-diff against v10:
1: a4a5aefa3e = 1: 814c53b402 git-compat-util: add strtoul_ul() with error handling
2: c67e79804e = 2: 04f41100c4 cat-file: add declaration of variable i inside its for loop
3: 7f0b824714 = 3: 3af67e6648 t1006: split test utility functions into new "lib-cat-file.sh"
4: 0d22d6af6e = 4: cb1088e436 fetch-pack: refactor packet writing
5: 34c34c7464 = 5: 614daac4bb fetch-pack: move fetch initialization
6: 54dd237c45 = 6: 4bc403fa2c serve: advertise object-info feature
7: 90a3d987d5 ! 7: adae08d5a8 transport: add client support for object-info
@@ transport.c: 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 && 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 };
8: 9d932c2cb2 ! 8: 975d39cb6a cat-file: add remote-object-info to batch-command
@@ builtin/cat-file.c
+#include "alias.h"
+#include "remote.h"
+#include "transport.h"
++
++/* Maximum length for a remote URL. While no universal standard exists,
++ * 8K is assumed to be a reasonable limit.
++ */
++#define MAX_REMOTE_URL_LEN (8*1024)
++/* Maximum number of objects allowed in a single remote-object-info request. */
++#define MAX_ALLOWED_OBJ_LIMIT 10000
++/* Maximum input size permitted for the remote-object-info command. */
++#define MAX_REMOTE_OBJ_INFO_LINE (MAX_REMOTE_URL_LEN + MAX_ALLOWED_OBJ_LIMIT * (GIT_MAX_HEXSZ + 1))
enum batch_mode {
BATCH_MODE_CONTENTS,
@@ builtin/cat-file.c: static void parse_cmd_info(struct batch_options *opt,
+{
+ int count;
+ const char **argv;
++ char *line_to_split;
++
++ if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
++ die(_("remote-object-info command input overflow "
++ "(no more than %d objects are allowed)"),
++ MAX_ALLOWED_OBJ_LIMIT);
+
-+ char *line_to_split = xstrdup_or_null(line);
++ line_to_split = xstrdup(line);
+ count = split_cmdline(line_to_split, &argv);
++ if (count < 0)
++ die(_("split remote-object-info command"));
++
+ if (get_remote_info(opt, count, argv))
+ goto cleanup;
+Signature · 1 line
2.48.1
PATCH v11 1/8 git-compat-util: add strtoul_ul() with error handling
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 commit introduces strtoul_ul() to address this gap, enabling the return of an unsigned long with proper error handling.
--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
git-compat-util.h +20 -0
@@ -1353,6 +1353,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to an unsigned long using the standard library's strtoul, +* with additional error handling to ensure robustness. +*/ +static inline int strtoul_ul (char const *s,int base,unsigned long *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.48.1
PATCH v11 2/8 cat-file: add declaration of variable i inside its for loop
In reply to Eric JuSome 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 | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -676,12 +676,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -689,9 +687,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 ;
@@ -717,7 +713,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 };
@@ -727,7 +722,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
@@ -1331,9 +1331,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.48.1
PATCH v11 3/8 t1006: split test utility functions into new "lib-cat-file.sh"
In reply to Eric JuThis 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. 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(-) create mode 100644 t/lib-cat-file.sh
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
@@ -3,6 +3,7 @@ test_description='git cat-file' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-cat-file.sh test_cmdmode_usage () { test_expect_code 129 "$@" 2>err &&
@@ -98,18 +99,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 oid=$2
Signature · 1 line
2.48.1
PATCH v11 4/8 fetch-pack: refactor packet writing
In reply to Eric JuFrom: Calvin Wan <[email protected]>
Refactor write_fetch_command_and_capabilities() to a more general-purpose function, write_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 a2ba162c (object-info: support for retrieving object info, 2021-04-20).
Furthermore, write_command_and_capabilities() is moved to connect.c, making it accessible to additional commands in the future.
To move write_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously,
in fetch_pack.c, advertise_sid was a static variable, modified using git_config_get_bool().
In connect.c, we now initialize advertise_sid at the beginning by directly using git_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():
git_config_get_bool("transfer.advertisesid", &advertise_sid);About 1, since do_fetch_pack() is only relevant for protocol v1, this assignment can be ignored in our refactor, as write_command_and_capabilities() is only used in protocol v2.
About 2, git_config_get_bool() is from config.h and it is an out-of-box dependency of connect.c, so we can reuse it directly.
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 8 ++++++++ fetch-pack.c | 35 ++--------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-)
connect.c +34 -0
@@ -688,6 +688,40 @@ int server_supports(const char *feature)return !!server_feature_value (feature,NULL ); } +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; + +git_config_get_bool ("transfer.advertisesid" , &advertise_sid); + +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" )) +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 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 ) { +die (_ ("the server does not support algorithm '%s'" ), + the_hash_algo->name); + } +packet_buf_delim (req_buf); +} +enum protocol {PROTO_LOCAL =1 ,PROTO_FILE ,
connect.h +8 -0
@@ -30,4 +30,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested +* server capabilities/features into a request buffer. +*/ +struct string_list ; +void write_command_and_capabilities (struct strbuf *req_buf,const char *command, +const struct string_list *server_options); +#endif
fetch-pack.c +2 -33
@@ -1319,37 +1319,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; - -ensure_server_supports_v2 ("fetch" ); -packet_buf_write (req_buf,"command=fetch" ); -if (server_supports_v2 ("agent" )) -packet_buf_write (req_buf,"agent=%s" ,git_user_agent_sanitized ()); -if (advertise_sid &&server_supports_v2 ("session-id" )) -packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); -if (server_options && server_options->nr) { -ensure_server_supports_v2 ("server-option" ); -for (size_t i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) { -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,
@@ -1360,7 +1329,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" );
@@ -2188,7 +2157,7 @@ void negotiate_using_fetch(const struct oid_array *negotiation_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.48.1
PATCH v11 5/8 fetch-pack: move fetch initialization
In reply to Eric JuFrom: 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
@@ -1650,18 +1650,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.48.1
PATCH v11 6/8 serve: advertise object-info feature
In reply to Eric JuFrom: 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 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
serve.c +3 -1
@@ -68,7 +68,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",
@@ -76,6 +76,8 @@ static int object_info_advertise(struct repository *r, struct strbuf *value UNUS/* disabled by default */ advertise_object_info =0 ; } +if (value && advertise_object_info) +strbuf_addstr (value,"size" );return advertise_object_info; }
Signature · 1 line
2.48.1
PATCH v11 7/8 transport: add client support for object-info
In reply to Eric JuFrom: 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 | 85 +++++++++++++++++++++++++++++++++++++++++++++ fetch-object-info.h | 22 ++++++++++++ fetch-pack.c | 3 ++ fetch-pack.h | 2 ++ transport-helper.c | 11 ++++-- transport.c | 28 ++++++++++++++- transport.h | 11 ++++++ 8 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 fetch-object-info.c create mode 100644 fetch-object-info.h
Makefile +1 -0
@@ -1030,6 +1030,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 +85 -0
85 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 "object-store-ll.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
@@ -1656,6 +1656,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"
@@ -42,6 +43,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
transport-helper.c +9 -2
@@ -710,8 +710,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" ));
@@ -727,6 +727,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"
@@ -465,8 +466,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.server_options = transport->server_options; args.negotiation_tips = data->options.negotiation_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
@@ -5,6 +5,7 @@#include "remote.h" #include "list-objects-filter-options.h" #include "string-list.h" +#include "object-store.h" struct git_transport_options {unsigned thin :1 ;
@@ -30,6 +31,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;
@@ -53,6 +60,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.48.1
PATCH v11 8/8 cat-file: add remote-object-info to batch-command
In reply to Eric JuSince 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 | 114 +++++ object-file.c | 11 + object-store-ll.h | 3 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++ 5 files changed, 812 insertions(+), 4 deletions(-) create mode 100755 t/t1017-cat-file-remote-object-info.sh
Documentation/git-cat-file.adoc +20 -4
@@ -149,6 +149,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`
@@ -290,7 +297,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`. `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s`
@@ -298,13 +306,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
@@ -314,7 +323,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
@@ -396,6 +408,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 +114 -0
144 lines not on this page · raw message
object-file.c +11 -0
@@ -3161,3 +3161,14 @@ int read_loose_object(const char *path,munmap (map, mapsize);return ret; } + +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); +free (object_info->type_name); +}
object-store-ll.h +3 -0
@@ -553,4 +553,7 @@ int for_each_object_in_pack(struct packed_git *p,int for_each_packed_object (struct repository *repo,each_packed_object_fn cb,void *data,enum for_each_object_flags flags); +/* Free pointers inside of object_info, but not object_info itself */ +void free_object_info_contents (struct object_info *object_info); +#endif /* OBJECT_STORE_LL_H */
t/t1017-cat-file-remote-object-info.sh new file +664 -0
664 lines not on this page · raw message
Signature · 1 line
2.48.1
Quoted 5 lines · Eric Ju
builtin/cat-file.c +0
-0
builtin/cat-file.c line 36
Quoted 10 lines
builtin/cat-file.c +7
-0@@ -27,6 +27,18 @@#include "promisor-remote.h" #include "mailmap.h" #include "write-or-die.h" +#include "alias.h" +#include "remote.h" +#include "transport.h" + +/* Maximum length for a remote URL. While no universal standard exists, +* 8K is assumed to be a reasonable limit. +*/
Style. Our multi-line comment begins with slash-asterisk and ends with asterisk-slash both on their own line without anything else.
builtin/cat-file.c line 37
Quoted 1 line
builtin/cat-file.c +1
-0+#define MAX_REMOTE_URL_LEN (8*1024)
Here and ...
builtin/cat-file.c line 39
Quoted 2 lines
builtin/cat-file.c +2
-0+/* Maximum number of objects allowed in a single remote-object-info request. */ +#define MAX_ALLOWED_OBJ_LIMIT 10000
... here, please have a blank line.
builtin/cat-file.c line 41
Quoted 2 lines
builtin/cat-file.c +2
-0+/* Maximum input size permitted for the remote-object-info command. */ +#define MAX_REMOTE_OBJ_INFO_LINE (MAX_REMOTE_URL_LEN + MAX_ALLOWED_OBJ_LIMIT * (GIT_MAX_HEXSZ + 1))
This is an overly long line.
Quoted 7 lines
builtin/cat-file.c +3
-0@@ -579,6 +593,61 @@ 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) +{ +int retval =0 ;
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 608
Quoted 10 lines
builtin/cat-file.c +10
-0+struct remote *remote =NULL ; +struct object_id oid; +struct string_list object_info_options =STRING_LIST_INIT_NODUP ; +static struct transport *gtransport; + +/* +* Change the format to "%(objectname) %(objectsize)" when +* remote-object-info command is used. Once we start supporting objecttype +* the default format should change to DEFAULT_FORMAT. +*/
Style. Closing asterisk-slash aligns with the asterisk on the previous line.
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 617
Quoted 9 lines
builtin/cat-file.c +9
-0+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++) {
Pointless mixing of "size_t" and "int". We have declared "int argc", which is perfectly a sensible type, since we know that the value of it would not exceed MAX_ALLOWED_OBJ_LIMIT, which is 10000.
Quoted 17 lines
builtin/cat-file.c +17
-0+if (get_oid_hex (argv[i], &oid)) +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) { +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); +
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 644
Quoted 10 lines
builtin/cat-file.c +10
-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 ); + } + }else { + retval =-1 ; + }
Minor style nit, but when everything else is equal, writing the side of smaller body first would make it easier to follow if/else, i.e.
gtransport = transport_get(remote, NULL);
if (!gtransport->smart_options) {
/* error */
retval = -1;
} else {
... a lot of real code here ...
}builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 753
Quoted 12 lines
builtin/cat-file.c +12
-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; + +if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) +die (_ ("remote-object-info command input overflow " +"(no more than %d objects are allowed)" ), +MAX_ALLOWED_OBJ_LIMIT );
Nobody guarantees this user gave a request for more than 10000 objects; after all it may have been an overly long URL that busted the line length limit, no?
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 758
Quoted 4 lines
builtin/cat-file.c +4
-0+ line_to_split =xstrdup (line); + count =split_cmdline (line_to_split, &argv); +if (count <0 ) +die (_ ("split remote-object-info command" ));
Here, the code could check if count busts MAX_ALLOWED_OBJ_LIMIT, but it doesn't.
Quoted 13 lines · Junio C Hamano
Quoted 10 lines · Eric Ju
builtin/cat-file.c +2
-0@@ -27,6 +27,18 @@#include "promisor-remote.h" #include "mailmap.h" #include "write-or-die.h" +#include "alias.h" +#include "remote.h"
Quoted 9 lines
builtin/cat-file.c line 36
Quoted 5 lines
builtin/cat-file.c +5
-0+#include "transport.h" + +/* Maximum length for a remote URL. While no universal standard exists, +* 8K is assumed to be a reasonable limit. +*/ Style. Our multi-line comment begins with slash-asterisk and ends with asterisk-slash both on their own line without anything else.
Thank you. All the style-related comments will be fixed in the next patch.
Quoted 35 lines
builtin/cat-file.c line 37
Quoted 1 line
builtin/cat-file.c +1
-0+#define MAX_REMOTE_URL_LEN (8*1024)Here and ...
builtin/cat-file.c line 39
Quoted 2 lines
builtin/cat-file.c +2
-0+/* Maximum number of objects allowed in a single remote-object-info request. */ +#define MAX_ALLOWED_OBJ_LIMIT 10000... here, please have a blank line.
builtin/cat-file.c line 41
Quoted 2 lines
builtin/cat-file.c +2
-0+/* Maximum input size permitted for the remote-object-info command. */ +#define MAX_REMOTE_OBJ_INFO_LINE (MAX_REMOTE_URL_LEN + MAX_ALLOWED_OBJ_LIMIT * (GIT_MAX_HEXSZ + 1))This is an overly long line.
Quoted 7 lines
builtin/cat-file.c +3
-0@@ -579,6 +593,61 @@ 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) +{ +int retval =0 ;builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 608
Quoted 10 lines
builtin/cat-file.c +10
-0+struct remote *remote =NULL ; +struct object_id oid; +struct string_list object_info_options =STRING_LIST_INIT_NODUP ; +static struct transport *gtransport; + +/* +* Change the format to "%(objectname) %(objectsize)" when +* remote-object-info command is used. Once we start supporting objecttype +* the default format should change to DEFAULT_FORMAT. +*/ Style. Closing asterisk-slash aligns with the asterisk on the previous line.
Quoted 14 lines
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 617
Quoted 9 lines
builtin/cat-file.c +9
-0+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++) {Pointless mixing of "
size_t" and "int". We have declared "int argc", which is perfectly a sensible type, since we know that the value of it would not exceedMAX_ALLOWED_OBJ_LIMIT, which is 10000.
Thank you. Change to "int" instead.
Quoted 40 lines
Quoted 17 lines
builtin/cat-file.c +17
-0+if (get_oid_hex (argv[i], &oid)) +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) { +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); +builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 644
Quoted 10 lines
builtin/cat-file.c +10
-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 ); + } + }else { + retval =-1 ; + }Minor style nit, but when everything else is equal, writing the side of smaller body first would make it easier to follow if/else, i.e.
gtransport = transport_get(remote, NULL); if (!gtransport->smart_options) { /* error */ retval = -1; } else { ... a lot of real code here ... }
Quoted 16 lines
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 753
Quoted 12 lines
builtin/cat-file.c +12
-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; + +if (strlen (line) >=MAX_REMOTE_OBJ_INFO_LINE ) +die (_ ("remote-object-info command input overflow " +"(no more than %d objects are allowed)" ), +MAX_ALLOWED_OBJ_LIMIT );Nobody guarantees this user gave a request for more than 10000 objects; after all it may have been an overly long URL that busted the line length limit, no?
Yes, the error message is indeed misleading here. In the next patch, the behavior will be updated as follows:
1. If strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE, we will die with a clear error:
"remote-object-info input too long".
2. After step 1, when calling get_remote_info(opt, count, argv), we will check object_info_oids.nr.
If object_info_oids.nr > MAX_ALLOWED_OBJ_LIMIT, we will die with:
"no more than %d objects are allowed", MAX_ALLOWED_OBJ_LIMIT.
Quoted 8 lines
builtin/cat-file.c static void parse_cmd_info(struct batch_options *opt, · line 758
Quoted 5 lines
builtin/cat-file.c +4
-0+ line_to_split =xstrdup (line); + count =split_cmdline (line_to_split, &argv); +if (count <0 ) +die (_ ("split remote-object-info command" ));Here, the code could check if count busts
MAX_ALLOWED_OBJ_LIMIT, but it doesn't.
Yes, a check is needed here. Please see the previous reply.
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 633
Quoted 10 lines · Eric Ju
builtin/cat-file.c +9
-0+static int get_remote_info (struct batch_options *opt,int argc,const char **argv) + if (gtransport->smart_options) { +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);
BTW, this strstr() isn't quite sufficient to prevent problems, as it would not find placeholders which _do_ exist but which aren't handled. One of the first things I tried was:
git cat-file --batch-command='%(objecttype) %(objectsize)'
and feeding it "remote-object-info /path/to/repo some-oid". And it segfaulted.
-Peff
Quoted 18 lines · Jeff King
builtin/cat-file.c static void batch_one_object(const char *obj_name, · line 633
Quoted 10 lines · Eric Ju
builtin/cat-file.c +9
-0+static int get_remote_info (struct batch_options *opt,int argc,const char **argv) + if (gtransport->smart_options) { +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);BTW, this
strstr()isn't quite sufficient to prevent problems, as it would not find placeholders which _do_ exist but which aren't handled. One of the first things I tried was:
Quoted 6 lines
git cat-file --batch-command='%(objecttype) %(objectsize)'and feeding it "remote-object-info /path/to/repo some-oid". And it segfaulted.
-Peff
Thank you, Peff. Yes, you are right. It is a bug. I am adding a new logic in v12:
1. Iterating on the opt->format to see if there are any unsupported placeholders. If there is, error with unspported placeholders.
2. Adding more test cases to cover different formats, e.g., just %(objectsize), just %(objectname), mixed usage of supported and unsupported placeholders.
Quoted 11 lines · Peijian Ju
Quoted 10 lines
BTW, this
strstr()isn't quite sufficient to prevent problems, as it would not find placeholders which _do_ exist but which aren't handled. One of the first things I tried was:git cat-file --batch-command='%(objecttype) %(objectsize)'and feeding it "remote-object-info /path/to/repo some-oid". And it segfaulted.
-Peff
Quoted 7 lines
Thank you, Peff. Yes, you are right. It is a bug. I am adding a new logic in v12:
1. Iterating on theopt->formatto see if there are any unsupported placeholders. If there is, error with unspported placeholders.
2. Adding more test cases to cover different formats, e.g., just%(objectsize), just%(objectname), mixed usage of supported and unsupported placeholders.
Yes, though it would be nice for step 1 to avoid re-parsing the string. I think you could either:
1. After the mark_query pass in batch_objects(), check for unsupported pointers in expand_data. The downside here is that you'd have to match each one that you _don't_ allow (so if somebody adds a new one and forgets to update your list, it wouldn't be caught).
2. In expand_atom() or expand_format(), check an allow-list using is_atom(), when remote-mode is in use. The downside here is that I think we'd eventually want to move that parsing and formatting to the shared ref-filter API. But maybe that API could provide some kind of "check that this atom is allowed" function pointer.
I do wonder if there might be a way to also just notice that we don't have the requested information and handle it gracefully. I didn't reproduce it again just now, but I'd guess the segfault is due to feeding garbage to type_name() in expand_atom().
So maybe if we initialized expand_data fully (so that data->type is always OBJ_BAD or something) and then checked for a NULL return from type_name(), we could do something sensible in expand_atom(), like insert a blank string or similar. And then it is not an error to ask for %(objecttype), but you will just not get useful data for those entries. From the description of the protocol, it sounds like you could actually intermix remote and local object requests?
-Peff
Quoted 30 lines · Jeff King
Quoted 11 lines · Peijian Ju
Quoted 10 lines
BTW, this
strstr()isn't quite sufficient to prevent problems, as it would not find placeholders which _do_ exist but which aren't handled. One of the first things I tried was:git cat-file --batch-command='%(objecttype) %(objectsize)'and feeding it "remote-object-info /path/to/repo some-oid". And it segfaulted.
-Peff
Quoted 7 lines
Thank you, Peff. Yes, you are right. It is a bug. I am adding a new logic in v12:
1. Iterating on theopt->formatto see if there are any unsupported placeholders. If there is, error with unspported placeholders.
2. Adding more test cases to cover different formats, e.g., just%(objectsize), just%(objectname), mixed usage of supported and unsupported placeholders.Yes, though it would be nice for step 1 to avoid re-parsing the string. I think you could either:
1. After the
mark_querypass inbatch_objects(), check for unsupported pointers inexpand_data. The downside here is that you'd have to match each one that you _don't_ allow (so if somebody adds a new one and forgets to update your list, it wouldn't be caught).
Quoted 6 lines
2. In
expand_atom()orexpand_format(), check an allow-list usingis_atom(), when remote-mode is in use. The downside here is that I think we'd eventually want to move that parsing and formatting to the shared ref-filter API. But maybe that API could provide some kind of "check that this atom is allowed" function pointer.
Thank you, Peff. I prefer option 2. Maintaining an allow-list of supported placeholders seems more practical than tracking unsupported ones with a disallow-list. This approach has the added benefit that any newly added placeholders would automatically be treated as unsupported until explicitly added to the allow-list, reducing the chance of oversights.
Quoted 13 lines
I do wonder if there might be a way to also just notice that we don't have the requested information and handle it gracefully. I didn't reproduce it again just now, but I'd guess the segfault is due to feeding garbage to
type_name()inexpand_atom().So maybe if we initialized
expand_datafully (so thatdata->typeis alwaysOBJ_BADor something) and then checked for a NULL return fromtype_name(), we could do something sensible inexpand_atom(), like insert a blank string or similar. And then it is not an error to ask for %(objecttype), but you will just not get useful data for those entries. From the description of the protocol, it sounds like you could actually intermix remote and local object requests?
Quoted 1 line
-Peff
Thank you Peff. I like the idea "... it is not an error to ask for %(objecttype), but you will just not get useful data for those entries."
So if we do remote-object-info with format "%(objectname) %(objectsize) %(objecttype) %(objectsize:disk)", the response can be:
4346b22767c07e31d0f9b524fcb377972d957313 199 ??? ???
Where ??? means the placeholder is not yet supported. In this way we don't have to change the default format, and as new support for the placeholders is added, ??? will be replaced by meaningful data.
About intermixing remote and local object requests, do you mean what happens when remote-object-info is passed oids of objects that are available locally instead of on a remote? If so, I have these scenarios:
1. An object is on remote but not on local. This is what remote-object-info primarily focuses on: we retrieve info from remote without downloading the object.
2. An object is on remote as well as on local. I think remote-object-info should still retrieve info from remote instead of checking local data. After all, if the user knows the object is on local, they can use the info command. If remote-object-info is used, it means we are interested in the information stored on the remote.
3. An object is not on remote, but only on local. I think remote-object-info should fail in this case, since the remote doesn't have the object. The info command should be used in this case.
Quoted 8 lines · Peijian Ju
Thank you Peff. I like the idea "... it is not an error to ask for %(objecttype), but you will just not get useful data for those entries."
So if we do remote-object-info with format "%(objectname) %(objectsize) %(objecttype) %(objectsize:disk)", the response can be:
Quoted 6 lines
4346b22767c07e31d0f9b524fcb377972d957313199 ??? ???Where ??? means the placeholder is not yet supported. In this way we don't have to change the default format, and as new support for the placeholders is added, ??? will be replaced by meaningful data.
Yes, something like that. I don't know what the placeholder should be. In similar situations for the ref-filter printer, I think we use the empty string for unsupported cases. E.g.:
git for-each-ref --format='%(refname) %(tagger)'
will show the empty string for %(tagger) of non-tags. That lets you use conditionals like %(if) to switch behavior. The cat-file formatter doesn't use ref-filter now, but I think in the long run we'd want to unify them. So it probably makes sense to match its behavior.
Quoted 4 lines
About intermixing remote and local object requests, do you mean what happens when remote-object-info is passed oids of objects that are available locally instead of on a remote? If so, I have these scenarios:
No, I meant that --batch-command takes a single format string, but you can issue both local and remote requests to it. So for example:
git cat-file --batch-command='%(objectname) %(objecttype) %(objectsize)' <<\EOF info 683c54c999c301c2cd6f715c411407c413b1d84e remote-object-info c9d3534de317f31915f37e9d9c0d52d4cf901482 EOF
would show the local info for the first object, and remote info for the other. If you're only issuing remote-object-info commands, obviously it's dumb to include %(objecttype) which cannot be filled. But in the example above, it is possibly useful to get more data on the local objects, and a reduced set of data for the remote ones.
Quoted 11 lines
1. An object is on remote but not on local. This is what
remote-object-infoprimarily focuses on: we retrieve info from remote without downloading the object.
2. An object is on remote as well as on local. I thinkremote-object-infoshould still retrieve info from remote instead of checking local data. After all, if the user knows the object is on local, they can use theinfocommand. If remote-object-info is used, it means we are interested in the information stored on the remote.
3. An object is not on remote, but only on local. I think remote-object-info should fail in this case, since the remote doesn't have the object. The info command should be used in this case.
Yeah, agreed. remote-object-info should always predictably ask the remote.
-Peff
From: Pablo Sabater Jiménez <[email protected]>
Quoted 6 lines · Jeff King
In similar situations for the ref-filter printer, I think we use the empty string for unsupported cases. E.g.:
git for-each-ref --format='%(refname) %(tagger)'will show the empty string for %(tagger) of non-tags.
while playing with Eric's v11 for my proposal I tested some of the mentioned cases.
I tested this, and for-each-ref dies on unknown atoms:
$ git for-each-ref --format='%(test)' fatal: unknown field name: test
So the empty string behavior is just for known atoms but not applicable to a given ref type, like %(tagger) on a non-tag. For remote-object-info, the atoms are known to expand_atom() but the remote can't provide data for them, returning an empty string would be the closest match.
Quoted 7 lines
No, I meant that --batch-command takes a single format string, but you can issue both local and remote requests to it. So for example:
git cat-file --batch-command='%(objectname) %(objecttype) %(objectsize)' <<\EOF info 683c54c999c301c2cd6f715c411407c413b1d84e remote-object-info c9d3534de317f31915f37e9d9c0d52d4cf901482 EOF
While testing Eric's v11 on this case, I found a bug beyond the segfault:
when a local info query runs before remote-object-info in the same session, data->type retains stale data from the local query, and remote-object-info silently returns the wrong type.
To reproduce, query a commit locally then a blob remotely:
git cat-file --batch-command='%(objectname) %(objectsize) %(objecttype)' <<-EOF info <commit-oid> remote-object-info origin <blob-oid> EOF
example for my test case:
git cat-file --batch-command='%(objectname) %(objectsize) %(objecttype)' <<EOF
info fede5cd8e8da0ee5d984753286c77f80339ec832
remote-object-info file:///home/blopa/tests/CPU16-remote.git 9a455a4325cc0b0ba839f2414395257511ab6213 EOF
fede5cd8e8da0ee5d984753286c77f80339ec832 237 commit 9a455a4325cc0b0ba839f2414395257511ab6213 256 commit
git cat-file -t 9a455a4325cc0b0ba839f2414395257511ab6213 blob
The blob is being reported as "commit" because data->type isnt being cleared between commands. The size is correct (provided by the
server) but the type is wrong.
This is even worse because user wouldn't even receive any signal that there is an error.
To reproduce it, this is what I did:
Server repo needs transfer.advertiseobjectinfo true, and both client and server must run Eric's v11, I used file://.
In the meantime I'll keep testing
Eric's v11 and report any issues I find.
Pablo
v12 cover cat-file: add remote-object-info to batch-command
In reply to Eric JuThis path 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].
I have a question for the design:
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].
[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 v11:
- Rebased onto current master.
- Added fetch-object-info.c to meson.build.
- Added t1017-cat-file-remote-object-info.sh to t/meson.build.
- Fixed style feedback from v10.
- Replaced strstr() validation with a dynamic allow-list in expand_atom() based on server-advertised capabilities, filtering unsupported options in fetch_object_info() before sending.
- Unsupported placeholders return empty strings instead of die().
- Initialize data->type to OBJ_BAD with NULL check on type_name().
- Moved global variables to local scope in parse_cmd_remote_object_info.
- Decoupled batch_object_write() from sizep so output is always produced.
- Added remote_atom_map[] for protocol-to-atom name mapping.
- Included "type" in the client request path for forward-compatibility.
- Fixed memory leak of data->name in transport-helper disconnect.
- Changed error message "not our ref" to "server does not recognize object".
- Split fetch-pack refactoring into two commits (move + refactor).
- Updated caveats documentation and tests.
---
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_ul() with error handling
cat-file: add declaration of variable i inside its for loop
t1006: split test utility functions into new "lib-cat-file.sh"
cat-file: add remote-object-info to batch-command
Pablo Sabater (5):
transport-helper: fix memory leak of helper on disconnect
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 | 191 ++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 93 +++++ 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 | 684 +++++++++++++++++++++++++++++++++ transport-helper.c | 13 +- transport.c | 28 +- transport.h | 11 + 21 files changed, 1158 insertions(+), 74 deletions(-)
---
base-commit: 9ac3f193c05c2237e2b14ebaa1149e9fc8a1abe0
change-id: 20260608-ps-eric-work-rebase-b73ae84ba671
Best regards,
Signature · 1 line
Pablo Sabater <[email protected]>
RFC PATCH v12 1/12 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
RFC PATCH v12 2/12 git-compat-util: add strtoul_ul() 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.
Add strtoul_ul() to address this gap, enabling the return of an unsigned long 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 an unsigned long using the standard library's strtoul, +* with additional error handling to ensure robustness. +*/ +static inline int strtoul_ul (char const *s,int base,unsigned long *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 RFC v12 02/12] git-compat-util: add strtoul_ul() with error handling
Quoted 10 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.This variant is needed in a subsequent commit.
Add
strtoul_ul()to address this gap, enabling the
Nit: extra space here. Also, this could be conciser. Maybe something like:
"This variant is needed in a subsequent commit to enable returning an unsigned long with proper error handling."
Quoted 41 lines
return of an unsigned long with proper error handling.
Signed-off-by Pablo Sabater <[email protected]>--- git-compat-util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)git-compat-util.h +20
-1@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)return 0 ; } +/* +* Convert a string to an unsigned long using the standard library's strtoul, +* with additional error handling to ensure robustness. +*/ +static inline int strtoul_ul (char const *s,int base,unsigned long *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; --2.54.0
RFC PATCH v12 3/12 cat-file: add declaration of variable i inside its for loop
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 | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)
builtin/cat-file.c +3 -8
@@ -726,12 +726,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int 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);
@@ -739,9 +737,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 ;
@@ -768,7 +764,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 };
@@ -778,7 +773,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
Re: [PATCH GSoC RFC v12 03/12] cat-file: add declaration of variable i inside its for loop
Quoted 2 lines · Pablo Sabater
From: Eric Ju <[email protected]>
Subject: Re: [PATCH GSoC RFC v12 03/12] cat-file: add declaration of variable i inside its for loop
"add" sounds a bit strange, as the existing code wouldn't have compiled if the variable were never declared. What the patch did was to move (not add) the declaration of a function scope variable that is used to control for() loops. Would any of these work?
Subject: [PATCH GSOC v12 03/12] cat-file: narrow scope of loop counter
Subject: [PATCH GSOC v12 03/12] cat-file: declare loop counter inside for()
Quoted 5 lines
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.
Curious single line that is overly long?
Quoted 14 lines
Helped-by Christian Couder <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)builtin/cat-file.c +0
-0@@ -726,12 +726,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd,
builtin/cat-file.c static void dispatch_calls(struct batch_options *opt, · line 733
Quoted 10 lines
builtin/cat-file.c +1
-3int 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);
The loop limit "nr" will not become as large as size_t because the caller passes a platform natural "int" to the function. Wouldn't a stupid compiler give us warning on comparing unsigned size_t with signed int here?
builtin/cat-file.c static void dispatch_calls(struct batch_options *opt, · line 741
Quoted 9 lines
builtin/cat-file.c +1
-3@@ -739,9 +737,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);
No type change, so the result is as safe as the original.
Quoted 9 lines
builtin/cat-file.c +0
-1@@ -768,7 +764,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 };
builtin/cat-file.c static void batch_objects_command(struct batch_options *opt, · line 778
Quoted 7 lines
builtin/cat-file.c +1
-1@@ -778,7 +773,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 ;
ARRAY_SIZE() is some arithmetic over sizeof(*commands) and sizeof(commands), which is of type size_t, so this is better than the original. Use of size_t i of course is a natural way to index into commands[] array, so the result is just fine.
Quoted 5 lines
fetch-pack.c +0
-0
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1394
Quoted 9 lines
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);
server_options is a string_list whose .nr member is of type size_t, so this comparison is perfectly fine. Ditto for ->items[i].string that is a natural way to index into an array.
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1395
Quoted 1 line
fetch-pack.c +0
-0}
v
Re: [PATCH GSoC RFC v12 03/12] cat-file: add declaration of variable i inside its for loop
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Pablo Sabater
From: Eric Ju <[email protected]>
Subject: Re: [PATCH GSoC RFC v12 03/12] cat-file: add declaration of variable i inside its for loop
Quoted 8 lines
"add" sounds a bit strange, as the existing code wouldn't have compiled if the variable were never declared. What the patch did was to move (not add) the declaration of a function scope variable that is used to control
for()loops. Would any of these work?Subject: [PATCH GSOC v12 03/12] cat-file: narrow scope of loop counter
Subject: [PATCH GSOC v12 03/12] cat-file: declare loop counter insidefor()
Hi!
True, it's better to write "move" and about the title, works as well, I'll change them for the next version.
Quoted 7 lines
Quoted 5 lines
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.Curious single line that is overly long?
True, I'll wrap it up correctly in the next version.
Quoted 22 lines
Quoted 22 lines
Helped-by Christian Couder <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- builtin/cat-file.c | 11 +++-------- fetch-pack.c | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-)builtin/cat-file.c +0
-2@@ -726,12 +726,10 @@ static void dispatch_calls(struct batch_options *opt, struct queued_cmd *cmd, int nr) { - int i; -if (!opt->buffer_output)die (_ ("flush is only for --buffer mode" ));
Quoted 8 lines
builtin/cat-file.c static void dispatch_calls(struct batch_options *opt, · line 733
Quoted 3 lines
builtin/cat-file.c +1
-1-for (i =0 ; i < nr; i++) +for (size_t i =0 ; i < nr; i++) cmd[i].fn (opt, cmd[i].line, output, data);The loop limit "nr" will not become as large as
size_tbecause the caller passes a platform natural "int" to the function. Wouldn't a stupid compiler give us warning on comparing unsignedsize_twith signed int here?
Yes, I'll change "i" to be int.
Quoted 54 lines
builtin/cat-file.c static void dispatch_calls(struct batch_options *opt, · line 741
Quoted 10 lines
builtin/cat-file.c +1
-3@@ -739,9 +737,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);No type change, so the result is as safe as the original.
Quoted 9 lines
builtin/cat-file.c +0
-1@@ -768,7 +764,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 };builtin/cat-file.c static void batch_objects_command(struct batch_options *opt, · line 778
Quoted 7 lines
builtin/cat-file.c +1
-1@@ -778,7 +773,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 ;
ARRAY_SIZE()is some arithmetic oversizeof(*commands)and sizeof(commands), which is of typesize_t, so this is better than the original. Use ofsize_ti of course is a natural way to index into commands[] array, so the result is just fine.Quoted 5 lines
fetch-pack.c +0
-0fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1394
Quoted 9 lines
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);
server_optionsis astring_listwhose .nr member is of typesize_t, so this comparison is perfectly fine. Ditto for ->items[i].string that is a natural way to index into an array.
Quoted 3 lines
fetch-pack.c static void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 1395
Quoted 1 line
fetch-pack.c +0
-0}v
Thanks,
Pablo.
RFC PATCH v12 4/12 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. 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
Re: [PATCH GSoC RFC v12 04/12] t1006: split test utility functions into new "lib-cat-file.sh"
Quoted 8 lines · Pablo Sabater
From: 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. The goal is to improve code reuse and readability, enabling future tests to leverage these utilities without duplicating code.
Hmm, seems like a premature change to me. Do any of the subsequent commits require this refactor? Maybe the follow-up series that enables %objecttype support needs it? Did someone request this change in v11's feedback?
If any of those are true, I think it's worthwhile mentioning it here. That will make it easier to determine whether this change is truly necessary.
Quoted 62 lines
Signed-off-by Pablo Sabater <[email protected]>--- 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
-13@@ -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" --2.54.0
Re: [PATCH GSoC RFC v12 04/12] t1006: split test utility functions into new "lib-cat-file.sh"
Quoted 11 lines · Chandra Pratap
Quoted 8 lines · Pablo Sabater
From: 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. The goal is to improve code reuse and readability, enabling future tests to leverage these utilities without duplicating code.
Quoted 7 lines
Hmm, seems like a premature change to me. Do any of the subsequent commits require this refactor? Maybe the follow-up series that enables %objecttype support needs it? Did someone request this change in v11's feedback?
If any of those are true, I think it's worthwhile mentioning it here. That will make it easier to determine whether this change is truly necessary.
Yes, these functions are needed for "t1017" which is created later in the series [1] for the remote object info tests, so they are both used in "t1006" (where they were originally) and "t1017".
Quoted 61 lines
Quoted 61 lines
Signed-off-by Pablo Sabater <[email protected]>--- 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"
Quoted 2 lines
Quoted 2 lines
--
2.54.0
[1]: https://lore.kernel.org/git/[email protected]/
Thanks,
Pablo.
RFC PATCH v12 5/12 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.
To move write_fetch_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously in fetch_pack.c, advertise_sid was a static variable, modified using repo_config_get_bool().
In connect.c, we now 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 (!sever_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.
Move write_fetch_command_and_capabilities() to connect.c
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 4 ++++ fetch-pack.c | 31 ------------------------------- 3 files changed, 38 insertions(+), 31 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 -31
@@ -1376,37 +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; - -ensure_server_supports_v2 ("fetch" ); -packet_buf_write (req_buf,"command=fetch" ); -if (server_supports_v2 ("agent" )) -packet_buf_write (req_buf,"agent=%s" ,git_user_agent_sanitized ()); -if (advertise_sid &&server_supports_v2 ("session-id" )) -packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); -if (server_options && server_options->nr) { -ensure_server_supports_v2 ("server-option" ); -for (size_t i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1_LEGACY ) { -die (_ ("the server does not support algorithm '%s'" ), - the_hash_algo->name); - } -packet_buf_delim (req_buf); -} - static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, struct fetch_pack_args *args, const struct ref *wants, struct oidset *common,
Signature · 1 line
2.54.0
RFC PATCH v12 6/12 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 | 10 +++++----- connect.h | 8 ++++++-- fetch-pack.c | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-)
connect.c +5 -5
@@ -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" ))
@@ -727,7 +727,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,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 ) { + }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) {die (_ ("the server does not support algorithm '%s'" ), the_hash_algo->name); }
connect.h +6 -2
@@ -34,8 +34,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ struct string_list ; -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);#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
RFC PATCH v12 7/12 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
RFC PATCH v12 8/12 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
RFC PATCH v12 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 | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 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, 163 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 +87 -0
87 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 RFC v12 09/12] transport: add client support for object-info
Pablo Sabater <[email protected]> writes:
[jc: removed recipients from Cc: list whose addresses bounce]
Quoted 13 lines
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:
...fetch-object-info.c +4
-0+int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +struct packet_reader *reader,struct object_info *object_info_data, +const int stateless_rpc,const int fd_out) +{
fetch-object-info.c
Quoted 10 lines
fetch-object-info.c +10
-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,"" )) +die ("object-info: server does not recognize object %s" , + object_info_values.items[0 ].string); + +if (strtoul_ul (object_info_values.items[1 + size_index].string,10 , object_info_data[i].sizep))
Overly long lines need to be fixed, by using a shorter and crisper variable name in such a short scope, and line wrapping if needed.
More importantly, on this line (wrapped):
if (strtoul_ul(object_info_values.items[1 + size_index].string, 10, object_info_data[i].sizep))
we notice object_info_data[i] is of type "struct object_info", which is
struct object_info {
/* Request */
enum object_type *typep;
size_t *sizep;
off_t *disk_sizep;
...but the last parameter strtoul_ul() takes is unsurprisingly a pointer to "unsigned long", not a pointer to "size_t".
Which will break on 32-bit boxes where size_t is "unsigned int" that is 32-bit and different from "unsigned long".
Perhaps something along this line?
fetch-object-info.c +4 -1
@@ -75,14 +75,17 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arstring_list_split (&object_info_values, reader->line," " ,-1 );if (0 <= size_index) { +unsigned long sz;if (!strcmp (object_info_values.items[1 + size_index].string,"" ))die ("object-info: server does not recognize object %s" , object_info_values.items[0 ].string); -if (strtoul_ul (object_info_values.items[1 + size_index].string,10 , object_info_data[i].sizep)) +if (strtoul_ul (object_info_values.items[1 + size_index].string, +10 , &sz))die ("object-info: ref %s has invalid size %s" , object_info_values.items[0 ].string, object_info_values.items[1 + size_index].string); + *object_info_data[i].sizep = sz; }string_list_clear (&object_info_values,0 );
Re: [PATCH GSoC RFC v12 09/12] transport: add client support for object-info
Quoted 47 lines · Junio C Hamano
Pablo Sabater <[email protected]> writes:
[jc: removed recipients from Cc: list whose addresses bounce]
Quoted 13 lines
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:
...fetch-object-info.c +4
-0+int fetch_object_info (const enum protocol_version version,struct object_info_args *args, +struct packet_reader *reader,struct object_info *object_info_data, +const int stateless_rpc,const int fd_out) +{fetch-object-info.c
Quoted 10 lines
fetch-object-info.c +10
-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,"" )) +die ("object-info: server does not recognize object %s" , + object_info_values.items[0 ].string); + +if (strtoul_ul (object_info_values.items[1 + size_index].string,10 , object_info_data[i].sizep))Overly long lines need to be fixed, by using a shorter and crisper variable name in such a short scope, and line wrapping if needed.
More importantly, on this line (wrapped):
if (strtoul_ul(object_info_values.items[1 + size_index].string, 10, object_info_data[i].sizep))we notice object_info_data[i] is of type "struct
object_info", which isstruct object_info { /* Request */ enum object_type *typep; size_t *sizep; off_t *disk_sizep; ...
Quoted 7 lines
but the last parameter
strtoul_ul()takes is unsurprisingly a pointer to "unsigned long", not a pointer to "size_t".Which will break on 32-bit boxes where
size_tis "unsigned int" that is 32-bit and different from "unsigned long".Perhaps something along this line?
Not quite. This "size_t *sizep" has been very recently introduced by Dscho in a topic that is in-flight.
The ps/cat-file-remote-object-info topic alone does not have this type-mismatch problem. Below needs to be addressed as an evil merge at the integration side, so you have nothing to do. I'll have to tweak the merges.
Sorry for a false alarm.
Quoted 23 lines
fetch-object-info.c +4
-1@@ -75,14 +75,17 @@ int fetch_object_info(const enum protocol_version version, struct object_info_arstring_list_split (&object_info_values, reader->line," " ,-1 );if (0 <= size_index) { +unsigned long sz;if (!strcmp (object_info_values.items[1 + size_index].string,"" ))die ("object-info: server does not recognize object %s" , object_info_values.items[0 ].string); -if (strtoul_ul (object_info_values.items[1 + size_index].string,10 , object_info_data[i].sizep)) +if (strtoul_ul (object_info_values.items[1 + size_index].string, +10 , &sz))die ("object-info: ref %s has invalid size %s" , object_info_values.items[0 ].string, object_info_values.items[1 + size_index].string); + *object_info_data[i].sizep = sz; }string_list_clear (&object_info_values,0 );
RFC PATCH v12 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 | 128 ++++++- object-file.c | 10 + odb.h | 3 + t/meson.build | 1 + t/t1017-cat-file-remote-object-info.sh | 664 +++++++++++++++++++++++++++++++++ transport.c | 4 +- 7 files changed, 827 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 +127 -1
153 lines not on this page · raw message
object-file.c +10 -0
@@ -2356,3 +2356,13 @@ int odb_source_loose_read_object_stream(struct odb_read_stream **out,free (st);return -1 ; } + +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 +664 -0
664 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
RFC PATCH v12 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 | 28 +++++++++++++++++++---- 3 files changed, 60 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 +24 -4
@@ -205,6 +205,22 @@ info $tag_oid ) ' +# 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' '
@@ -544,10 +560,12 @@ test_expect_success 'remote-object-info fails on unspported filter option (objec 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 ) '
@@ -556,10 +574,12 @@ test_expect_success 'remote-object-info fails on unspported filter option (delta 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 v12 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 | 85 ++++++++++++++++++++++++++++++++--------------------- fetch-object-info.c | 6 ++++ 2 files changed, 58 insertions(+), 33 deletions(-)
builtin/cat-file.c +52 -33
@@ -341,13 +341,10 @@ 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 +356,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 ; }
@@ -686,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;/*
@@ -726,15 +717,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; }
@@ -820,6 +808,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,load_mailmap (); } +struct protocol_placeholder_entry { +const char *option; +const char *atom; +}; + +static const struct protocol_placeholder_entry remote_atom_map[] = { + {"size" ,"objectsize" }, + {"type" ,"objecttype" }, +/* +* Add new protocol options here. Even if the server doesn't support +* them the allow_list will drop them if the server doesn't advertise +* them. +*/ +}; +static void parse_cmd_remote_object_info (struct batch_options *opt,const char *line,struct strbuf *output,struct expand_data *data)
@@ -829,6 +832,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" ));
@@ -841,30 +845,44 @@ 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++) { 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. -*/ +/* +* When reaching here, it means remote-object-info can retrieve +* information from server without downloading them. +*/ +if (remote_object_info[i].sizep) 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 ; - } +if (remote_object_info[i].typep) + data->type = *remote_object_info[i].typep; + opt->batch_mode =BATCH_MODE_INFO ; + data->is_remote =1 ; +batch_object_write (argv[i +1 ], output, opt, data,NULL ,0 ); + data->is_remote =0 ; } data->skip_object_info =0 ; 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);
@@ -1177,6 +1195,7 @@ static int batch_objects(struct batch_options *opt) cleanup:strbuf_release (&input);strbuf_release (&output); +string_list_clear (&data.remote_allowed_atoms,0 ); warn_on_object_refname_ambiguity = save_warning;return retval; }
fetch-object-info.c +6 -0
@@ -39,6 +39,12 @@ 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" )); + +for (int i = 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 ); +send_object_info_request (fd_out, args);break ;case protocol_v1:
Signature · 1 line
2.54.0
Re: [PATCH GSoC RFC v12 12/12] cat-file: make remote-object-info allow-list dynamic
Quoted 216 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.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_atomsis populated with the placeholders.4.
expand_atom()checksremote_allowed_atomswith the same behaviour as the staticallow_listhad.Move
object_info_optionsout ofget_remote_infoso the caller which has data can select what options will be requested instead of requesting always size.
Movebatch_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_optionsso once the server supports it, the clients know already how to request it.Mentored-by Karthik Nayak <[email protected]>Mentored-by Chandra Pratap <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- builtin/cat-file.c | 85 ++++++++++++++++++++++++++++++++--------------------- fetch-object-info.c | 6 ++++ 2 files changed, 58 insertions(+), 33 deletions(-)builtin/cat-file.c +52
-33@@ -341,13 +341,10 @@ 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 +356,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 ; }@@ -686,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;/* @@ -726,15 +717,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; }@@ -820,6 +808,21 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,load_mailmap (); } +struct protocol_placeholder_entry { +const char *option; +const char *atom; +}; + +static const struct protocol_placeholder_entry remote_atom_map[] = { + {"size" ,"objectsize" }, + {"type" ,"objecttype" }, +/* +* Add new protocol options here. Even if the server doesn't support +* them the allow_list will drop them if the server doesn't advertise +* them. +*/ +}; +static void parse_cmd_remote_object_info (struct batch_options *opt,const char *line,struct strbuf *output,struct expand_data *data)@@ -829,6 +832,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" ));@@ -841,30 +845,44 @@ 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++) { 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. -*/ +/* +* When reaching here, it means remote-object-info can retrieve +* information from server without downloading them. +*/ +if (remote_object_info[i].sizep) 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 ; - } +if (remote_object_info[i].typep) + data->type = *remote_object_info[i].typep; + opt->batch_mode =BATCH_MODE_INFO ; + data->is_remote =1 ; +batch_object_write (argv[i +1 ], output, opt, data,NULL ,0 ); + data->is_remote =0 ; } data->skip_object_info =0 ; 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);@@ -1177,6 +1195,7 @@ static int batch_objects(struct batch_options *opt) cleanup:strbuf_release (&input);strbuf_release (&output); +string_list_clear (&data.remote_allowed_atoms,0 ); warn_on_object_refname_ambiguity = save_warning;return retval; }fetch-object-info.c +0
-0
fetch-object-info.c int fetch_object_info(const enum protocol_version version, struct object_info_ar · line 43
Quoted 5 lines
fetch-object-info.c +2
-0@@ -39,6 +39,12 @@ 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" )); + +for (int i = args->object_info_options->nr -1 ; i >=0 ; i--)
Isn't args->object_info_options->nr of type size_t? We should probably do something
like:
for (size_t i = 0; i < args->args->object_info_options->nr; i++)
instead.
Quoted 10 lines
fetch-object-info.c +4
-1+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 ); +send_object_info_request (fd_out, args);break ;case protocol_v1: --2.54.0
Other than these, the patch series LGTM for now.
Thanks,
Chandra.
Re: [PATCH GSoC RFC v12 12/12] cat-file: make remote-object-info allow-list dynamic
Quoted 44 lines · Chandra Pratap
Quoted 42 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.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_atomsis populated with the placeholders.4.
expand_atom()checksremote_allowed_atomswith the same behaviour as the staticallow_listhad.Move
object_info_optionsout ofget_remote_infoso the caller which has data can select what options will be requested instead of requesting always size.
Movebatch_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_optionsso once the server supports it, the clients know already how to request it.Mentored-by Karthik Nayak <[email protected]>Mentored-by Chandra Pratap <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- builtin/cat-file.c | 85 ++++++++++++++++++++++++++++++++--------------------- fetch-object-info.c | 6 ++++ 2 files changed, 58 insertions(+), 33 deletions(-)
[snip]
Quoted 15 lines
fetch-object-info.c int fetch_object_info(const enum protocol_version version, struct object_info_ar · line 43
Quoted 10 lines
fetch-object-info.c +2
-0@@ -39,6 +39,12 @@ 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" )); + +for (int i = args->object_info_options->nr -1 ; i >=0 ; i--)Isn't
args->object_info_options->nrof typesize_t? We should probably do something
like:
Quoted 3 lines
for (
size_ti = 0; i <args->args->object_info_options->nr; i++)instead.
Hi!
void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util)
{
if (list->strdup_strings)
free(list->items[i].string);
if (free_util)
free(list->items[i].util);
list->items[i] = list->items[list->nr-1];
list->nr--;
}I made it backwards because of "list->items[i] = list->items[list->nr
- 1];" If we made it from 0..nr and we delete the first element, for the next iteration, the last element is at [0] but we are on [1] and that swapped element never gets evaluated.
About size_t, yes, it is size_t but because we go backwards 0 - 1 would fail, also unsorted_string_list_delete_item() signature has "int i". The options that can be on that list will be a small number so there should be no problem, should I cast it explicitly?
Quoted 9 lines
Quoted 9 lines
fetch-object-info.c +4
-0+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 ); +send_object_info_request (fd_out, args);break ;case protocol_v1:
Quoted 7 lines
Quoted 2 lines
--
2.54.0Other than these, the patch series LGTM for now.
Thanks,
Chandra.
Thanks,
Pablo.
Re: [PATCH GSoC RFC v12 12/12] cat-file: make remote-object-info allow-list dynamic
Quoted 34 lines · Pablo Sabater
[snip]
Quoted 15 lines
fetch-object-info.c int fetch_object_info(const enum protocol_version version, struct object_info_ar · line 43
Quoted 10 lines
fetch-object-info.c +2
-0@@ -39,6 +39,12 @@ 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" )); + +for (int i = args->object_info_options->nr -1 ; i >=0 ; i--)Isn't
args->object_info_options->nrof typesize_t? We should probably do something
like:Quoted 3 lines
for (
size_ti = 0; i <args->args->object_info_options->nr; i++)instead.
Hi!
void
unsorted_string_list_delete_item(struct string_list *list, int i, int free_util){ if (list->strdup_strings) free(list->items[i].string); if (free_util) free(list->items[i].util); list->items[i] = list->items[list->nr-1]; list->nr--; }
Quoted 4 lines
I made it backwards because of "list->items[i] = list->items[list->nr
- 1];" If we made it from 0..nr and we delete the first element, for the next iteration, the last element is at [0] but we are on [1] and that swapped element never gets evaluated.
Makes sense now.
Quoted 4 lines
About
size_t, yes, it issize_tbut because we go backwards 0 - 1 would fail, alsounsorted_string_list_delete_item()signature has "int i". The options that can be on that list will be a small number so there should be no problem, should I cast it explicitly?
Yes, I think explicit casting with a short comment explaining why it is fine to do so will be much better.
Thanks,
Chandra.
Re: [PATCH GSoC RFC v12 12/12] cat-file: make remote-object-info allow-list dynamic
In reply to Chandra PratapQuoted 6 lines · Chandra Pratap
Quoted 5 lines · Pablo Sabater
The static allow-list in
expand_atom()is hardcoded to only allow "objectname" and "objectsize" for remote queries. This works because
...
}
You just forced readers to skip over 200+ lines of quoted material, looking for something interesting you have said in response to comment on the patch in vain.
Quoted 15 lines
fetch-object-info.c int fetch_object_info(const enum protocol_version version, struct object_info_ar · line 43
Quoted 10 lines
fetch-object-info.c +2
-0@@ -39,6 +39,12 @@ 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" )); + +for (int i = args->object_info_options->nr -1 ; i >=0 ; i--)Isn't
args->object_info_options->nrof typesize_t? We should probably do something
like:
Quoted 3 lines
for (
size_ti = 0; i <args->args->object_info_options->nr; i++)instead.
This is a valid observation and a careful reading like this is very much appreciated. It is unfortunate that it was buried by 200+ lines of irrelevant material before we find it.
Thanks.
Quoted 15 lines
Quoted 10 lines
fetch-object-info.c +4
-1+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 ); +send_object_info_request (fd_out, args);break ;case protocol_v1: --2.54.0
Other than these, the patch series LGTM for now.
Thanks,
Chandra.
v13 cover cat-file: add remote-object-info to batch-command
In reply to Pablo SabaterThis path 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].
I have a question for the design:
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?
2. _tangent/not related with this series_
'a2ba162cda' is designed to only work with full OIDs, which is inconsistent with local info that 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.
Alternatively, would sending a list of possible OIDs to the client so it can re-request with the correct one be ok?
[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 v12:
- Remote-object-info no longer dies when the server doesn't recognize the object, printing "<oid> missing" like info does.
- On 12th commit explicitly cast to int and add a comment explaining why the backward iteration of the list.
- Renamed 3rd commit and in the commit, change the signature of dispatch_calls() as it is only called with size_t instead of ints.
- Because remote-object-info does not support short oids add a check to improve the error report if the oid passed is valid but not long enough or if it is an invalid oid.
- Fixed overly long lines.
- Reworded 4th commit.
- Avoid unnecessary request to the server when no placeholder is supported.
---
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_ul() 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 (5):
transport-helper: fix memory leak of helper on disconnect
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 | 221 ++++++++++- connect.c | 34 ++ connect.h | 8 + fetch-object-info.c | 106 +++++ 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, 1215 insertions(+), 75 deletions(-)
---
base-commit: 4621f8ce5e9b97aa2e8d0d9ffe9d25df2471074d
change-id: 20260608-ps-eric-work-rebase-b73ae84ba671
Best regards,
Signature · 1 line
Pablo Sabater <[email protected]>
RFC PATCH v13 1/12 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
RFC PATCH v13 2/12 git-compat-util: add strtoul_ul() 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.
This variant is needed in a subsequent commit to enable returning an unsigned long 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 an unsigned long using the standard library's strtoul, +* with additional error handling to ensure robustness. +*/ +static inline int strtoul_ul (char const *s,int base,unsigned long *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 RFC v13 02/12] git-compat-util: add strtoul_ul() with error handling
Quoted 7 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.
Quoted 6 lines
This variant is needed in a subsequent commit.
This variant is needed in a subsequent commit to enable returning an unsigned long with proper error handling.
Signed-off-by Pablo Sabater <[email protected]>
I should have removed the second paragraph which is duplicated, I quickly fixed the last paragraph but didn't realize it contained the same as above, sorry.
RFC PATCH v13 3/12 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
@@ -723,14 +723,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);
@@ -738,9 +736,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 ;
@@ -767,7 +763,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 };
@@ -777,7 +772,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
Re: [PATCH GSoC RFC v13 03/12] cat-file: declare loop counter inside for()
Quoted 22 lines · Pablo Sabater
From: 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.Helped-by Christian Couder <[email protected]>Signed-off-by Eric Ju <[email protected]>Signed-off-by Pablo Sabater <[email protected]>--- builtin/cat-file.c | 13 ++++--------- fetch-pack.c | 3 +-- 2 files changed, 5 insertions(+), 11 deletions(-)builtin/cat-file.c +0
-0
builtin/cat-file.c static void dispatch_calls(struct batch_options *opt, · line 727
Quoted 6 lines
builtin/cat-file.c +1
-1@@ -723,14 +723,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)
This is fine since both the locations that dispatch_calls() are called with, supply a size_t.
The rest seems to have been already validated by Junio.
Looks good
[snip]
RFC PATCH v13 4/12 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. 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
Re: [PATCH GSoC RFC v13 04/12] t1006: split test utility functions into new "lib-cat-file.sh"
Quoted 10 lines · Pablo Sabater
From: 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. The goal is to improve code reuse and readability, enabling future tests to leverage these utilities without duplicating code.Signed-off-by Pablo Sabater <[email protected]>
I forgot to mention that it is needed in a subsequent commit, sorry. It will be in the next version.
RFC PATCH v13 5/12 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.
To move write_fetch_command_and_capabilities() to connect.c, we need to adjust how advertise_sid is managed. Previously in fetch_pack.c, advertise_sid was a static variable, modified using repo_config_get_bool().
In connect.c, we now 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 (!sever_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.
Move write_fetch_command_and_capabilities() to connect.c
--- connect.c | 34 ++++++++++++++++++++++++++++++++++ connect.h | 4 ++++ fetch-pack.c | 31 ------------------------------- 3 files changed, 38 insertions(+), 31 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 -31
@@ -1376,37 +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; - -ensure_server_supports_v2 ("fetch" ); -packet_buf_write (req_buf,"command=fetch" ); -if (server_supports_v2 ("agent" )) -packet_buf_write (req_buf,"agent=%s" ,git_user_agent_sanitized ()); -if (advertise_sid &&server_supports_v2 ("session-id" )) -packet_buf_write (req_buf,"session-id=%s" ,trace2_session_id ()); -if (server_options && server_options->nr) { -ensure_server_supports_v2 ("server-option" ); -for (size_t i =0 ; i < server_options->nr; i++) -packet_buf_write (req_buf,"server-option=%s" , - server_options->items[i].string); - } - -if (server_feature_v2 ("object-format" , &hash_name)) { -int hash_algo =hash_algo_by_name (hash_name); -if (hash_algo_by_ptr (the_hash_algo) != hash_algo) -die (_ ("mismatched algorithms: client %s; server %s" ), - the_hash_algo->name, hash_name); -packet_buf_write (req_buf,"object-format=%s" , the_hash_algo->name); - }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1_LEGACY ) { -die (_ ("the server does not support algorithm '%s'" ), - the_hash_algo->name); - } -packet_buf_delim (req_buf); -} - static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, struct fetch_pack_args *args, const struct ref *wants, struct oidset *common,
Signature · 1 line
2.54.0
Re: [PATCH GSoC RFC v13 05/12] fetch-pack: move function to connect.c
Quoted 2 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill be refactored in a subsequent
Nit: the rest of this patch's body referes to this function as:write_fetch_command_and_capabilities()
Let's use that here as well.
Quoted 8 lines
commit 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 need to adjust howadvertise_sidis managed. Previously infetch_pack.c,advertise_sidwas a static variable, modified usingrepo_config_get_bool().
Quoted 8 lines
In
connect.c, we now initializeadvertise_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 (!sever_supports("session_id"))
s/sever/server
Quoted 4 lines
advertise_sid = 0;
2. In function `fetch_pack_config()`:
repo_config_get_bool("transfer.advertisesid", &advertise_sid);Quoted 8 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.Move
write_fetch_command_and_capabilities()toconnect.c
Nit: this is a better patch header than "move function to connect.c", since it better describes the exact change we intend to make.
Let's use it instead.
Re: [PATCH GSoC RFC v13 05/12] fetch-pack: move function to connect.c
Quoted 9 lines · Chandra Pratap
Quoted 2 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill be refactored in a subsequentNit: the rest of this patch's body referes to this function as:
write_fetch_command_and_capabilities()Let's use that here as well.
I'll do that, thanks.
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.
To move
write_fetch_command_and_capabilities()toconnect.c, we need to adjust howadvertise_sidis managed. Previously infetch_pack.c,advertise_sidwas a static variable, modified usingrepo_config_get_bool().In
connect.c, we now initializeadvertise_sidat the begining by directly usingrepo_config_get_bool(). This change is safe because:
Quoted 7 lines
Quoted 5 lines
In the original
fetch-pack.ccode, there are only two places that writeadvertise_sid:1. In function
do_fetch_pack():if (!sever_supports("session_id"))s/sever/server
True, thanks.
Quoted 12 lines
Quoted 12 lines
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.
Quoted 6 lines
Quoted 1 line
Move
write_fetch_command_and_capabilities()toconnect.cNit: this is a better patch header than "move function to
connect.c", since it better describes the exact change we intend to make.Let's use it instead.
Okay, I'll use it.
Thanks for the review,
Pablo
Re: [PATCH GSoC RFC v13 05/12] fetch-pack: move function to connect.c
In reply to Pablo SabaterQuoted 3 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill 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.
Okay.
Quoted 4 lines
To move
write_fetch_command_and_capabilities()toconnect.c, we need to adjust howadvertise_sidis managed. Previously infetch_pack.c,advertise_sidwas a static variable, modified usingrepo_config_get_bool().
Nit: What's missing is why do we need to move it to 'connect.c', I assume this is because it being generic means its better placed in connect.c over 'fetch-pack.c'. Would be nice to explicitly mention that perhaps?
Quoted 13 lines
In
connect.c, we now initializeadvertise_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 (!sever_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);
Quoted 9 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.Move
write_fetch_command_and_capabilities()toconnect.c
Nit: Wouldn't it then make sense to split this into two?
1. Drop usage of the static advertise_sid within write_fetch_command_and_capabilities().
2. Move write_fetch_command_and_capabilities() to connect.c
That way the second patch is simply a move?
[snip]
Re: [PATCH GSoC RFC v13 05/12] fetch-pack: move function to connect.c
Quoted 14 lines · Karthik Nayak
Quoted 3 lines · Pablo Sabater
write_fetch_command_and_capabilitieswill 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.Okay.
Quoted 4 lines
To move
write_fetch_command_and_capabilities()toconnect.c, we need to adjust howadvertise_sidis managed. Previously infetch_pack.c,advertise_sidwas a static variable, modified usingrepo_config_get_bool().
Quoted 4 lines
Nit: What's missing is why do we need to move it to '
connect.c', I assume this is because it being generic means its better placed inconnect.cover 'fetch-pack.c'. Would be nice to explicitly mention that perhaps?
True, it is for that reason, I'll write it explicitly in the next version, thanks!
Quoted 24 lines
Quoted 14 lines
In
connect.c, we now initializeadvertise_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 (!sever_supports("session_id")) advertise_sid = 0; 2. In function `fetch_pack_config()`: repo_config_get_bool("transfer.advertisesid", &advertise_sid);Quoted 9 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.Move
write_fetch_command_and_capabilities()toconnect.c
Quoted 6 lines
Nit: Wouldn't it then make sense to split this into two?
1. Drop usage of the staticadvertise_sidwithinwrite_fetch_command_and_capabilities().
2. Movewrite_fetch_command_and_capabilities()toconnect.cThat way the second patch is simply a move?
Okay, seems fair, I'll do that, thanks.
Quoted 2 lines
[snip]
Thanks for the review,
Pablo
RFC PATCH v13 6/12 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 | 10 +++++----- connect.h | 8 ++++++-- fetch-pack.c | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-)
connect.c +5 -5
@@ -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" ))
@@ -727,7 +727,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,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 ) { + }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) {die (_ ("the server does not support algorithm '%s'" ), the_hash_algo->name); }
connect.h +6 -2
@@ -34,8 +34,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ struct string_list ; -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);#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 RFC v13 06/12] connect: refactor packet writing
Pablo Sabater <[email protected]> writes:
[snip]
Quoted 26 lines
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.c void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 733
Quoted 8 lines
connect.c +1
-1@@ -727,7 +727,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,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 ) { + }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) {die (_ ("the server does not support algorithm '%s'" ), the_hash_algo->name); }
Why did we make this change? If the server doesn't support v2, then the object format should be GIT_HASH_SHA1_LEGACY. While the value of it is indeed GIT_HASH_SHA1, it indicates a scenario where there was no option to select object hash, which is the scenario here.
If there is a reason to make such a change, perhaps we should highlight this in the commit message.
connect.h void check_stateless_delimiter(int stateless_rpc, · line 41
Quoted 13 lines
connect.h +4
-0@@ -34,8 +34,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error); +/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ struct string_list ;
The comment should be above the function and not the forward declaration.
While we're here, why not #include "string-list.h" and remove the forward declaration, is there a circular dependency?
Quoted 31 lines
connect.h +2
-2-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);#endif fetch-pack.c +2
-3@@ -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" ); --2.54.0
Re: [PATCH GSoC RFC v13 06/12] connect: refactor packet writing
Quoted 45 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
[snip]
Quoted 26 lines
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.c void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 733
Quoted 8 lines
connect.c +1
-1@@ -727,7 +727,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,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 ) { + }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) {die (_ ("the server does not support algorithm '%s'" ), the_hash_algo->name); }Why did we make this change? If the server doesn't support v2, then the object format should be
GIT_HASH_SHA1_LEGACY. While the value of it is indeedGIT_HASH_SHA1, it indicates a scenario where there was no option to select object hash, which is the scenario here.
Quoted 2 lines
If there is a reason to make such a change, perhaps we should highlight this in the commit message.
Hi!
There should be no diff related to that line, In some point between Eric's last version (v11) and mine's firs (v12) the original code changed. On the diff from v11 [1] the object format is the same, i didn't notice this change and it's wrong, I'll fix it for v14, Thanks!
Quoted 9 lines
Quoted 9 lines
connect.h +0
-0@@ -34,8 +34,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error);
Quoted 8 lines
connect.h void check_stateless_delimiter(int stateless_rpc, · line 41
Quoted 5 lines
connect.h +4
-0+/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ struct string_list ;The comment should be above the function and not the forward declaration.
True, I'll fix it for v14.
Quoted 3 lines
While we're here, why not
#include "string-list.h"and remove the forward declaration, is there a circular dependency?
I believe this was right because from what I know forward declarations are prefered in headers when in this case, the struct is only used as a pointer. Investigating, this came from a review from patrick [2].
[snip]
[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/
Thanks for the review,
Pablo.
Re: [PATCH GSoC RFC v13 06/12] connect: refactor packet writing
Quoted 77 lines · Pablo Sabater
Quoted 45 lines · Karthik Nayak
Pablo Sabater <[email protected]> writes:
[snip]
Quoted 26 lines
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.c void write_fetch_command_and_capabilities(struct strbuf *req_buf, · line 733
Quoted 8 lines
connect.c +1
-1@@ -727,7 +727,7 @@ void write_fetch_command_and_capabilities(struct strbuf *req_buf,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 ) { + }else if (hash_algo_by_ptr (the_hash_algo) !=GIT_HASH_SHA1 ) {die (_ ("the server does not support algorithm '%s'" ), the_hash_algo->name); }Why did we make this change? If the server doesn't support v2, then the object format should be
GIT_HASH_SHA1_LEGACY. While the value of it is indeedGIT_HASH_SHA1, it indicates a scenario where there was no option to select object hash, which is the scenario here.Quoted 2 lines
If there is a reason to make such a change, perhaps we should highlight this in the commit message.
Hi!
There should be no diff related to that line, In some point between Eric's last version (v11) and mine's firs (v12) the original code changed. On the diff from v11 [1] the object format is the same, i didn't notice this change and it's wrong, I'll fix it for v14, Thanks!Quoted 9 lines
Quoted 9 lines
connect.h +0
-0@@ -34,8 +34,12 @@ void check_stateless_delimiter(int stateless_rpc,struct packet_reader *reader, const char *error);Quoted 8 lines
connect.h void check_stateless_delimiter(int stateless_rpc, · line 41
Quoted 5 lines
connect.h +4
-0+/* +* Writes a command along with the requested server capabilities/features into a +* request buffer. +*/ struct string_list ;The comment should be above the function and not the forward declaration.
True, I'll fix it for v14.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
While we're here, why not
#include "string-list.h"and remove the forward declaration, is there a circular dependency?I believe this was right because from what I know forward declarations are prefered in headers when in this case, the struct is only used as a pointer. Investigating, this came from a review from patrick [2].
That's fair.
Nit: Maybe add this context to the commit message?
Thanks
Quoted 7 lines
[snip]
[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/Thanks for the review,
Pablo.
Re: [PATCH GSoC RFC v13 06/12] connect: refactor packet writing
In reply to Karthik NayakQuoted 11 lines · Karthik Nayak
Quoted 5 lines
+/*
+ * Writes a command along with the requested server capabilities/features into a
+ * request buffer.
+ */
structstring_list;The comment should be above the function and not the forward declaration.
While we're here, why not
#include "string-list.h"and remove the forward declaration, is there a circular dependency?
Isn't it to avoid unnecessary include? When the header itself only needs to know about the presence of the type, and not the concrete shape of the type (e.g., because it only uses a pointer to that type), it may be overkill to include the entire header file.
Re: [PATCH GSoC RFC v13 06/12] connect: refactor packet writing
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Karthik Nayak
Quoted 5 lines
+/*
+ * Writes a command along with the requested server capabilities/features into a
+ * request buffer.
+ */
structstring_list;The comment should be above the function and not the forward declaration.
Quoted 7 lines
Quoted 2 lines
While we're here, why not
#include "string-list.h"and remove the forward declaration, is there a circular dependency?Isn't it to avoid unnecessary include? When the header itself only needs to know about the presence of the type, and not the concrete shape of the type (e.g., because it only uses a pointer to that type), it may be overkill to include the entire header file.
Indeed that seems to be it. Makes sense to me.
RFC PATCH v13 7/12 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
RFC PATCH v13 8/12 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