Bruno Albuquerque | a2ba162 | 2021-04-20 16:38:31 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "protocol-caps.h" |
| 3 | #include "gettext.h" |
| 4 | #include "pkt-line.h" |
| 5 | #include "strvec.h" |
| 6 | #include "hash.h" |
| 7 | #include "object.h" |
| 8 | #include "object-store.h" |
| 9 | #include "string-list.h" |
| 10 | #include "strbuf.h" |
| 11 | |
| 12 | struct requested_info { |
| 13 | unsigned size : 1; |
| 14 | }; |
| 15 | |
| 16 | /* |
| 17 | * Parses oids from the given line and collects them in the given |
| 18 | * oid_str_list. Returns 1 if parsing was successful and 0 otherwise. |
| 19 | */ |
| 20 | static int parse_oid(const char *line, struct string_list *oid_str_list) |
| 21 | { |
| 22 | const char *arg; |
| 23 | |
| 24 | if (!skip_prefix(line, "oid ", &arg)) |
| 25 | return 0; |
| 26 | |
| 27 | string_list_append(oid_str_list, arg); |
| 28 | |
| 29 | return 1; |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Validates and send requested info back to the client. Any errors detected |
| 34 | * are returned as they are detected. |
| 35 | */ |
| 36 | static void send_info(struct repository *r, struct packet_writer *writer, |
| 37 | struct string_list *oid_str_list, |
| 38 | struct requested_info *info) |
| 39 | { |
| 40 | struct string_list_item *item; |
| 41 | struct strbuf send_buffer = STRBUF_INIT; |
| 42 | |
| 43 | if (!oid_str_list->nr) |
| 44 | return; |
| 45 | |
| 46 | if (info->size) |
| 47 | packet_writer_write(writer, "size"); |
| 48 | |
| 49 | for_each_string_list_item (item, oid_str_list) { |
| 50 | const char *oid_str = item->string; |
| 51 | struct object_id oid; |
| 52 | unsigned long object_size; |
| 53 | |
| 54 | if (get_oid_hex(oid_str, &oid) < 0) { |
| 55 | packet_writer_error( |
| 56 | writer, |
| 57 | "object-info: protocol error, expected to get oid, not '%s'", |
| 58 | oid_str); |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | strbuf_addstr(&send_buffer, oid_str); |
| 63 | |
| 64 | if (info->size) { |
| 65 | if (oid_object_info(r, &oid, &object_size) < 0) { |
| 66 | strbuf_addstr(&send_buffer, " "); |
| 67 | } else { |
| 68 | strbuf_addf(&send_buffer, " %lu", object_size); |
| 69 | } |
| 70 | } |
| 71 | |
Ævar Arnfjörð Bjarmason | 88682b0 | 2021-08-31 15:46:42 +0200 | [diff] [blame] | 72 | packet_writer_write(writer, "%s", send_buffer.buf); |
| 73 | strbuf_reset(&send_buffer); |
Bruno Albuquerque | a2ba162 | 2021-04-20 16:38:31 -0700 | [diff] [blame] | 74 | } |
Ævar Arnfjörð Bjarmason | 88682b0 | 2021-08-31 15:46:42 +0200 | [diff] [blame] | 75 | strbuf_release(&send_buffer); |
Bruno Albuquerque | a2ba162 | 2021-04-20 16:38:31 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ævar Arnfjörð Bjarmason | 28a592e | 2021-08-05 03:25:38 +0200 | [diff] [blame] | 78 | int cap_object_info(struct repository *r, struct packet_reader *request) |
Bruno Albuquerque | a2ba162 | 2021-04-20 16:38:31 -0700 | [diff] [blame] | 79 | { |
| 80 | struct requested_info info; |
| 81 | struct packet_writer writer; |
| 82 | struct string_list oid_str_list = STRING_LIST_INIT_DUP; |
| 83 | |
| 84 | packet_writer_init(&writer, 1); |
| 85 | |
| 86 | while (packet_reader_read(request) == PACKET_READ_NORMAL) { |
| 87 | if (!strcmp("size", request->line)) { |
| 88 | info.size = 1; |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (parse_oid(request->line, &oid_str_list)) |
| 93 | continue; |
| 94 | |
| 95 | packet_writer_error(&writer, |
| 96 | "object-info: unexpected line: '%s'", |
| 97 | request->line); |
| 98 | } |
| 99 | |
| 100 | if (request->status != PACKET_READ_FLUSH) { |
| 101 | packet_writer_error( |
| 102 | &writer, "object-info: expected flush after arguments"); |
| 103 | die(_("object-info: expected flush after arguments")); |
| 104 | } |
| 105 | |
| 106 | send_info(r, &writer, &oid_str_list, &info); |
| 107 | |
| 108 | string_list_clear(&oid_str_list, 1); |
| 109 | |
| 110 | packet_flush(1); |
| 111 | |
| 112 | return 0; |
| 113 | } |