Skip to content

Commit

Permalink
Merge branch 'ba/object-info'
Browse files Browse the repository at this point in the history
Over-the-wire protocol learns a new request type to ask for object
sizes given a list of object names.

* ba/object-info:
  object-info: support for retrieving object info
  • Loading branch information
gitster committed May 13, 2021
2 parents daffa89 + a2ba162 commit eede711
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Documentation/technical/protocol-v2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,34 @@ packet-line, and must not contain non-printable or whitespace characters. The
current implementation uses trace2 session IDs (see
link:api-trace2.html[api-trace2] for details), but this may change and users of
the session ID should not rely on this fact.

object-info
~~~~~~~~~~~

`object-info` is the command to retrieve information about one or more objects.
Its main purpose is to allow a client to make decisions based on this
information without having to fully fetch objects. Object size is the only
information that is currently supported.

An `object-info` request takes the following arguments:

size
Requests size information to be returned for each listed object id.

oid <oid>
Indicates to the server an object which the client wants to obtain
information for.

The response of `object-info` is a list of the the requested object ids
and associated requested information, each separated by a single space.

output = info flush-pkt

info = PKT-LINE(attrs) LF)
*PKT-LINE(obj-info LF)

attrs = attr | attrs SP attrs

attr = "size"

obj-info = obj-id SP obj-size
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ LIB_OBJS += progress.o
LIB_OBJS += promisor-remote.o
LIB_OBJS += prompt.o
LIB_OBJS += protocol.o
LIB_OBJS += protocol-caps.o
LIB_OBJS += prune-packed.o
LIB_OBJS += quote.o
LIB_OBJS += range-diff.o
Expand Down
113 changes: 113 additions & 0 deletions protocol-caps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "git-compat-util.h"
#include "protocol-caps.h"
#include "gettext.h"
#include "pkt-line.h"
#include "strvec.h"
#include "hash.h"
#include "object.h"
#include "object-store.h"
#include "string-list.h"
#include "strbuf.h"

struct requested_info {
unsigned size : 1;
};

/*
* Parses oids from the given line and collects them in the given
* oid_str_list. Returns 1 if parsing was successful and 0 otherwise.
*/
static int parse_oid(const char *line, struct string_list *oid_str_list)
{
const char *arg;

if (!skip_prefix(line, "oid ", &arg))
return 0;

string_list_append(oid_str_list, arg);

return 1;
}

/*
* Validates and send requested info back to the client. Any errors detected
* are returned as they are detected.
*/
static void send_info(struct repository *r, struct packet_writer *writer,
struct string_list *oid_str_list,
struct requested_info *info)
{
struct string_list_item *item;
struct strbuf send_buffer = STRBUF_INIT;

if (!oid_str_list->nr)
return;

if (info->size)
packet_writer_write(writer, "size");

for_each_string_list_item (item, oid_str_list) {
const char *oid_str = item->string;
struct object_id oid;
unsigned long object_size;

if (get_oid_hex(oid_str, &oid) < 0) {
packet_writer_error(
writer,
"object-info: protocol error, expected to get oid, not '%s'",
oid_str);
continue;
}

strbuf_addstr(&send_buffer, oid_str);

if (info->size) {
if (oid_object_info(r, &oid, &object_size) < 0) {
strbuf_addstr(&send_buffer, " ");
} else {
strbuf_addf(&send_buffer, " %lu", object_size);
}
}

packet_writer_write(writer, "%s",
strbuf_detach(&send_buffer, NULL));
}
}

int cap_object_info(struct repository *r, struct strvec *keys,
struct packet_reader *request)
{
struct requested_info info;
struct packet_writer writer;
struct string_list oid_str_list = STRING_LIST_INIT_DUP;

packet_writer_init(&writer, 1);

while (packet_reader_read(request) == PACKET_READ_NORMAL) {
if (!strcmp("size", request->line)) {
info.size = 1;
continue;
}

if (parse_oid(request->line, &oid_str_list))
continue;

packet_writer_error(&writer,
"object-info: unexpected line: '%s'",
request->line);
}

if (request->status != PACKET_READ_FLUSH) {
packet_writer_error(
&writer, "object-info: expected flush after arguments");
die(_("object-info: expected flush after arguments"));
}

send_info(r, &writer, &oid_str_list, &info);

string_list_clear(&oid_str_list, 1);

packet_flush(1);

return 0;
}
10 changes: 10 additions & 0 deletions protocol-caps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef PROTOCOL_CAPS_H
#define PROTOCOL_CAPS_H

struct repository;
struct strvec;
struct packet_reader;
int cap_object_info(struct repository *r, struct strvec *keys,
struct packet_reader *request);

#endif /* PROTOCOL_CAPS_H */
2 changes: 2 additions & 0 deletions serve.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "version.h"
#include "strvec.h"
#include "ls-refs.h"
#include "protocol-caps.h"
#include "serve.h"
#include "upload-pack.h"

Expand Down Expand Up @@ -78,6 +79,7 @@ static struct protocol_capability capabilities[] = {
{ "server-option", always_advertise, NULL },
{ "object-format", object_format_advertise, NULL },
{ "session-id", session_id_advertise, NULL },
{ "object-info", always_advertise, cap_object_info },
};

static void advertise_capabilities(void)
Expand Down
26 changes: 26 additions & 0 deletions t/t5701-git-serve.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test_expect_success 'test capability advertisement' '
fetch=shallow
server-option
object-format=$(test_oid algo)
object-info
0000
EOF
Expand Down Expand Up @@ -240,4 +241,29 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
grep "unexpected line: .this-is-not-a-command." err
'

# Test the basics of object-info
#
test_expect_success 'basics of object-info' '
test-tool pkt-line pack >in <<-EOF &&
command=object-info
object-format=$(test_oid algo)
0001
size
oid $(git rev-parse two:two.t)
oid $(git rev-parse two:two.t)
0000
EOF
cat >expect <<-EOF &&
size
$(git rev-parse two:two.t) $(wc -c <two.t | xargs)
$(git rev-parse two:two.t) $(wc -c <two.t | xargs)
0000
EOF
test-tool serve-v2 --stateless-rpc <in >out &&
test-tool pkt-line unpack <out >actual &&
test_cmp expect actual
'

test_done

0 comments on commit eede711

Please sign in to comment.