blob: 0052c6a4dce1fad808a5bdd2c92c0827c5453d18 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newren5579f442023-04-11 00:41:48 -07003#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00005#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00006#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00007#include "hex.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -07008#include "refs.h"
9#include "pkt-line.h"
Junio C Hamano958c24b2006-09-10 03:20:24 -070010#include "sideband.h"
Stefan Beller109cd762018-06-28 18:21:51 -070011#include "repository.h"
Elijah Newrena034e912023-05-16 06:34:06 +000012#include "object-store-ll.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000013#include "oid-array.h"
Junio C Hamanof6b42a82005-10-13 18:57:40 -070014#include "object.h"
Johannes Schindelinf0243f22005-10-28 04:48:32 +020015#include "commit.h"
Johannes Schindelin9b8dc262006-10-30 20:08:43 +010016#include "diff.h"
17#include "revision.h"
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000018#include "list-objects-filter-options.h"
Johannes Sixtcc41fa82007-10-19 21:47:59 +020019#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070020#include "connect.h"
Junio C Hamano051e4002011-08-05 13:54:06 -070021#include "sigchain.h"
Jeff Kingff5effd2012-08-03 12:19:16 -040022#include "version.h"
Junio C Hamanodaebaa72013-01-18 16:08:30 -080023#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040024#include "strvec.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000025#include "trace2.h"
Brandon Williamsaa9bab22017-10-16 10:55:26 -070026#include "protocol.h"
Brandon Williamsa3d6b532018-03-14 11:31:41 -070027#include "upload-pack.h"
Derrick Stolee829a3212018-08-20 18:24:34 +000028#include "commit-graph.h"
Derrick Stoleeba3ca1e2018-07-20 16:33:13 +000029#include "commit-reach.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060030#include "shallow.h"
Elijah Newrend48be352023-03-21 06:26:07 +000031#include "write-or-die.h"
Robert Coupb8f58c22023-10-17 21:12:47 +000032#include "json-writer.h"
Jeff Kingb0650632024-02-28 17:38:40 -050033#include "strmap.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -070034
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070035/* Remember to update object flag allocation in object.h */
Junio C Hamano937a5152006-07-05 21:28:20 -070036#define THEY_HAVE (1u << 11)
37#define OUR_REF (1u << 12)
38#define WANTED (1u << 13)
39#define COMMON_KNOWN (1u << 14)
Junio C Hamano937a5152006-07-05 21:28:20 -070040
Johannes Schindelinf53514b2006-10-30 20:09:53 +010041#define SHALLOW (1u << 16)
42#define NOT_SHALLOW (1u << 17)
43#define CLIENT_SHALLOW (1u << 18)
Junio C Hamano390eb362013-01-28 21:49:57 -080044#define HIDDEN_REF (1u << 19)
Johannes Schindelinf53514b2006-10-30 20:09:53 +010045
Jonathan Tand1035ca2018-10-18 13:43:29 -070046#define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
47 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
48
Christian Couder629060d2020-06-11 14:05:12 +020049/* Enum for allowed unadvertised object request (UOR) */
50enum allow_uor {
51 /* Allow specifying sha1 if it is a ref tip. */
52 ALLOW_TIP_SHA1 = 0x01,
53 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
54 ALLOW_REACHABLE_SHA1 = 0x02,
55 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
56 ALLOW_ANY_SHA1 = 0x07
57};
H. Peter Anvin960decc2005-10-19 14:27:01 -070058
Christian Coudera8497282020-06-04 19:54:39 +020059/*
60 * Please annotate, and if possible group together, fields used only
61 * for protocol v0 or only for protocol v2.
62 */
Christian Coudere8498322020-05-15 12:04:44 +020063struct upload_pack_data {
Christian Coudera8497282020-06-04 19:54:39 +020064 struct string_list symref; /* v0 only */
Christian Coudere8498322020-05-15 12:04:44 +020065 struct object_array want_obj;
66 struct object_array have_obj;
Jeff Kingb0650632024-02-28 17:38:40 -050067 struct strmap wanted_refs; /* v2 only */
Taylor Blauc45841f2023-07-10 17:12:33 -040068 struct strvec hidden_refs;
Christian Coudere8498322020-05-15 12:04:44 +020069
70 struct object_array shallows;
Jeff King388b96d2024-02-28 17:37:44 -050071 struct oidset deepen_not;
Christian Couderde0e9f72020-06-11 14:05:10 +020072 struct object_array extra_edge_obj;
Christian Coudere8498322020-05-15 12:04:44 +020073 int depth;
74 timestamp_t deepen_since;
75 int deepen_rev_list;
76 int deepen_relative;
Christian Couderf203a882020-06-04 19:54:46 +020077 int keepalive;
Christian Couder35b43a12020-06-11 14:05:09 +020078 int shallow_nr;
Christian Couderf01c7912020-06-11 14:05:17 +020079 timestamp_t oldest_have;
Christian Coudere8498322020-05-15 12:04:44 +020080
Christian Couderd40f04e2020-06-04 19:54:40 +020081 unsigned int timeout; /* v0 only */
Christian Coudere9d882b2020-06-04 19:54:44 +020082 enum {
83 NO_MULTI_ACK = 0,
84 MULTI_ACK = 1,
85 MULTI_ACK_DETAILED = 2
86 } multi_ack; /* v0 only */
Christian Couderd40f04e2020-06-04 19:54:40 +020087
Christian Couderf8edd1c2020-06-04 19:54:41 +020088 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
89 int use_sideband;
90
Jonathan Tandd4b7322020-06-10 13:57:23 -070091 struct string_list uri_protocols;
Christian Couder629060d2020-06-11 14:05:12 +020092 enum allow_uor allow_uor;
Christian Couderf1514c62020-06-11 14:05:11 +020093
Christian Coudere8498322020-05-15 12:04:44 +020094 struct list_objects_filter_options filter_options;
Taylor Blau6dd34562020-08-03 14:00:10 -040095 struct string_list allowed_filters;
Christian Coudere8498322020-05-15 12:04:44 +020096
97 struct packet_writer writer;
98
Patrick Steinhardt1b261c22024-05-27 13:46:39 +020099 char *pack_objects_hook;
Christian Couder339a9842020-06-04 19:54:50 +0200100
Christian Coudera8497282020-06-04 19:54:39 +0200101 unsigned stateless_rpc : 1; /* v0 only */
Christian Couderd40f04e2020-06-04 19:54:40 +0200102 unsigned no_done : 1; /* v0 only */
103 unsigned daemon_mode : 1; /* v0 only */
Christian Couder59a90262020-06-04 19:54:42 +0200104 unsigned filter_capability_requested : 1; /* v0 only */
Christian Coudere8498322020-05-15 12:04:44 +0200105
106 unsigned use_thin_pack : 1;
107 unsigned use_ofs_delta : 1;
108 unsigned no_progress : 1;
109 unsigned use_include_tag : 1;
Jonathan Tan9c1e6572021-05-04 14:16:01 -0700110 unsigned wait_for_done : 1;
Christian Couder59abe192020-06-04 19:54:47 +0200111 unsigned allow_filter : 1;
Taylor Blau6dd34562020-08-03 14:00:10 -0400112 unsigned allow_filter_fallback : 1;
Taylor Blau5b01a4e2020-08-03 14:00:17 -0400113 unsigned long tree_filter_max_depth;
Christian Coudera8497282020-06-04 19:54:39 +0200114
115 unsigned done : 1; /* v2 only */
Christian Couderd1d7a942020-06-04 19:54:48 +0200116 unsigned allow_ref_in_want : 1; /* v2 only */
Christian Coudere3835cd2020-06-04 19:54:49 +0200117 unsigned allow_sideband_all : 1; /* v2 only */
Jeff Kingfae96272024-02-28 17:37:13 -0500118 unsigned seen_haves : 1; /* v2 only */
Jeff Kinga922bfa2024-02-28 17:50:50 -0500119 unsigned allow_packfile_uris : 1; /* v2 only */
Josh Steadmon791e1ad2020-11-11 15:29:27 -0800120 unsigned advertise_sid : 1;
brian m. carlson933e3a42023-05-17 19:24:43 +0000121 unsigned sent_capabilities : 1;
Christian Coudere8498322020-05-15 12:04:44 +0200122};
123
124static void upload_pack_data_init(struct upload_pack_data *data)
125{
Christian Couder438528f2020-05-15 12:04:49 +0200126 struct string_list symref = STRING_LIST_INIT_DUP;
Jeff Kingb0650632024-02-28 17:38:40 -0500127 struct strmap wanted_refs = STRMAP_INIT;
Taylor Blauc45841f2023-07-10 17:12:33 -0400128 struct strvec hidden_refs = STRVEC_INIT;
Christian Coudere8498322020-05-15 12:04:44 +0200129 struct object_array want_obj = OBJECT_ARRAY_INIT;
130 struct object_array have_obj = OBJECT_ARRAY_INIT;
Christian Coudere8498322020-05-15 12:04:44 +0200131 struct object_array shallows = OBJECT_ARRAY_INIT;
Jeff King388b96d2024-02-28 17:37:44 -0500132 struct oidset deepen_not = OID_ARRAY_INIT;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700133 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Christian Couderde0e9f72020-06-11 14:05:10 +0200134 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
Taylor Blau6dd34562020-08-03 14:00:10 -0400135 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
Christian Coudere8498322020-05-15 12:04:44 +0200136
137 memset(data, 0, sizeof(*data));
Christian Couder438528f2020-05-15 12:04:49 +0200138 data->symref = symref;
Christian Coudere8498322020-05-15 12:04:44 +0200139 data->wanted_refs = wanted_refs;
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +0100140 data->hidden_refs = hidden_refs;
Christian Coudere8498322020-05-15 12:04:44 +0200141 data->want_obj = want_obj;
142 data->have_obj = have_obj;
Christian Coudere8498322020-05-15 12:04:44 +0200143 data->shallows = shallows;
144 data->deepen_not = deepen_not;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700145 data->uri_protocols = uri_protocols;
Christian Couderde0e9f72020-06-11 14:05:10 +0200146 data->extra_edge_obj = extra_edge_obj;
Taylor Blau6dd34562020-08-03 14:00:10 -0400147 data->allowed_filters = allowed_filters;
148 data->allow_filter_fallback = 1;
Taylor Blau5b01a4e2020-08-03 14:00:17 -0400149 data->tree_filter_max_depth = ULONG_MAX;
Christian Coudere8498322020-05-15 12:04:44 +0200150 packet_writer_init(&data->writer, 1);
Jeff King2a01bde2022-09-11 01:03:07 -0400151 list_objects_filter_init(&data->filter_options);
Christian Couderf203a882020-06-04 19:54:46 +0200152
153 data->keepalive = 5;
Josh Steadmon791e1ad2020-11-11 15:29:27 -0800154 data->advertise_sid = 0;
Christian Coudere8498322020-05-15 12:04:44 +0200155}
156
157static void upload_pack_data_clear(struct upload_pack_data *data)
158{
Christian Couder438528f2020-05-15 12:04:49 +0200159 string_list_clear(&data->symref, 1);
Jeff Kingb0650632024-02-28 17:38:40 -0500160 strmap_clear(&data->wanted_refs, 1);
Taylor Blauc45841f2023-07-10 17:12:33 -0400161 strvec_clear(&data->hidden_refs);
Christian Coudere8498322020-05-15 12:04:44 +0200162 object_array_clear(&data->want_obj);
163 object_array_clear(&data->have_obj);
Christian Coudere8498322020-05-15 12:04:44 +0200164 object_array_clear(&data->shallows);
Jeff King388b96d2024-02-28 17:37:44 -0500165 oidset_clear(&data->deepen_not);
Christian Couderde0e9f72020-06-11 14:05:10 +0200166 object_array_clear(&data->extra_edge_obj);
Christian Coudere8498322020-05-15 12:04:44 +0200167 list_objects_filter_release(&data->filter_options);
Taylor Blau8d133f52020-12-03 13:55:18 -0500168 string_list_clear(&data->allowed_filters, 0);
Christian Couder339a9842020-06-04 19:54:50 +0200169
170 free((char *)data->pack_objects_hook);
Christian Coudere8498322020-05-15 12:04:44 +0200171}
172
Christian Couderd40f04e2020-06-04 19:54:40 +0200173static void reset_timeout(unsigned int timeout)
H. Peter Anvin960decc2005-10-19 14:27:01 -0700174{
175 alarm(timeout);
176}
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700177
Christian Couderf8edd1c2020-06-04 19:54:41 +0200178static void send_client_data(int fd, const char *data, ssize_t sz,
179 int use_sideband)
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700180{
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +0200181 if (use_sideband) {
182 send_sideband(1, fd, data, sz, use_sideband);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200183 return;
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +0200184 }
Junio C Hamano958c24b2006-09-10 03:20:24 -0700185 if (fd == 3)
186 /* emergency quit */
187 fd = 2;
188 if (fd == 2) {
Andy Whitcroft93822c22007-01-08 15:58:23 +0000189 /* XXX: are we happy to lose stuff here? */
Junio C Hamano958c24b2006-09-10 03:20:24 -0700190 xwrite(fd, data, sz);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200191 return;
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700192 }
Jeff Kingcdf4fb82013-02-20 15:01:56 -0500193 write_or_die(fd, data, sz);
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700194}
195
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700196static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
197{
198 FILE *fp = cb_data;
199 if (graft->nr_parent == -1)
brian m. carlson7683e2e2015-03-13 23:39:34 +0000200 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700201 return 0;
202}
203
Jonathan Tanacaaca72020-06-10 13:57:21 -0700204struct output_state {
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100205 /*
206 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
207 * sideband-64k the band designator takes up 1 byte of space. Because
208 * relay_pack_data keeps the last byte to itself, we make the buffer 1
209 * byte bigger than the intended maximum write size.
210 */
211 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
Jonathan Tanacaaca72020-06-10 13:57:21 -0700212 int used;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700213 unsigned packfile_uris_started : 1;
214 unsigned packfile_started : 1;
Jonathan Tanacaaca72020-06-10 13:57:21 -0700215};
216
217static int relay_pack_data(int pack_objects_out, struct output_state *os,
Jonathan Tandd4b7322020-06-10 13:57:23 -0700218 int use_sideband, int write_packfile_line)
Jonathan Tanacaaca72020-06-10 13:57:21 -0700219{
220 /*
221 * We keep the last byte to ourselves
222 * in case we detect broken rev-list, so that we
223 * can leave the stream corrupted. This is
224 * unfortunate -- unpack-objects would happily
225 * accept a valid packdata with trailing garbage,
226 * so appending garbage after we pass all the
227 * pack data is not good enough to signal
228 * breakage to downstream.
229 */
230 ssize_t readsz;
231
232 readsz = xread(pack_objects_out, os->buffer + os->used,
233 sizeof(os->buffer) - os->used);
234 if (readsz < 0) {
235 return readsz;
236 }
237 os->used += readsz;
238
Jonathan Tandd4b7322020-06-10 13:57:23 -0700239 while (!os->packfile_started) {
240 char *p;
241 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
242 os->packfile_started = 1;
243 if (write_packfile_line) {
244 if (os->packfile_uris_started)
245 packet_delim(1);
246 packet_write_fmt(1, "\1packfile\n");
247 }
248 break;
249 }
250 if ((p = memchr(os->buffer, '\n', os->used))) {
251 if (!os->packfile_uris_started) {
252 os->packfile_uris_started = 1;
253 if (!write_packfile_line)
254 BUG("packfile_uris requires sideband-all");
255 packet_write_fmt(1, "\1packfile-uris\n");
256 }
257 *p = '\0';
258 packet_write_fmt(1, "\1%s\n", os->buffer);
259
260 os->used -= p - os->buffer + 1;
261 memmove(os->buffer, p + 1, os->used);
262 } else {
263 /*
264 * Incomplete line.
265 */
266 return readsz;
267 }
268 }
269
Jonathan Tanacaaca72020-06-10 13:57:21 -0700270 if (os->used > 1) {
271 send_client_data(1, os->buffer, os->used - 1, use_sideband);
272 os->buffer[0] = os->buffer[os->used - 1];
273 os->used = 1;
274 } else {
275 send_client_data(1, os->buffer, os->used, use_sideband);
276 os->used = 0;
277 }
278
279 return readsz;
280}
281
Jonathan Tandd4b7322020-06-10 13:57:23 -0700282static void create_pack_file(struct upload_pack_data *pack_data,
283 const struct string_list *uri_protocols)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700284{
René Scharfed3180272014-08-19 21:09:35 +0200285 struct child_process pack_objects = CHILD_PROCESS_INIT;
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100286 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
Jonathan Tanacaaca72020-06-10 13:57:21 -0700287 char progress[128];
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700288 char abort_msg[] = "aborting due to possible repository "
289 "corruption on the remote side.";
Junio C Hamano1456b042009-12-10 12:17:11 -0800290 ssize_t sz;
Michael Procter65a36292016-02-25 12:13:26 +0000291 int i;
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700292 FILE *pipe_fd;
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700293
Christian Couder339a9842020-06-04 19:54:50 +0200294 if (!pack_data->pack_objects_hook)
Jeff King20b20a22016-05-18 18:45:37 -0400295 pack_objects.git_cmd = 1;
296 else {
Jeff Kingc972bf42020-07-28 16:25:12 -0400297 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
298 strvec_push(&pack_objects.args, "git");
Jeff King20b20a22016-05-18 18:45:37 -0400299 pack_objects.use_shell = 1;
300 }
301
Christian Couder35b43a12020-06-11 14:05:09 +0200302 if (pack_data->shallow_nr) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400303 strvec_push(&pack_objects.args, "--shallow-file");
304 strvec_push(&pack_objects.args, "");
Nick Edelenf0cea832009-06-10 01:50:18 +0200305 }
Jeff Kingc972bf42020-07-28 16:25:12 -0400306 strvec_push(&pack_objects.args, "pack-objects");
307 strvec_push(&pack_objects.args, "--revs");
Jeff Kingb5a20682020-06-04 19:54:38 +0200308 if (pack_data->use_thin_pack)
Jeff Kingc972bf42020-07-28 16:25:12 -0400309 strvec_push(&pack_objects.args, "--thin");
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700310
Jeff Kingc972bf42020-07-28 16:25:12 -0400311 strvec_push(&pack_objects.args, "--stdout");
Christian Couder35b43a12020-06-11 14:05:09 +0200312 if (pack_data->shallow_nr)
Jeff Kingc972bf42020-07-28 16:25:12 -0400313 strvec_push(&pack_objects.args, "--shallow");
Jeff Kingb5a20682020-06-04 19:54:38 +0200314 if (!pack_data->no_progress)
Jeff Kingc972bf42020-07-28 16:25:12 -0400315 strvec_push(&pack_objects.args, "--progress");
Jeff Kingb5a20682020-06-04 19:54:38 +0200316 if (pack_data->use_ofs_delta)
Jeff Kingc972bf42020-07-28 16:25:12 -0400317 strvec_push(&pack_objects.args, "--delta-base-offset");
Jeff Kingb5a20682020-06-04 19:54:38 +0200318 if (pack_data->use_include_tag)
Jeff Kingc972bf42020-07-28 16:25:12 -0400319 strvec_push(&pack_objects.args, "--include-tag");
Christian Couderc9f03252020-05-15 12:04:53 +0200320 if (pack_data->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700321 const char *spec =
Christian Couderc9f03252020-05-15 12:04:53 +0200322 expand_list_objects_filter_spec(&pack_data->filter_options);
Jacob Vosmaerad5df6b2021-01-28 17:04:53 +0100323 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000324 }
Jonathan Tandd4b7322020-06-10 13:57:23 -0700325 if (uri_protocols) {
326 for (i = 0; i < uri_protocols->nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -0400327 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
Jonathan Tandd4b7322020-06-10 13:57:23 -0700328 uri_protocols->items[i].string);
329 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200330
Jeff Kingb9612192011-04-06 17:33:33 -0400331 pack_objects.in = -1;
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200332 pack_objects.out = -1;
333 pack_objects.err = -1;
Jeff King309a4022020-12-01 07:15:13 -0500334 pack_objects.clean_on_exit = 1;
Johannes Sixt21edd3f2007-10-19 21:48:03 +0200335
Johannes Sixt4c324c02007-11-04 20:46:48 +0100336 if (start_command(&pack_objects))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700337 die("git upload-pack: unable to fork git-pack-objects");
Johannes Schindelin83a5ad62007-02-20 03:01:44 +0100338
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700339 pipe_fd = xfdopen(pack_objects.in, "w");
Nick Edelenf0cea832009-06-10 01:50:18 +0200340
Christian Couder35b43a12020-06-11 14:05:09 +0200341 if (pack_data->shallow_nr)
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700342 for_each_commit_graft(write_one_shallow, pipe_fd);
343
Christian Couderc9f03252020-05-15 12:04:53 +0200344 for (i = 0; i < pack_data->want_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700345 fprintf(pipe_fd, "%s\n",
Christian Couderc9f03252020-05-15 12:04:53 +0200346 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700347 fprintf(pipe_fd, "--not\n");
Christian Couderc9f03252020-05-15 12:04:53 +0200348 for (i = 0; i < pack_data->have_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700349 fprintf(pipe_fd, "%s\n",
Christian Couderc9f03252020-05-15 12:04:53 +0200350 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
Christian Couderde0e9f72020-06-11 14:05:10 +0200351 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700352 fprintf(pipe_fd, "%s\n",
Christian Couderde0e9f72020-06-11 14:05:10 +0200353 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700354 fprintf(pipe_fd, "\n");
355 fflush(pipe_fd);
356 fclose(pipe_fd);
Nick Edelenf0cea832009-06-10 01:50:18 +0200357
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200358 /* We read from pack_objects.err to capture stderr output for
359 * progress bar, and pack_objects.out to capture the pack data.
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700360 */
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700361
362 while (1) {
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700363 struct pollfd pfd[2];
Christian Couderf203a882020-06-04 19:54:46 +0200364 int pe, pu, pollsize, polltimeout;
Jeff King05e95152013-09-08 05:01:31 -0400365 int ret;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700366
Christian Couderd40f04e2020-06-04 19:54:40 +0200367 reset_timeout(pack_data->timeout);
Matthias Lederhofer0d516ad2006-07-18 19:14:51 +0200368
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700369 pollsize = 0;
Junio C Hamano363b7812006-06-20 22:48:23 -0700370 pe = pu = -1;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700371
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200372 if (0 <= pack_objects.out) {
373 pfd[pollsize].fd = pack_objects.out;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700374 pfd[pollsize].events = POLLIN;
375 pu = pollsize;
376 pollsize++;
377 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200378 if (0 <= pack_objects.err) {
379 pfd[pollsize].fd = pack_objects.err;
Junio C Hamano363b7812006-06-20 22:48:23 -0700380 pfd[pollsize].events = POLLIN;
381 pe = pollsize;
382 pollsize++;
383 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700384
Johannes Sixt4c324c02007-11-04 20:46:48 +0100385 if (!pollsize)
386 break;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700387
Christian Couderf203a882020-06-04 19:54:46 +0200388 polltimeout = pack_data->keepalive < 0
389 ? -1
390 : 1000 * pack_data->keepalive;
391
392 ret = poll(pfd, pollsize, polltimeout);
Edward Thomson6c71f8b2014-08-22 15:19:11 +0000393
Jeff King05e95152013-09-08 05:01:31 -0400394 if (ret < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100395 if (errno != EINTR) {
Nguyễn Thái Ngọc Duyd2b6afa2016-05-08 16:47:59 +0700396 error_errno("poll failed, resuming");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100397 sleep(1);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700398 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700399 continue;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100400 }
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500401 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
402 /* Status ready; we ship that in the side-band
403 * or dump to the standard error.
404 */
405 sz = xread(pack_objects.err, progress,
406 sizeof(progress));
407 if (0 < sz)
Christian Couderf8edd1c2020-06-04 19:54:41 +0200408 send_client_data(2, progress, sz,
409 pack_data->use_sideband);
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500410 else if (sz == 0) {
411 close(pack_objects.err);
412 pack_objects.err = -1;
413 }
414 else
415 goto fail;
416 /* give priority to status messages */
417 continue;
418 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100419 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
Jonathan Tanacaaca72020-06-10 13:57:21 -0700420 int result = relay_pack_data(pack_objects.out,
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100421 output_state,
Jonathan Tandd4b7322020-06-10 13:57:23 -0700422 pack_data->use_sideband,
423 !!uri_protocols);
Jonathan Tanacaaca72020-06-10 13:57:21 -0700424
425 if (result == 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100426 close(pack_objects.out);
427 pack_objects.out = -1;
Jonathan Tanacaaca72020-06-10 13:57:21 -0700428 } else if (result < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100429 goto fail;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100430 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700431 }
Jeff King05e95152013-09-08 05:01:31 -0400432
433 /*
434 * We hit the keepalive timeout without saying anything; send
435 * an empty message on the data sideband just to let the other
436 * side know we're still working on it, but don't have any data
437 * yet.
438 *
439 * If we don't have a sideband channel, there's no room in the
440 * protocol to say anything, so those clients are just out of
441 * luck.
442 */
Christian Couderf8edd1c2020-06-04 19:54:41 +0200443 if (!ret && pack_data->use_sideband) {
Jeff King05e95152013-09-08 05:01:31 -0400444 static const char buf[] = "0005\1";
445 write_or_die(1, buf, 5);
446 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700447 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100448
449 if (finish_command(&pack_objects)) {
Junio C Hamano7e44c932008-08-31 09:39:19 -0700450 error("git upload-pack: git-pack-objects died with error.");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100451 goto fail;
452 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100453
454 /* flush the data */
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100455 if (output_state->used > 0) {
456 send_client_data(1, output_state->buffer, output_state->used,
Christian Couderf8edd1c2020-06-04 19:54:41 +0200457 pack_data->use_sideband);
Johannes Sixt4c324c02007-11-04 20:46:48 +0100458 fprintf(stderr, "flushed.\n");
459 }
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100460 free(output_state);
Christian Couderf8edd1c2020-06-04 19:54:41 +0200461 if (pack_data->use_sideband)
Johannes Sixt4c324c02007-11-04 20:46:48 +0100462 packet_flush(1);
463 return;
464
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700465 fail:
Ævar Arnfjörð Bjarmasonc68d5db2022-07-28 01:13:42 +0200466 free(output_state);
SZEDER Gábor3f4c7a02024-02-25 19:34:52 +0100467 send_client_data(3, abort_msg, strlen(abort_msg),
Christian Couderf8edd1c2020-06-04 19:54:41 +0200468 pack_data->use_sideband);
Junio C Hamano7e44c932008-08-31 09:39:19 -0700469 die("git upload-pack: %s", abort_msg);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700470}
471
Christian Couderea2c6e62020-06-11 14:05:18 +0200472static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700473{
Junio C Hamano937a5152006-07-05 21:28:20 -0700474 int we_knew_they_have = 0;
Jeff Kinga6ca6012024-02-28 17:39:03 -0500475 struct object *o = parse_object_with_flags(the_repository, oid,
Jeff King6cd05e72024-02-28 17:39:07 -0500476 PARSE_OBJECT_SKIP_HASH_CHECK |
477 PARSE_OBJECT_DISCARD_TREE);
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700478
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700479 if (!o)
brian m. carlsoncf939822017-05-06 22:10:28 +0000480 die("oops (%s)", oid_to_hex(oid));
Junio C Hamano182a8da2006-08-12 22:16:51 -0700481 if (o->type == OBJ_COMMIT) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700482 struct commit_list *parents;
Junio C Hamano937a5152006-07-05 21:28:20 -0700483 struct commit *commit = (struct commit *)o;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700484 if (o->flags & THEY_HAVE)
Junio C Hamano937a5152006-07-05 21:28:20 -0700485 we_knew_they_have = 1;
486 else
487 o->flags |= THEY_HAVE;
Christian Couderf01c7912020-06-11 14:05:17 +0200488 if (!data->oldest_have || (commit->date < data->oldest_have))
489 data->oldest_have = commit->date;
Junio C Hamano937a5152006-07-05 21:28:20 -0700490 for (parents = commit->parents;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700491 parents;
492 parents = parents->next)
493 parents->item->object.flags |= THEY_HAVE;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700494 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700495 if (!we_knew_they_have) {
Christian Couder460ed0d2020-06-11 14:05:16 +0200496 add_object_array(o, NULL, &data->have_obj);
Junio C Hamano937a5152006-07-05 21:28:20 -0700497 return 1;
498 }
499 return 0;
500}
501
Christian Couderea2c6e62020-06-11 14:05:18 +0200502static int got_oid(struct upload_pack_data *data,
503 const char *hex, struct object_id *oid)
504{
505 if (get_oid_hex(hex, oid))
506 die("git upload-pack: expected SHA1 object, got '%s'", hex);
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200507 if (!repo_has_object_file_with_flags(the_repository, oid,
508 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
Christian Couderea2c6e62020-06-11 14:05:18 +0200509 return -1;
510 return do_got_oid(data, oid);
511}
512
Christian Couder08667342020-06-11 14:05:15 +0200513static int ok_to_give_up(struct upload_pack_data *data)
Junio C Hamano937a5152006-07-05 21:28:20 -0700514{
Abhishek Kumard7f92782021-01-16 18:11:13 +0000515 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
Junio C Hamano937a5152006-07-05 21:28:20 -0700516
Christian Couder08667342020-06-11 14:05:15 +0200517 if (!data->have_obj.nr)
Junio C Hamano937a5152006-07-05 21:28:20 -0700518 return 0;
519
Christian Couder08667342020-06-11 14:05:15 +0200520 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
Christian Couderf01c7912020-06-11 14:05:17 +0200521 COMMON_KNOWN, data->oldest_have,
Derrick Stolee4fbcca42018-07-20 16:33:28 +0000522 min_generation);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700523}
524
Christian Couder07977692020-05-15 12:04:46 +0200525static int get_common_commits(struct upload_pack_data *data,
526 struct packet_reader *reader)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700527{
brian m. carlsoncf939822017-05-06 22:10:28 +0000528 struct object_id oid;
529 char last_hex[GIT_MAX_HEXSZ + 1];
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700530 int got_common = 0;
531 int got_other = 0;
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700532 int sent_ready = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700533
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400534 for (;;) {
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700535 const char *arg;
536
Christian Couderd40f04e2020-06-04 19:54:40 +0200537 reset_timeout(data->timeout);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700538
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800539 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
Christian Coudere9d882b2020-06-04 19:54:44 +0200540 if (data->multi_ack == MULTI_ACK_DETAILED
Christian Couder07977692020-05-15 12:04:46 +0200541 && got_common
542 && !got_other
Christian Couder08667342020-06-11 14:05:15 +0200543 && ok_to_give_up(data)) {
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700544 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700545 packet_write_fmt(1, "ACK %s ready\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700546 }
Christian Couder53d69502020-06-04 19:54:43 +0200547 if (data->have_obj.nr == 0 || data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700548 packet_write_fmt(1, "NAK\n");
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700549
Christian Couderd40f04e2020-06-04 19:54:40 +0200550 if (data->no_done && sent_ready) {
Lars Schneider81c634e2016-10-16 16:20:29 -0700551 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700552 return 0;
553 }
Christian Couderdf654ab2020-05-15 12:04:52 +0200554 if (data->stateless_rpc)
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700555 exit(0);
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700556 got_common = 0;
557 got_other = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700558 continue;
559 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800560 if (skip_prefix(reader->line, "have ", &arg)) {
Christian Couder460ed0d2020-06-11 14:05:16 +0200561 switch (got_oid(data, arg, &oid)) {
Junio C Hamano937a5152006-07-05 21:28:20 -0700562 case -1: /* they have what we do not */
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700563 got_other = 1;
Christian Couder53d69502020-06-04 19:54:43 +0200564 if (data->multi_ack
Christian Couder08667342020-06-11 14:05:15 +0200565 && ok_to_give_up(data)) {
brian m. carlsoncf939822017-05-06 22:10:28 +0000566 const char *hex = oid_to_hex(&oid);
Christian Coudere9d882b2020-06-04 19:54:44 +0200567 if (data->multi_ack == MULTI_ACK_DETAILED) {
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700568 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700569 packet_write_fmt(1, "ACK %s ready\n", hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700570 } else
Lars Schneider81c634e2016-10-16 16:20:29 -0700571 packet_write_fmt(1, "ACK %s continue\n", hex);
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700572 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700573 break;
574 default:
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700575 got_common = 1;
brian m. carlson55dc2272018-05-02 00:25:51 +0000576 oid_to_hex_r(last_hex, &oid);
Christian Coudere9d882b2020-06-04 19:54:44 +0200577 if (data->multi_ack == MULTI_ACK_DETAILED)
Lars Schneider81c634e2016-10-16 16:20:29 -0700578 packet_write_fmt(1, "ACK %s common\n", last_hex);
Christian Couder53d69502020-06-04 19:54:43 +0200579 else if (data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700580 packet_write_fmt(1, "ACK %s continue\n", last_hex);
Christian Couder07977692020-05-15 12:04:46 +0200581 else if (data->have_obj.nr == 1)
Lars Schneider81c634e2016-10-16 16:20:29 -0700582 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano937a5152006-07-05 21:28:20 -0700583 break;
Junio C Hamanoaf2d3aa2005-10-25 14:55:24 -0700584 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700585 continue;
586 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800587 if (!strcmp(reader->line, "done")) {
Christian Couder07977692020-05-15 12:04:46 +0200588 if (data->have_obj.nr > 0) {
Christian Couder53d69502020-06-04 19:54:43 +0200589 if (data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700590 packet_write_fmt(1, "ACK %s\n", last_hex);
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200591 return 0;
592 }
Lars Schneider81c634e2016-10-16 16:20:29 -0700593 packet_write_fmt(1, "NAK\n");
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700594 return -1;
595 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800596 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700597 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700598}
599
Taylor Blau18b6b1b2023-07-10 17:12:45 -0400600static int allow_hidden_refs(enum allow_uor allow_uor)
601{
602 if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1)
603 return 1;
604 return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
605}
606
607static void for_each_namespaced_ref_1(each_ref_fn fn,
608 struct upload_pack_data *data)
609{
610 const char **excludes = NULL;
611 /*
612 * If `data->allow_uor` allows fetching hidden refs, we need to
613 * mark all references (including hidden ones), to check in
614 * `is_our_ref()` below.
615 *
616 * Otherwise, we only care about whether each reference's object
617 * has the OUR_REF bit set or not, so do not need to visit
618 * hidden references.
619 */
620 if (allow_hidden_refs(data->allow_uor))
621 excludes = hidden_refs_to_excludes(&data->hidden_refs);
622
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200623 refs_for_each_namespaced_ref(get_main_ref_store(the_repository),
624 excludes, fn, data);
Taylor Blau18b6b1b2023-07-10 17:12:45 -0400625}
626
627
Christian Couder629060d2020-06-11 14:05:12 +0200628static int is_our_ref(struct object *o, enum allow_uor allow_uor)
Junio C Hamano390eb362013-01-28 21:49:57 -0800629{
Taylor Blau18b6b1b2023-07-10 17:12:45 -0400630 return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF);
Junio C Hamano390eb362013-01-28 21:49:57 -0800631}
632
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700633/*
634 * on successful case, it's up to the caller to close cmd->out
635 */
636static int do_reachable_revlist(struct child_process *cmd,
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700637 struct object_array *src,
Christian Couderf1514c62020-06-11 14:05:11 +0200638 struct object_array *reachable,
Christian Couder629060d2020-06-11 14:05:12 +0200639 enum allow_uor allow_uor)
Junio C Hamano051e4002011-08-05 13:54:06 -0700640{
Junio C Hamano051e4002011-08-05 13:54:06 -0700641 struct object *o;
René Scharfea698d672020-08-12 18:52:55 +0200642 FILE *cmd_in = NULL;
Junio C Hamano051e4002011-08-05 13:54:06 -0700643 int i;
644
Ævar Arnfjörð Bjarmason2b709892021-11-25 23:52:20 +0100645 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700646 cmd->git_cmd = 1;
647 cmd->no_stderr = 1;
648 cmd->in = -1;
649 cmd->out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700650
651 /*
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700652 * If the next rev-list --stdin encounters an unknown commit,
653 * it terminates, which will cause SIGPIPE in the write loop
Junio C Hamano051e4002011-08-05 13:54:06 -0700654 * below.
655 */
656 sigchain_push(SIGPIPE, SIG_IGN);
657
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700658 if (start_command(cmd))
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700659 goto error;
660
René Scharfea698d672020-08-12 18:52:55 +0200661 cmd_in = xfdopen(cmd->in, "w");
662
Junio C Hamano051e4002011-08-05 13:54:06 -0700663 for (i = get_max_object_index(); 0 < i; ) {
664 o = get_indexed_object(--i);
Brian Harring2a745322011-08-23 22:47:17 -0700665 if (!o)
666 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700667 if (reachable && o->type == OBJ_COMMIT)
668 o->flags &= ~TMP_MARK;
Christian Couder629060d2020-06-11 14:05:12 +0200669 if (!is_our_ref(o, allow_uor))
Junio C Hamano051e4002011-08-05 13:54:06 -0700670 continue;
René Scharfea698d672020-08-12 18:52:55 +0200671 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700672 goto error;
673 }
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700674 for (i = 0; i < src->nr; i++) {
675 o = src->objects[i].item;
Christian Couder629060d2020-06-11 14:05:12 +0200676 if (is_our_ref(o, allow_uor)) {
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700677 if (reachable)
678 add_object_array(o, NULL, reachable);
Junio C Hamano051e4002011-08-05 13:54:06 -0700679 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700680 }
681 if (reachable && o->type == OBJ_COMMIT)
682 o->flags |= TMP_MARK;
René Scharfea698d672020-08-12 18:52:55 +0200683 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700684 goto error;
685 }
René Scharfea698d672020-08-12 18:52:55 +0200686 if (ferror(cmd_in) || fflush(cmd_in))
687 goto error;
688 fclose(cmd_in);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700689 cmd->in = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700690 sigchain_pop(SIGPIPE);
691
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700692 return 0;
693
694error:
695 sigchain_pop(SIGPIPE);
696
René Scharfea698d672020-08-12 18:52:55 +0200697 if (cmd_in)
698 fclose(cmd_in);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700699 if (cmd->out >= 0)
700 close(cmd->out);
701 return -1;
702}
703
Christian Couderf1514c62020-06-11 14:05:11 +0200704static int get_reachable_list(struct upload_pack_data *data,
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700705 struct object_array *reachable)
706{
707 struct child_process cmd = CHILD_PROCESS_INIT;
708 int i;
709 struct object *o;
brian m. carlson55dc2272018-05-02 00:25:51 +0000710 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
711 const unsigned hexsz = the_hash_algo->hexsz;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700712
Christian Couderf1514c62020-06-11 14:05:11 +0200713 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
Christian Couder629060d2020-06-11 14:05:12 +0200714 data->allow_uor) < 0)
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700715 return -1;
716
brian m. carlson55dc2272018-05-02 00:25:51 +0000717 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
Jeff King62b89d42019-06-20 03:40:54 -0400718 struct object_id oid;
brian m. carlson55dc2272018-05-02 00:25:51 +0000719 const char *p;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700720
Jeff King62b89d42019-06-20 03:40:54 -0400721 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700722 break;
723
Jeff Kingd0229ab2019-06-20 03:41:14 -0400724 o = lookup_object(the_repository, &oid);
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700725 if (o && o->type == OBJ_COMMIT) {
726 o->flags &= ~TMP_MARK;
727 }
728 }
729 for (i = get_max_object_index(); 0 < i; i--) {
730 o = get_indexed_object(i - 1);
731 if (o && o->type == OBJ_COMMIT &&
732 (o->flags & TMP_MARK)) {
733 add_object_array(o, NULL, reachable);
734 o->flags &= ~TMP_MARK;
735 }
736 }
737 close(cmd.out);
738
739 if (finish_command(&cmd))
740 return -1;
741
742 return 0;
743}
744
Christian Couder629060d2020-06-11 14:05:12 +0200745static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700746{
747 struct child_process cmd = CHILD_PROCESS_INIT;
748 char buf[1];
749 int i;
750
Christian Couder629060d2020-06-11 14:05:12 +0200751 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700752 return 1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700753
754 /*
755 * The commits out of the rev-list are not ancestors of
756 * our ref.
757 */
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700758 i = read_in_full(cmd.out, buf, 1);
Junio C Hamano051e4002011-08-05 13:54:06 -0700759 if (i)
760 goto error;
761 close(cmd.out);
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700762 cmd.out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700763
764 /*
765 * rev-list may have died by encountering a bad commit
766 * in the history, in which case we do want to bail out
767 * even when it showed no commit.
768 */
769 if (finish_command(&cmd))
770 goto error;
771
772 /* All the non-tip ones are ancestors of what we advertised */
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700773 return 0;
Junio C Hamano051e4002011-08-05 13:54:06 -0700774
775error:
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700776 if (cmd.out >= 0)
777 close(cmd.out);
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700778 return 1;
779}
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700780
Christian Couderb08c9742020-05-15 12:04:51 +0200781static void check_non_tip(struct upload_pack_data *data)
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700782{
783 int i;
784
785 /*
786 * In the normal in-process case without
787 * uploadpack.allowReachableSHA1InWant,
788 * non-tip requests can never happen.
789 */
Christian Couder629060d2020-06-11 14:05:12 +0200790 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700791 goto error;
Christian Couder629060d2020-06-11 14:05:12 +0200792 if (!has_unreachable(&data->want_obj, data->allow_uor))
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700793 /* All the non-tip ones are ancestors of what we advertised */
794 return;
Junio C Hamano051e4002011-08-05 13:54:06 -0700795
796error:
797 /* Pick one of them (we know there at least is one) */
Christian Couderb08c9742020-05-15 12:04:51 +0200798 for (i = 0; i < data->want_obj.nr; i++) {
799 struct object *o = data->want_obj.objects[i].item;
Christian Couder629060d2020-06-11 14:05:12 +0200800 if (!is_our_ref(o, data->allow_uor)) {
Derrick Stolee7ba7c522023-08-10 14:40:50 +0000801 error("git upload-pack: not our ref %s",
802 oid_to_hex(&o->oid));
Christian Couderb08c9742020-05-15 12:04:51 +0200803 packet_writer_error(&data->writer,
Jeff King014ade72019-04-13 01:53:34 -0400804 "upload-pack: not our ref %s",
805 oid_to_hex(&o->oid));
Patrick Steinhardt5f33a842023-08-16 08:06:59 +0200806 exit(128);
Jeff King014ade72019-04-13 01:53:34 -0400807 }
Junio C Hamano051e4002011-08-05 13:54:06 -0700808 }
809}
810
Christian Couder35b43a12020-06-11 14:05:09 +0200811static void send_shallow(struct upload_pack_data *data,
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800812 struct commit_list *result)
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700813{
814 while (result) {
815 struct object *object = &result->item->object;
816 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
Christian Couder35b43a12020-06-11 14:05:09 +0200817 packet_writer_write(&data->writer, "shallow %s",
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800818 oid_to_hex(&object->oid));
Stefan Beller19143f12018-05-17 15:51:44 -0700819 register_shallow(the_repository, &object->oid);
Christian Couder35b43a12020-06-11 14:05:09 +0200820 data->shallow_nr++;
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700821 }
822 result = result->next;
823 }
824}
825
Christian Couder329f9962020-06-11 14:05:08 +0200826static void send_unshallow(struct upload_pack_data *data)
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700827{
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700828 int i;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700829
Christian Couder329f9962020-06-11 14:05:08 +0200830 for (i = 0; i < data->shallows.nr; i++) {
831 struct object *object = data->shallows.objects[i].item;
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700832 if (object->flags & NOT_SHALLOW) {
833 struct commit_list *parents;
Christian Couder329f9962020-06-11 14:05:08 +0200834 packet_writer_write(&data->writer, "unshallow %s",
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800835 oid_to_hex(&object->oid));
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700836 object->flags &= ~CLIENT_SHALLOW;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700837 /*
838 * We want to _register_ "object" as shallow, but we
839 * also need to traverse object's parents to deepen a
840 * shallow clone. Unregister it for now so we can
841 * parse and add the parents to the want list, then
842 * re-register it.
843 */
brian m. carlsone92b8482017-05-06 22:10:06 +0000844 unregister_shallow(&object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700845 object->parsed = 0;
846 parse_commit_or_die((struct commit *)object);
847 parents = ((struct commit *)object)->parents;
848 while (parents) {
849 add_object_array(&parents->item->object,
Christian Couder329f9962020-06-11 14:05:08 +0200850 NULL, &data->want_obj);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700851 parents = parents->next;
852 }
Christian Couderde0e9f72020-06-11 14:05:10 +0200853 add_object_array(object, NULL, &data->extra_edge_obj);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700854 }
855 /* make sure commit traversal conforms to client */
Stefan Beller19143f12018-05-17 15:51:44 -0700856 register_shallow(the_repository, &object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700857 }
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700858}
859
Jonathan Tan5056cf42018-12-18 13:24:35 -0800860static int check_ref(const char *refname_full, const struct object_id *oid,
861 int flag, void *cb_data);
Christian Couderb1492f22020-06-11 14:05:06 +0200862static void deepen(struct upload_pack_data *data, int depth)
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700863{
Stefan Bellerc8813482018-05-17 15:51:46 -0700864 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700865 int i;
866
Christian Couderb1492f22020-06-11 14:05:06 +0200867 for (i = 0; i < data->shallows.nr; i++) {
868 struct object *object = data->shallows.objects[i].item;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700869 object->flags |= NOT_SHALLOW;
870 }
Christian Couderb1492f22020-06-11 14:05:06 +0200871 } else if (data->deepen_relative) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700872 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
873 struct commit_list *result;
874
Jonathan Tan5056cf42018-12-18 13:24:35 -0800875 /*
876 * Checking for reachable shallows requires that our refs be
877 * marked with OUR_REF.
878 */
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200879 refs_head_ref_namespaced(get_main_ref_store(the_repository),
880 check_ref, data);
Taylor Blau18b6b1b2023-07-10 17:12:45 -0400881 for_each_namespaced_ref_1(check_ref, data);
Jonathan Tan5056cf42018-12-18 13:24:35 -0800882
Christian Couderf1514c62020-06-11 14:05:11 +0200883 get_reachable_list(data, &reachable_shallows);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700884 result = get_shallow_commits(&reachable_shallows,
885 depth + 1,
886 SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200887 send_shallow(data, result);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700888 free_commit_list(result);
889 object_array_clear(&reachable_shallows);
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700890 } else {
891 struct commit_list *result;
892
Christian Couderb1492f22020-06-11 14:05:06 +0200893 result = get_shallow_commits(&data->want_obj, depth,
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700894 SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200895 send_shallow(data, result);
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700896 free_commit_list(result);
897 }
898
Christian Couder329f9962020-06-11 14:05:08 +0200899 send_unshallow(data);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700900}
901
Christian Couder446e42c2020-06-11 14:05:07 +0200902static void deepen_by_rev_list(struct upload_pack_data *data,
903 int ac,
904 const char **av)
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700905{
906 struct commit_list *result;
907
Jeff King6abada12019-09-12 10:44:45 -0400908 disable_commit_graph(the_repository);
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700909 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200910 send_shallow(data, result);
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700911 free_commit_list(result);
Christian Couder329f9962020-06-11 14:05:08 +0200912 send_unshallow(data);
Brandon Williams685fbd32018-03-15 10:31:28 -0700913}
914
915/* Returns 1 if a shallow list is sent or 0 otherwise */
Christian Couderee703c82020-06-11 14:05:05 +0200916static int send_shallow_list(struct upload_pack_data *data)
Brandon Williams685fbd32018-03-15 10:31:28 -0700917{
918 int ret = 0;
919
Christian Couderee703c82020-06-11 14:05:05 +0200920 if (data->depth > 0 && data->deepen_rev_list)
Brandon Williams685fbd32018-03-15 10:31:28 -0700921 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
Christian Couderee703c82020-06-11 14:05:05 +0200922 if (data->depth > 0) {
Christian Couderb1492f22020-06-11 14:05:06 +0200923 deepen(data, data->depth);
Brandon Williams685fbd32018-03-15 10:31:28 -0700924 ret = 1;
Christian Couderee703c82020-06-11 14:05:05 +0200925 } else if (data->deepen_rev_list) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400926 struct strvec av = STRVEC_INIT;
Brandon Williams685fbd32018-03-15 10:31:28 -0700927 int i;
928
Jeff Kingc972bf42020-07-28 16:25:12 -0400929 strvec_push(&av, "rev-list");
Christian Couderee703c82020-06-11 14:05:05 +0200930 if (data->deepen_since)
Jeff Kingc972bf42020-07-28 16:25:12 -0400931 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
Jeff King388b96d2024-02-28 17:37:44 -0500932 if (oidset_size(&data->deepen_not)) {
933 const struct object_id *oid;
934 struct oidset_iter iter;
Jeff Kingc972bf42020-07-28 16:25:12 -0400935 strvec_push(&av, "--not");
Jeff King388b96d2024-02-28 17:37:44 -0500936 oidset_iter_init(&data->deepen_not, &iter);
937 while ((oid = oidset_iter_next(&iter)))
Jeff King720ba252024-02-28 17:37:20 -0500938 strvec_push(&av, oid_to_hex(oid));
Jeff Kingc972bf42020-07-28 16:25:12 -0400939 strvec_push(&av, "--not");
Brandon Williams685fbd32018-03-15 10:31:28 -0700940 }
Christian Couderee703c82020-06-11 14:05:05 +0200941 for (i = 0; i < data->want_obj.nr; i++) {
942 struct object *o = data->want_obj.objects[i].item;
Jeff Kingc972bf42020-07-28 16:25:12 -0400943 strvec_push(&av, oid_to_hex(&o->oid));
Brandon Williams685fbd32018-03-15 10:31:28 -0700944 }
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400945 deepen_by_rev_list(data, av.nr, av.v);
Jeff Kingc972bf42020-07-28 16:25:12 -0400946 strvec_clear(&av);
Brandon Williams685fbd32018-03-15 10:31:28 -0700947 ret = 1;
948 } else {
Christian Couderee703c82020-06-11 14:05:05 +0200949 if (data->shallows.nr > 0) {
Brandon Williams685fbd32018-03-15 10:31:28 -0700950 int i;
Christian Couderee703c82020-06-11 14:05:05 +0200951 for (i = 0; i < data->shallows.nr; i++)
Junio C Hamano00624d62018-07-18 12:20:27 -0700952 register_shallow(the_repository,
Christian Couderee703c82020-06-11 14:05:05 +0200953 &data->shallows.objects[i].item->oid);
Brandon Williams685fbd32018-03-15 10:31:28 -0700954 }
955 }
956
Christian Couder35b43a12020-06-11 14:05:09 +0200957 data->shallow_nr += data->shallows.nr;
Brandon Williams685fbd32018-03-15 10:31:28 -0700958 return ret;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700959}
960
Brandon Williamsae2948f2018-03-14 11:31:42 -0700961static int process_shallow(const char *line, struct object_array *shallows)
962{
963 const char *arg;
964 if (skip_prefix(line, "shallow ", &arg)) {
965 struct object_id oid;
966 struct object *object;
967 if (get_oid_hex(arg, &oid))
968 die("invalid shallow line: %s", line);
Stefan Beller109cd762018-06-28 18:21:51 -0700969 object = parse_object(the_repository, &oid);
Brandon Williamsae2948f2018-03-14 11:31:42 -0700970 if (!object)
971 return 1;
972 if (object->type != OBJ_COMMIT)
973 die("invalid shallow object %s", oid_to_hex(&oid));
974 if (!(object->flags & CLIENT_SHALLOW)) {
975 object->flags |= CLIENT_SHALLOW;
976 add_object_array(object, NULL, shallows);
977 }
978 return 1;
979 }
980
981 return 0;
982}
983
984static int process_deepen(const char *line, int *depth)
985{
986 const char *arg;
987 if (skip_prefix(line, "deepen ", &arg)) {
988 char *end = NULL;
989 *depth = (int)strtol(arg, &end, 0);
990 if (!end || *end || *depth <= 0)
991 die("Invalid deepen: %s", line);
992 return 1;
993 }
994
995 return 0;
996}
997
998static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
999{
1000 const char *arg;
1001 if (skip_prefix(line, "deepen-since ", &arg)) {
1002 char *end = NULL;
1003 *deepen_since = parse_timestamp(arg, &end, 0);
1004 if (!end || *end || !deepen_since ||
1005 /* revisions.c's max_age -1 is special */
1006 *deepen_since == -1)
1007 die("Invalid deepen-since: %s", line);
1008 *deepen_rev_list = 1;
1009 return 1;
1010 }
1011 return 0;
1012}
1013
Jeff King388b96d2024-02-28 17:37:44 -05001014static int process_deepen_not(const char *line, struct oidset *deepen_not, int *deepen_rev_list)
Brandon Williamsae2948f2018-03-14 11:31:42 -07001015{
1016 const char *arg;
1017 if (skip_prefix(line, "deepen-not ", &arg)) {
1018 char *ref = NULL;
1019 struct object_id oid;
Nguyễn Thái Ngọc Duy0b1dbf52019-04-06 18:34:27 +07001020 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
Brandon Williamsae2948f2018-03-14 11:31:42 -07001021 die("git upload-pack: ambiguous deepen-not: %s", line);
Jeff King388b96d2024-02-28 17:37:44 -05001022 oidset_insert(deepen_not, &oid);
Brandon Williamsae2948f2018-03-14 11:31:42 -07001023 free(ref);
1024 *deepen_rev_list = 1;
1025 return 1;
1026 }
1027 return 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001028}
1029
Taylor Blau6dd34562020-08-03 14:00:10 -04001030NORETURN __attribute__((format(printf,2,3)))
1031static void send_err_and_die(struct upload_pack_data *data,
1032 const char *fmt, ...)
1033{
1034 struct strbuf buf = STRBUF_INIT;
1035 va_list ap;
1036
1037 va_start(ap, fmt);
1038 strbuf_vaddf(&buf, fmt, ap);
1039 va_end(ap);
1040
1041 packet_writer_error(&data->writer, "%s", buf.buf);
1042 die("%s", buf.buf);
1043}
1044
1045static void check_one_filter(struct upload_pack_data *data,
1046 struct list_objects_filter_options *opts)
1047{
1048 const char *key = list_object_filter_config_name(opts->choice);
1049 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1050 key);
1051 int allowed;
1052
1053 if (item)
1054 allowed = (intptr_t)item->util;
1055 else
1056 allowed = data->allow_filter_fallback;
1057
1058 if (!allowed)
1059 send_err_and_die(data, "filter '%s' not supported", key);
Taylor Blau5b01a4e2020-08-03 14:00:17 -04001060
1061 if (opts->choice == LOFC_TREE_DEPTH &&
1062 opts->tree_exclude_depth > data->tree_filter_max_depth)
1063 send_err_and_die(data,
1064 "tree filter allows max depth %lu, but got %lu",
1065 data->tree_filter_max_depth,
1066 opts->tree_exclude_depth);
Taylor Blau6dd34562020-08-03 14:00:10 -04001067}
1068
1069static void check_filter_recurse(struct upload_pack_data *data,
1070 struct list_objects_filter_options *opts)
1071{
1072 size_t i;
1073
1074 check_one_filter(data, opts);
1075 if (opts->choice != LOFC_COMBINE)
1076 return;
1077
1078 for (i = 0; i < opts->sub_nr; i++)
1079 check_filter_recurse(data, &opts->sub[i]);
1080}
1081
1082static void die_if_using_banned_filter(struct upload_pack_data *data)
1083{
1084 check_filter_recurse(data, &data->filter_options);
1085}
1086
Christian Couderd92ae2c2020-05-15 12:04:47 +02001087static void receive_needs(struct upload_pack_data *data,
1088 struct packet_reader *reader)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001089{
Junio C Hamano051e4002011-08-05 13:54:06 -07001090 int has_non_tip = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001091
Christian Couder35b43a12020-06-11 14:05:09 +02001092 data->shallow_nr = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001093 for (;;) {
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001094 struct object *o;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001095 const char *features;
brian m. carlsoncf939822017-05-06 22:10:28 +00001096 struct object_id oid_buf;
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +07001097 const char *arg;
Jeff King7ce4c8f2023-04-14 17:25:20 -04001098 size_t feature_len;
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +07001099
Christian Couderd40f04e2020-06-04 19:54:40 +02001100 reset_timeout(data->timeout);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001101 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Johannes Schindelined09aef2006-10-30 20:09:06 +01001102 break;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001103
Christian Couder7a516762020-05-15 12:04:54 +02001104 if (process_shallow(reader->line, &data->shallows))
Johannes Schindelined09aef2006-10-30 20:09:06 +01001105 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001106 if (process_deepen(reader->line, &data->depth))
Johannes Schindelin016e6cc2006-10-30 20:09:29 +01001107 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001108 if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +07001109 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001110 if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
Nguyễn Thái Ngọc Duy269a7a82016-06-12 17:54:03 +07001111 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -07001112
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001113 if (skip_prefix(reader->line, "filter ", &arg)) {
Christian Couder59a90262020-06-04 19:54:42 +02001114 if (!data->filter_capability_requested)
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001115 die("git upload-pack: filtering capability not negotiated");
Christian Couderd92ae2c2020-05-15 12:04:47 +02001116 list_objects_filter_die_if_populated(&data->filter_options);
1117 parse_list_objects_filter(&data->filter_options, arg);
Taylor Blau6dd34562020-08-03 14:00:10 -04001118 die_if_using_banned_filter(data);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001119 continue;
1120 }
Junio C Hamano9bfa0f92018-05-08 15:59:15 +09001121
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001122 if (!skip_prefix(reader->line, "want ", &arg) ||
brian m. carlson55dc2272018-05-02 00:25:51 +00001123 parse_oid_hex(arg, &oid_buf, &features))
Junio C Hamano7e44c932008-08-31 09:39:19 -07001124 die("git upload-pack: protocol error, "
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001125 "expected to get object ID, not '%s'", reader->line);
Junio C Hamanof47182c2012-01-08 22:06:19 +01001126
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001127 if (parse_feature_request(features, "deepen-relative"))
Christian Couder7a516762020-05-15 12:04:54 +02001128 data->deepen_relative = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001129 if (parse_feature_request(features, "multi_ack_detailed"))
Christian Coudere9d882b2020-06-04 19:54:44 +02001130 data->multi_ack = MULTI_ACK_DETAILED;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001131 else if (parse_feature_request(features, "multi_ack"))
Christian Coudere9d882b2020-06-04 19:54:44 +02001132 data->multi_ack = MULTI_ACK;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001133 if (parse_feature_request(features, "no-done"))
Christian Couderd40f04e2020-06-04 19:54:40 +02001134 data->no_done = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001135 if (parse_feature_request(features, "thin-pack"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001136 data->use_thin_pack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001137 if (parse_feature_request(features, "ofs-delta"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001138 data->use_ofs_delta = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001139 if (parse_feature_request(features, "side-band-64k"))
Christian Couderf8edd1c2020-06-04 19:54:41 +02001140 data->use_sideband = LARGE_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001141 else if (parse_feature_request(features, "side-band"))
Christian Couderf8edd1c2020-06-04 19:54:41 +02001142 data->use_sideband = DEFAULT_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001143 if (parse_feature_request(features, "no-progress"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001144 data->no_progress = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001145 if (parse_feature_request(features, "include-tag"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001146 data->use_include_tag = 1;
Christian Couder59abe192020-06-04 19:54:47 +02001147 if (data->allow_filter &&
1148 parse_feature_request(features, "filter"))
Christian Couder59a90262020-06-04 19:54:42 +02001149 data->filter_capability_requested = 1;
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001150
Josh Steadmon82959462020-11-11 15:29:32 -08001151 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1152 if (arg) {
1153 char *client_sid = xstrndup(arg, feature_len);
1154 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1155 free(client_sid);
1156 }
1157
Jeff Kinga6ca6012024-02-28 17:39:03 -05001158 o = parse_object_with_flags(the_repository, &oid_buf,
Jeff King6cd05e72024-02-28 17:39:07 -05001159 PARSE_OBJECT_SKIP_HASH_CHECK |
1160 PARSE_OBJECT_DISCARD_TREE);
Jonathan Tanbdb31ea2017-02-23 10:43:03 -08001161 if (!o) {
Christian Couder4ace0282020-05-15 12:04:48 +02001162 packet_writer_error(&data->writer,
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001163 "upload-pack: not our ref %s",
1164 oid_to_hex(&oid_buf));
Elijah Newren9f9aa762010-07-31 14:11:46 -06001165 die("git upload-pack: not our ref %s",
brian m. carlsoncf939822017-05-06 22:10:28 +00001166 oid_to_hex(&oid_buf));
Jonathan Tanbdb31ea2017-02-23 10:43:03 -08001167 }
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001168 if (!(o->flags & WANTED)) {
1169 o->flags |= WANTED;
Christian Couder629060d2020-06-11 14:05:12 +02001170 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1171 || is_our_ref(o, data->allow_uor)))
Junio C Hamano051e4002011-08-05 13:54:06 -07001172 has_non_tip = 1;
Christian Couderd92ae2c2020-05-15 12:04:47 +02001173 add_object_array(o, NULL, &data->want_obj);
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001174 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001175 }
Johannes Sixt9462e3f2009-06-16 20:41:16 +02001176
Junio C Hamano051e4002011-08-05 13:54:06 -07001177 /*
1178 * We have sent all our refs already, and the other end
1179 * should have chosen out of them. When we are operating
1180 * in the stateless RPC mode, however, their choice may
1181 * have been based on the set of older refs advertised
1182 * by another process that handled the initial request.
1183 */
1184 if (has_non_tip)
Christian Couderb08c9742020-05-15 12:04:51 +02001185 check_non_tip(data);
Junio C Hamano051e4002011-08-05 13:54:06 -07001186
Christian Couderf8edd1c2020-06-04 19:54:41 +02001187 if (!data->use_sideband && data->daemon_mode)
Jeff Kingb5a20682020-06-04 19:54:38 +02001188 data->no_progress = 1;
Johannes Sixt9462e3f2009-06-16 20:41:16 +02001189
Christian Couder7a516762020-05-15 12:04:54 +02001190 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
Johannes Schindelinf53514b2006-10-30 20:09:53 +01001191 return;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +07001192
Christian Couderee703c82020-06-11 14:05:05 +02001193 if (send_shallow_list(data))
Brandon Williams685fbd32018-03-15 10:31:28 -07001194 packet_flush(1);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001195}
1196
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001197/* return non-zero if the ref is hidden, otherwise 0 */
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001198static int mark_our_ref(const char *refname, const char *refname_full,
Taylor Blauc45841f2023-07-10 17:12:33 -04001199 const struct object_id *oid, const struct strvec *hidden_refs)
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001200{
Jeff King45a187c2021-04-13 03:16:36 -04001201 struct object *o = lookup_unknown_object(the_repository, oid);
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001202
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001203 if (ref_is_hidden(refname, refname_full, hidden_refs)) {
Junio C Hamano390eb362013-01-28 21:49:57 -08001204 o->flags |= HIDDEN_REF;
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001205 return 1;
Junio C Hamano390eb362013-01-28 21:49:57 -08001206 }
Junio C Hamano3f1da572013-01-28 20:45:43 -08001207 o->flags |= OUR_REF;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001208 return 0;
1209}
1210
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001211static int check_ref(const char *refname_full, const struct object_id *oid,
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001212 int flag UNUSED, void *cb_data)
Jeff Kinge1727552015-03-13 00:42:12 -04001213{
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001214 const char *refname = strip_namespace(refname_full);
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001215 struct upload_pack_data *data = cb_data;
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001216
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001217 mark_our_ref(refname, refname_full, oid, &data->hidden_refs);
Jeff Kinge1727552015-03-13 00:42:12 -04001218 return 0;
1219}
1220
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001221static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1222{
1223 struct string_list_item *item;
1224
1225 if (!symref->nr)
1226 return;
1227 for_each_string_list_item(item, symref)
1228 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1229}
1230
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001231static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1232 if (d->advertise_sid)
1233 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1234}
1235
brian m. carlson933e3a42023-05-17 19:24:43 +00001236static void write_v0_ref(struct upload_pack_data *data,
1237 const char *refname, const char *refname_nons,
1238 const struct object_id *oid)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001239{
Johannes Schindelined09aef2006-10-30 20:09:06 +01001240 static const char *capabilities = "multi_ack thin-pack side-band"
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001241 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1242 " deepen-relative no-progress include-tag multi_ack_detailed";
Michael Haggerty21758af2015-05-25 18:39:13 +00001243 struct object_id peeled;
Carl Worthb5b16992006-02-17 16:14:52 -08001244
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001245 if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs))
brian m. carlson933e3a42023-05-17 19:24:43 +00001246 return;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001247
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001248 if (capabilities) {
1249 struct strbuf symref_info = STRBUF_INIT;
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001250 struct strbuf session_id = STRBUF_INIT;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001251
Christian Couder762f9272020-05-15 12:04:50 +02001252 format_symref_info(&symref_info, &data->symref);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001253 format_session_id(&session_id, data);
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001254 packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
Michael Haggerty363e98b2015-05-25 18:39:12 +00001255 oid_to_hex(oid), refname_nons,
Junio C Hamanocf2ad8e2011-03-29 10:24:59 -07001256 0, capabilities,
Christian Couder629060d2020-06-11 14:05:12 +02001257 (data->allow_uor & ALLOW_TIP_SHA1) ?
Fredrik Medley7199c092015-05-21 22:23:38 +02001258 " allow-tip-sha1-in-want" : "",
Christian Couder629060d2020-06-11 14:05:12 +02001259 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
Fredrik Medley68ee6282015-05-21 22:23:39 +02001260 " allow-reachable-sha1-in-want" : "",
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001261 data->no_done ? " no-done" : "",
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001262 symref_info.buf,
Christian Couder59abe192020-06-04 19:54:47 +02001263 data->allow_filter ? " filter" : "",
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001264 session_id.buf,
brian m. carlsonbf30dbf2020-05-25 19:58:51 +00001265 the_hash_algo->name,
Jeff Kingff5effd2012-08-03 12:19:16 -04001266 git_user_agent_sanitized());
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001267 strbuf_release(&symref_info);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001268 strbuf_release(&session_id);
brian m. carlson933e3a42023-05-17 19:24:43 +00001269 data->sent_capabilities = 1;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001270 } else {
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001271 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001272 }
Johannes Schindelin1f5881b2005-10-28 05:56:41 +02001273 capabilities = NULL;
Patrick Steinhardt30aaff42024-05-17 10:19:04 +02001274 if (!peel_iterated_oid(the_repository, oid, &peeled))
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001275 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
brian m. carlson933e3a42023-05-17 19:24:43 +00001276 return;
1277}
1278
1279static int send_ref(const char *refname, const struct object_id *oid,
1280 int flag UNUSED, void *cb_data)
1281{
1282 write_v0_ref(cb_data, refname, strip_namespace(refname), oid);
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001283 return 0;
1284}
1285
Jeff King63e14ee2022-08-19 06:08:32 -04001286static int find_symref(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001287 const struct object_id *oid UNUSED,
Michael Haggerty7dabd052015-05-25 18:39:10 +00001288 int flag, void *cb_data)
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001289{
1290 const char *symref_target;
1291 struct string_list_item *item;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001292
1293 if ((flag & REF_ISSYMREF) == 0)
1294 return 0;
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02001295 symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1296 refname, 0, NULL, &flag);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001297 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1298 die("'%s' is a symref but it is not?", refname);
Jeff King533e0882019-05-23 02:11:21 -04001299 item = string_list_append(cb_data, strip_namespace(refname));
1300 item->util = xstrdup(strip_namespace(symref_target));
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001301 return 0;
1302}
1303
Taylor Blau6dd34562020-08-03 14:00:10 -04001304static int parse_object_filter_config(const char *var, const char *value,
Glen Choo8868b1e2023-06-28 19:26:27 +00001305 const struct key_value_info *kvi,
1306 struct upload_pack_data *data)
Taylor Blau6dd34562020-08-03 14:00:10 -04001307{
1308 struct strbuf buf = STRBUF_INIT;
1309 const char *sub, *key;
1310 size_t sub_len;
1311
1312 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1313 return 0;
1314
1315 if (!sub) {
1316 if (!strcmp(key, "allow"))
1317 data->allow_filter_fallback = git_config_bool(var, value);
1318 return 0;
1319 }
1320
1321 strbuf_add(&buf, sub, sub_len);
1322
1323 if (!strcmp(key, "allow"))
1324 string_list_insert(&data->allowed_filters, buf.buf)->util =
1325 (void *)(intptr_t)git_config_bool(var, value);
Taylor Blau5b01a4e2020-08-03 14:00:17 -04001326 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1327 if (!value) {
1328 strbuf_release(&buf);
1329 return config_error_nonbool(var);
1330 }
1331 string_list_insert(&data->allowed_filters, buf.buf)->util =
1332 (void *)(intptr_t)1;
Glen Choo8868b1e2023-06-28 19:26:27 +00001333 data->tree_filter_max_depth = git_config_ulong(var, value,
1334 kvi);
Taylor Blau5b01a4e2020-08-03 14:00:17 -04001335 }
Taylor Blau6dd34562020-08-03 14:00:10 -04001336
1337 strbuf_release(&buf);
1338 return 0;
1339}
1340
Glen Chooa4e7e312023-06-28 19:26:22 +00001341static int upload_pack_config(const char *var, const char *value,
Glen Choo8868b1e2023-06-28 19:26:27 +00001342 const struct config_context *ctx,
Glen Chooa4e7e312023-06-28 19:26:22 +00001343 void *cb_data)
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001344{
Christian Couderf203a882020-06-04 19:54:46 +02001345 struct upload_pack_data *data = cb_data;
1346
Fredrik Medley7199c092015-05-21 22:23:38 +02001347 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1348 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001349 data->allow_uor |= ALLOW_TIP_SHA1;
Fredrik Medley7199c092015-05-21 22:23:38 +02001350 else
Christian Couder629060d2020-06-11 14:05:12 +02001351 data->allow_uor &= ~ALLOW_TIP_SHA1;
Fredrik Medley68ee6282015-05-21 22:23:39 +02001352 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1353 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001354 data->allow_uor |= ALLOW_REACHABLE_SHA1;
Fredrik Medley68ee6282015-05-21 22:23:39 +02001355 else
Christian Couder629060d2020-06-11 14:05:12 +02001356 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
David Turnerf8edeaa2016-11-11 12:23:48 -05001357 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1358 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001359 data->allow_uor |= ALLOW_ANY_SHA1;
David Turnerf8edeaa2016-11-11 12:23:48 -05001360 else
Christian Couder629060d2020-06-11 14:05:12 +02001361 data->allow_uor &= ~ALLOW_ANY_SHA1;
Fredrik Medley7199c092015-05-21 22:23:38 +02001362 } else if (!strcmp("uploadpack.keepalive", var)) {
Glen Choo8868b1e2023-06-28 19:26:27 +00001363 data->keepalive = git_config_int(var, value, ctx->kvi);
Christian Couderf203a882020-06-04 19:54:46 +02001364 if (!data->keepalive)
1365 data->keepalive = -1;
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001366 } else if (!strcmp("uploadpack.allowfilter", var)) {
Christian Couder59abe192020-06-04 19:54:47 +02001367 data->allow_filter = git_config_bool(var, value);
Brandon Williams516e2b72018-06-27 15:30:17 -07001368 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
Christian Couderd1d7a942020-06-04 19:54:48 +02001369 data->allow_ref_in_want = git_config_bool(var, value);
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001370 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
Christian Coudere3835cd2020-06-04 19:54:49 +02001371 data->allow_sideband_all = git_config_bool(var, value);
Jeff Kinga922bfa2024-02-28 17:50:50 -05001372 } else if (!strcmp("uploadpack.blobpackfileuri", var)) {
1373 if (value)
1374 data->allow_packfile_uris = 1;
Elijah Newren8e712ef2019-04-25 07:58:54 -07001375 } else if (!strcmp("core.precomposeunicode", var)) {
1376 precomposed_unicode = git_config_bool(var, value);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001377 } else if (!strcmp("transfer.advertisesid", var)) {
1378 data->advertise_sid = git_config_bool(var, value);
Jeff King05e95152013-09-08 05:01:31 -04001379 }
Jeff Kingaaaa8812018-10-24 03:27:52 -04001380
Glen Choo8868b1e2023-06-28 19:26:27 +00001381 if (parse_object_filter_config(var, value, ctx->kvi, data) < 0)
Jeff Kingd43a21b2020-12-03 03:09:42 -05001382 return -1;
Taylor Blau6dd34562020-08-03 14:00:10 -04001383
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001384 return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs);
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001385}
1386
Glen Chooa4e7e312023-06-28 19:26:22 +00001387static int upload_pack_protected_config(const char *var, const char *value,
1388 const struct config_context *ctx UNUSED,
1389 void *cb_data)
Glen Choo5b3c6502022-07-14 21:27:59 +00001390{
1391 struct upload_pack_data *data = cb_data;
1392
1393 if (!strcmp("uploadpack.packobjectshook", var))
1394 return git_config_string(&data->pack_objects_hook, var, value);
1395 return 0;
1396}
1397
Jeff King922fdef2024-02-28 17:46:47 -05001398static void get_upload_pack_config(struct repository *r,
1399 struct upload_pack_data *data)
Glen Choo5b3c6502022-07-14 21:27:59 +00001400{
Jeff King922fdef2024-02-28 17:46:47 -05001401 repo_config(r, upload_pack_config, data);
Glen Choo5b3c6502022-07-14 21:27:59 +00001402 git_protected_config(upload_pack_protected_config, data);
Jeff King37aa89b2024-02-28 17:47:18 -05001403
1404 data->allow_sideband_all |= git_env_bool("GIT_TEST_SIDEBAND_ALL", 0);
Glen Choo5b3c6502022-07-14 21:27:59 +00001405}
1406
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001407void upload_pack(const int advertise_refs, const int stateless_rpc,
1408 const int timeout)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001409{
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001410 struct packet_reader reader;
Christian Couderebf8ebc2020-05-15 12:04:45 +02001411 struct upload_pack_data data;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001412
Christian Couderebf8ebc2020-05-15 12:04:45 +02001413 upload_pack_data_init(&data);
Jeff King922fdef2024-02-28 17:46:47 -05001414 get_upload_pack_config(the_repository, &data);
Christian Couder8a0e6f12020-06-04 19:54:45 +02001415
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001416 data.stateless_rpc = stateless_rpc;
1417 data.timeout = timeout;
1418 if (data.timeout)
1419 data.daemon_mode = 1;
Christian Couderdf654ab2020-05-15 12:04:52 +02001420
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02001421 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1422 find_symref, &data.symref);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001423
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001424 if (advertise_refs || !data.stateless_rpc) {
Christian Couderd40f04e2020-06-04 19:54:40 +02001425 reset_timeout(data.timeout);
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001426 if (advertise_refs)
1427 data.no_done = 1;
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02001428 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1429 send_ref, &data);
Taylor Blau18b6b1b2023-07-10 17:12:45 -04001430 for_each_namespaced_ref_1(send_ref, &data);
brian m. carlson933e3a42023-05-17 19:24:43 +00001431 if (!data.sent_capabilities) {
1432 const char *refname = "capabilities^{}";
1433 write_v0_ref(&data, refname, refname, null_oid());
1434 }
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001435 /*
1436 * fflush stdout before calling advertise_shallow_grafts because send_ref
1437 * uses stdio.
1438 */
1439 fflush_or_die(stdout);
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001440 advertise_shallow_grafts(1);
1441 packet_flush(1);
1442 } else {
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02001443 refs_head_ref_namespaced(get_main_ref_store(the_repository),
1444 check_ref, &data);
Taylor Blau18b6b1b2023-07-10 17:12:45 -04001445 for_each_namespaced_ref_1(check_ref, &data);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001446 }
1447
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001448 if (!advertise_refs) {
Christian Couderebf8ebc2020-05-15 12:04:45 +02001449 packet_reader_init(&reader, 0, NULL, 0,
1450 PACKET_READ_CHOMP_NEWLINE |
1451 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001452
Christian Couderd92ae2c2020-05-15 12:04:47 +02001453 receive_needs(&data, &reader);
Daniel Duvallfb3d1a02020-10-30 19:39:02 -07001454
1455 /*
1456 * An EOF at this exact point in negotiation should be
1457 * acceptable from stateless clients as they will consume the
1458 * shallow list before doing subsequent rpc with haves/etc.
1459 */
1460 if (data.stateless_rpc)
1461 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1462
1463 if (data.want_obj.nr &&
1464 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1465 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
Christian Couder07977692020-05-15 12:04:46 +02001466 get_common_commits(&data, &reader);
Ramsay Jonescae2ee12020-06-16 00:00:20 +01001467 create_pack_file(&data, NULL);
Christian Couderebf8ebc2020-05-15 12:04:45 +02001468 }
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001469 }
Christian Couder08450ef2020-05-08 10:01:15 +02001470
Christian Couderebf8ebc2020-05-15 12:04:45 +02001471 upload_pack_data_clear(&data);
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001472}
Brandon Williams3145ea92018-03-15 10:31:27 -07001473
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001474static int parse_want(struct packet_writer *writer, const char *line,
1475 struct object_array *want_obj)
Brandon Williams3145ea92018-03-15 10:31:27 -07001476{
1477 const char *arg;
1478 if (skip_prefix(line, "want ", &arg)) {
1479 struct object_id oid;
1480 struct object *o;
1481
1482 if (get_oid_hex(arg, &oid))
1483 die("git upload-pack: protocol error, "
1484 "expected to get oid, not '%s'", line);
1485
Jeff King9a8c3c42022-09-06 19:06:25 -04001486 o = parse_object_with_flags(the_repository, &oid,
Jeff King6cd05e72024-02-28 17:39:07 -05001487 PARSE_OBJECT_SKIP_HASH_CHECK |
1488 PARSE_OBJECT_DISCARD_TREE);
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001489
Brandon Williams3145ea92018-03-15 10:31:27 -07001490 if (!o) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001491 packet_writer_error(writer,
1492 "upload-pack: not our ref %s",
1493 oid_to_hex(&oid));
Brandon Williams3145ea92018-03-15 10:31:27 -07001494 die("git upload-pack: not our ref %s",
1495 oid_to_hex(&oid));
1496 }
1497
1498 if (!(o->flags & WANTED)) {
1499 o->flags |= WANTED;
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001500 add_object_array(o, NULL, want_obj);
Brandon Williams3145ea92018-03-15 10:31:27 -07001501 }
1502
1503 return 1;
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001504 }
1505
1506 return 0;
1507}
Brandon Williams3145ea92018-03-15 10:31:27 -07001508
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001509static int parse_want_ref(struct packet_writer *writer, const char *line,
Jeff Kingb0650632024-02-28 17:38:40 -05001510 struct strmap *wanted_refs,
Taylor Blauc45841f2023-07-10 17:12:33 -04001511 struct strvec *hidden_refs,
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001512 struct object_array *want_obj)
Brandon Williams516e2b72018-06-27 15:30:17 -07001513{
Kim Altintop39551402021-08-13 06:23:50 +00001514 const char *refname_nons;
1515 if (skip_prefix(line, "want-ref ", &refname_nons)) {
Brandon Williams516e2b72018-06-27 15:30:17 -07001516 struct object_id oid;
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001517 struct object *o = NULL;
Kim Altintop39551402021-08-13 06:23:50 +00001518 struct strbuf refname = STRBUF_INIT;
Brandon Williams516e2b72018-06-27 15:30:17 -07001519
Kim Altintop39551402021-08-13 06:23:50 +00001520 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001521 if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) ||
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02001522 refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) {
Kim Altintop39551402021-08-13 06:23:50 +00001523 packet_writer_error(writer, "unknown ref %s", refname_nons);
1524 die("unknown ref %s", refname_nons);
Brandon Williams516e2b72018-06-27 15:30:17 -07001525 }
Kim Altintop39551402021-08-13 06:23:50 +00001526 strbuf_release(&refname);
Brandon Williams516e2b72018-06-27 15:30:17 -07001527
Jeff Kingb0650632024-02-28 17:38:40 -05001528 if (strmap_put(wanted_refs, refname_nons, oiddup(&oid))) {
1529 packet_writer_error(writer, "duplicate want-ref %s",
1530 refname_nons);
1531 die("duplicate want-ref %s", refname_nons);
1532 }
Brandon Williams516e2b72018-06-27 15:30:17 -07001533
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001534 if (!starts_with(refname_nons, "refs/tags/")) {
1535 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1536 if (commit)
1537 o = &commit->object;
1538 }
1539
1540 if (!o)
1541 o = parse_object_or_die(&oid, refname_nons);
1542
Brandon Williams516e2b72018-06-27 15:30:17 -07001543 if (!(o->flags & WANTED)) {
1544 o->flags |= WANTED;
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001545 add_object_array(o, NULL, want_obj);
Brandon Williams516e2b72018-06-27 15:30:17 -07001546 }
1547
1548 return 1;
1549 }
1550
1551 return 0;
1552}
1553
Jeff Kingfae96272024-02-28 17:37:13 -05001554static int parse_have(const char *line, struct upload_pack_data *data)
Brandon Williams3145ea92018-03-15 10:31:27 -07001555{
1556 const char *arg;
1557 if (skip_prefix(line, "have ", &arg)) {
1558 struct object_id oid;
1559
Jeff Kingfae96272024-02-28 17:37:13 -05001560 got_oid(data, arg, &oid);
1561 data->seen_haves = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001562 return 1;
1563 }
1564
1565 return 0;
1566}
1567
Robert Coupb8f58c22023-10-17 21:12:47 +00001568static void trace2_fetch_info(struct upload_pack_data *data)
1569{
1570 struct json_writer jw = JSON_WRITER_INIT;
1571
1572 jw_object_begin(&jw, 0);
Jeff Kingfae96272024-02-28 17:37:13 -05001573 jw_object_intmax(&jw, "haves", data->have_obj.nr);
Robert Coupb8f58c22023-10-17 21:12:47 +00001574 jw_object_intmax(&jw, "wants", data->want_obj.nr);
Jeff Kingb0650632024-02-28 17:38:40 -05001575 jw_object_intmax(&jw, "want-refs", strmap_get_size(&data->wanted_refs));
Robert Coupb8f58c22023-10-17 21:12:47 +00001576 jw_object_intmax(&jw, "depth", data->depth);
1577 jw_object_intmax(&jw, "shallows", data->shallows.nr);
1578 jw_object_bool(&jw, "deepen-since", data->deepen_since);
Jeff King388b96d2024-02-28 17:37:44 -05001579 jw_object_intmax(&jw, "deepen-not", oidset_size(&data->deepen_not));
Robert Coupb8f58c22023-10-17 21:12:47 +00001580 jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1581 if (data->filter_options.choice)
1582 jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1583 else
1584 jw_object_null(&jw, "filter");
1585 jw_end(&jw);
1586
1587 trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1588
1589 jw_release(&jw);
1590}
1591
Brandon Williams3145ea92018-03-15 10:31:27 -07001592static void process_args(struct packet_reader *request,
Christian Couder389f1612020-05-15 12:04:43 +02001593 struct upload_pack_data *data)
Brandon Williams3145ea92018-03-15 10:31:27 -07001594{
Jeff King4845b772020-03-27 04:03:38 -04001595 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001596 const char *arg = request->line;
Jonathan Tanba957102018-05-03 16:46:56 -07001597 const char *p;
Brandon Williams3145ea92018-03-15 10:31:27 -07001598
1599 /* process want */
Christian Couder389f1612020-05-15 12:04:43 +02001600 if (parse_want(&data->writer, arg, &data->want_obj))
Brandon Williams3145ea92018-03-15 10:31:27 -07001601 continue;
Christian Couderd1d7a942020-06-04 19:54:48 +02001602 if (data->allow_ref_in_want &&
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001603 parse_want_ref(&data->writer, arg, &data->wanted_refs,
Patrick Steinhardt9b67eb62022-11-17 06:46:43 +01001604 &data->hidden_refs, &data->want_obj))
Brandon Williams516e2b72018-06-27 15:30:17 -07001605 continue;
Brandon Williams3145ea92018-03-15 10:31:27 -07001606 /* process have line */
Jeff Kingfae96272024-02-28 17:37:13 -05001607 if (parse_have(arg, data))
Brandon Williams3145ea92018-03-15 10:31:27 -07001608 continue;
1609
1610 /* process args like thin-pack */
1611 if (!strcmp(arg, "thin-pack")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001612 data->use_thin_pack = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001613 continue;
1614 }
1615 if (!strcmp(arg, "ofs-delta")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001616 data->use_ofs_delta = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001617 continue;
1618 }
1619 if (!strcmp(arg, "no-progress")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001620 data->no_progress = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001621 continue;
1622 }
1623 if (!strcmp(arg, "include-tag")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001624 data->use_include_tag = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001625 continue;
1626 }
1627 if (!strcmp(arg, "done")) {
1628 data->done = 1;
1629 continue;
1630 }
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001631 if (!strcmp(arg, "wait-for-done")) {
1632 data->wait_for_done = 1;
1633 continue;
1634 }
Brandon Williams3145ea92018-03-15 10:31:27 -07001635
Brandon Williams685fbd32018-03-15 10:31:28 -07001636 /* Shallow related arguments */
1637 if (process_shallow(arg, &data->shallows))
1638 continue;
1639 if (process_deepen(arg, &data->depth))
1640 continue;
1641 if (process_deepen_since(arg, &data->deepen_since,
1642 &data->deepen_rev_list))
1643 continue;
1644 if (process_deepen_not(arg, &data->deepen_not,
1645 &data->deepen_rev_list))
1646 continue;
1647 if (!strcmp(arg, "deepen-relative")) {
1648 data->deepen_relative = 1;
1649 continue;
1650 }
1651
Christian Couder59abe192020-06-04 19:54:47 +02001652 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
Christian Couder08450ef2020-05-08 10:01:15 +02001653 list_objects_filter_die_if_populated(&data->filter_options);
1654 parse_list_objects_filter(&data->filter_options, p);
Taylor Blau6dd34562020-08-03 14:00:10 -04001655 die_if_using_banned_filter(data);
Jonathan Tanba957102018-05-03 16:46:56 -07001656 continue;
1657 }
1658
Jeff King37aa89b2024-02-28 17:47:18 -05001659 if (data->allow_sideband_all &&
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001660 !strcmp(arg, "sideband-all")) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001661 data->writer.use_sideband = 1;
1662 continue;
1663 }
1664
Jeff Kinga922bfa2024-02-28 17:50:50 -05001665 if (data->allow_packfile_uris &&
1666 skip_prefix(arg, "packfile-uris ", &p)) {
Jeff King179776f2024-02-28 17:38:46 -05001667 if (data->uri_protocols.nr)
1668 send_err_and_die(data,
1669 "multiple packfile-uris lines forbidden");
Jonathan Tandd4b7322020-06-10 13:57:23 -07001670 string_list_split(&data->uri_protocols, p, ',', -1);
1671 continue;
1672 }
1673
Brandon Williams3145ea92018-03-15 10:31:27 -07001674 /* ignore unknown lines maybe? */
Jonathan Tan7cc6ed22018-05-01 17:31:29 -07001675 die("unexpected line: '%s'", arg);
Brandon Williams3145ea92018-03-15 10:31:27 -07001676 }
Jeff King4845b772020-03-27 04:03:38 -04001677
Jonathan Tandd4b7322020-06-10 13:57:23 -07001678 if (data->uri_protocols.nr && !data->writer.use_sideband)
1679 string_list_clear(&data->uri_protocols, 0);
1680
Jeff King4845b772020-03-27 04:03:38 -04001681 if (request->status != PACKET_READ_FLUSH)
1682 die(_("expected flush after fetch arguments"));
Robert Coupb8f58c22023-10-17 21:12:47 +00001683
1684 if (trace2_is_enabled())
1685 trace2_fetch_info(data);
Brandon Williams3145ea92018-03-15 10:31:27 -07001686}
1687
Jeff Kingfae96272024-02-28 17:37:13 -05001688static int send_acks(struct upload_pack_data *data, struct object_array *acks)
Brandon Williams3145ea92018-03-15 10:31:27 -07001689{
1690 int i;
1691
Christian Couder6fbbc432020-06-11 14:05:14 +02001692 packet_writer_write(&data->writer, "acknowledgments\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001693
1694 /* Send Acks */
1695 if (!acks->nr)
Christian Couder6fbbc432020-06-11 14:05:14 +02001696 packet_writer_write(&data->writer, "NAK\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001697
1698 for (i = 0; i < acks->nr; i++) {
Christian Couder6fbbc432020-06-11 14:05:14 +02001699 packet_writer_write(&data->writer, "ACK %s\n",
Jeff Kingfae96272024-02-28 17:37:13 -05001700 oid_to_hex(&acks->objects[i].item->oid));
Brandon Williams3145ea92018-03-15 10:31:27 -07001701 }
1702
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001703 if (!data->wait_for_done && ok_to_give_up(data)) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001704 /* Send Ready */
Christian Couder6fbbc432020-06-11 14:05:14 +02001705 packet_writer_write(&data->writer, "ready\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001706 return 1;
1707 }
1708
1709 return 0;
1710}
1711
Christian Couder389f1612020-05-15 12:04:43 +02001712static int process_haves_and_send_acks(struct upload_pack_data *data)
Brandon Williams3145ea92018-03-15 10:31:27 -07001713{
Brandon Williams3145ea92018-03-15 10:31:27 -07001714 int ret = 0;
1715
Brandon Williams3145ea92018-03-15 10:31:27 -07001716 if (data->done) {
1717 ret = 1;
Jeff Kingfae96272024-02-28 17:37:13 -05001718 } else if (send_acks(data, &data->have_obj)) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001719 packet_writer_delim(&data->writer);
Brandon Williams3145ea92018-03-15 10:31:27 -07001720 ret = 1;
1721 } else {
1722 /* Add Flush */
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001723 packet_writer_flush(&data->writer);
Brandon Williams3145ea92018-03-15 10:31:27 -07001724 ret = 0;
1725 }
1726
Brandon Williams3145ea92018-03-15 10:31:27 -07001727 return ret;
1728}
1729
Brandon Williams516e2b72018-06-27 15:30:17 -07001730static void send_wanted_ref_info(struct upload_pack_data *data)
1731{
Jeff Kingb0650632024-02-28 17:38:40 -05001732 struct hashmap_iter iter;
1733 const struct strmap_entry *e;
Brandon Williams516e2b72018-06-27 15:30:17 -07001734
Jeff Kingb0650632024-02-28 17:38:40 -05001735 if (strmap_empty(&data->wanted_refs))
Brandon Williams516e2b72018-06-27 15:30:17 -07001736 return;
1737
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001738 packet_writer_write(&data->writer, "wanted-refs\n");
Brandon Williams516e2b72018-06-27 15:30:17 -07001739
Jeff Kingb0650632024-02-28 17:38:40 -05001740 strmap_for_each_entry(&data->wanted_refs, &iter, e) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001741 packet_writer_write(&data->writer, "%s %s\n",
Jeff Kingb0650632024-02-28 17:38:40 -05001742 oid_to_hex(e->value),
1743 e->key);
Brandon Williams516e2b72018-06-27 15:30:17 -07001744 }
1745
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001746 packet_writer_delim(&data->writer);
Brandon Williams516e2b72018-06-27 15:30:17 -07001747}
1748
Christian Couder389f1612020-05-15 12:04:43 +02001749static void send_shallow_info(struct upload_pack_data *data)
Brandon Williams685fbd32018-03-15 10:31:28 -07001750{
1751 /* No shallow info needs to be sent */
1752 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
Junio C Hamano00624d62018-07-18 12:20:27 -07001753 !is_repository_shallow(the_repository))
Brandon Williams685fbd32018-03-15 10:31:28 -07001754 return;
1755
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001756 packet_writer_write(&data->writer, "shallow-info\n");
Brandon Williams685fbd32018-03-15 10:31:28 -07001757
Christian Couderee703c82020-06-11 14:05:05 +02001758 if (!send_shallow_list(data) &&
Junio C Hamano00624d62018-07-18 12:20:27 -07001759 is_repository_shallow(the_repository))
Christian Couderb1492f22020-06-11 14:05:06 +02001760 deepen(data, INFINITE_DEPTH);
Brandon Williams685fbd32018-03-15 10:31:28 -07001761
1762 packet_delim(1);
1763}
1764
Brandon Williams3145ea92018-03-15 10:31:27 -07001765enum fetch_state {
1766 FETCH_PROCESS_ARGS = 0,
1767 FETCH_SEND_ACKS,
1768 FETCH_SEND_PACK,
1769 FETCH_DONE,
1770};
1771
Jeff King922fdef2024-02-28 17:46:47 -05001772int upload_pack_v2(struct repository *r, struct packet_reader *request)
Brandon Williams3145ea92018-03-15 10:31:27 -07001773{
1774 enum fetch_state state = FETCH_PROCESS_ARGS;
1775 struct upload_pack_data data;
Jonathan Tand1035ca2018-10-18 13:43:29 -07001776
1777 clear_object_flags(ALL_FLAGS);
Brandon Williams3145ea92018-03-15 10:31:27 -07001778
1779 upload_pack_data_init(&data);
Christian Couderf8edd1c2020-06-04 19:54:41 +02001780 data.use_sideband = LARGE_PACKET_MAX;
Jeff King922fdef2024-02-28 17:46:47 -05001781 get_upload_pack_config(r, &data);
Christian Couder8a0e6f12020-06-04 19:54:45 +02001782
Brandon Williams3145ea92018-03-15 10:31:27 -07001783 while (state != FETCH_DONE) {
1784 switch (state) {
1785 case FETCH_PROCESS_ARGS:
Christian Couder389f1612020-05-15 12:04:43 +02001786 process_args(request, &data);
Brandon Williams3145ea92018-03-15 10:31:27 -07001787
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001788 if (!data.want_obj.nr && !data.wait_for_done) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001789 /*
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001790 * Request didn't contain any 'want' lines (and
1791 * the request does not contain
1792 * "wait-for-done", in which it is reasonable
1793 * to just send 'have's without 'want's); guess
1794 * they didn't want anything.
Brandon Williams3145ea92018-03-15 10:31:27 -07001795 */
1796 state = FETCH_DONE;
Jeff Kingfae96272024-02-28 17:37:13 -05001797 } else if (data.seen_haves) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001798 /*
1799 * Request had 'have' lines, so lets ACK them.
1800 */
1801 state = FETCH_SEND_ACKS;
1802 } else {
1803 /*
1804 * Request had 'want's but no 'have's so we can
1805 * immedietly go to construct and send a pack.
1806 */
1807 state = FETCH_SEND_PACK;
1808 }
1809 break;
1810 case FETCH_SEND_ACKS:
Christian Couder389f1612020-05-15 12:04:43 +02001811 if (process_haves_and_send_acks(&data))
Brandon Williams3145ea92018-03-15 10:31:27 -07001812 state = FETCH_SEND_PACK;
1813 else
1814 state = FETCH_DONE;
1815 break;
1816 case FETCH_SEND_PACK:
Brandon Williams516e2b72018-06-27 15:30:17 -07001817 send_wanted_ref_info(&data);
Christian Couder389f1612020-05-15 12:04:43 +02001818 send_shallow_info(&data);
Brandon Williams685fbd32018-03-15 10:31:28 -07001819
Jonathan Tandd4b7322020-06-10 13:57:23 -07001820 if (data.uri_protocols.nr) {
1821 create_pack_file(&data, &data.uri_protocols);
1822 } else {
1823 packet_writer_write(&data.writer, "packfile\n");
1824 create_pack_file(&data, NULL);
1825 }
Brandon Williams3145ea92018-03-15 10:31:27 -07001826 state = FETCH_DONE;
1827 break;
1828 case FETCH_DONE:
1829 continue;
1830 }
1831 }
1832
1833 upload_pack_data_clear(&data);
1834 return 0;
1835}
Brandon Williams685fbd32018-03-15 10:31:28 -07001836
1837int upload_pack_advertise(struct repository *r,
1838 struct strbuf *value)
1839{
Jeff King9a7b2292024-02-28 17:48:18 -05001840 struct upload_pack_data data;
Brandon Williams516e2b72018-06-27 15:30:17 -07001841
Jeff King9a7b2292024-02-28 17:48:18 -05001842 upload_pack_data_init(&data);
1843 get_upload_pack_config(r, &data);
1844
Brandon Williams685fbd32018-03-15 10:31:28 -07001845 if (value) {
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001846 strbuf_addstr(value, "shallow wait-for-done");
Brandon Williams516e2b72018-06-27 15:30:17 -07001847
Jeff King9a7b2292024-02-28 17:48:18 -05001848 if (data.allow_filter)
Jonathan Tanba957102018-05-03 16:46:56 -07001849 strbuf_addstr(value, " filter");
Brandon Williams516e2b72018-06-27 15:30:17 -07001850
Jeff King9a7b2292024-02-28 17:48:18 -05001851 if (data.allow_ref_in_want)
Brandon Williams516e2b72018-06-27 15:30:17 -07001852 strbuf_addstr(value, " ref-in-want");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001853
Jeff King9a7b2292024-02-28 17:48:18 -05001854 if (data.allow_sideband_all)
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001855 strbuf_addstr(value, " sideband-all");
Jonathan Tandd4b7322020-06-10 13:57:23 -07001856
Jeff Kinga922bfa2024-02-28 17:50:50 -05001857 if (data.allow_packfile_uris)
Jonathan Tandd4b7322020-06-10 13:57:23 -07001858 strbuf_addstr(value, " packfile-uris");
Jonathan Tanba957102018-05-03 16:46:56 -07001859 }
Brandon Williams516e2b72018-06-27 15:30:17 -07001860
Jeff King9a7b2292024-02-28 17:48:18 -05001861 upload_pack_data_clear(&data);
1862
Brandon Williams685fbd32018-03-15 10:31:28 -07001863 return 1;
1864}