blob: 75f4cbb0a70951a85c8e5a4dba853f027ee7cf4b [file] [log] [blame]
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -07001#include "git-compat-util.h"
2#include "protocol-caps.h"
3#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -07005#include "pkt-line.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00006#include "hash-ll.h"
Elijah Newren1c028402023-02-24 00:09:32 +00007#include "hex.h"
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -07008#include "object.h"
Elijah Newrena034e912023-05-16 06:34:06 +00009#include "object-store-ll.h"
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070010#include "string-list.h"
11#include "strbuf.h"
12
13struct requested_info {
14 unsigned size : 1;
15};
16
17/*
18 * Parses oids from the given line and collects them in the given
19 * oid_str_list. Returns 1 if parsing was successful and 0 otherwise.
20 */
21static int parse_oid(const char *line, struct string_list *oid_str_list)
22{
23 const char *arg;
24
25 if (!skip_prefix(line, "oid ", &arg))
26 return 0;
27
28 string_list_append(oid_str_list, arg);
29
30 return 1;
31}
32
33/*
34 * Validates and send requested info back to the client. Any errors detected
35 * are returned as they are detected.
36 */
37static void send_info(struct repository *r, struct packet_writer *writer,
38 struct string_list *oid_str_list,
39 struct requested_info *info)
40{
41 struct string_list_item *item;
42 struct strbuf send_buffer = STRBUF_INIT;
43
44 if (!oid_str_list->nr)
45 return;
46
47 if (info->size)
48 packet_writer_write(writer, "size");
49
50 for_each_string_list_item (item, oid_str_list) {
51 const char *oid_str = item->string;
52 struct object_id oid;
53 unsigned long object_size;
54
55 if (get_oid_hex(oid_str, &oid) < 0) {
56 packet_writer_error(
57 writer,
58 "object-info: protocol error, expected to get oid, not '%s'",
59 oid_str);
60 continue;
61 }
62
63 strbuf_addstr(&send_buffer, oid_str);
64
65 if (info->size) {
66 if (oid_object_info(r, &oid, &object_size) < 0) {
67 strbuf_addstr(&send_buffer, " ");
68 } else {
69 strbuf_addf(&send_buffer, " %lu", object_size);
70 }
71 }
72
Ævar Arnfjörð Bjarmason88682b02021-08-31 15:46:42 +020073 packet_writer_write(writer, "%s", send_buffer.buf);
74 strbuf_reset(&send_buffer);
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070075 }
Ævar Arnfjörð Bjarmason88682b02021-08-31 15:46:42 +020076 strbuf_release(&send_buffer);
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070077}
78
Ævar Arnfjörð Bjarmason28a592e2021-08-05 03:25:38 +020079int cap_object_info(struct repository *r, struct packet_reader *request)
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070080{
Jiang Xindc12ee72023-04-02 21:05:57 +080081 struct requested_info info = { 0 };
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070082 struct packet_writer writer;
83 struct string_list oid_str_list = STRING_LIST_INIT_DUP;
84
85 packet_writer_init(&writer, 1);
86
87 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
88 if (!strcmp("size", request->line)) {
89 info.size = 1;
90 continue;
91 }
92
93 if (parse_oid(request->line, &oid_str_list))
94 continue;
95
96 packet_writer_error(&writer,
97 "object-info: unexpected line: '%s'",
98 request->line);
99 }
100
101 if (request->status != PACKET_READ_FLUSH) {
102 packet_writer_error(
103 &writer, "object-info: expected flush after arguments");
104 die(_("object-info: expected flush after arguments"));
105 }
106
107 send_info(r, &writer, &oid_str_list, &info);
108
109 string_list_clear(&oid_str_list, 1);
110
111 packet_flush(1);
112
113 return 0;
114}