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. Add remote-object-info command to cat-file --batch-command. While info takes object ids one at a time, this creates overhead when making requests to a server. So remote-object-info instead can take multiple object ids at once. The cat-file --batch-command command is generally implemented in the following manner: - Receive and parse input from user - Call respective function attached to command - Get object info, print object info In --buffer mode, this changes to: - Receive and parse input from user - Store respective function attached to command in a queue - After flush, loop through commands in queue - Call respective function attached to command - Get object info, print object info Notice how the getting and printing of object info is accomplished one at a time. As described above, this creates a problem for making requests to a server. Therefore, remote-object-info is implemented in the following manner: - Receive and parse input from user If command is remote-object-info: - Get object info from remote - Loop through and print each object info Else: - Call respective function attached to command - Parse input, get object info, print object info And finally for --buffer mode remote-object-info: - Receive and parse input from user - Store respective function attached to command in a queue - After flush, loop through commands in queue: If command is remote-object-info: - Get object info from remote - Loop through and print each object info Else: - Call respective function attached to command - Get object info, print object info To summarize, remote-object-info gets object info from the remote and then loops through the object info passed in, printing the info. In order for remote-object-info to avoid remote communication overhead in the non-buffer mode, the objects are passed in as such: remote-object-info <remote> <oid> <oid> ... <oid> rather than remote-object-info <remote> <oid> remote-object-info <remote> <oid> ... remote-object-info <remote> <oid> Helped-by: Jonathan Tan <jonathantanmy@google.com> Helped-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Eric Ju <eric.peijian@gmail.com> Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Ju committed Jul 10, 2026 at 18:41 UTC 1efc3016a031eaa36749d52a367f6470e29c0f8f
6 files changed +859 -8
Documentation/git-cat-file.adoc
+22 -7
@@ -169,6 +169,13 @@ info <object>::
169 Print object info for object reference `<object>`. This corresponds to the
170 output of `--batch-check`.
171
172 +remote-object-info <remote> <object>...::
173 + Print object info for object references `<object>` at specified
174 + `<remote>` without downloading objects from the remote.
175 + Raise an error when the `object-info` capability is not supported by the remote.
176 + Raise an error when no object references are provided.
177 + This command may be combined with `--buffer`.
178 +
179 flush::
180 Used with `--buffer` to execute all preceding commands that were issued
181 since the beginning or since the last flush was issued. When `--buffer`
@@ -301,7 +308,8 @@ one per line, and print information based on the command given. With
308 `--batch-command`, the `info` command followed by an object will print
309 information about the object the same way `--batch-check` would, and the
310 `contents` command followed by an object prints contents in the same way
304 -`--batch` would.
311 +`--batch` would. The `remote-object-info` command followed by a remote and
312 +objects IDs prints object info from the remote without downloading the objects.
313
314 You can specify the information shown for each object by using a custom
315 `<format>`. The `<format>` is copied literally to stdout for each
@@ -324,15 +332,12 @@ newline. The available atoms are:
332 reports).
333
334 `objectsize:disk`::
327 - The size, in bytes, that the object takes up on disk. See the
328 - note about on-disk sizes in the `CAVEATS` section below.
335 + The size, in bytes, that the object takes up on disk.
336
337 `deltabase`::
338 If the object is stored as a delta on-disk, this expands to the
339 full hex representation of the delta base object name.
333 - Otherwise, expands to the null OID (all zeroes). See `CAVEATS`
334 - below.
335 -
340 + Otherwise, expands to the null OID (all zeroes).
341 `rest`::
342 If this atom is used in the output string, input lines are split
343 at the first whitespace boundary. All characters before that
@@ -340,8 +345,14 @@ newline. The available atoms are:
345 after that first run of whitespace (i.e., the "rest" of the
346 line) are output in place of the `%(rest)` atom.
347
348 +The command `remote-object-info` only supports the `%(objectname)` and
349 +`%(objectsize)` placeholders. See `CAVEATS` below for more information.
350 +
351 If no format is specified, the default format is `%(objectname)
344 -%(objecttype) %(objectsize)`.
352 +%(objecttype) %(objectsize)`, except for `remote-object-info` commands which
353 +use `%(objectname) %(objectsize)` because "%(objecttype)" is not supported yet.
354 +WARNING: When "%(objecttype)" is supported, the default format WILL be unified,
355 +so DO NOT RELY on the current default format to stay the same!!!
356
357 If `--batch` is specified, or if `--batch-command` is used with the `contents`
358 command, the object information is followed by the object contents (consisting
@@ -438,6 +449,10 @@ scripting purposes.
449 CAVEATS
450 -------
451
452 +Note that since only `%(objectname)` and `%(objectsize)` are currently
453 +supported by the `remote-object-info` command. Using any other placeholder in
454 +the format string will raise an error.
455 +
456 Note that the sizes of objects on disk are reported accurately, but care
457 should be taken in drawing conclusions about which refs or objects are
458 responsible for disk usage. The size of a packed non-delta object may be
builtin/cat-file.c
+143 -1
@@ -29,6 +29,22 @@
29 #include "promisor-remote.h"
30 #include "mailmap.h"
31 #include "write-or-die.h"
32 +#include "alias.h"
33 +#include "remote.h"
34 +#include "transport.h"
35 +
36 +/*
37 + * Maximum length for a remote URL. While no universal standard exists,
38 + * 8K is assumed to be a reasonable limit.
39 + */
40 +#define MAX_REMOTE_URL_LEN (8 * 1024)
41 +
42 +/* Maximum number of objects allowed in a single remote-object-info request. */
43 +#define MAX_ALLOWED_OBJ_LIMIT 10000
44 +
45 +/* Maximum input size permitted for the remote-object-info command. */
46 +#define MAX_REMOTE_OBJ_INFO_LINE \
47 + (MAX_REMOTE_URL_LEN + MAX_ALLOWED_OBJ_LIMIT * (GIT_MAX_HEXSZ + 1))
48
49 enum batch_mode {
50 BATCH_MODE_CONTENTS,
@@ -638,6 +654,80 @@ out:
654 object_context_release(&ctx);
655 }
656
657 +static int get_remote_info(struct batch_options *opt,
658 + int argc,
659 + const char **argv,
660 + struct object_info **remote_object_info,
661 + struct oid_array *object_info_oids)
662 +{
663 + int retval = 0;
664 + struct remote *remote = NULL;
665 + struct object_id oid;
666 + struct string_list object_info_options = STRING_LIST_INIT_NODUP;
667 + struct transport *gtransport;
668 +
669 + /*
670 + * TODO: Change the format to "%(objectname) %(objectsize)" when
671 + * remote-object-info command is used. Once we start supporting objecttype
672 + * the default format should change to DEFAULT_FORMAT.
673 + */
674 + if (!opt->format)
675 + opt->format = "%(objectname) %(objectsize)";
676 +
677 + remote = remote_get(argv[0]);
678 + if (!remote)
679 + die(_("must supply valid remote when using remote-object-info"));
680 +
681 + oid_array_clear(object_info_oids);
682 + for (size_t i = 1; i < argc; i++) {
683 + if (get_oid_hex(argv[i], &oid)) {
684 + size_t len = strlen(argv[i]);
685 +
686 + if (len < the_hash_algo->hexsz && len >= 4) {
687 + size_t j;
688 + for (j = 0; j < len; j++)
689 + if (!isxdigit(argv[i][j]))
690 + break;
691 + if (j == len)
692 + die(_("remote-object-info does not support "
693 + "short oids, %d characters required"),
694 + (int)the_hash_algo->hexsz);
695 + }
696 + die(_("not a valid object name '%s'"), argv[i]);
697 + }
698 + oid_array_append(object_info_oids, &oid);
699 + }
700 +
701 + if (!object_info_oids->nr)
702 + die(_("remote-object-info requires objects"));
703 +
704 + gtransport = transport_get(remote, NULL);
705 +
706 + if (!gtransport->smart_options) {
707 + retval = -1;
708 + goto cleanup;
709 + }
710 +
711 + CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
712 + gtransport->smart_options->object_info_oids = object_info_oids;
713 +
714 + /* 'objectsize' is the only option currently supported */
715 + if (!strstr(opt->format, "%(objectsize)"))
716 + die(_("%s is currently not supported with remote-object-info"), opt->format);
717 +
718 + string_list_append(&object_info_options, "size");
719 +
720 + if (object_info_options.nr > 0) {
721 + gtransport->smart_options->object_info_options = &object_info_options;
722 + gtransport->smart_options->object_info_data = *remote_object_info;
723 + retval = transport_fetch_object_info(gtransport);
724 + }
725 +cleanup:
726 + string_list_clear(&object_info_options, 0);
727 + transport_disconnect(gtransport);
728 + return retval;
729 +}
730 +
731 struct object_cb_data {
732 struct batch_options *opt;
733 struct expand_data *expand;
@@ -719,6 +809,57 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
809 load_mailmap();
810 }
811
812 +static void parse_cmd_remote_object_info(struct batch_options *opt,
813 + const char *line, struct strbuf *output,
814 + struct expand_data *data)
815 +{
816 + int count;
817 + const char **argv;
818 + char *line_to_split;
819 + struct object_info *remote_object_info = NULL;
820 + struct oid_array object_info_oids = OID_ARRAY_INIT;
821 +
822 + if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
823 + die(_("remote-object-info command too long"));
824 +
825 + line_to_split = xstrdup(line);
826 + count = split_cmdline(line_to_split, &argv);
827 + if (count < 0)
828 + die(_("remote-object-info: %s"), split_cmdline_strerror(count));
829 + if (count - 1 > MAX_ALLOWED_OBJ_LIMIT)
830 + die(_("remote-object-info supports at most %d objects"),
831 + MAX_ALLOWED_OBJ_LIMIT);
832 +
833 + if (get_remote_info(opt, count, argv, &remote_object_info,
834 + &object_info_oids))
835 + goto cleanup;
836 +
837 + data->skip_object_info = 1;
838 + for (size_t i = 0; i < object_info_oids.nr; i++) {
839 + data->oid = object_info_oids.oid[i];
840 + if (remote_object_info[i].sizep) {
841 + /*
842 + * When reaching here, it means remote-object-info can retrieve
843 + * information from server without downloading them.
844 + */
845 + data->size = *remote_object_info[i].sizep;
846 + opt->batch_mode = BATCH_MODE_INFO;
847 + batch_object_write(argv[i + 1], output, opt, data, NULL, 0);
848 + } else {
849 + report_object_status(opt, oid_to_hex(&data->oid), &data->oid, "missing");
850 + }
851 + }
852 + data->skip_object_info = 0;
853 +
854 +cleanup:
855 + for (size_t i = 0; i < object_info_oids.nr; i++)
856 + free_object_info_contents(&remote_object_info[i]);
857 + free(line_to_split);
858 + free(argv);
859 + free(remote_object_info);
860 + oid_array_clear(&object_info_oids);
861 +}
862 +
863 static void dispatch_calls(struct batch_options *opt,
864 struct strbuf *output,
865 struct expand_data *data,
@@ -750,8 +891,9 @@ static const struct parse_cmd {
891 } commands[] = {
892 { "contents", parse_cmd_contents, 1 },
893 { "info", parse_cmd_info, 1 },
753 - { "flush", NULL, 0 },
894 { "mailmap", parse_cmd_mailmap, 1 },
895 + { "remote-object-info", parse_cmd_remote_object_info, 1 },
896 + { "flush", NULL, 0 },
897 };
898
899 static void batch_objects_command(struct batch_options *opt,
object-file.c
+10
@@ -1694,3 +1694,13 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
1694
1695 return &transaction->base;
1696 }
1697 +
1698 +void free_object_info_contents(struct object_info *object_info)
1699 +{
1700 + if (!object_info)
1701 + return;
1702 + free(object_info->typep);
1703 + free(object_info->sizep);
1704 + free(object_info->disk_sizep);
1705 + free(object_info->delta_base_oid);
1706 +}
odb.h
+3
@@ -573,4 +573,7 @@ void parse_alternates(const char *string,
573 const char *relative_base,
574 struct strvec *out);
575
576 +/* Free pointers inside of object_info, but not object_info itself */
577 +void free_object_info_contents(struct object_info *object_info);
578 +
579 #endif /* ODB_H */
t/meson.build
+1
@@ -170,6 +170,7 @@ integration_tests = [
170 't1014-read-tree-confusing.sh',
171 't1015-read-index-unmerged.sh',
172 't1016-compatObjectFormat.sh',
173 + 't1017-cat-file-remote-object-info.sh',
174 't1020-subdirectory.sh',
175 't1022-read-tree-partial-clone.sh',
176 't1050-large.sh',
t/t1017-cat-file-remote-object-info.sh new
+680
@@ -0,0 +1,680 @@
1 +#!/bin/sh
2 +
3 +test_description='git cat-file --batch-command with remote-object-info command'
4 +
5 +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 +
8 +. ./test-lib.sh
9 +. "$TEST_DIRECTORY"/lib-cat-file.sh
10 +
11 +hello_content="Hello World"
12 +hello_size=$(strlen "$hello_content")
13 +hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
14 +hello_short_oid=$(git rev-parse --short "$hello_oid")
15 +
16 +unstored_content="Hello Git"
17 +unstored_oid=$(echo_without_newline "$unstored_content" | git hash-object --stdin)
18 +
19 +# This is how we get 13:
20 +# 13 = <file mode> + <a_space> + <file name> + <a_null>, where
21 +# file mode is 100644, which is 6 characters;
22 +# file name is hello, which is 5 characters
23 +# a space is 1 character and a null is 1 character
24 +tree_size=$(($(test_oid rawsz) + 13))
25 +
26 +commit_message="Initial commit"
27 +
28 +# This is how we get 137:
29 +# 137 = <tree header> + <a_space> + <a newline> +
30 +# <Author line> + <a newline> +
31 +# <Committer line> + <a newline> +
32 +# <a newline> +
33 +# <commit message length>
34 +# An easier way to calculate is: 1. use `git cat-file commit <commit hash> | wc -c`,
35 +# to get 177, 2. then deduct 40 hex characters to get 137
36 +commit_size=$(($(test_oid hexsz) + 137))
37 +
38 +tag_header_without_oid="type blob
39 +tag hellotag
40 +tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
41 +tag_header_without_timestamp="object $hello_oid
42 +$tag_header_without_oid"
43 +tag_description="This is a tag"
44 +tag_content="$tag_header_without_timestamp 0 +0000
45 +
46 +$tag_description"
47 +
48 +tag_oid=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
49 +tag_size=$(strlen "$tag_content")
50 +
51 +set_transport_variables () {
52 + hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
53 + tree_oid=$(git -C "$1" write-tree)
54 + commit_oid=$(echo_without_newline "$commit_message" | git -C "$1" commit-tree $tree_oid)
55 + tag_oid=$(echo_without_newline "$tag_content" | git -C "$1" hash-object -t tag --stdin -w)
56 + tag_size=$(strlen "$tag_content")
57 +}
58 +
59 +# This section tests --batch-command with remote-object-info command
60 +# Since "%(objecttype)" is currently not supported by the command remote-object-info ,
61 +# the filters are set to "%(objectname) %(objectsize)" in some test cases.
62 +
63 +# Test --batch-command remote-object-info with 'git://' transport with
64 +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability
65 +. "$TEST_DIRECTORY"/lib-git-daemon.sh
66 +start_git_daemon --export-all --enable=receive-pack
67 +daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
68 +
69 +test_expect_success 'create repo to be served by git-daemon' '
70 + git init "$daemon_parent" &&
71 + echo_without_newline "$hello_content" > $daemon_parent/hello &&
72 + git -C "$daemon_parent" update-index --add hello &&
73 + git -C "$daemon_parent" config transfer.advertiseobjectinfo true &&
74 + git clone "$GIT_DAEMON_URL/parent" -n "$daemon_parent/daemon_client_empty"
75 +'
76 +
77 +test_expect_success 'batch-command remote-object-info git://' '
78 + (
79 + set_transport_variables "$daemon_parent" &&
80 + cd "$daemon_parent/daemon_client_empty" &&
81 +
82 + # These results prove remote-object-info can get object info from the remote
83 + echo "$hello_oid $hello_size" >expect &&
84 + echo "$tree_oid $tree_size" >>expect &&
85 + echo "$commit_oid $commit_size" >>expect &&
86 + echo "$tag_oid $tag_size" >>expect &&
87 +
88 + # These results prove remote-object-info did not download objects from the remote
89 + echo "$hello_oid missing" >>expect &&
90 + echo "$tree_oid missing" >>expect &&
91 + echo "$commit_oid missing" >>expect &&
92 + echo "$tag_oid missing" >>expect &&
93 +
94 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
95 + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
96 + remote-object-info "$GIT_DAEMON_URL/parent" $tree_oid
97 + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid
98 + remote-object-info "$GIT_DAEMON_URL/parent" $tag_oid
99 + info $hello_oid
100 + info $tree_oid
101 + info $commit_oid
102 + info $tag_oid
103 + EOF
104 + test_cmp expect actual
105 + )
106 +'
107 +
108 +test_expect_success 'batch-command remote-object-info git:// multiple sha1 per line' '
109 + (
110 + set_transport_variables "$daemon_parent" &&
111 + cd "$daemon_parent/daemon_client_empty" &&
112 +
113 + # These results prove remote-object-info can get object info from the remote
114 + echo "$hello_oid $hello_size" >expect &&
115 + echo "$tree_oid $tree_size" >>expect &&
116 + echo "$commit_oid $commit_size" >>expect &&
117 + echo "$tag_oid $tag_size" >>expect &&
118 +
119 + # These results prove remote-object-info did not download objects from the remote
120 + echo "$hello_oid missing" >>expect &&
121 + echo "$tree_oid missing" >>expect &&
122 + echo "$commit_oid missing" >>expect &&
123 + echo "$tag_oid missing" >>expect &&
124 +
125 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
126 + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid $commit_oid $tag_oid
127 + info $hello_oid
128 + info $tree_oid
129 + info $commit_oid
130 + info $tag_oid
131 + EOF
132 + test_cmp expect actual
133 + )
134 +'
135 +
136 +test_expect_success 'batch-command remote-object-info git:// default filter' '
137 + (
138 + set_transport_variables "$daemon_parent" &&
139 + cd "$daemon_parent/daemon_client_empty" &&
140 +
141 + echo "$hello_oid $hello_size" >expect &&
142 + echo "$tree_oid $tree_size" >>expect &&
143 + echo "$commit_oid $commit_size" >>expect &&
144 + echo "$tag_oid $tag_size" >>expect &&
145 + GIT_TRACE_PACKET=1 git cat-file --batch-command >actual <<-EOF &&
146 + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid
147 + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid $tag_oid
148 + EOF
149 + test_cmp expect actual
150 + )
151 +'
152 +
153 +test_expect_success 'batch-command --buffer remote-object-info git://' '
154 + (
155 + set_transport_variables "$daemon_parent" &&
156 + cd "$daemon_parent/daemon_client_empty" &&
157 +
158 + # These results prove remote-object-info can get object info from the remote
159 + echo "$hello_oid $hello_size" >expect &&
160 + echo "$tree_oid $tree_size" >>expect &&
161 + echo "$commit_oid $commit_size" >>expect &&
162 + echo "$tag_oid $tag_size" >>expect &&
163 +
164 + # These results prove remote-object-info did not download objects from the remote
165 + echo "$hello_oid missing" >>expect &&
166 + echo "$tree_oid missing" >>expect &&
167 + echo "$commit_oid missing" >>expect &&
168 + echo "$tag_oid missing" >>expect &&
169 +
170 + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF &&
171 + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid
172 + remote-object-info "$GIT_DAEMON_URL/parent" $commit_oid $tag_oid
173 + info $hello_oid
174 + info $tree_oid
175 + info $commit_oid
176 + info $tag_oid
177 + flush
178 + EOF
179 + test_cmp expect actual
180 + )
181 +'
182 +
183 +test_expect_success 'batch-command -Z remote-object-info git:// default filter' '
184 + (
185 + set_transport_variables "$daemon_parent" &&
186 + cd "$daemon_parent/daemon_client_empty" &&
187 +
188 + printf "%s\0" "$hello_oid $hello_size" >expect &&
189 + printf "%s\0" "$tree_oid $tree_size" >>expect &&
190 + printf "%s\0" "$commit_oid $commit_size" >>expect &&
191 + printf "%s\0" "$tag_oid $tag_size" >>expect &&
192 +
193 + printf "%s\0" "$hello_oid missing" >>expect &&
194 + printf "%s\0" "$tree_oid missing" >>expect &&
195 + printf "%s\0" "$commit_oid missing" >>expect &&
196 + printf "%s\0" "$tag_oid missing" >>expect &&
197 +
198 + batch_input="remote-object-info $GIT_DAEMON_URL/parent $hello_oid $tree_oid
199 +remote-object-info $GIT_DAEMON_URL/parent $commit_oid $tag_oid
200 +info $hello_oid
201 +info $tree_oid
202 +info $commit_oid
203 +info $tag_oid
204 +" &&
205 + echo_without_newline_nul "$batch_input" >commands_null_delimited &&
206 +
207 + git cat-file --batch-command -Z < commands_null_delimited >actual &&
208 + test_cmp expect actual
209 + )
210 +'
211 +
212 +test_expect_success 'remote-object-info does not support short oids' '
213 + (
214 + set_transport_variables "$daemon_parent" &&
215 + cd "$daemon_parent/daemon_client_empty" &&
216 +
217 + test_must_fail git cat-file --batch-command 2>err <<-EOF &&
218 + remote-object-info $GIT_DAEMON_URL/parent $hello_short_oid
219 + EOF
220 + test_grep "does not support short oids" err
221 + )
222 +'
223 +
224 +test_expect_success 'remote-object-info does not die on missing oid like info' '
225 + (
226 + set_transport_variables "$daemon_parent" &&
227 + cd "$daemon_parent/daemon_client_empty" &&
228 +
229 + git cat-file --batch-command >local <<-EOF &&
230 + info $unstored_oid
231 + EOF
232 + git cat-file --batch-command >remote <<-EOF &&
233 + remote-object-info $GIT_DAEMON_URL/parent $unstored_oid
234 + EOF
235 + test_cmp local remote
236 + )
237 +'
238 +
239 +# Test --batch-command remote-object-info with 'git://' and
240 +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
241 +test_expect_success 'batch-command remote-object-info git:// fails when transfer.advertiseobjectinfo=false' '
242 + (
243 + git -C "$daemon_parent" config transfer.advertiseobjectinfo false &&
244 + set_transport_variables "$daemon_parent" &&
245 +
246 + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
247 + remote-object-info $GIT_DAEMON_URL/parent $hello_oid $tree_oid $commit_oid $tag_oid
248 + EOF
249 + test_grep "object-info capability is not enabled on the server" err &&
250 +
251 + # revert server state back
252 + git -C "$daemon_parent" config transfer.advertiseobjectinfo true
253 +
254 + )
255 +'
256 +
257 +stop_git_daemon
258 +
259 +# Test --batch-command remote-object-info with 'file://' transport with
260 +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability
261 +# shellcheck disable=SC2016
262 +test_expect_success 'create repo to be served by file:// transport' '
263 + git init server &&
264 + git -C server config protocol.version 2 &&
265 + git -C server config transfer.advertiseobjectinfo true &&
266 + echo_without_newline "$hello_content" > server/hello &&
267 + git -C server update-index --add hello &&
268 + git clone -n "file://$(pwd)/server" file_client_empty
269 +'
270 +
271 +test_expect_success 'batch-command remote-object-info file://' '
272 + (
273 + set_transport_variables "server" &&
274 + server_path="$(pwd)/server" &&
275 + cd file_client_empty &&
276 +
277 + # These results prove remote-object-info can get object info from the remote
278 + echo "$hello_oid $hello_size" >expect &&
279 + echo "$tree_oid $tree_size" >>expect &&
280 + echo "$commit_oid $commit_size" >>expect &&
281 + echo "$tag_oid $tag_size" >>expect &&
282 +
283 + # These results prove remote-object-info did not download objects from the remote
284 + echo "$hello_oid missing" >>expect &&
285 + echo "$tree_oid missing" >>expect &&
286 + echo "$commit_oid missing" >>expect &&
287 + echo "$tag_oid missing" >>expect &&
288 +
289 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
290 + remote-object-info "file://${server_path}" $hello_oid
291 + remote-object-info "file://${server_path}" $tree_oid
292 + remote-object-info "file://${server_path}" $commit_oid
293 + remote-object-info "file://${server_path}" $tag_oid
294 + info $hello_oid
295 + info $tree_oid
296 + info $commit_oid
297 + info $tag_oid
298 + EOF
299 + test_cmp expect actual
300 + )
301 +'
302 +
303 +test_expect_success 'batch-command remote-object-info file:// multiple sha1 per line' '
304 + (
305 + set_transport_variables "server" &&
306 + server_path="$(pwd)/server" &&
307 + cd file_client_empty &&
308 +
309 + # These results prove remote-object-info can get object info from the remote
310 + echo "$hello_oid $hello_size" >expect &&
311 + echo "$tree_oid $tree_size" >>expect &&
312 + echo "$commit_oid $commit_size" >>expect &&
313 + echo "$tag_oid $tag_size" >>expect &&
314 +
315 + # These results prove remote-object-info did not download objects from the remote
316 + echo "$hello_oid missing" >>expect &&
317 + echo "$tree_oid missing" >>expect &&
318 + echo "$commit_oid missing" >>expect &&
319 + echo "$tag_oid missing" >>expect &&
320 +
321 +
322 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
323 + remote-object-info "file://${server_path}" $hello_oid $tree_oid $commit_oid $tag_oid
324 + info $hello_oid
325 + info $tree_oid
326 + info $commit_oid
327 + info $tag_oid
328 + EOF
329 + test_cmp expect actual
330 + )
331 +'
332 +
333 +test_expect_success 'batch-command --buffer remote-object-info file://' '
334 + (
335 + set_transport_variables "server" &&
336 + server_path="$(pwd)/server" &&
337 + cd file_client_empty &&
338 +
339 + # These results prove remote-object-info can get object info from the remote
340 + echo "$hello_oid $hello_size" >expect &&
341 + echo "$tree_oid $tree_size" >>expect &&
342 + echo "$commit_oid $commit_size" >>expect &&
343 + echo "$tag_oid $tag_size" >>expect &&
344 +
345 + # These results prove remote-object-info did not download objects from the remote
346 + echo "$hello_oid missing" >>expect &&
347 + echo "$tree_oid missing" >>expect &&
348 + echo "$commit_oid missing" >>expect &&
349 + echo "$tag_oid missing" >>expect &&
350 +
351 + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF &&
352 + remote-object-info "file://${server_path}" $hello_oid $tree_oid
353 + remote-object-info "file://${server_path}" $commit_oid $tag_oid
354 + info $hello_oid
355 + info $tree_oid
356 + info $commit_oid
357 + info $tag_oid
358 + flush
359 + EOF
360 + test_cmp expect actual
361 + )
362 +'
363 +
364 +test_expect_success 'batch-command remote-object-info file:// default filter' '
365 + (
366 + set_transport_variables "server" &&
367 + server_path="$(pwd)/server" &&
368 + cd file_client_empty &&
369 +
370 + echo "$hello_oid $hello_size" >expect &&
371 + echo "$tree_oid $tree_size" >>expect &&
372 + echo "$commit_oid $commit_size" >>expect &&
373 + echo "$tag_oid $tag_size" >>expect &&
374 +
375 + git cat-file --batch-command >actual <<-EOF &&
376 + remote-object-info "file://${server_path}" $hello_oid $tree_oid
377 + remote-object-info "file://${server_path}" $commit_oid $tag_oid
378 + EOF
379 + test_cmp expect actual
380 + )
381 +'
382 +
383 +test_expect_success 'batch-command -Z remote-object-info file:// default filter' '
384 + (
385 + set_transport_variables "server" &&
386 + server_path="$(pwd)/server" &&
387 + cd file_client_empty &&
388 +
389 + printf "%s\0" "$hello_oid $hello_size" >expect &&
390 + printf "%s\0" "$tree_oid $tree_size" >>expect &&
391 + printf "%s\0" "$commit_oid $commit_size" >>expect &&
392 + printf "%s\0" "$tag_oid $tag_size" >>expect &&
393 +
394 + printf "%s\0" "$hello_oid missing" >>expect &&
395 + printf "%s\0" "$tree_oid missing" >>expect &&
396 + printf "%s\0" "$commit_oid missing" >>expect &&
397 + printf "%s\0" "$tag_oid missing" >>expect &&
398 +
399 + batch_input="remote-object-info \"file://${server_path}\" $hello_oid $tree_oid
400 +remote-object-info \"file://${server_path}\" $commit_oid $tag_oid
401 +info $hello_oid
402 +info $tree_oid
403 +info $commit_oid
404 +info $tag_oid
405 +" &&
406 + echo_without_newline_nul "$batch_input" >commands_null_delimited &&
407 +
408 + git cat-file --batch-command -Z < commands_null_delimited >actual &&
409 + test_cmp expect actual
410 + )
411 +'
412 +
413 +# Test --batch-command remote-object-info with 'file://' and
414 +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
415 +test_expect_success 'batch-command remote-object-info file:// fails when transfer.advertiseobjectinfo=false' '
416 + (
417 + set_transport_variables "server" &&
418 + server_path="$(pwd)/server" &&
419 + git -C "${server_path}" config transfer.advertiseobjectinfo false &&
420 +
421 + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
422 + remote-object-info "file://${server_path}" $hello_oid $tree_oid $commit_oid $tag_oid
423 + EOF
424 + test_grep "object-info capability is not enabled on the server" err &&
425 +
426 + # revert server state back
427 + git -C "${server_path}" config transfer.advertiseobjectinfo true
428 + )
429 +'
430 +
431 +# Test --batch-command remote-object-info with 'http://' transport with
432 +# transfer.advertiseobjectinfo set to true, i.e. server has object-info capability
433 +
434 +. "$TEST_DIRECTORY"/lib-httpd.sh
435 +start_httpd
436 +
437 +test_expect_success 'create repo to be served by http:// transport' '
438 + git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
439 + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
440 + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true &&
441 + echo_without_newline "$hello_content" > $HTTPD_DOCUMENT_ROOT_PATH/http_parent/hello &&
442 + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" update-index --add hello &&
443 + git clone "$HTTPD_URL/smart/http_parent" -n "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty"
444 +'
445 +
446 +test_expect_success 'batch-command remote-object-info http://' '
447 + (
448 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
449 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
450 +
451 + # These results prove remote-object-info can get object info from the remote
452 + echo "$hello_oid $hello_size" >expect &&
453 + echo "$tree_oid $tree_size" >>expect &&
454 + echo "$commit_oid $commit_size" >>expect &&
455 + echo "$tag_oid $tag_size" >>expect &&
456 +
457 + # These results prove remote-object-info did not download objects from the remote
458 + echo "$hello_oid missing" >>expect &&
459 + echo "$tree_oid missing" >>expect &&
460 + echo "$commit_oid missing" >>expect &&
461 + echo "$tag_oid missing" >>expect &&
462 +
463 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
464 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
465 + remote-object-info "$HTTPD_URL/smart/http_parent" $tree_oid
466 + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid
467 + remote-object-info "$HTTPD_URL/smart/http_parent" $tag_oid
468 + info $hello_oid
469 + info $tree_oid
470 + info $commit_oid
471 + info $tag_oid
472 + EOF
473 + test_cmp expect actual
474 + )
475 +'
476 +
477 +test_expect_success 'batch-command remote-object-info http:// one line' '
478 + (
479 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
480 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
481 +
482 + # These results prove remote-object-info can get object info from the remote
483 + echo "$hello_oid $hello_size" >expect &&
484 + echo "$tree_oid $tree_size" >>expect &&
485 + echo "$commit_oid $commit_size" >>expect &&
486 + echo "$tag_oid $tag_size" >>expect &&
487 +
488 + # These results prove remote-object-info did not download objects from the remote
489 + echo "$hello_oid missing" >>expect &&
490 + echo "$tree_oid missing" >>expect &&
491 + echo "$commit_oid missing" >>expect &&
492 + echo "$tag_oid missing" >>expect &&
493 +
494 + git cat-file --batch-command="%(objectname) %(objectsize)" >actual <<-EOF &&
495 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid $commit_oid $tag_oid
496 + info $hello_oid
497 + info $tree_oid
498 + info $commit_oid
499 + info $tag_oid
500 + EOF
501 + test_cmp expect actual
502 + )
503 +'
504 +
505 +test_expect_success 'batch-command --buffer remote-object-info http://' '
506 + (
507 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
508 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
509 +
510 + # These results prove remote-object-info can get object info from the remote
511 + echo "$hello_oid $hello_size" >expect &&
512 + echo "$tree_oid $tree_size" >>expect &&
513 + echo "$commit_oid $commit_size" >>expect &&
514 + echo "$tag_oid $tag_size" >>expect &&
515 +
516 + # These results prove remote-object-info did not download objects from the remote
517 + echo "$hello_oid missing" >>expect &&
518 + echo "$tree_oid missing" >>expect &&
519 + echo "$commit_oid missing" >>expect &&
520 + echo "$tag_oid missing" >>expect &&
521 +
522 + git cat-file --batch-command="%(objectname) %(objectsize)" --buffer >actual <<-EOF &&
523 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid
524 + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid $tag_oid
525 + info $hello_oid
526 + info $tree_oid
527 + info $commit_oid
528 + info $tag_oid
529 + flush
530 + EOF
531 + test_cmp expect actual
532 + )
533 +'
534 +
535 +test_expect_success 'batch-command remote-object-info http:// default filter' '
536 + (
537 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
538 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
539 +
540 + echo "$hello_oid $hello_size" >expect &&
541 + echo "$tree_oid $tree_size" >>expect &&
542 + echo "$commit_oid $commit_size" >>expect &&
543 + echo "$tag_oid $tag_size" >>expect &&
544 +
545 + git cat-file --batch-command >actual <<-EOF &&
546 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid
547 + remote-object-info "$HTTPD_URL/smart/http_parent" $commit_oid $tag_oid
548 + EOF
549 + test_cmp expect actual
550 + )
551 +'
552 +
553 +test_expect_success 'batch-command -Z remote-object-info http:// default filter' '
554 + (
555 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
556 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
557 +
558 + printf "%s\0" "$hello_oid $hello_size" >expect &&
559 + printf "%s\0" "$tree_oid $tree_size" >>expect &&
560 + printf "%s\0" "$commit_oid $commit_size" >>expect &&
561 + printf "%s\0" "$tag_oid $tag_size" >>expect &&
562 +
563 + batch_input="remote-object-info $HTTPD_URL/smart/http_parent $hello_oid $tree_oid
564 +remote-object-info $HTTPD_URL/smart/http_parent $commit_oid $tag_oid
565 +" &&
566 + echo_without_newline_nul "$batch_input" >commands_null_delimited &&
567 +
568 + git cat-file --batch-command -Z < commands_null_delimited >actual &&
569 + test_cmp expect actual
570 + )
571 +'
572 +
573 +test_expect_success 'remote-object-info fails on unsupported filter option (objectsize:disk)' '
574 + (
575 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
576 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
577 +
578 + test_must_fail git cat-file --batch-command="%(objectsize:disk)" 2>err <<-EOF &&
579 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
580 + EOF
581 + test_grep "%(objectsize:disk) is currently not supported with remote-object-info" err
582 + )
583 +'
584 +
585 +test_expect_success 'remote-object-info fails on unsupported filter option (deltabase)' '
586 + (
587 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
588 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
589 +
590 + test_must_fail git cat-file --batch-command="%(deltabase)" 2>err <<-EOF &&
591 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
592 + EOF
593 + test_grep "%(deltabase) is currently not supported with remote-object-info" err
594 + )
595 +'
596 +
597 +test_expect_success 'remote-object-info fails on server with legacy protocol' '
598 + (
599 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
600 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
601 +
602 + test_must_fail git -c protocol.version=0 cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
603 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
604 + EOF
605 + test_grep "object-info requires protocol v2" err
606 + )
607 +'
608 +
609 +test_expect_success 'remote-object-info fails on server with legacy protocol with default filter' '
610 + (
611 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
612 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
613 +
614 + test_must_fail git -c protocol.version=0 cat-file --batch-command 2>err <<-EOF &&
615 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid
616 + EOF
617 + test_grep "object-info requires protocol v2" err
618 + )
619 +'
620 +
621 +test_expect_success 'remote-object-info fails on malformed OID' '
622 + (
623 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
624 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
625 + malformed_object_id="this_id_is_not_valid" &&
626 +
627 + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
628 + remote-object-info "$HTTPD_URL/smart/http_parent" $malformed_object_id
629 + EOF
630 + test_grep "not a valid object name '$malformed_object_id'" err
631 + )
632 +'
633 +
634 +test_expect_success 'remote-object-info fails on malformed OID with default filter' '
635 + (
636 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
637 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
638 + malformed_object_id="this_id_is_not_valid" &&
639 +
640 + test_must_fail git cat-file --batch-command 2>err <<-EOF &&
641 + remote-object-info "$HTTPD_URL/smart/http_parent" $malformed_object_id
642 + EOF
643 + test_grep "not a valid object name '$malformed_object_id'" err
644 + )
645 +'
646 +
647 +test_expect_success 'remote-object-info fails on not providing OID' '
648 + (
649 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
650 + cd "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
651 +
652 + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
653 + remote-object-info "$HTTPD_URL/smart/http_parent"
654 + EOF
655 + test_grep "remote-object-info requires objects" err
656 + )
657 +'
658 +
659 +
660 +# Test --batch-command remote-object-info with 'http://' transport and
661 +# transfer.advertiseobjectinfo set to false, i.e. server does not have object-info capability
662 +test_expect_success 'batch-command remote-object-info http:// fails when transfer.advertiseobjectinfo=false ' '
663 + (
664 + set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
665 + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo false &&
666 +
667 + test_must_fail git cat-file --batch-command="%(objectname) %(objectsize)" 2>err <<-EOF &&
668 + remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid $commit_oid $tag_oid
669 + EOF
670 + test_grep "object-info capability is not enabled on the server" err &&
671 +
672 + # revert server state back
673 + git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config transfer.advertiseobjectinfo true
674 + )
675 +'
676 +
677 +# DO NOT add non-httpd-specific tests here, because the last part of this
678 +# test script is only executed when httpd is available and enabled.
679 +
680 +test_done