Skip to content

Commit 1efc301

Browse files
peijianjugitster
authored andcommitted
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>
1 parent 82e288a commit 1efc301

6 files changed

Lines changed: 859 additions & 8 deletions

File tree

Documentation/git-cat-file.adoc

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ info <object>::
169169
Print object info for object reference `<object>`. This corresponds to the
170170
output of `--batch-check`.
171171

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+
172179
flush::
173180
Used with `--buffer` to execute all preceding commands that were issued
174181
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
301308
`--batch-command`, the `info` command followed by an object will print
302309
information about the object the same way `--batch-check` would, and the
303310
`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.
305313

306314
You can specify the information shown for each object by using a custom
307315
`<format>`. The `<format>` is copied literally to stdout for each
@@ -324,24 +332,27 @@ newline. The available atoms are:
324332
reports).
325333

326334
`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.
329336

330337
`deltabase`::
331338
If the object is stored as a delta on-disk, this expands to the
332339
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).
336341
`rest`::
337342
If this atom is used in the output string, input lines are split
338343
at the first whitespace boundary. All characters before that
339344
whitespace are considered to be the object name; characters
340345
after that first run of whitespace (i.e., the "rest" of the
341346
line) are output in place of the `%(rest)` atom.
342347

348+
The command `remote-object-info` only supports the `%(objectname)` and
349+
`%(objectsize)` placeholders. See `CAVEATS` below for more information.
350+
343351
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!!!
345356

346357
If `--batch` is specified, or if `--batch-command` is used with the `contents`
347358
command, the object information is followed by the object contents (consisting
@@ -438,6 +449,10 @@ scripting purposes.
438449
CAVEATS
439450
-------
440451
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+
441456
Note that the sizes of objects on disk are reported accurately, but care
442457
should be taken in drawing conclusions about which refs or objects are
443458
responsible for disk usage. The size of a packed non-delta object may be

builtin/cat-file.c

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@
2929
#include "promisor-remote.h"
3030
#include "mailmap.h"
3131
#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))
3248

3349
enum batch_mode {
3450
BATCH_MODE_CONTENTS,
@@ -638,6 +654,80 @@ static void batch_one_object(const char *obj_name,
638654
object_context_release(&ctx);
639655
}
640656

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+
641731
struct object_cb_data {
642732
struct batch_options *opt;
643733
struct expand_data *expand;
@@ -719,6 +809,57 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
719809
load_mailmap();
720810
}
721811

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+
722863
static void dispatch_calls(struct batch_options *opt,
723864
struct strbuf *output,
724865
struct expand_data *data,
@@ -750,8 +891,9 @@ static const struct parse_cmd {
750891
} commands[] = {
751892
{ "contents", parse_cmd_contents, 1 },
752893
{ "info", parse_cmd_info, 1 },
753-
{ "flush", NULL, 0 },
754894
{ "mailmap", parse_cmd_mailmap, 1 },
895+
{ "remote-object-info", parse_cmd_remote_object_info, 1 },
896+
{ "flush", NULL, 0 },
755897
};
756898

757899
static void batch_objects_command(struct batch_options *opt,

object-file.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,3 +1694,13 @@ struct odb_transaction *odb_transaction_files_begin(struct odb_source *source)
16941694

16951695
return &transaction->base;
16961696
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,7 @@ void parse_alternates(const char *string,
573573
const char *relative_base,
574574
struct strvec *out);
575575

576+
/* Free pointers inside of object_info, but not object_info itself */
577+
void free_object_info_contents(struct object_info *object_info);
578+
576579
#endif /* ODB_H */

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ integration_tests = [
170170
't1014-read-tree-confusing.sh',
171171
't1015-read-index-unmerged.sh',
172172
't1016-compatObjectFormat.sh',
173+
't1017-cat-file-remote-object-info.sh',
173174
't1020-subdirectory.sh',
174175
't1022-read-tree-partial-clone.sh',
175176
't1050-large.sh',

0 commit comments

Comments
 (0)