blob: 0b8311bd685f002ffb38672c670372b7e698de95 [file] [log] [blame]
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -07003#include "refs.h"
4#include "pkt-line.h"
Junio C Hamano958c24b2006-09-10 03:20:24 -07005#include "sideband.h"
Stefan Beller109cd762018-06-28 18:21:51 -07006#include "repository.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07007#include "object-store.h"
Junio C Hamanof6b42a82005-10-13 18:57:40 -07008#include "tag.h"
9#include "object.h"
Johannes Schindelinf0243f22005-10-28 04:48:32 +020010#include "commit.h"
Johannes Schindelin9b8dc262006-10-30 20:08:43 +010011#include "diff.h"
12#include "revision.h"
13#include "list-objects.h"
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000014#include "list-objects-filter.h"
15#include "list-objects-filter-options.h"
Johannes Sixtcc41fa82007-10-19 21:47:59 +020016#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070017#include "connect.h"
Junio C Hamano051e4002011-08-05 13:54:06 -070018#include "sigchain.h"
Jeff Kingff5effd2012-08-03 12:19:16 -040019#include "version.h"
Junio C Hamanodaebaa72013-01-18 16:08:30 -080020#include "string-list.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040021#include "strvec.h"
Jeff King5411b102016-10-11 17:20:16 -040022#include "prio-queue.h"
Brandon Williamsaa9bab22017-10-16 10:55:26 -070023#include "protocol.h"
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000024#include "quote.h"
Brandon Williamsa3d6b532018-03-14 11:31:41 -070025#include "upload-pack.h"
Brandon Williams3145ea92018-03-15 10:31:27 -070026#include "serve.h"
Derrick Stolee829a3212018-08-20 18:24:34 +000027#include "commit-graph.h"
Derrick Stoleeba3ca1e2018-07-20 16:33:13 +000028#include "commit-reach.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060029#include "shallow.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -070030
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070031/* Remember to update object flag allocation in object.h */
Junio C Hamano937a5152006-07-05 21:28:20 -070032#define THEY_HAVE (1u << 11)
33#define OUR_REF (1u << 12)
34#define WANTED (1u << 13)
35#define COMMON_KNOWN (1u << 14)
Junio C Hamano937a5152006-07-05 21:28:20 -070036
Johannes Schindelinf53514b2006-10-30 20:09:53 +010037#define SHALLOW (1u << 16)
38#define NOT_SHALLOW (1u << 17)
39#define CLIENT_SHALLOW (1u << 18)
Junio C Hamano390eb362013-01-28 21:49:57 -080040#define HIDDEN_REF (1u << 19)
Johannes Schindelinf53514b2006-10-30 20:09:53 +010041
Jonathan Tand1035ca2018-10-18 13:43:29 -070042#define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
43 NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
44
Christian Couder629060d2020-06-11 14:05:12 +020045/* Enum for allowed unadvertised object request (UOR) */
46enum allow_uor {
47 /* Allow specifying sha1 if it is a ref tip. */
48 ALLOW_TIP_SHA1 = 0x01,
49 /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50 ALLOW_REACHABLE_SHA1 = 0x02,
51 /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52 ALLOW_ANY_SHA1 = 0x07
53};
H. Peter Anvin960decc2005-10-19 14:27:01 -070054
Christian Coudera8497282020-06-04 19:54:39 +020055/*
56 * Please annotate, and if possible group together, fields used only
57 * for protocol v0 or only for protocol v2.
58 */
Christian Coudere8498322020-05-15 12:04:44 +020059struct upload_pack_data {
Christian Coudera8497282020-06-04 19:54:39 +020060 struct string_list symref; /* v0 only */
Christian Coudere8498322020-05-15 12:04:44 +020061 struct object_array want_obj;
62 struct object_array have_obj;
Christian Coudera8497282020-06-04 19:54:39 +020063 struct oid_array haves; /* v2 only */
64 struct string_list wanted_refs; /* v2 only */
Christian Coudere8498322020-05-15 12:04:44 +020065
66 struct object_array shallows;
67 struct string_list deepen_not;
Christian Couderde0e9f72020-06-11 14:05:10 +020068 struct object_array extra_edge_obj;
Christian Coudere8498322020-05-15 12:04:44 +020069 int depth;
70 timestamp_t deepen_since;
71 int deepen_rev_list;
72 int deepen_relative;
Christian Couderf203a882020-06-04 19:54:46 +020073 int keepalive;
Christian Couder35b43a12020-06-11 14:05:09 +020074 int shallow_nr;
Christian Couderf01c7912020-06-11 14:05:17 +020075 timestamp_t oldest_have;
Christian Coudere8498322020-05-15 12:04:44 +020076
Christian Couderd40f04e2020-06-04 19:54:40 +020077 unsigned int timeout; /* v0 only */
Christian Coudere9d882b2020-06-04 19:54:44 +020078 enum {
79 NO_MULTI_ACK = 0,
80 MULTI_ACK = 1,
81 MULTI_ACK_DETAILED = 2
82 } multi_ack; /* v0 only */
Christian Couderd40f04e2020-06-04 19:54:40 +020083
Christian Couderf8edd1c2020-06-04 19:54:41 +020084 /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
85 int use_sideband;
86
Jonathan Tandd4b7322020-06-10 13:57:23 -070087 struct string_list uri_protocols;
Christian Couder629060d2020-06-11 14:05:12 +020088 enum allow_uor allow_uor;
Christian Couderf1514c62020-06-11 14:05:11 +020089
Christian Coudere8498322020-05-15 12:04:44 +020090 struct list_objects_filter_options filter_options;
Taylor Blau6dd34562020-08-03 14:00:10 -040091 struct string_list allowed_filters;
Christian Coudere8498322020-05-15 12:04:44 +020092
93 struct packet_writer writer;
94
Christian Couder339a9842020-06-04 19:54:50 +020095 const char *pack_objects_hook;
96
Christian Coudera8497282020-06-04 19:54:39 +020097 unsigned stateless_rpc : 1; /* v0 only */
Christian Couderd40f04e2020-06-04 19:54:40 +020098 unsigned no_done : 1; /* v0 only */
99 unsigned daemon_mode : 1; /* v0 only */
Christian Couder59a90262020-06-04 19:54:42 +0200100 unsigned filter_capability_requested : 1; /* v0 only */
Christian Coudere8498322020-05-15 12:04:44 +0200101
102 unsigned use_thin_pack : 1;
103 unsigned use_ofs_delta : 1;
104 unsigned no_progress : 1;
105 unsigned use_include_tag : 1;
Jonathan Tan9c1e6572021-05-04 14:16:01 -0700106 unsigned wait_for_done : 1;
Christian Couder59abe192020-06-04 19:54:47 +0200107 unsigned allow_filter : 1;
Taylor Blau6dd34562020-08-03 14:00:10 -0400108 unsigned allow_filter_fallback : 1;
Taylor Blau5b01a4e2020-08-03 14:00:17 -0400109 unsigned long tree_filter_max_depth;
Christian Coudera8497282020-06-04 19:54:39 +0200110
111 unsigned done : 1; /* v2 only */
Christian Couderd1d7a942020-06-04 19:54:48 +0200112 unsigned allow_ref_in_want : 1; /* v2 only */
Christian Coudere3835cd2020-06-04 19:54:49 +0200113 unsigned allow_sideband_all : 1; /* v2 only */
Josh Steadmon791e1ad2020-11-11 15:29:27 -0800114 unsigned advertise_sid : 1;
Christian Coudere8498322020-05-15 12:04:44 +0200115};
116
117static void upload_pack_data_init(struct upload_pack_data *data)
118{
Christian Couder438528f2020-05-15 12:04:49 +0200119 struct string_list symref = STRING_LIST_INIT_DUP;
Christian Coudere8498322020-05-15 12:04:44 +0200120 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
121 struct object_array want_obj = OBJECT_ARRAY_INIT;
122 struct object_array have_obj = OBJECT_ARRAY_INIT;
123 struct oid_array haves = OID_ARRAY_INIT;
124 struct object_array shallows = OBJECT_ARRAY_INIT;
125 struct string_list deepen_not = STRING_LIST_INIT_DUP;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700126 struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Christian Couderde0e9f72020-06-11 14:05:10 +0200127 struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
Taylor Blau6dd34562020-08-03 14:00:10 -0400128 struct string_list allowed_filters = STRING_LIST_INIT_DUP;
Christian Coudere8498322020-05-15 12:04:44 +0200129
130 memset(data, 0, sizeof(*data));
Christian Couder438528f2020-05-15 12:04:49 +0200131 data->symref = symref;
Christian Coudere8498322020-05-15 12:04:44 +0200132 data->wanted_refs = wanted_refs;
133 data->want_obj = want_obj;
134 data->have_obj = have_obj;
135 data->haves = haves;
136 data->shallows = shallows;
137 data->deepen_not = deepen_not;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700138 data->uri_protocols = uri_protocols;
Christian Couderde0e9f72020-06-11 14:05:10 +0200139 data->extra_edge_obj = extra_edge_obj;
Taylor Blau6dd34562020-08-03 14:00:10 -0400140 data->allowed_filters = allowed_filters;
141 data->allow_filter_fallback = 1;
Taylor Blau5b01a4e2020-08-03 14:00:17 -0400142 data->tree_filter_max_depth = ULONG_MAX;
Christian Coudere8498322020-05-15 12:04:44 +0200143 packet_writer_init(&data->writer, 1);
Jeff King2a01bde2022-09-11 01:03:07 -0400144 list_objects_filter_init(&data->filter_options);
Christian Couderf203a882020-06-04 19:54:46 +0200145
146 data->keepalive = 5;
Josh Steadmon791e1ad2020-11-11 15:29:27 -0800147 data->advertise_sid = 0;
Christian Coudere8498322020-05-15 12:04:44 +0200148}
149
150static void upload_pack_data_clear(struct upload_pack_data *data)
151{
Christian Couder438528f2020-05-15 12:04:49 +0200152 string_list_clear(&data->symref, 1);
Christian Coudere8498322020-05-15 12:04:44 +0200153 string_list_clear(&data->wanted_refs, 1);
154 object_array_clear(&data->want_obj);
155 object_array_clear(&data->have_obj);
156 oid_array_clear(&data->haves);
157 object_array_clear(&data->shallows);
158 string_list_clear(&data->deepen_not, 0);
Christian Couderde0e9f72020-06-11 14:05:10 +0200159 object_array_clear(&data->extra_edge_obj);
Christian Coudere8498322020-05-15 12:04:44 +0200160 list_objects_filter_release(&data->filter_options);
Taylor Blau8d133f52020-12-03 13:55:18 -0500161 string_list_clear(&data->allowed_filters, 0);
Christian Couder339a9842020-06-04 19:54:50 +0200162
163 free((char *)data->pack_objects_hook);
Christian Coudere8498322020-05-15 12:04:44 +0200164}
165
Christian Couderd40f04e2020-06-04 19:54:40 +0200166static void reset_timeout(unsigned int timeout)
H. Peter Anvin960decc2005-10-19 14:27:01 -0700167{
168 alarm(timeout);
169}
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700170
Christian Couderf8edd1c2020-06-04 19:54:41 +0200171static void send_client_data(int fd, const char *data, ssize_t sz,
172 int use_sideband)
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700173{
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +0200174 if (use_sideband) {
175 send_sideband(1, fd, data, sz, use_sideband);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200176 return;
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +0200177 }
Junio C Hamano958c24b2006-09-10 03:20:24 -0700178 if (fd == 3)
179 /* emergency quit */
180 fd = 2;
181 if (fd == 2) {
Andy Whitcroft93822c22007-01-08 15:58:23 +0000182 /* XXX: are we happy to lose stuff here? */
Junio C Hamano958c24b2006-09-10 03:20:24 -0700183 xwrite(fd, data, sz);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200184 return;
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700185 }
Jeff Kingcdf4fb82013-02-20 15:01:56 -0500186 write_or_die(fd, data, sz);
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700187}
188
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700189static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
190{
191 FILE *fp = cb_data;
192 if (graft->nr_parent == -1)
brian m. carlson7683e2e2015-03-13 23:39:34 +0000193 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700194 return 0;
195}
196
Jonathan Tanacaaca72020-06-10 13:57:21 -0700197struct output_state {
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100198 /*
199 * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with
200 * sideband-64k the band designator takes up 1 byte of space. Because
201 * relay_pack_data keeps the last byte to itself, we make the buffer 1
202 * byte bigger than the intended maximum write size.
203 */
204 char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1];
Jonathan Tanacaaca72020-06-10 13:57:21 -0700205 int used;
Jonathan Tandd4b7322020-06-10 13:57:23 -0700206 unsigned packfile_uris_started : 1;
207 unsigned packfile_started : 1;
Jonathan Tanacaaca72020-06-10 13:57:21 -0700208};
209
210static int relay_pack_data(int pack_objects_out, struct output_state *os,
Jonathan Tandd4b7322020-06-10 13:57:23 -0700211 int use_sideband, int write_packfile_line)
Jonathan Tanacaaca72020-06-10 13:57:21 -0700212{
213 /*
214 * We keep the last byte to ourselves
215 * in case we detect broken rev-list, so that we
216 * can leave the stream corrupted. This is
217 * unfortunate -- unpack-objects would happily
218 * accept a valid packdata with trailing garbage,
219 * so appending garbage after we pass all the
220 * pack data is not good enough to signal
221 * breakage to downstream.
222 */
223 ssize_t readsz;
224
225 readsz = xread(pack_objects_out, os->buffer + os->used,
226 sizeof(os->buffer) - os->used);
227 if (readsz < 0) {
228 return readsz;
229 }
230 os->used += readsz;
231
Jonathan Tandd4b7322020-06-10 13:57:23 -0700232 while (!os->packfile_started) {
233 char *p;
234 if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
235 os->packfile_started = 1;
236 if (write_packfile_line) {
237 if (os->packfile_uris_started)
238 packet_delim(1);
239 packet_write_fmt(1, "\1packfile\n");
240 }
241 break;
242 }
243 if ((p = memchr(os->buffer, '\n', os->used))) {
244 if (!os->packfile_uris_started) {
245 os->packfile_uris_started = 1;
246 if (!write_packfile_line)
247 BUG("packfile_uris requires sideband-all");
248 packet_write_fmt(1, "\1packfile-uris\n");
249 }
250 *p = '\0';
251 packet_write_fmt(1, "\1%s\n", os->buffer);
252
253 os->used -= p - os->buffer + 1;
254 memmove(os->buffer, p + 1, os->used);
255 } else {
256 /*
257 * Incomplete line.
258 */
259 return readsz;
260 }
261 }
262
Jonathan Tanacaaca72020-06-10 13:57:21 -0700263 if (os->used > 1) {
264 send_client_data(1, os->buffer, os->used - 1, use_sideband);
265 os->buffer[0] = os->buffer[os->used - 1];
266 os->used = 1;
267 } else {
268 send_client_data(1, os->buffer, os->used, use_sideband);
269 os->used = 0;
270 }
271
272 return readsz;
273}
274
Jonathan Tandd4b7322020-06-10 13:57:23 -0700275static void create_pack_file(struct upload_pack_data *pack_data,
276 const struct string_list *uri_protocols)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700277{
René Scharfed3180272014-08-19 21:09:35 +0200278 struct child_process pack_objects = CHILD_PROCESS_INIT;
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100279 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
Jonathan Tanacaaca72020-06-10 13:57:21 -0700280 char progress[128];
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700281 char abort_msg[] = "aborting due to possible repository "
282 "corruption on the remote side.";
Junio C Hamano1456b042009-12-10 12:17:11 -0800283 ssize_t sz;
Michael Procter65a36292016-02-25 12:13:26 +0000284 int i;
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700285 FILE *pipe_fd;
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700286
Christian Couder339a9842020-06-04 19:54:50 +0200287 if (!pack_data->pack_objects_hook)
Jeff King20b20a22016-05-18 18:45:37 -0400288 pack_objects.git_cmd = 1;
289 else {
Jeff Kingc972bf42020-07-28 16:25:12 -0400290 strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
291 strvec_push(&pack_objects.args, "git");
Jeff King20b20a22016-05-18 18:45:37 -0400292 pack_objects.use_shell = 1;
293 }
294
Christian Couder35b43a12020-06-11 14:05:09 +0200295 if (pack_data->shallow_nr) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400296 strvec_push(&pack_objects.args, "--shallow-file");
297 strvec_push(&pack_objects.args, "");
Nick Edelenf0cea832009-06-10 01:50:18 +0200298 }
Jeff Kingc972bf42020-07-28 16:25:12 -0400299 strvec_push(&pack_objects.args, "pack-objects");
300 strvec_push(&pack_objects.args, "--revs");
Jeff Kingb5a20682020-06-04 19:54:38 +0200301 if (pack_data->use_thin_pack)
Jeff Kingc972bf42020-07-28 16:25:12 -0400302 strvec_push(&pack_objects.args, "--thin");
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700303
Jeff Kingc972bf42020-07-28 16:25:12 -0400304 strvec_push(&pack_objects.args, "--stdout");
Christian Couder35b43a12020-06-11 14:05:09 +0200305 if (pack_data->shallow_nr)
Jeff Kingc972bf42020-07-28 16:25:12 -0400306 strvec_push(&pack_objects.args, "--shallow");
Jeff Kingb5a20682020-06-04 19:54:38 +0200307 if (!pack_data->no_progress)
Jeff Kingc972bf42020-07-28 16:25:12 -0400308 strvec_push(&pack_objects.args, "--progress");
Jeff Kingb5a20682020-06-04 19:54:38 +0200309 if (pack_data->use_ofs_delta)
Jeff Kingc972bf42020-07-28 16:25:12 -0400310 strvec_push(&pack_objects.args, "--delta-base-offset");
Jeff Kingb5a20682020-06-04 19:54:38 +0200311 if (pack_data->use_include_tag)
Jeff Kingc972bf42020-07-28 16:25:12 -0400312 strvec_push(&pack_objects.args, "--include-tag");
Christian Couderc9f03252020-05-15 12:04:53 +0200313 if (pack_data->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700314 const char *spec =
Christian Couderc9f03252020-05-15 12:04:53 +0200315 expand_list_objects_filter_spec(&pack_data->filter_options);
Jacob Vosmaerad5df6b2021-01-28 17:04:53 +0100316 strvec_pushf(&pack_objects.args, "--filter=%s", spec);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000317 }
Jonathan Tandd4b7322020-06-10 13:57:23 -0700318 if (uri_protocols) {
319 for (i = 0; i < uri_protocols->nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -0400320 strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
Jonathan Tandd4b7322020-06-10 13:57:23 -0700321 uri_protocols->items[i].string);
322 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200323
Jeff Kingb9612192011-04-06 17:33:33 -0400324 pack_objects.in = -1;
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200325 pack_objects.out = -1;
326 pack_objects.err = -1;
Jeff King309a4022020-12-01 07:15:13 -0500327 pack_objects.clean_on_exit = 1;
Johannes Sixt21edd3f2007-10-19 21:48:03 +0200328
Johannes Sixt4c324c02007-11-04 20:46:48 +0100329 if (start_command(&pack_objects))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700330 die("git upload-pack: unable to fork git-pack-objects");
Johannes Schindelin83a5ad62007-02-20 03:01:44 +0100331
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700332 pipe_fd = xfdopen(pack_objects.in, "w");
Nick Edelenf0cea832009-06-10 01:50:18 +0200333
Christian Couder35b43a12020-06-11 14:05:09 +0200334 if (pack_data->shallow_nr)
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700335 for_each_commit_graft(write_one_shallow, pipe_fd);
336
Christian Couderc9f03252020-05-15 12:04:53 +0200337 for (i = 0; i < pack_data->want_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700338 fprintf(pipe_fd, "%s\n",
Christian Couderc9f03252020-05-15 12:04:53 +0200339 oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700340 fprintf(pipe_fd, "--not\n");
Christian Couderc9f03252020-05-15 12:04:53 +0200341 for (i = 0; i < pack_data->have_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700342 fprintf(pipe_fd, "%s\n",
Christian Couderc9f03252020-05-15 12:04:53 +0200343 oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
Christian Couderde0e9f72020-06-11 14:05:10 +0200344 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700345 fprintf(pipe_fd, "%s\n",
Christian Couderde0e9f72020-06-11 14:05:10 +0200346 oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700347 fprintf(pipe_fd, "\n");
348 fflush(pipe_fd);
349 fclose(pipe_fd);
Nick Edelenf0cea832009-06-10 01:50:18 +0200350
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200351 /* We read from pack_objects.err to capture stderr output for
352 * progress bar, and pack_objects.out to capture the pack data.
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700353 */
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700354
355 while (1) {
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700356 struct pollfd pfd[2];
Christian Couderf203a882020-06-04 19:54:46 +0200357 int pe, pu, pollsize, polltimeout;
Jeff King05e95152013-09-08 05:01:31 -0400358 int ret;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700359
Christian Couderd40f04e2020-06-04 19:54:40 +0200360 reset_timeout(pack_data->timeout);
Matthias Lederhofer0d516ad2006-07-18 19:14:51 +0200361
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700362 pollsize = 0;
Junio C Hamano363b7812006-06-20 22:48:23 -0700363 pe = pu = -1;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700364
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200365 if (0 <= pack_objects.out) {
366 pfd[pollsize].fd = pack_objects.out;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700367 pfd[pollsize].events = POLLIN;
368 pu = pollsize;
369 pollsize++;
370 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200371 if (0 <= pack_objects.err) {
372 pfd[pollsize].fd = pack_objects.err;
Junio C Hamano363b7812006-06-20 22:48:23 -0700373 pfd[pollsize].events = POLLIN;
374 pe = pollsize;
375 pollsize++;
376 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700377
Johannes Sixt4c324c02007-11-04 20:46:48 +0100378 if (!pollsize)
379 break;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700380
Christian Couderf203a882020-06-04 19:54:46 +0200381 polltimeout = pack_data->keepalive < 0
382 ? -1
383 : 1000 * pack_data->keepalive;
384
385 ret = poll(pfd, pollsize, polltimeout);
Edward Thomson6c71f8b2014-08-22 15:19:11 +0000386
Jeff King05e95152013-09-08 05:01:31 -0400387 if (ret < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100388 if (errno != EINTR) {
Nguyễn Thái Ngọc Duyd2b6afa2016-05-08 16:47:59 +0700389 error_errno("poll failed, resuming");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100390 sleep(1);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700391 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700392 continue;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100393 }
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500394 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
395 /* Status ready; we ship that in the side-band
396 * or dump to the standard error.
397 */
398 sz = xread(pack_objects.err, progress,
399 sizeof(progress));
400 if (0 < sz)
Christian Couderf8edd1c2020-06-04 19:54:41 +0200401 send_client_data(2, progress, sz,
402 pack_data->use_sideband);
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500403 else if (sz == 0) {
404 close(pack_objects.err);
405 pack_objects.err = -1;
406 }
407 else
408 goto fail;
409 /* give priority to status messages */
410 continue;
411 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100412 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
Jonathan Tanacaaca72020-06-10 13:57:21 -0700413 int result = relay_pack_data(pack_objects.out,
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100414 output_state,
Jonathan Tandd4b7322020-06-10 13:57:23 -0700415 pack_data->use_sideband,
416 !!uri_protocols);
Jonathan Tanacaaca72020-06-10 13:57:21 -0700417
418 if (result == 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100419 close(pack_objects.out);
420 pack_objects.out = -1;
Jonathan Tanacaaca72020-06-10 13:57:21 -0700421 } else if (result < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100422 goto fail;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100423 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700424 }
Jeff King05e95152013-09-08 05:01:31 -0400425
426 /*
427 * We hit the keepalive timeout without saying anything; send
428 * an empty message on the data sideband just to let the other
429 * side know we're still working on it, but don't have any data
430 * yet.
431 *
432 * If we don't have a sideband channel, there's no room in the
433 * protocol to say anything, so those clients are just out of
434 * luck.
435 */
Christian Couderf8edd1c2020-06-04 19:54:41 +0200436 if (!ret && pack_data->use_sideband) {
Jeff King05e95152013-09-08 05:01:31 -0400437 static const char buf[] = "0005\1";
438 write_or_die(1, buf, 5);
439 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700440 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100441
442 if (finish_command(&pack_objects)) {
Junio C Hamano7e44c932008-08-31 09:39:19 -0700443 error("git upload-pack: git-pack-objects died with error.");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100444 goto fail;
445 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100446
447 /* flush the data */
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100448 if (output_state->used > 0) {
449 send_client_data(1, output_state->buffer, output_state->used,
Christian Couderf8edd1c2020-06-04 19:54:41 +0200450 pack_data->use_sideband);
Johannes Sixt4c324c02007-11-04 20:46:48 +0100451 fprintf(stderr, "flushed.\n");
452 }
Jacob Vosmaer55a96512021-12-14 20:46:26 +0100453 free(output_state);
Christian Couderf8edd1c2020-06-04 19:54:41 +0200454 if (pack_data->use_sideband)
Johannes Sixt4c324c02007-11-04 20:46:48 +0100455 packet_flush(1);
456 return;
457
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700458 fail:
Ævar Arnfjörð Bjarmasonc68d5db2022-07-28 01:13:42 +0200459 free(output_state);
Christian Couderf8edd1c2020-06-04 19:54:41 +0200460 send_client_data(3, abort_msg, sizeof(abort_msg),
461 pack_data->use_sideband);
Junio C Hamano7e44c932008-08-31 09:39:19 -0700462 die("git upload-pack: %s", abort_msg);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700463}
464
Christian Couderea2c6e62020-06-11 14:05:18 +0200465static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700466{
Junio C Hamano937a5152006-07-05 21:28:20 -0700467 int we_knew_they_have = 0;
Christian Couderea2c6e62020-06-11 14:05:18 +0200468 struct object *o = parse_object(the_repository, oid);
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700469
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700470 if (!o)
brian m. carlsoncf939822017-05-06 22:10:28 +0000471 die("oops (%s)", oid_to_hex(oid));
Junio C Hamano182a8da2006-08-12 22:16:51 -0700472 if (o->type == OBJ_COMMIT) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700473 struct commit_list *parents;
Junio C Hamano937a5152006-07-05 21:28:20 -0700474 struct commit *commit = (struct commit *)o;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700475 if (o->flags & THEY_HAVE)
Junio C Hamano937a5152006-07-05 21:28:20 -0700476 we_knew_they_have = 1;
477 else
478 o->flags |= THEY_HAVE;
Christian Couderf01c7912020-06-11 14:05:17 +0200479 if (!data->oldest_have || (commit->date < data->oldest_have))
480 data->oldest_have = commit->date;
Junio C Hamano937a5152006-07-05 21:28:20 -0700481 for (parents = commit->parents;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700482 parents;
483 parents = parents->next)
484 parents->item->object.flags |= THEY_HAVE;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700485 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700486 if (!we_knew_they_have) {
Christian Couder460ed0d2020-06-11 14:05:16 +0200487 add_object_array(o, NULL, &data->have_obj);
Junio C Hamano937a5152006-07-05 21:28:20 -0700488 return 1;
489 }
490 return 0;
491}
492
Christian Couderea2c6e62020-06-11 14:05:18 +0200493static int got_oid(struct upload_pack_data *data,
494 const char *hex, struct object_id *oid)
495{
496 if (get_oid_hex(hex, oid))
497 die("git upload-pack: expected SHA1 object, got '%s'", hex);
Jonathan Tan77aa0942020-07-16 11:09:50 -0700498 if (!has_object_file_with_flags(oid,
499 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
Christian Couderea2c6e62020-06-11 14:05:18 +0200500 return -1;
501 return do_got_oid(data, oid);
502}
503
Christian Couder08667342020-06-11 14:05:15 +0200504static int ok_to_give_up(struct upload_pack_data *data)
Junio C Hamano937a5152006-07-05 21:28:20 -0700505{
Abhishek Kumard7f92782021-01-16 18:11:13 +0000506 timestamp_t min_generation = GENERATION_NUMBER_ZERO;
Junio C Hamano937a5152006-07-05 21:28:20 -0700507
Christian Couder08667342020-06-11 14:05:15 +0200508 if (!data->have_obj.nr)
Junio C Hamano937a5152006-07-05 21:28:20 -0700509 return 0;
510
Christian Couder08667342020-06-11 14:05:15 +0200511 return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
Christian Couderf01c7912020-06-11 14:05:17 +0200512 COMMON_KNOWN, data->oldest_have,
Derrick Stolee4fbcca42018-07-20 16:33:28 +0000513 min_generation);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700514}
515
Christian Couder07977692020-05-15 12:04:46 +0200516static int get_common_commits(struct upload_pack_data *data,
517 struct packet_reader *reader)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700518{
brian m. carlsoncf939822017-05-06 22:10:28 +0000519 struct object_id oid;
520 char last_hex[GIT_MAX_HEXSZ + 1];
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700521 int got_common = 0;
522 int got_other = 0;
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700523 int sent_ready = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700524
Johannes Schindelinf0243f22005-10-28 04:48:32 +0200525 save_commit_buffer = 0;
526
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400527 for (;;) {
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700528 const char *arg;
529
Christian Couderd40f04e2020-06-04 19:54:40 +0200530 reset_timeout(data->timeout);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700531
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800532 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
Christian Coudere9d882b2020-06-04 19:54:44 +0200533 if (data->multi_ack == MULTI_ACK_DETAILED
Christian Couder07977692020-05-15 12:04:46 +0200534 && got_common
535 && !got_other
Christian Couder08667342020-06-11 14:05:15 +0200536 && ok_to_give_up(data)) {
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700537 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700538 packet_write_fmt(1, "ACK %s ready\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700539 }
Christian Couder53d69502020-06-04 19:54:43 +0200540 if (data->have_obj.nr == 0 || data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700541 packet_write_fmt(1, "NAK\n");
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700542
Christian Couderd40f04e2020-06-04 19:54:40 +0200543 if (data->no_done && sent_ready) {
Lars Schneider81c634e2016-10-16 16:20:29 -0700544 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700545 return 0;
546 }
Christian Couderdf654ab2020-05-15 12:04:52 +0200547 if (data->stateless_rpc)
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700548 exit(0);
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700549 got_common = 0;
550 got_other = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700551 continue;
552 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800553 if (skip_prefix(reader->line, "have ", &arg)) {
Christian Couder460ed0d2020-06-11 14:05:16 +0200554 switch (got_oid(data, arg, &oid)) {
Junio C Hamano937a5152006-07-05 21:28:20 -0700555 case -1: /* they have what we do not */
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700556 got_other = 1;
Christian Couder53d69502020-06-04 19:54:43 +0200557 if (data->multi_ack
Christian Couder08667342020-06-11 14:05:15 +0200558 && ok_to_give_up(data)) {
brian m. carlsoncf939822017-05-06 22:10:28 +0000559 const char *hex = oid_to_hex(&oid);
Christian Coudere9d882b2020-06-04 19:54:44 +0200560 if (data->multi_ack == MULTI_ACK_DETAILED) {
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700561 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700562 packet_write_fmt(1, "ACK %s ready\n", hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700563 } else
Lars Schneider81c634e2016-10-16 16:20:29 -0700564 packet_write_fmt(1, "ACK %s continue\n", hex);
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700565 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700566 break;
567 default:
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700568 got_common = 1;
brian m. carlson55dc2272018-05-02 00:25:51 +0000569 oid_to_hex_r(last_hex, &oid);
Christian Coudere9d882b2020-06-04 19:54:44 +0200570 if (data->multi_ack == MULTI_ACK_DETAILED)
Lars Schneider81c634e2016-10-16 16:20:29 -0700571 packet_write_fmt(1, "ACK %s common\n", last_hex);
Christian Couder53d69502020-06-04 19:54:43 +0200572 else if (data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700573 packet_write_fmt(1, "ACK %s continue\n", last_hex);
Christian Couder07977692020-05-15 12:04:46 +0200574 else if (data->have_obj.nr == 1)
Lars Schneider81c634e2016-10-16 16:20:29 -0700575 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano937a5152006-07-05 21:28:20 -0700576 break;
Junio C Hamanoaf2d3aa2005-10-25 14:55:24 -0700577 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700578 continue;
579 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800580 if (!strcmp(reader->line, "done")) {
Christian Couder07977692020-05-15 12:04:46 +0200581 if (data->have_obj.nr > 0) {
Christian Couder53d69502020-06-04 19:54:43 +0200582 if (data->multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700583 packet_write_fmt(1, "ACK %s\n", last_hex);
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200584 return 0;
585 }
Lars Schneider81c634e2016-10-16 16:20:29 -0700586 packet_write_fmt(1, "NAK\n");
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700587 return -1;
588 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800589 die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700590 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700591}
592
Christian Couder629060d2020-06-11 14:05:12 +0200593static int is_our_ref(struct object *o, enum allow_uor allow_uor)
Junio C Hamano390eb362013-01-28 21:49:57 -0800594{
Christian Couder629060d2020-06-11 14:05:12 +0200595 int allow_hidden_ref = (allow_uor &
596 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Fredrik Medley7199c092015-05-21 22:23:38 +0200597 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
Junio C Hamano390eb362013-01-28 21:49:57 -0800598}
599
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700600/*
601 * on successful case, it's up to the caller to close cmd->out
602 */
603static int do_reachable_revlist(struct child_process *cmd,
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700604 struct object_array *src,
Christian Couderf1514c62020-06-11 14:05:11 +0200605 struct object_array *reachable,
Christian Couder629060d2020-06-11 14:05:12 +0200606 enum allow_uor allow_uor)
Junio C Hamano051e4002011-08-05 13:54:06 -0700607{
Junio C Hamano051e4002011-08-05 13:54:06 -0700608 struct object *o;
René Scharfea698d672020-08-12 18:52:55 +0200609 FILE *cmd_in = NULL;
Junio C Hamano051e4002011-08-05 13:54:06 -0700610 int i;
611
Ævar Arnfjörð Bjarmason2b709892021-11-25 23:52:20 +0100612 strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700613 cmd->git_cmd = 1;
614 cmd->no_stderr = 1;
615 cmd->in = -1;
616 cmd->out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700617
618 /*
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700619 * If the next rev-list --stdin encounters an unknown commit,
620 * it terminates, which will cause SIGPIPE in the write loop
Junio C Hamano051e4002011-08-05 13:54:06 -0700621 * below.
622 */
623 sigchain_push(SIGPIPE, SIG_IGN);
624
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700625 if (start_command(cmd))
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700626 goto error;
627
René Scharfea698d672020-08-12 18:52:55 +0200628 cmd_in = xfdopen(cmd->in, "w");
629
Junio C Hamano051e4002011-08-05 13:54:06 -0700630 for (i = get_max_object_index(); 0 < i; ) {
631 o = get_indexed_object(--i);
Brian Harring2a745322011-08-23 22:47:17 -0700632 if (!o)
633 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700634 if (reachable && o->type == OBJ_COMMIT)
635 o->flags &= ~TMP_MARK;
Christian Couder629060d2020-06-11 14:05:12 +0200636 if (!is_our_ref(o, allow_uor))
Junio C Hamano051e4002011-08-05 13:54:06 -0700637 continue;
René Scharfea698d672020-08-12 18:52:55 +0200638 if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700639 goto error;
640 }
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700641 for (i = 0; i < src->nr; i++) {
642 o = src->objects[i].item;
Christian Couder629060d2020-06-11 14:05:12 +0200643 if (is_our_ref(o, allow_uor)) {
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700644 if (reachable)
645 add_object_array(o, NULL, reachable);
Junio C Hamano051e4002011-08-05 13:54:06 -0700646 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700647 }
648 if (reachable && o->type == OBJ_COMMIT)
649 o->flags |= TMP_MARK;
René Scharfea698d672020-08-12 18:52:55 +0200650 if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700651 goto error;
652 }
René Scharfea698d672020-08-12 18:52:55 +0200653 if (ferror(cmd_in) || fflush(cmd_in))
654 goto error;
655 fclose(cmd_in);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700656 cmd->in = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700657 sigchain_pop(SIGPIPE);
658
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700659 return 0;
660
661error:
662 sigchain_pop(SIGPIPE);
663
René Scharfea698d672020-08-12 18:52:55 +0200664 if (cmd_in)
665 fclose(cmd_in);
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700666 if (cmd->out >= 0)
667 close(cmd->out);
668 return -1;
669}
670
Christian Couderf1514c62020-06-11 14:05:11 +0200671static int get_reachable_list(struct upload_pack_data *data,
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700672 struct object_array *reachable)
673{
674 struct child_process cmd = CHILD_PROCESS_INIT;
675 int i;
676 struct object *o;
brian m. carlson55dc2272018-05-02 00:25:51 +0000677 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
678 const unsigned hexsz = the_hash_algo->hexsz;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700679
Christian Couderf1514c62020-06-11 14:05:11 +0200680 if (do_reachable_revlist(&cmd, &data->shallows, reachable,
Christian Couder629060d2020-06-11 14:05:12 +0200681 data->allow_uor) < 0)
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700682 return -1;
683
brian m. carlson55dc2272018-05-02 00:25:51 +0000684 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
Jeff King62b89d42019-06-20 03:40:54 -0400685 struct object_id oid;
brian m. carlson55dc2272018-05-02 00:25:51 +0000686 const char *p;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700687
Jeff King62b89d42019-06-20 03:40:54 -0400688 if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700689 break;
690
Jeff Kingd0229ab2019-06-20 03:41:14 -0400691 o = lookup_object(the_repository, &oid);
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700692 if (o && o->type == OBJ_COMMIT) {
693 o->flags &= ~TMP_MARK;
694 }
695 }
696 for (i = get_max_object_index(); 0 < i; i--) {
697 o = get_indexed_object(i - 1);
698 if (o && o->type == OBJ_COMMIT &&
699 (o->flags & TMP_MARK)) {
700 add_object_array(o, NULL, reachable);
701 o->flags &= ~TMP_MARK;
702 }
703 }
704 close(cmd.out);
705
706 if (finish_command(&cmd))
707 return -1;
708
709 return 0;
710}
711
Christian Couder629060d2020-06-11 14:05:12 +0200712static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700713{
714 struct child_process cmd = CHILD_PROCESS_INIT;
715 char buf[1];
716 int i;
717
Christian Couder629060d2020-06-11 14:05:12 +0200718 if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700719 return 1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700720
721 /*
722 * The commits out of the rev-list are not ancestors of
723 * our ref.
724 */
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700725 i = read_in_full(cmd.out, buf, 1);
Junio C Hamano051e4002011-08-05 13:54:06 -0700726 if (i)
727 goto error;
728 close(cmd.out);
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700729 cmd.out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700730
731 /*
732 * rev-list may have died by encountering a bad commit
733 * in the history, in which case we do want to bail out
734 * even when it showed no commit.
735 */
736 if (finish_command(&cmd))
737 goto error;
738
739 /* All the non-tip ones are ancestors of what we advertised */
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700740 return 0;
Junio C Hamano051e4002011-08-05 13:54:06 -0700741
742error:
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700743 if (cmd.out >= 0)
744 close(cmd.out);
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700745 return 1;
746}
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700747
Christian Couderb08c9742020-05-15 12:04:51 +0200748static void check_non_tip(struct upload_pack_data *data)
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700749{
750 int i;
751
752 /*
753 * In the normal in-process case without
754 * uploadpack.allowReachableSHA1InWant,
755 * non-tip requests can never happen.
756 */
Christian Couder629060d2020-06-11 14:05:12 +0200757 if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700758 goto error;
Christian Couder629060d2020-06-11 14:05:12 +0200759 if (!has_unreachable(&data->want_obj, data->allow_uor))
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700760 /* All the non-tip ones are ancestors of what we advertised */
761 return;
Junio C Hamano051e4002011-08-05 13:54:06 -0700762
763error:
764 /* Pick one of them (we know there at least is one) */
Christian Couderb08c9742020-05-15 12:04:51 +0200765 for (i = 0; i < data->want_obj.nr; i++) {
766 struct object *o = data->want_obj.objects[i].item;
Christian Couder629060d2020-06-11 14:05:12 +0200767 if (!is_our_ref(o, data->allow_uor)) {
Christian Couderb08c9742020-05-15 12:04:51 +0200768 packet_writer_error(&data->writer,
Jeff King014ade72019-04-13 01:53:34 -0400769 "upload-pack: not our ref %s",
770 oid_to_hex(&o->oid));
Junio C Hamano051e4002011-08-05 13:54:06 -0700771 die("git upload-pack: not our ref %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000772 oid_to_hex(&o->oid));
Jeff King014ade72019-04-13 01:53:34 -0400773 }
Junio C Hamano051e4002011-08-05 13:54:06 -0700774 }
775}
776
Christian Couder35b43a12020-06-11 14:05:09 +0200777static void send_shallow(struct upload_pack_data *data,
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800778 struct commit_list *result)
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700779{
780 while (result) {
781 struct object *object = &result->item->object;
782 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
Christian Couder35b43a12020-06-11 14:05:09 +0200783 packet_writer_write(&data->writer, "shallow %s",
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800784 oid_to_hex(&object->oid));
Stefan Beller19143f12018-05-17 15:51:44 -0700785 register_shallow(the_repository, &object->oid);
Christian Couder35b43a12020-06-11 14:05:09 +0200786 data->shallow_nr++;
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700787 }
788 result = result->next;
789 }
790}
791
Christian Couder329f9962020-06-11 14:05:08 +0200792static void send_unshallow(struct upload_pack_data *data)
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700793{
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700794 int i;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700795
Christian Couder329f9962020-06-11 14:05:08 +0200796 for (i = 0; i < data->shallows.nr; i++) {
797 struct object *object = data->shallows.objects[i].item;
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700798 if (object->flags & NOT_SHALLOW) {
799 struct commit_list *parents;
Christian Couder329f9962020-06-11 14:05:08 +0200800 packet_writer_write(&data->writer, "unshallow %s",
Jonathan Tanbc2e7952019-01-15 11:40:27 -0800801 oid_to_hex(&object->oid));
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700802 object->flags &= ~CLIENT_SHALLOW;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700803 /*
804 * We want to _register_ "object" as shallow, but we
805 * also need to traverse object's parents to deepen a
806 * shallow clone. Unregister it for now so we can
807 * parse and add the parents to the want list, then
808 * re-register it.
809 */
brian m. carlsone92b8482017-05-06 22:10:06 +0000810 unregister_shallow(&object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700811 object->parsed = 0;
812 parse_commit_or_die((struct commit *)object);
813 parents = ((struct commit *)object)->parents;
814 while (parents) {
815 add_object_array(&parents->item->object,
Christian Couder329f9962020-06-11 14:05:08 +0200816 NULL, &data->want_obj);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700817 parents = parents->next;
818 }
Christian Couderde0e9f72020-06-11 14:05:10 +0200819 add_object_array(object, NULL, &data->extra_edge_obj);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700820 }
821 /* make sure commit traversal conforms to client */
Stefan Beller19143f12018-05-17 15:51:44 -0700822 register_shallow(the_repository, &object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700823 }
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700824}
825
Jonathan Tan5056cf42018-12-18 13:24:35 -0800826static int check_ref(const char *refname_full, const struct object_id *oid,
827 int flag, void *cb_data);
Christian Couderb1492f22020-06-11 14:05:06 +0200828static void deepen(struct upload_pack_data *data, int depth)
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700829{
Stefan Bellerc8813482018-05-17 15:51:46 -0700830 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700831 int i;
832
Christian Couderb1492f22020-06-11 14:05:06 +0200833 for (i = 0; i < data->shallows.nr; i++) {
834 struct object *object = data->shallows.objects[i].item;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700835 object->flags |= NOT_SHALLOW;
836 }
Christian Couderb1492f22020-06-11 14:05:06 +0200837 } else if (data->deepen_relative) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700838 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
839 struct commit_list *result;
840
Jonathan Tan5056cf42018-12-18 13:24:35 -0800841 /*
842 * Checking for reachable shallows requires that our refs be
843 * marked with OUR_REF.
844 */
845 head_ref_namespaced(check_ref, NULL);
846 for_each_namespaced_ref(check_ref, NULL);
847
Christian Couderf1514c62020-06-11 14:05:11 +0200848 get_reachable_list(data, &reachable_shallows);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700849 result = get_shallow_commits(&reachable_shallows,
850 depth + 1,
851 SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200852 send_shallow(data, result);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700853 free_commit_list(result);
854 object_array_clear(&reachable_shallows);
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700855 } else {
856 struct commit_list *result;
857
Christian Couderb1492f22020-06-11 14:05:06 +0200858 result = get_shallow_commits(&data->want_obj, depth,
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700859 SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200860 send_shallow(data, result);
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700861 free_commit_list(result);
862 }
863
Christian Couder329f9962020-06-11 14:05:08 +0200864 send_unshallow(data);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700865}
866
Christian Couder446e42c2020-06-11 14:05:07 +0200867static void deepen_by_rev_list(struct upload_pack_data *data,
868 int ac,
869 const char **av)
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700870{
871 struct commit_list *result;
872
Jeff King6abada12019-09-12 10:44:45 -0400873 disable_commit_graph(the_repository);
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700874 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
Christian Couder35b43a12020-06-11 14:05:09 +0200875 send_shallow(data, result);
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700876 free_commit_list(result);
Christian Couder329f9962020-06-11 14:05:08 +0200877 send_unshallow(data);
Brandon Williams685fbd32018-03-15 10:31:28 -0700878}
879
880/* Returns 1 if a shallow list is sent or 0 otherwise */
Christian Couderee703c82020-06-11 14:05:05 +0200881static int send_shallow_list(struct upload_pack_data *data)
Brandon Williams685fbd32018-03-15 10:31:28 -0700882{
883 int ret = 0;
884
Christian Couderee703c82020-06-11 14:05:05 +0200885 if (data->depth > 0 && data->deepen_rev_list)
Brandon Williams685fbd32018-03-15 10:31:28 -0700886 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
Christian Couderee703c82020-06-11 14:05:05 +0200887 if (data->depth > 0) {
Christian Couderb1492f22020-06-11 14:05:06 +0200888 deepen(data, data->depth);
Brandon Williams685fbd32018-03-15 10:31:28 -0700889 ret = 1;
Christian Couderee703c82020-06-11 14:05:05 +0200890 } else if (data->deepen_rev_list) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400891 struct strvec av = STRVEC_INIT;
Brandon Williams685fbd32018-03-15 10:31:28 -0700892 int i;
893
Jeff Kingc972bf42020-07-28 16:25:12 -0400894 strvec_push(&av, "rev-list");
Christian Couderee703c82020-06-11 14:05:05 +0200895 if (data->deepen_since)
Jeff Kingc972bf42020-07-28 16:25:12 -0400896 strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
Christian Couderee703c82020-06-11 14:05:05 +0200897 if (data->deepen_not.nr) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400898 strvec_push(&av, "--not");
Christian Couderee703c82020-06-11 14:05:05 +0200899 for (i = 0; i < data->deepen_not.nr; i++) {
900 struct string_list_item *s = data->deepen_not.items + i;
Jeff Kingc972bf42020-07-28 16:25:12 -0400901 strvec_push(&av, s->string);
Brandon Williams685fbd32018-03-15 10:31:28 -0700902 }
Jeff Kingc972bf42020-07-28 16:25:12 -0400903 strvec_push(&av, "--not");
Brandon Williams685fbd32018-03-15 10:31:28 -0700904 }
Christian Couderee703c82020-06-11 14:05:05 +0200905 for (i = 0; i < data->want_obj.nr; i++) {
906 struct object *o = data->want_obj.objects[i].item;
Jeff Kingc972bf42020-07-28 16:25:12 -0400907 strvec_push(&av, oid_to_hex(&o->oid));
Brandon Williams685fbd32018-03-15 10:31:28 -0700908 }
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400909 deepen_by_rev_list(data, av.nr, av.v);
Jeff Kingc972bf42020-07-28 16:25:12 -0400910 strvec_clear(&av);
Brandon Williams685fbd32018-03-15 10:31:28 -0700911 ret = 1;
912 } else {
Christian Couderee703c82020-06-11 14:05:05 +0200913 if (data->shallows.nr > 0) {
Brandon Williams685fbd32018-03-15 10:31:28 -0700914 int i;
Christian Couderee703c82020-06-11 14:05:05 +0200915 for (i = 0; i < data->shallows.nr; i++)
Junio C Hamano00624d62018-07-18 12:20:27 -0700916 register_shallow(the_repository,
Christian Couderee703c82020-06-11 14:05:05 +0200917 &data->shallows.objects[i].item->oid);
Brandon Williams685fbd32018-03-15 10:31:28 -0700918 }
919 }
920
Christian Couder35b43a12020-06-11 14:05:09 +0200921 data->shallow_nr += data->shallows.nr;
Brandon Williams685fbd32018-03-15 10:31:28 -0700922 return ret;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700923}
924
Brandon Williamsae2948f2018-03-14 11:31:42 -0700925static int process_shallow(const char *line, struct object_array *shallows)
926{
927 const char *arg;
928 if (skip_prefix(line, "shallow ", &arg)) {
929 struct object_id oid;
930 struct object *object;
931 if (get_oid_hex(arg, &oid))
932 die("invalid shallow line: %s", line);
Stefan Beller109cd762018-06-28 18:21:51 -0700933 object = parse_object(the_repository, &oid);
Brandon Williamsae2948f2018-03-14 11:31:42 -0700934 if (!object)
935 return 1;
936 if (object->type != OBJ_COMMIT)
937 die("invalid shallow object %s", oid_to_hex(&oid));
938 if (!(object->flags & CLIENT_SHALLOW)) {
939 object->flags |= CLIENT_SHALLOW;
940 add_object_array(object, NULL, shallows);
941 }
942 return 1;
943 }
944
945 return 0;
946}
947
948static int process_deepen(const char *line, int *depth)
949{
950 const char *arg;
951 if (skip_prefix(line, "deepen ", &arg)) {
952 char *end = NULL;
953 *depth = (int)strtol(arg, &end, 0);
954 if (!end || *end || *depth <= 0)
955 die("Invalid deepen: %s", line);
956 return 1;
957 }
958
959 return 0;
960}
961
962static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
963{
964 const char *arg;
965 if (skip_prefix(line, "deepen-since ", &arg)) {
966 char *end = NULL;
967 *deepen_since = parse_timestamp(arg, &end, 0);
968 if (!end || *end || !deepen_since ||
969 /* revisions.c's max_age -1 is special */
970 *deepen_since == -1)
971 die("Invalid deepen-since: %s", line);
972 *deepen_rev_list = 1;
973 return 1;
974 }
975 return 0;
976}
977
978static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
979{
980 const char *arg;
981 if (skip_prefix(line, "deepen-not ", &arg)) {
982 char *ref = NULL;
983 struct object_id oid;
Nguyễn Thái Ngọc Duy0b1dbf52019-04-06 18:34:27 +0700984 if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
Brandon Williamsae2948f2018-03-14 11:31:42 -0700985 die("git upload-pack: ambiguous deepen-not: %s", line);
986 string_list_append(deepen_not, ref);
987 free(ref);
988 *deepen_rev_list = 1;
989 return 1;
990 }
991 return 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700992}
993
Taylor Blau6dd34562020-08-03 14:00:10 -0400994NORETURN __attribute__((format(printf,2,3)))
995static void send_err_and_die(struct upload_pack_data *data,
996 const char *fmt, ...)
997{
998 struct strbuf buf = STRBUF_INIT;
999 va_list ap;
1000
1001 va_start(ap, fmt);
1002 strbuf_vaddf(&buf, fmt, ap);
1003 va_end(ap);
1004
1005 packet_writer_error(&data->writer, "%s", buf.buf);
1006 die("%s", buf.buf);
1007}
1008
1009static void check_one_filter(struct upload_pack_data *data,
1010 struct list_objects_filter_options *opts)
1011{
1012 const char *key = list_object_filter_config_name(opts->choice);
1013 struct string_list_item *item = string_list_lookup(&data->allowed_filters,
1014 key);
1015 int allowed;
1016
1017 if (item)
1018 allowed = (intptr_t)item->util;
1019 else
1020 allowed = data->allow_filter_fallback;
1021
1022 if (!allowed)
1023 send_err_and_die(data, "filter '%s' not supported", key);
Taylor Blau5b01a4e2020-08-03 14:00:17 -04001024
1025 if (opts->choice == LOFC_TREE_DEPTH &&
1026 opts->tree_exclude_depth > data->tree_filter_max_depth)
1027 send_err_and_die(data,
1028 "tree filter allows max depth %lu, but got %lu",
1029 data->tree_filter_max_depth,
1030 opts->tree_exclude_depth);
Taylor Blau6dd34562020-08-03 14:00:10 -04001031}
1032
1033static void check_filter_recurse(struct upload_pack_data *data,
1034 struct list_objects_filter_options *opts)
1035{
1036 size_t i;
1037
1038 check_one_filter(data, opts);
1039 if (opts->choice != LOFC_COMBINE)
1040 return;
1041
1042 for (i = 0; i < opts->sub_nr; i++)
1043 check_filter_recurse(data, &opts->sub[i]);
1044}
1045
1046static void die_if_using_banned_filter(struct upload_pack_data *data)
1047{
1048 check_filter_recurse(data, &data->filter_options);
1049}
1050
Christian Couderd92ae2c2020-05-15 12:04:47 +02001051static void receive_needs(struct upload_pack_data *data,
1052 struct packet_reader *reader)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001053{
Junio C Hamano051e4002011-08-05 13:54:06 -07001054 int has_non_tip = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001055
Christian Couder35b43a12020-06-11 14:05:09 +02001056 data->shallow_nr = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001057 for (;;) {
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001058 struct object *o;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001059 const char *features;
brian m. carlsoncf939822017-05-06 22:10:28 +00001060 struct object_id oid_buf;
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +07001061 const char *arg;
Josh Steadmon82959462020-11-11 15:29:32 -08001062 int feature_len;
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +07001063
Christian Couderd40f04e2020-06-04 19:54:40 +02001064 reset_timeout(data->timeout);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001065 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Johannes Schindelined09aef2006-10-30 20:09:06 +01001066 break;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001067
Christian Couder7a516762020-05-15 12:04:54 +02001068 if (process_shallow(reader->line, &data->shallows))
Johannes Schindelined09aef2006-10-30 20:09:06 +01001069 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001070 if (process_deepen(reader->line, &data->depth))
Johannes Schindelin016e6cc2006-10-30 20:09:29 +01001071 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001072 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 +07001073 continue;
Christian Couder7a516762020-05-15 12:04:54 +02001074 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 +07001075 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -07001076
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001077 if (skip_prefix(reader->line, "filter ", &arg)) {
Christian Couder59a90262020-06-04 19:54:42 +02001078 if (!data->filter_capability_requested)
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001079 die("git upload-pack: filtering capability not negotiated");
Christian Couderd92ae2c2020-05-15 12:04:47 +02001080 list_objects_filter_die_if_populated(&data->filter_options);
1081 parse_list_objects_filter(&data->filter_options, arg);
Taylor Blau6dd34562020-08-03 14:00:10 -04001082 die_if_using_banned_filter(data);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001083 continue;
1084 }
Junio C Hamano9bfa0f92018-05-08 15:59:15 +09001085
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001086 if (!skip_prefix(reader->line, "want ", &arg) ||
brian m. carlson55dc2272018-05-02 00:25:51 +00001087 parse_oid_hex(arg, &oid_buf, &features))
Junio C Hamano7e44c932008-08-31 09:39:19 -07001088 die("git upload-pack: protocol error, "
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001089 "expected to get object ID, not '%s'", reader->line);
Junio C Hamanof47182c2012-01-08 22:06:19 +01001090
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001091 if (parse_feature_request(features, "deepen-relative"))
Christian Couder7a516762020-05-15 12:04:54 +02001092 data->deepen_relative = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001093 if (parse_feature_request(features, "multi_ack_detailed"))
Christian Coudere9d882b2020-06-04 19:54:44 +02001094 data->multi_ack = MULTI_ACK_DETAILED;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001095 else if (parse_feature_request(features, "multi_ack"))
Christian Coudere9d882b2020-06-04 19:54:44 +02001096 data->multi_ack = MULTI_ACK;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001097 if (parse_feature_request(features, "no-done"))
Christian Couderd40f04e2020-06-04 19:54:40 +02001098 data->no_done = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001099 if (parse_feature_request(features, "thin-pack"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001100 data->use_thin_pack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001101 if (parse_feature_request(features, "ofs-delta"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001102 data->use_ofs_delta = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001103 if (parse_feature_request(features, "side-band-64k"))
Christian Couderf8edd1c2020-06-04 19:54:41 +02001104 data->use_sideband = LARGE_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001105 else if (parse_feature_request(features, "side-band"))
Christian Couderf8edd1c2020-06-04 19:54:41 +02001106 data->use_sideband = DEFAULT_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001107 if (parse_feature_request(features, "no-progress"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001108 data->no_progress = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +01001109 if (parse_feature_request(features, "include-tag"))
Jeff Kingb5a20682020-06-04 19:54:38 +02001110 data->use_include_tag = 1;
Christian Couder59abe192020-06-04 19:54:47 +02001111 if (data->allow_filter &&
1112 parse_feature_request(features, "filter"))
Christian Couder59a90262020-06-04 19:54:42 +02001113 data->filter_capability_requested = 1;
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001114
Josh Steadmon82959462020-11-11 15:29:32 -08001115 arg = parse_feature_value(features, "session-id", &feature_len, NULL);
1116 if (arg) {
1117 char *client_sid = xstrndup(arg, feature_len);
1118 trace2_data_string("transfer", NULL, "client-sid", client_sid);
1119 free(client_sid);
1120 }
1121
Stefan Beller109cd762018-06-28 18:21:51 -07001122 o = parse_object(the_repository, &oid_buf);
Jonathan Tanbdb31ea2017-02-23 10:43:03 -08001123 if (!o) {
Christian Couder4ace0282020-05-15 12:04:48 +02001124 packet_writer_error(&data->writer,
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001125 "upload-pack: not our ref %s",
1126 oid_to_hex(&oid_buf));
Elijah Newren9f9aa762010-07-31 14:11:46 -06001127 die("git upload-pack: not our ref %s",
brian m. carlsoncf939822017-05-06 22:10:28 +00001128 oid_to_hex(&oid_buf));
Jonathan Tanbdb31ea2017-02-23 10:43:03 -08001129 }
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001130 if (!(o->flags & WANTED)) {
1131 o->flags |= WANTED;
Christian Couder629060d2020-06-11 14:05:12 +02001132 if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
1133 || is_our_ref(o, data->allow_uor)))
Junio C Hamano051e4002011-08-05 13:54:06 -07001134 has_non_tip = 1;
Christian Couderd92ae2c2020-05-15 12:04:47 +02001135 add_object_array(o, NULL, &data->want_obj);
Junio C Hamano565ebbf2005-10-24 18:59:18 -07001136 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001137 }
Johannes Sixt9462e3f2009-06-16 20:41:16 +02001138
Junio C Hamano051e4002011-08-05 13:54:06 -07001139 /*
1140 * We have sent all our refs already, and the other end
1141 * should have chosen out of them. When we are operating
1142 * in the stateless RPC mode, however, their choice may
1143 * have been based on the set of older refs advertised
1144 * by another process that handled the initial request.
1145 */
1146 if (has_non_tip)
Christian Couderb08c9742020-05-15 12:04:51 +02001147 check_non_tip(data);
Junio C Hamano051e4002011-08-05 13:54:06 -07001148
Christian Couderf8edd1c2020-06-04 19:54:41 +02001149 if (!data->use_sideband && data->daemon_mode)
Jeff Kingb5a20682020-06-04 19:54:38 +02001150 data->no_progress = 1;
Johannes Sixt9462e3f2009-06-16 20:41:16 +02001151
Christian Couder7a516762020-05-15 12:04:54 +02001152 if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
Johannes Schindelinf53514b2006-10-30 20:09:53 +01001153 return;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +07001154
Christian Couderee703c82020-06-11 14:05:05 +02001155 if (send_shallow_list(data))
Brandon Williams685fbd32018-03-15 10:31:28 -07001156 packet_flush(1);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -07001157}
1158
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001159/* return non-zero if the ref is hidden, otherwise 0 */
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001160static int mark_our_ref(const char *refname, const char *refname_full,
1161 const struct object_id *oid)
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001162{
Jeff King45a187c2021-04-13 03:16:36 -04001163 struct object *o = lookup_unknown_object(the_repository, oid);
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001164
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001165 if (ref_is_hidden(refname, refname_full)) {
Junio C Hamano390eb362013-01-28 21:49:57 -08001166 o->flags |= HIDDEN_REF;
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001167 return 1;
Junio C Hamano390eb362013-01-28 21:49:57 -08001168 }
Junio C Hamano3f1da572013-01-28 20:45:43 -08001169 o->flags |= OUR_REF;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001170 return 0;
1171}
1172
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001173static int check_ref(const char *refname_full, const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001174 int flag UNUSED, void *cb_data UNUSED)
Jeff Kinge1727552015-03-13 00:42:12 -04001175{
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001176 const char *refname = strip_namespace(refname_full);
1177
1178 mark_our_ref(refname, refname_full, oid);
Jeff Kinge1727552015-03-13 00:42:12 -04001179 return 0;
1180}
1181
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001182static void format_symref_info(struct strbuf *buf, struct string_list *symref)
1183{
1184 struct string_list_item *item;
1185
1186 if (!symref->nr)
1187 return;
1188 for_each_string_list_item(item, symref)
1189 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1190}
1191
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001192static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
1193 if (d->advertise_sid)
1194 strbuf_addf(buf, " session-id=%s", trace2_session_id());
1195}
1196
Michael Haggerty363e98b2015-05-25 18:39:12 +00001197static int send_ref(const char *refname, const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001198 int flag UNUSED, void *cb_data)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001199{
Johannes Schindelined09aef2006-10-30 20:09:06 +01001200 static const char *capabilities = "multi_ack thin-pack side-band"
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001201 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1202 " deepen-relative no-progress include-tag multi_ack_detailed";
Josh Triplett6b01ecf2011-07-08 16:13:32 -07001203 const char *refname_nons = strip_namespace(refname);
Michael Haggerty21758af2015-05-25 18:39:13 +00001204 struct object_id peeled;
Christian Couder762f9272020-05-15 12:04:50 +02001205 struct upload_pack_data *data = cb_data;
Carl Worthb5b16992006-02-17 16:14:52 -08001206
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001207 if (mark_our_ref(refname_nons, refname, oid))
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001208 return 0;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001209
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001210 if (capabilities) {
1211 struct strbuf symref_info = STRBUF_INIT;
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001212 struct strbuf session_id = STRBUF_INIT;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001213
Christian Couder762f9272020-05-15 12:04:50 +02001214 format_symref_info(&symref_info, &data->symref);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001215 format_session_id(&session_id, data);
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001216 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 +00001217 oid_to_hex(oid), refname_nons,
Junio C Hamanocf2ad8e2011-03-29 10:24:59 -07001218 0, capabilities,
Christian Couder629060d2020-06-11 14:05:12 +02001219 (data->allow_uor & ALLOW_TIP_SHA1) ?
Fredrik Medley7199c092015-05-21 22:23:38 +02001220 " allow-tip-sha1-in-want" : "",
Christian Couder629060d2020-06-11 14:05:12 +02001221 (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
Fredrik Medley68ee6282015-05-21 22:23:39 +02001222 " allow-reachable-sha1-in-want" : "",
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001223 data->no_done ? " no-done" : "",
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001224 symref_info.buf,
Christian Couder59abe192020-06-04 19:54:47 +02001225 data->allow_filter ? " filter" : "",
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001226 session_id.buf,
brian m. carlsonbf30dbf2020-05-25 19:58:51 +00001227 the_hash_algo->name,
Jeff Kingff5effd2012-08-03 12:19:16 -04001228 git_user_agent_sanitized());
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001229 strbuf_release(&symref_info);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001230 strbuf_release(&session_id);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001231 } else {
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001232 packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001233 }
Johannes Schindelin1f5881b2005-10-28 05:56:41 +02001234 capabilities = NULL;
Jeff King36a31792021-01-20 14:44:43 -05001235 if (!peel_iterated_oid(oid, &peeled))
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001236 packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001237 return 0;
1238}
1239
Jeff King63e14ee2022-08-19 06:08:32 -04001240static int find_symref(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001241 const struct object_id *oid UNUSED,
Michael Haggerty7dabd052015-05-25 18:39:10 +00001242 int flag, void *cb_data)
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001243{
1244 const char *symref_target;
1245 struct string_list_item *item;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001246
1247 if ((flag & REF_ISSYMREF) == 0)
1248 return 0;
René Scharfe744c0402017-09-23 11:45:04 +02001249 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001250 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1251 die("'%s' is a symref but it is not?", refname);
Jeff King533e0882019-05-23 02:11:21 -04001252 item = string_list_append(cb_data, strip_namespace(refname));
1253 item->util = xstrdup(strip_namespace(symref_target));
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001254 return 0;
1255}
1256
Taylor Blau6dd34562020-08-03 14:00:10 -04001257static int parse_object_filter_config(const char *var, const char *value,
1258 struct upload_pack_data *data)
1259{
1260 struct strbuf buf = STRBUF_INIT;
1261 const char *sub, *key;
1262 size_t sub_len;
1263
1264 if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
1265 return 0;
1266
1267 if (!sub) {
1268 if (!strcmp(key, "allow"))
1269 data->allow_filter_fallback = git_config_bool(var, value);
1270 return 0;
1271 }
1272
1273 strbuf_add(&buf, sub, sub_len);
1274
1275 if (!strcmp(key, "allow"))
1276 string_list_insert(&data->allowed_filters, buf.buf)->util =
1277 (void *)(intptr_t)git_config_bool(var, value);
Taylor Blau5b01a4e2020-08-03 14:00:17 -04001278 else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
1279 if (!value) {
1280 strbuf_release(&buf);
1281 return config_error_nonbool(var);
1282 }
1283 string_list_insert(&data->allowed_filters, buf.buf)->util =
1284 (void *)(intptr_t)1;
1285 data->tree_filter_max_depth = git_config_ulong(var, value);
1286 }
Taylor Blau6dd34562020-08-03 14:00:10 -04001287
1288 strbuf_release(&buf);
1289 return 0;
1290}
1291
Christian Couder8a0e6f12020-06-04 19:54:45 +02001292static int upload_pack_config(const char *var, const char *value, void *cb_data)
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001293{
Christian Couderf203a882020-06-04 19:54:46 +02001294 struct upload_pack_data *data = cb_data;
1295
Fredrik Medley7199c092015-05-21 22:23:38 +02001296 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1297 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001298 data->allow_uor |= ALLOW_TIP_SHA1;
Fredrik Medley7199c092015-05-21 22:23:38 +02001299 else
Christian Couder629060d2020-06-11 14:05:12 +02001300 data->allow_uor &= ~ALLOW_TIP_SHA1;
Fredrik Medley68ee6282015-05-21 22:23:39 +02001301 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1302 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001303 data->allow_uor |= ALLOW_REACHABLE_SHA1;
Fredrik Medley68ee6282015-05-21 22:23:39 +02001304 else
Christian Couder629060d2020-06-11 14:05:12 +02001305 data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
David Turnerf8edeaa2016-11-11 12:23:48 -05001306 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1307 if (git_config_bool(var, value))
Christian Couder629060d2020-06-11 14:05:12 +02001308 data->allow_uor |= ALLOW_ANY_SHA1;
David Turnerf8edeaa2016-11-11 12:23:48 -05001309 else
Christian Couder629060d2020-06-11 14:05:12 +02001310 data->allow_uor &= ~ALLOW_ANY_SHA1;
Fredrik Medley7199c092015-05-21 22:23:38 +02001311 } else if (!strcmp("uploadpack.keepalive", var)) {
Christian Couderf203a882020-06-04 19:54:46 +02001312 data->keepalive = git_config_int(var, value);
1313 if (!data->keepalive)
1314 data->keepalive = -1;
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001315 } else if (!strcmp("uploadpack.allowfilter", var)) {
Christian Couder59abe192020-06-04 19:54:47 +02001316 data->allow_filter = git_config_bool(var, value);
Brandon Williams516e2b72018-06-27 15:30:17 -07001317 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
Christian Couderd1d7a942020-06-04 19:54:48 +02001318 data->allow_ref_in_want = git_config_bool(var, value);
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001319 } else if (!strcmp("uploadpack.allowsidebandall", var)) {
Christian Coudere3835cd2020-06-04 19:54:49 +02001320 data->allow_sideband_all = git_config_bool(var, value);
Elijah Newren8e712ef2019-04-25 07:58:54 -07001321 } else if (!strcmp("core.precomposeunicode", var)) {
1322 precomposed_unicode = git_config_bool(var, value);
Josh Steadmon791e1ad2020-11-11 15:29:27 -08001323 } else if (!strcmp("transfer.advertisesid", var)) {
1324 data->advertise_sid = git_config_bool(var, value);
Jeff King05e95152013-09-08 05:01:31 -04001325 }
Jeff Kingaaaa8812018-10-24 03:27:52 -04001326
Jeff Kingd43a21b2020-12-03 03:09:42 -05001327 if (parse_object_filter_config(var, value, data) < 0)
1328 return -1;
Taylor Blau6dd34562020-08-03 14:00:10 -04001329
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001330 return parse_hide_refs_config(var, value, "uploadpack");
1331}
1332
Glen Choo5b3c6502022-07-14 21:27:59 +00001333static int upload_pack_protected_config(const char *var, const char *value, void *cb_data)
1334{
1335 struct upload_pack_data *data = cb_data;
1336
1337 if (!strcmp("uploadpack.packobjectshook", var))
1338 return git_config_string(&data->pack_objects_hook, var, value);
1339 return 0;
1340}
1341
1342static void get_upload_pack_config(struct upload_pack_data *data)
1343{
1344 git_config(upload_pack_config, data);
1345 git_protected_config(upload_pack_protected_config, data);
1346}
1347
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001348void upload_pack(const int advertise_refs, const int stateless_rpc,
1349 const int timeout)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001350{
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001351 struct packet_reader reader;
Christian Couderebf8ebc2020-05-15 12:04:45 +02001352 struct upload_pack_data data;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001353
Christian Couderebf8ebc2020-05-15 12:04:45 +02001354 upload_pack_data_init(&data);
Glen Choo5b3c6502022-07-14 21:27:59 +00001355 get_upload_pack_config(&data);
Christian Couder8a0e6f12020-06-04 19:54:45 +02001356
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001357 data.stateless_rpc = stateless_rpc;
1358 data.timeout = timeout;
1359 if (data.timeout)
1360 data.daemon_mode = 1;
Christian Couderdf654ab2020-05-15 12:04:52 +02001361
Christian Couder438528f2020-05-15 12:04:49 +02001362 head_ref_namespaced(find_symref, &data.symref);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001363
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001364 if (advertise_refs || !data.stateless_rpc) {
Christian Couderd40f04e2020-06-04 19:54:40 +02001365 reset_timeout(data.timeout);
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001366 if (advertise_refs)
1367 data.no_done = 1;
Christian Couder762f9272020-05-15 12:04:50 +02001368 head_ref_namespaced(send_ref, &data);
1369 for_each_namespaced_ref(send_ref, &data);
Jacob Vosmaer70afef52021-09-01 14:54:42 +02001370 /*
1371 * fflush stdout before calling advertise_shallow_grafts because send_ref
1372 * uses stdio.
1373 */
1374 fflush_or_die(stdout);
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001375 advertise_shallow_grafts(1);
1376 packet_flush(1);
1377 } else {
1378 head_ref_namespaced(check_ref, NULL);
1379 for_each_namespaced_ref(check_ref, NULL);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001380 }
1381
Ævar Arnfjörð Bjarmasonf234da82021-08-05 03:25:42 +02001382 if (!advertise_refs) {
Christian Couderebf8ebc2020-05-15 12:04:45 +02001383 packet_reader_init(&reader, 0, NULL, 0,
1384 PACKET_READ_CHOMP_NEWLINE |
1385 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -08001386
Christian Couderd92ae2c2020-05-15 12:04:47 +02001387 receive_needs(&data, &reader);
Daniel Duvallfb3d1a02020-10-30 19:39:02 -07001388
1389 /*
1390 * An EOF at this exact point in negotiation should be
1391 * acceptable from stateless clients as they will consume the
1392 * shallow list before doing subsequent rpc with haves/etc.
1393 */
1394 if (data.stateless_rpc)
1395 reader.options |= PACKET_READ_GENTLE_ON_EOF;
1396
1397 if (data.want_obj.nr &&
1398 packet_reader_peek(&reader) != PACKET_READ_EOF) {
1399 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
Christian Couder07977692020-05-15 12:04:46 +02001400 get_common_commits(&data, &reader);
Ramsay Jonescae2ee12020-06-16 00:00:20 +01001401 create_pack_file(&data, NULL);
Christian Couderebf8ebc2020-05-15 12:04:45 +02001402 }
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001403 }
Christian Couder08450ef2020-05-08 10:01:15 +02001404
Christian Couderebf8ebc2020-05-15 12:04:45 +02001405 upload_pack_data_clear(&data);
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001406}
Brandon Williams3145ea92018-03-15 10:31:27 -07001407
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001408static int parse_want(struct packet_writer *writer, const char *line,
1409 struct object_array *want_obj)
Brandon Williams3145ea92018-03-15 10:31:27 -07001410{
1411 const char *arg;
1412 if (skip_prefix(line, "want ", &arg)) {
1413 struct object_id oid;
1414 struct object *o;
1415
1416 if (get_oid_hex(arg, &oid))
1417 die("git upload-pack: protocol error, "
1418 "expected to get oid, not '%s'", line);
1419
Jeff King9a8c3c42022-09-06 19:06:25 -04001420 o = parse_object_with_flags(the_repository, &oid,
1421 PARSE_OBJECT_SKIP_HASH_CHECK);
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001422
Brandon Williams3145ea92018-03-15 10:31:27 -07001423 if (!o) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001424 packet_writer_error(writer,
1425 "upload-pack: not our ref %s",
1426 oid_to_hex(&oid));
Brandon Williams3145ea92018-03-15 10:31:27 -07001427 die("git upload-pack: not our ref %s",
1428 oid_to_hex(&oid));
1429 }
1430
1431 if (!(o->flags & WANTED)) {
1432 o->flags |= WANTED;
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001433 add_object_array(o, NULL, want_obj);
Brandon Williams3145ea92018-03-15 10:31:27 -07001434 }
1435
1436 return 1;
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001437 }
1438
1439 return 0;
1440}
Brandon Williams3145ea92018-03-15 10:31:27 -07001441
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001442static int parse_want_ref(struct packet_writer *writer, const char *line,
1443 struct string_list *wanted_refs,
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001444 struct object_array *want_obj)
Brandon Williams516e2b72018-06-27 15:30:17 -07001445{
Kim Altintop39551402021-08-13 06:23:50 +00001446 const char *refname_nons;
1447 if (skip_prefix(line, "want-ref ", &refname_nons)) {
Brandon Williams516e2b72018-06-27 15:30:17 -07001448 struct object_id oid;
1449 struct string_list_item *item;
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001450 struct object *o = NULL;
Kim Altintop39551402021-08-13 06:23:50 +00001451 struct strbuf refname = STRBUF_INIT;
Brandon Williams516e2b72018-06-27 15:30:17 -07001452
Kim Altintop39551402021-08-13 06:23:50 +00001453 strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons);
1454 if (ref_is_hidden(refname_nons, refname.buf) ||
1455 read_ref(refname.buf, &oid)) {
1456 packet_writer_error(writer, "unknown ref %s", refname_nons);
1457 die("unknown ref %s", refname_nons);
Brandon Williams516e2b72018-06-27 15:30:17 -07001458 }
Kim Altintop39551402021-08-13 06:23:50 +00001459 strbuf_release(&refname);
Brandon Williams516e2b72018-06-27 15:30:17 -07001460
Kim Altintop39551402021-08-13 06:23:50 +00001461 item = string_list_append(wanted_refs, refname_nons);
Brandon Williams516e2b72018-06-27 15:30:17 -07001462 item->util = oiddup(&oid);
1463
Patrick Steinhardt4de65622022-03-01 10:33:37 +01001464 if (!starts_with(refname_nons, "refs/tags/")) {
1465 struct commit *commit = lookup_commit_in_graph(the_repository, &oid);
1466 if (commit)
1467 o = &commit->object;
1468 }
1469
1470 if (!o)
1471 o = parse_object_or_die(&oid, refname_nons);
1472
Brandon Williams516e2b72018-06-27 15:30:17 -07001473 if (!(o->flags & WANTED)) {
1474 o->flags |= WANTED;
Jonathan Tan1d1243f2018-10-18 13:43:28 -07001475 add_object_array(o, NULL, want_obj);
Brandon Williams516e2b72018-06-27 15:30:17 -07001476 }
1477
1478 return 1;
1479 }
1480
1481 return 0;
1482}
1483
Brandon Williams3145ea92018-03-15 10:31:27 -07001484static int parse_have(const char *line, struct oid_array *haves)
1485{
1486 const char *arg;
1487 if (skip_prefix(line, "have ", &arg)) {
1488 struct object_id oid;
1489
1490 if (get_oid_hex(arg, &oid))
1491 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1492 oid_array_append(haves, &oid);
1493 return 1;
1494 }
1495
1496 return 0;
1497}
1498
1499static void process_args(struct packet_reader *request,
Christian Couder389f1612020-05-15 12:04:43 +02001500 struct upload_pack_data *data)
Brandon Williams3145ea92018-03-15 10:31:27 -07001501{
Jeff King4845b772020-03-27 04:03:38 -04001502 while (packet_reader_read(request) == PACKET_READ_NORMAL) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001503 const char *arg = request->line;
Jonathan Tanba957102018-05-03 16:46:56 -07001504 const char *p;
Brandon Williams3145ea92018-03-15 10:31:27 -07001505
1506 /* process want */
Christian Couder389f1612020-05-15 12:04:43 +02001507 if (parse_want(&data->writer, arg, &data->want_obj))
Brandon Williams3145ea92018-03-15 10:31:27 -07001508 continue;
Christian Couderd1d7a942020-06-04 19:54:48 +02001509 if (data->allow_ref_in_want &&
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001510 parse_want_ref(&data->writer, arg, &data->wanted_refs,
Christian Couder389f1612020-05-15 12:04:43 +02001511 &data->want_obj))
Brandon Williams516e2b72018-06-27 15:30:17 -07001512 continue;
Brandon Williams3145ea92018-03-15 10:31:27 -07001513 /* process have line */
1514 if (parse_have(arg, &data->haves))
1515 continue;
1516
1517 /* process args like thin-pack */
1518 if (!strcmp(arg, "thin-pack")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001519 data->use_thin_pack = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001520 continue;
1521 }
1522 if (!strcmp(arg, "ofs-delta")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001523 data->use_ofs_delta = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001524 continue;
1525 }
1526 if (!strcmp(arg, "no-progress")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001527 data->no_progress = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001528 continue;
1529 }
1530 if (!strcmp(arg, "include-tag")) {
Jeff Kingb5a20682020-06-04 19:54:38 +02001531 data->use_include_tag = 1;
Brandon Williams3145ea92018-03-15 10:31:27 -07001532 continue;
1533 }
1534 if (!strcmp(arg, "done")) {
1535 data->done = 1;
1536 continue;
1537 }
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001538 if (!strcmp(arg, "wait-for-done")) {
1539 data->wait_for_done = 1;
1540 continue;
1541 }
Brandon Williams3145ea92018-03-15 10:31:27 -07001542
Brandon Williams685fbd32018-03-15 10:31:28 -07001543 /* Shallow related arguments */
1544 if (process_shallow(arg, &data->shallows))
1545 continue;
1546 if (process_deepen(arg, &data->depth))
1547 continue;
1548 if (process_deepen_since(arg, &data->deepen_since,
1549 &data->deepen_rev_list))
1550 continue;
1551 if (process_deepen_not(arg, &data->deepen_not,
1552 &data->deepen_rev_list))
1553 continue;
1554 if (!strcmp(arg, "deepen-relative")) {
1555 data->deepen_relative = 1;
1556 continue;
1557 }
1558
Christian Couder59abe192020-06-04 19:54:47 +02001559 if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
Christian Couder08450ef2020-05-08 10:01:15 +02001560 list_objects_filter_die_if_populated(&data->filter_options);
1561 parse_list_objects_filter(&data->filter_options, p);
Taylor Blau6dd34562020-08-03 14:00:10 -04001562 die_if_using_banned_filter(data);
Jonathan Tanba957102018-05-03 16:46:56 -07001563 continue;
1564 }
1565
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001566 if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
Christian Coudere3835cd2020-06-04 19:54:49 +02001567 data->allow_sideband_all) &&
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001568 !strcmp(arg, "sideband-all")) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001569 data->writer.use_sideband = 1;
1570 continue;
1571 }
1572
Jonathan Tandd4b7322020-06-10 13:57:23 -07001573 if (skip_prefix(arg, "packfile-uris ", &p)) {
1574 string_list_split(&data->uri_protocols, p, ',', -1);
1575 continue;
1576 }
1577
Brandon Williams3145ea92018-03-15 10:31:27 -07001578 /* ignore unknown lines maybe? */
Jonathan Tan7cc6ed22018-05-01 17:31:29 -07001579 die("unexpected line: '%s'", arg);
Brandon Williams3145ea92018-03-15 10:31:27 -07001580 }
Jeff King4845b772020-03-27 04:03:38 -04001581
Jonathan Tandd4b7322020-06-10 13:57:23 -07001582 if (data->uri_protocols.nr && !data->writer.use_sideband)
1583 string_list_clear(&data->uri_protocols, 0);
1584
Jeff King4845b772020-03-27 04:03:38 -04001585 if (request->status != PACKET_READ_FLUSH)
1586 die(_("expected flush after fetch arguments"));
Brandon Williams3145ea92018-03-15 10:31:27 -07001587}
1588
Christian Couder8dcf2272020-06-11 14:05:13 +02001589static int process_haves(struct upload_pack_data *data, struct oid_array *common)
Brandon Williams3145ea92018-03-15 10:31:27 -07001590{
1591 int i;
1592
1593 /* Process haves */
Christian Couder8dcf2272020-06-11 14:05:13 +02001594 for (i = 0; i < data->haves.nr; i++) {
1595 const struct object_id *oid = &data->haves.oid[i];
Brandon Williams3145ea92018-03-15 10:31:27 -07001596
Jonathan Tan77aa0942020-07-16 11:09:50 -07001597 if (!has_object_file_with_flags(oid,
1598 OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
Brandon Williams3145ea92018-03-15 10:31:27 -07001599 continue;
1600
1601 oid_array_append(common, oid);
1602
Christian Couderea2c6e62020-06-11 14:05:18 +02001603 do_got_oid(data, oid);
Brandon Williams3145ea92018-03-15 10:31:27 -07001604 }
1605
1606 return 0;
1607}
1608
Christian Couder6fbbc432020-06-11 14:05:14 +02001609static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
Brandon Williams3145ea92018-03-15 10:31:27 -07001610{
1611 int i;
1612
Christian Couder6fbbc432020-06-11 14:05:14 +02001613 packet_writer_write(&data->writer, "acknowledgments\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001614
1615 /* Send Acks */
1616 if (!acks->nr)
Christian Couder6fbbc432020-06-11 14:05:14 +02001617 packet_writer_write(&data->writer, "NAK\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001618
1619 for (i = 0; i < acks->nr; i++) {
Christian Couder6fbbc432020-06-11 14:05:14 +02001620 packet_writer_write(&data->writer, "ACK %s\n",
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001621 oid_to_hex(&acks->oid[i]));
Brandon Williams3145ea92018-03-15 10:31:27 -07001622 }
1623
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001624 if (!data->wait_for_done && ok_to_give_up(data)) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001625 /* Send Ready */
Christian Couder6fbbc432020-06-11 14:05:14 +02001626 packet_writer_write(&data->writer, "ready\n");
Brandon Williams3145ea92018-03-15 10:31:27 -07001627 return 1;
1628 }
1629
1630 return 0;
1631}
1632
Christian Couder389f1612020-05-15 12:04:43 +02001633static int process_haves_and_send_acks(struct upload_pack_data *data)
Brandon Williams3145ea92018-03-15 10:31:27 -07001634{
1635 struct oid_array common = OID_ARRAY_INIT;
Brandon Williams3145ea92018-03-15 10:31:27 -07001636 int ret = 0;
1637
Christian Couder8dcf2272020-06-11 14:05:13 +02001638 process_haves(data, &common);
Brandon Williams3145ea92018-03-15 10:31:27 -07001639 if (data->done) {
1640 ret = 1;
Christian Couder6fbbc432020-06-11 14:05:14 +02001641 } else if (send_acks(data, &common)) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001642 packet_writer_delim(&data->writer);
Brandon Williams3145ea92018-03-15 10:31:27 -07001643 ret = 1;
1644 } else {
1645 /* Add Flush */
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001646 packet_writer_flush(&data->writer);
Brandon Williams3145ea92018-03-15 10:31:27 -07001647 ret = 0;
1648 }
1649
Brandon Williams3145ea92018-03-15 10:31:27 -07001650 oid_array_clear(&data->haves);
1651 oid_array_clear(&common);
1652 return ret;
1653}
1654
Brandon Williams516e2b72018-06-27 15:30:17 -07001655static void send_wanted_ref_info(struct upload_pack_data *data)
1656{
1657 const struct string_list_item *item;
1658
1659 if (!data->wanted_refs.nr)
1660 return;
1661
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001662 packet_writer_write(&data->writer, "wanted-refs\n");
Brandon Williams516e2b72018-06-27 15:30:17 -07001663
1664 for_each_string_list_item(item, &data->wanted_refs) {
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001665 packet_writer_write(&data->writer, "%s %s\n",
1666 oid_to_hex(item->util),
1667 item->string);
Brandon Williams516e2b72018-06-27 15:30:17 -07001668 }
1669
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001670 packet_writer_delim(&data->writer);
Brandon Williams516e2b72018-06-27 15:30:17 -07001671}
1672
Christian Couder389f1612020-05-15 12:04:43 +02001673static void send_shallow_info(struct upload_pack_data *data)
Brandon Williams685fbd32018-03-15 10:31:28 -07001674{
1675 /* No shallow info needs to be sent */
1676 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
Junio C Hamano00624d62018-07-18 12:20:27 -07001677 !is_repository_shallow(the_repository))
Brandon Williams685fbd32018-03-15 10:31:28 -07001678 return;
1679
Jonathan Tanbc2e7952019-01-15 11:40:27 -08001680 packet_writer_write(&data->writer, "shallow-info\n");
Brandon Williams685fbd32018-03-15 10:31:28 -07001681
Christian Couderee703c82020-06-11 14:05:05 +02001682 if (!send_shallow_list(data) &&
Junio C Hamano00624d62018-07-18 12:20:27 -07001683 is_repository_shallow(the_repository))
Christian Couderb1492f22020-06-11 14:05:06 +02001684 deepen(data, INFINITE_DEPTH);
Brandon Williams685fbd32018-03-15 10:31:28 -07001685
1686 packet_delim(1);
1687}
1688
Brandon Williams3145ea92018-03-15 10:31:27 -07001689enum fetch_state {
1690 FETCH_PROCESS_ARGS = 0,
1691 FETCH_SEND_ACKS,
1692 FETCH_SEND_PACK,
1693 FETCH_DONE,
1694};
1695
Ævar Arnfjörð Bjarmason28a592e2021-08-05 03:25:38 +02001696int upload_pack_v2(struct repository *r, struct packet_reader *request)
Brandon Williams3145ea92018-03-15 10:31:27 -07001697{
1698 enum fetch_state state = FETCH_PROCESS_ARGS;
1699 struct upload_pack_data data;
Jonathan Tand1035ca2018-10-18 13:43:29 -07001700
1701 clear_object_flags(ALL_FLAGS);
Brandon Williams3145ea92018-03-15 10:31:27 -07001702
1703 upload_pack_data_init(&data);
Christian Couderf8edd1c2020-06-04 19:54:41 +02001704 data.use_sideband = LARGE_PACKET_MAX;
Glen Choo5b3c6502022-07-14 21:27:59 +00001705 get_upload_pack_config(&data);
Christian Couder8a0e6f12020-06-04 19:54:45 +02001706
Brandon Williams3145ea92018-03-15 10:31:27 -07001707 while (state != FETCH_DONE) {
1708 switch (state) {
1709 case FETCH_PROCESS_ARGS:
Christian Couder389f1612020-05-15 12:04:43 +02001710 process_args(request, &data);
Brandon Williams3145ea92018-03-15 10:31:27 -07001711
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001712 if (!data.want_obj.nr && !data.wait_for_done) {
Brandon Williams3145ea92018-03-15 10:31:27 -07001713 /*
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001714 * Request didn't contain any 'want' lines (and
1715 * the request does not contain
1716 * "wait-for-done", in which it is reasonable
1717 * to just send 'have's without 'want's); guess
1718 * they didn't want anything.
Brandon Williams3145ea92018-03-15 10:31:27 -07001719 */
1720 state = FETCH_DONE;
1721 } else if (data.haves.nr) {
1722 /*
1723 * Request had 'have' lines, so lets ACK them.
1724 */
1725 state = FETCH_SEND_ACKS;
1726 } else {
1727 /*
1728 * Request had 'want's but no 'have's so we can
1729 * immedietly go to construct and send a pack.
1730 */
1731 state = FETCH_SEND_PACK;
1732 }
1733 break;
1734 case FETCH_SEND_ACKS:
Christian Couder389f1612020-05-15 12:04:43 +02001735 if (process_haves_and_send_acks(&data))
Brandon Williams3145ea92018-03-15 10:31:27 -07001736 state = FETCH_SEND_PACK;
1737 else
1738 state = FETCH_DONE;
1739 break;
1740 case FETCH_SEND_PACK:
Brandon Williams516e2b72018-06-27 15:30:17 -07001741 send_wanted_ref_info(&data);
Christian Couder389f1612020-05-15 12:04:43 +02001742 send_shallow_info(&data);
Brandon Williams685fbd32018-03-15 10:31:28 -07001743
Jonathan Tandd4b7322020-06-10 13:57:23 -07001744 if (data.uri_protocols.nr) {
1745 create_pack_file(&data, &data.uri_protocols);
1746 } else {
1747 packet_writer_write(&data.writer, "packfile\n");
1748 create_pack_file(&data, NULL);
1749 }
Brandon Williams3145ea92018-03-15 10:31:27 -07001750 state = FETCH_DONE;
1751 break;
1752 case FETCH_DONE:
1753 continue;
1754 }
1755 }
1756
1757 upload_pack_data_clear(&data);
1758 return 0;
1759}
Brandon Williams685fbd32018-03-15 10:31:28 -07001760
1761int upload_pack_advertise(struct repository *r,
1762 struct strbuf *value)
1763{
Jonathan Tanba957102018-05-03 16:46:56 -07001764 if (value) {
1765 int allow_filter_value;
Brandon Williams516e2b72018-06-27 15:30:17 -07001766 int allow_ref_in_want;
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001767 int allow_sideband_all_value;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001768 char *str = NULL;
Brandon Williams516e2b72018-06-27 15:30:17 -07001769
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001770 strbuf_addstr(value, "shallow wait-for-done");
Brandon Williams516e2b72018-06-27 15:30:17 -07001771
Jonathan Tanba957102018-05-03 16:46:56 -07001772 if (!repo_config_get_bool(the_repository,
1773 "uploadpack.allowfilter",
1774 &allow_filter_value) &&
1775 allow_filter_value)
1776 strbuf_addstr(value, " filter");
Brandon Williams516e2b72018-06-27 15:30:17 -07001777
1778 if (!repo_config_get_bool(the_repository,
1779 "uploadpack.allowrefinwant",
1780 &allow_ref_in_want) &&
1781 allow_ref_in_want)
1782 strbuf_addstr(value, " ref-in-want");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001783
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001784 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
1785 (!repo_config_get_bool(the_repository,
1786 "uploadpack.allowsidebandall",
1787 &allow_sideband_all_value) &&
1788 allow_sideband_all_value))
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001789 strbuf_addstr(value, " sideband-all");
Jonathan Tandd4b7322020-06-10 13:57:23 -07001790
1791 if (!repo_config_get_string(the_repository,
1792 "uploadpack.blobpackfileuri",
1793 &str) &&
1794 str) {
1795 strbuf_addstr(value, " packfile-uris");
1796 free(str);
1797 }
Jonathan Tanba957102018-05-03 16:46:56 -07001798 }
Brandon Williams516e2b72018-06-27 15:30:17 -07001799
Brandon Williams685fbd32018-03-15 10:31:28 -07001800 return 1;
1801}